Skip to main content
← Back to kicad-monkey Rust Docs kicad-monkey Rust Crate API Reference

kicad_monkey_core/
schematic_connectivity.rs

1use crate::schematic_bus_connectivity::{
2    SchematicConnectivityGeometry, build_schematic_bus_subgraphs_with_geometry,
3    collect_schematic_bus_aliases,
4};
5use crate::schematic_segment_index::{SchematicSegment, SchematicSegmentIndex};
6use crate::{
7    SchematicBundleIndex, SchematicDriverPriority, SchematicLabelScope, SchematicPoint,
8    SourceBundleError, SourceBundleErrorKind, canonical_bus_member_name,
9};
10use std::collections::{HashMap, HashSet};
11
12mod driver_selection;
13mod driver_types;
14mod graphical_ids;
15mod pin_naming;
16mod render_ids;
17pub(crate) mod stacked_pins;
18mod wire_union;
19use driver_selection::resolve_driver;
20pub use driver_types::{
21    SchematicGraphicalIds, SchematicLabelDriver, SchematicPinDriver, SchematicWireDriverKind,
22    SchematicWireSubgraph,
23};
24pub use pin_naming::SchematicSubpartSettings;
25pub(crate) use pin_naming::subpart_reference;
26use pin_naming::{PinNaming, build_pin_namings};
27pub(crate) use render_ids::schematic_sheet_pin_group_id;
28use stacked_pins::expand_stacked_pin;
29use wire_union::WirePointUnion;
30
31pub fn build_schematic_occurrence_subgraphs(
32    index: &SchematicBundleIndex,
33    occurrence_index: usize,
34) -> Result<Vec<SchematicWireSubgraph>, SourceBundleError> {
35    build_schematic_occurrence_subgraphs_with_settings(
36        index,
37        occurrence_index,
38        index.subpart_settings(),
39    )
40}
41
42pub fn build_schematic_occurrence_subgraphs_with_settings(
43    index: &SchematicBundleIndex,
44    occurrence_index: usize,
45    subparts: SchematicSubpartSettings,
46) -> Result<Vec<SchematicWireSubgraph>, SourceBundleError> {
47    build_schematic_occurrence_subgraphs_with_options(index, occurrence_index, subparts, false)
48}
49
50pub(crate) fn build_schematic_occurrence_subgraphs_with_options(
51    index: &SchematicBundleIndex,
52    occurrence_index: usize,
53    subparts: SchematicSubpartSettings,
54    include_off_board_symbols: bool,
55) -> Result<Vec<SchematicWireSubgraph>, SourceBundleError> {
56    let builder =
57        OccurrenceBuilder::new(index, occurrence_index, subparts, include_off_board_symbols)?;
58    let aliases = collect_schematic_bus_aliases(builder.definition)?;
59    Ok(builder.build_with_aliases(&aliases)?.wire_subgraphs)
60}
61
62pub(crate) struct SchematicOccurrenceDesignSubgraphs {
63    pub(crate) wire_subgraphs: Vec<SchematicWireSubgraph>,
64    pub(crate) bus_subgraphs: Vec<crate::SchematicBusSubgraph>,
65}
66
67pub(crate) fn build_schematic_occurrence_design_subgraphs<'a>(
68    index: &'a SchematicBundleIndex,
69    occurrence_index: usize,
70    subparts: SchematicSubpartSettings,
71    include_off_board_symbols: bool,
72    aliases: &HashMap<&'a str, &'a [String]>,
73) -> Result<SchematicOccurrenceDesignSubgraphs, SourceBundleError> {
74    OccurrenceBuilder::new(index, occurrence_index, subparts, include_off_board_symbols)?
75        .build_with_aliases(aliases)
76}
77
78struct OccurrenceBuilder<'a> {
79    index: &'a SchematicBundleIndex,
80    definition: &'a crate::SchematicDefinition,
81    occurrence_index: usize,
82    subparts: SchematicSubpartSettings,
83    include_off_board_symbols: bool,
84}
85
86struct PinSemantics {
87    at: SchematicPoint,
88    priority: SchematicDriverPriority,
89    power_value: String,
90    is_power: bool,
91    implicit: bool,
92}
93
94struct PinPowerSemantics {
95    priority: SchematicDriverPriority,
96    power_value: String,
97    is_power: bool,
98    implicit: bool,
99}
100
101impl<'a> OccurrenceBuilder<'a> {
102    fn new(
103        index: &'a SchematicBundleIndex,
104        occurrence_index: usize,
105        subparts: SchematicSubpartSettings,
106        include_off_board_symbols: bool,
107    ) -> Result<Self, SourceBundleError> {
108        let occurrence = index.occurrence(occurrence_index).ok_or_else(|| {
109            SourceBundleError::new(
110                SourceBundleErrorKind::Schematic,
111                None,
112                "schematic occurrence index is out of range",
113            )
114        })?;
115        let definition = index.definition(&occurrence.source_path).ok_or_else(|| {
116            SourceBundleError::new(
117                SourceBundleErrorKind::MissingSource,
118                Some(&occurrence.source_path),
119                "schematic occurrence definition is missing",
120            )
121        })?;
122        Ok(Self {
123            index,
124            definition,
125            occurrence_index,
126            subparts,
127            include_off_board_symbols,
128        })
129    }
130
131    fn build_with_aliases(
132        mut self,
133        aliases: &HashMap<&'a str, &'a [String]>,
134    ) -> Result<SchematicOccurrenceDesignSubgraphs, SourceBundleError> {
135        let mut geometry = SchematicConnectivityGeometry::build(self.definition)?;
136        let bus_subgraphs =
137            build_schematic_bus_subgraphs_with_geometry(self.definition, aliases, &mut geometry)?;
138        let entry_index = self.build_entry_index()?;
139        let mut union = WirePointUnion::default();
140        self.seed_carrier_points(&mut union)?;
141        self.attach_junctions(&mut union, geometry.wire_index())?;
142        let mut labels = self.collect_labels(&mut union)?;
143        self.attach_labels(&mut union, &labels, &geometry, &entry_index)?;
144        let mut pins = self.collect_pins(&mut union)?;
145        self.merge_jumper_pins(&mut union, &pins)?;
146        self.merge_same_sheet_names(&mut union, &labels, &pins);
147        self.merge_bus_member_taps(&mut union, &labels, &bus_subgraphs)?;
148        let wire_subgraphs = self.realize_subgraphs(&mut union, &mut pins, &mut labels)?;
149        Ok(SchematicOccurrenceDesignSubgraphs {
150            wire_subgraphs,
151            bus_subgraphs,
152        })
153    }
154
155    fn build_entry_index(&self) -> Result<SchematicSegmentIndex, SourceBundleError> {
156        let mut segments = Vec::with_capacity(self.definition.bus_entries.len());
157        for entry in &self.definition.bus_entries {
158            let end = entry
159                .wire_end()
160                .ok_or_else(|| self.error("bus-entry endpoint overflows"))?;
161            if let Some(segment) = SchematicSegment::new(entry.at, end, segments.len()) {
162                segments.push(segment);
163            }
164        }
165        SchematicSegmentIndex::build(segments, &self.definition.source_path)
166    }
167
168    fn seed_carrier_points(&self, union: &mut WirePointUnion) -> Result<(), SourceBundleError> {
169        for wire in &self.definition.wires {
170            let mut previous = None;
171            for point in &wire.points {
172                let current = union.add(*point, usize::MAX, self.definition)?;
173                if let Some(previous) = previous {
174                    union.union(previous, current);
175                }
176                previous = Some(current);
177            }
178        }
179        for bus in &self.definition.buses {
180            for point in &bus.points {
181                union.add(*point, usize::MAX, self.definition)?;
182            }
183        }
184        for entry in &self.definition.bus_entries {
185            union.add(entry.at, usize::MAX, self.definition)?;
186            union.add(
187                entry
188                    .wire_end()
189                    .ok_or_else(|| self.error("bus-entry endpoint overflows"))?,
190                usize::MAX,
191                self.definition,
192            )?;
193        }
194        Ok(())
195    }
196
197    fn attach_junctions(
198        &mut self,
199        union: &mut WirePointUnion,
200        wire_index: &SchematicSegmentIndex,
201    ) -> Result<(), SourceBundleError> {
202        for junction in &self.definition.junctions {
203            union.add(junction.at, usize::MAX, self.definition)?;
204            self.attach_point(union, junction.at, wire_index)?;
205        }
206        Ok(())
207    }
208
209    fn collect_labels(
210        &mut self,
211        union: &mut WirePointUnion,
212    ) -> Result<Vec<SchematicLabelDriver>, SourceBundleError> {
213        let mut out = Vec::new();
214        for scope in [
215            SchematicLabelScope::Local,
216            SchematicLabelScope::Global,
217            SchematicLabelScope::Hierarchical,
218        ] {
219            for label in self
220                .definition
221                .labels
222                .iter()
223                .filter(|label| label.scope == scope)
224            {
225                let (priority, kind) = label_type(scope);
226                let shape = if scope == SchematicLabelScope::Hierarchical {
227                    label.shape.clone()
228                } else {
229                    String::new()
230                };
231                self.push_label(
232                    &mut out,
233                    SchematicLabelDriver {
234                        text: label.text.clone(),
235                        at: label.at,
236                        priority,
237                        kind,
238                        shape,
239                        source_uuid: label.uuid.clone(),
240                        render_id: label.uuid.clone(),
241                        source_order: 0,
242                    },
243                )?;
244                union.add(label.at, usize::MAX, self.definition)?;
245            }
246        }
247        for sheet in &self.definition.sheets {
248            for pin in &sheet.pins {
249                self.push_label(
250                    &mut out,
251                    SchematicLabelDriver {
252                        text: pin.name.clone(),
253                        at: pin.at,
254                        priority: SchematicDriverPriority::SheetPin,
255                        kind: SchematicWireDriverKind::SheetPin,
256                        shape: pin.shape.as_str().to_owned(),
257                        source_uuid: pin.uuid.clone(),
258                        render_id: schematic_sheet_pin_group_id(sheet, pin),
259                        source_order: 0,
260                    },
261                )?;
262                union.add(pin.at, usize::MAX, self.definition)?;
263            }
264        }
265        Ok(out)
266    }
267
268    fn push_label(
269        &mut self,
270        out: &mut Vec<SchematicLabelDriver>,
271        mut driver: SchematicLabelDriver,
272    ) -> Result<(), SourceBundleError> {
273        driver.source_order = out.len();
274        out.push(driver);
275        Ok(())
276    }
277
278    fn attach_labels(
279        &mut self,
280        union: &mut WirePointUnion,
281        labels: &[SchematicLabelDriver],
282        geometry: &SchematicConnectivityGeometry,
283        entry_index: &SchematicSegmentIndex,
284    ) -> Result<(), SourceBundleError> {
285        for label in labels {
286            self.attach_point(union, label.at, geometry.wire_index())?;
287            self.attach_point(union, label.at, geometry.bus_index())?;
288            self.attach_point(union, label.at, entry_index)?;
289        }
290        Ok(())
291    }
292
293    fn attach_point(
294        &mut self,
295        union: &mut WirePointUnion,
296        point: SchematicPoint,
297        index: &SchematicSegmentIndex,
298    ) -> Result<(), SourceBundleError> {
299        index.for_each_containing(point, |segment| {
300            if point == segment.a || point == segment.b {
301                return true;
302            }
303            union.union_points(point, segment.a);
304            union.union_points(point, segment.b);
305            true
306        })
307    }
308
309    fn collect_pins(
310        &mut self,
311        union: &mut WirePointUnion,
312    ) -> Result<Vec<SchematicPinDriver>, SourceBundleError> {
313        let effective = self.index.effective_symbols(self.occurrence_index, None)?;
314        let terminals = self.index.symbol_terminals(self.occurrence_index)?;
315        let terminals = terminals
316            .iter()
317            .filter(|terminal| {
318                self.include_off_board_symbols
319                    || effective
320                        .get(terminal.symbol_index)
321                        .is_some_and(|symbol| symbol.on_board)
322            })
323            .collect::<Vec<_>>();
324        let namings = build_pin_namings(
325            self.definition,
326            &terminals,
327            &effective,
328            self.subparts,
329            usize::MAX,
330        )?;
331        let mut out = Vec::new();
332        let mut hidden_nc_sequence = 0_i64;
333        for (terminal, naming) in terminals.iter().copied().zip(&namings) {
334            let Some(symbol) = effective.get(terminal.symbol_index) else {
335                continue;
336            };
337            let semantics = self.pin_semantics(terminal, symbol, &mut hidden_nc_sequence)?;
338            self.push_terminal_pins(&mut out, union, terminal, semantics, naming)?;
339        }
340        Ok(out)
341    }
342
343    fn pin_semantics(
344        &self,
345        terminal: &crate::SchematicSymbolTerminal,
346        symbol: &crate::SchematicEffectiveSymbol,
347        hidden_nc_sequence: &mut i64,
348    ) -> Result<PinSemantics, SourceBundleError> {
349        let placed = &self.definition.symbols[terminal.symbol_index];
350        let library = self.definition.library_symbol_for_placement(placed);
351        let power = classify_pin_power(terminal, symbol, library);
352        let at = self.pin_connectivity_point(terminal, hidden_nc_sequence)?;
353        Ok(PinSemantics {
354            at,
355            priority: power.priority,
356            power_value: power.power_value,
357            is_power: power.is_power,
358            implicit: power.implicit,
359        })
360    }
361
362    fn pin_connectivity_point(
363        &self,
364        terminal: &crate::SchematicSymbolTerminal,
365        hidden_nc_sequence: &mut i64,
366    ) -> Result<SchematicPoint, SourceBundleError> {
367        if terminal.hidden && terminal.electrical_type == "no_connect" {
368            let offset = 1_000_000_000_000_i64
369                .checked_add(*hidden_nc_sequence)
370                .ok_or_else(|| self.error("hidden no-connect sequence overflows"))?;
371            *hidden_nc_sequence = hidden_nc_sequence
372                .checked_add(1)
373                .ok_or_else(|| self.error("hidden no-connect sequence overflows"))?;
374            Ok(SchematicPoint {
375                x_iu: terminal
376                    .at
377                    .x_iu
378                    .checked_sub(offset)
379                    .ok_or_else(|| self.error("hidden no-connect coordinate overflows"))?,
380                y_iu: terminal.at.y_iu,
381            })
382        } else {
383            Ok(terminal.at)
384        }
385    }
386
387    fn push_terminal_pins(
388        &mut self,
389        out: &mut Vec<SchematicPinDriver>,
390        union: &mut WirePointUnion,
391        terminal: &crate::SchematicSymbolTerminal,
392        semantics: PinSemantics,
393        naming: &PinNaming,
394    ) -> Result<(), SourceBundleError> {
395        let expanded = expand_stacked_pin(&terminal.pin_number, &self.definition.source_path)?;
396        for pin_number in expanded {
397            let stacked = pin_number != terminal.pin_number;
398            let pin_name = if stacked && terminal.pin_name.is_empty() {
399                pin_number.clone()
400            } else if stacked {
401                format!("{}_{}", terminal.pin_name, pin_number)
402            } else {
403                terminal.pin_name.clone()
404            };
405            let normalized_number = if pin_number == "~" {
406                String::new()
407            } else {
408                pin_number
409            };
410            out.push(SchematicPinDriver {
411                symbol_index: terminal.symbol_index,
412                symbol_uuid: terminal.symbol_uuid.clone(),
413                reference: terminal.reference.clone(),
414                pin_number: normalized_number,
415                pin_name,
416                electrical_type: terminal.electrical_type.clone(),
417                hidden: terminal.hidden,
418                at: semantics.at,
419                priority: semantics.priority,
420                kind: pin_kind(semantics.priority),
421                power_value: semantics.power_value.clone(),
422                has_multiple: naming.has_multiple,
423                designator_with_unit: naming.designator_with_unit.clone(),
424                parent_pin_count: naming.parent_pin_count,
425                is_power: semantics.is_power,
426                is_implicit_hidden_power: semantics.implicit,
427                source_pin_uuid: naming.source_pin_uuid.clone(),
428                pin_svg_id: naming.pin_svg_id.clone(),
429                source_order: out.len(),
430            });
431        }
432        union.add(semantics.at, usize::MAX, self.definition)?;
433        Ok(())
434    }
435
436    fn merge_same_sheet_names(
437        &self,
438        union: &mut WirePointUnion,
439        labels: &[SchematicLabelDriver],
440        pins: &[SchematicPinDriver],
441    ) {
442        let mut by_name = HashMap::<&str, usize>::new();
443        for label in labels.iter().filter(|label| {
444            matches!(
445                label.kind,
446                SchematicWireDriverKind::LocalLabel | SchematicWireDriverKind::HierarchicalLabel
447            )
448        }) {
449            merge_named_point(&mut by_name, union, &label.text, label.at);
450        }
451        for pin in pins
452            .iter()
453            .filter(|pin| pin.is_power && !pin.power_value.is_empty())
454        {
455            merge_named_point(&mut by_name, union, &pin.power_value, pin.at);
456        }
457    }
458
459    fn merge_jumper_pins(
460        &self,
461        union: &mut WirePointUnion,
462        pins: &[SchematicPinDriver],
463    ) -> Result<(), SourceBundleError> {
464        let active_symbols = self
465            .definition
466            .symbols
467            .iter()
468            .enumerate()
469            .filter_map(|(index, placed)| {
470                self.definition
471                    .library_symbol_for_placement(placed)
472                    .filter(|library| {
473                        library.duplicate_pin_numbers_are_jumpers
474                            || !library.jumper_pin_groups.is_empty()
475                    })
476                    .map(|_| index)
477            })
478            .collect::<HashSet<_>>();
479        let mut by_symbol = HashMap::<usize, HashMap<&str, Vec<usize>>>::new();
480        for pin in pins
481            .iter()
482            .filter(|pin| active_symbols.contains(&pin.symbol_index))
483        {
484            let Some(root) = union.root_for_point(pin.at) else {
485                continue;
486            };
487            by_symbol
488                .entry(pin.symbol_index)
489                .or_default()
490                .entry(&pin.pin_number)
491                .or_default()
492                .push(root);
493        }
494        let mut work = 0_usize;
495        for (symbol_index, pins_by_number) in by_symbol {
496            let placed = &self.definition.symbols[symbol_index];
497            let Some(library) = self.definition.library_symbol_for_placement(placed) else {
498                continue;
499            };
500            if library.duplicate_pin_numbers_are_jumpers {
501                for roots in pins_by_number.values() {
502                    union_roots_bounded(
503                        union,
504                        roots.iter().copied(),
505                        &mut work,
506                        usize::MAX,
507                        self.definition,
508                    )?;
509                }
510            }
511            for group in &library.jumper_pin_groups {
512                let roots = group
513                    .iter()
514                    .filter_map(|number| pins_by_number.get(number.as_str()))
515                    .flatten()
516                    .copied();
517                union_roots_bounded(union, roots, &mut work, usize::MAX, self.definition)?;
518            }
519        }
520        Ok(())
521    }
522
523    fn merge_bus_member_taps(
524        &self,
525        union: &mut WirePointUnion,
526        labels: &[SchematicLabelDriver],
527        bus_subgraphs: &[crate::SchematicBusSubgraph],
528    ) -> Result<(), SourceBundleError> {
529        let mut label_by_root = HashMap::<usize, String>::new();
530        for label in labels
531            .iter()
532            .filter(|label| label.kind == SchematicWireDriverKind::LocalLabel)
533        {
534            if let Some(root) = union.root_for_point(label.at) {
535                label_by_root
536                    .entry(root)
537                    .or_insert_with(|| canonical_bus_member_name(&label.text));
538            }
539        }
540        for bus in bus_subgraphs.iter().filter(|bus| !bus.members.is_empty()) {
541            let members = bus
542                .members
543                .iter()
544                .map(|member| canonical_bus_member_name(member))
545                .collect::<HashSet<_>>();
546            let mut first_by_member = HashMap::<&str, usize>::new();
547            for tap in &bus.tap_wire_coords {
548                let Some(root) = union.root_for_point(*tap) else {
549                    continue;
550                };
551                let Some(member) = label_by_root.get(&root).map(String::as_str) else {
552                    continue;
553                };
554                if !members.contains(member) {
555                    continue;
556                }
557                if let Some(first) = first_by_member.get(member) {
558                    union.union(*first, root);
559                } else {
560                    first_by_member.insert(member, root);
561                }
562            }
563        }
564        Ok(())
565    }
566
567    fn realize_subgraphs(
568        &mut self,
569        union: &mut WirePointUnion,
570        pins: &mut Vec<SchematicPinDriver>,
571        labels: &mut Vec<SchematicLabelDriver>,
572    ) -> Result<Vec<SchematicWireSubgraph>, SourceBundleError> {
573        union.ensure_group_limit(usize::MAX, self.definition)?;
574        let mut pins_by_root = HashMap::<usize, Vec<SchematicPinDriver>>::new();
575        for pin in pins.drain(..) {
576            let root = union
577                .root_for_point(pin.at)
578                .ok_or_else(|| self.error("pin connectivity root is missing"))?;
579            pins_by_root.entry(root).or_default().push(pin);
580        }
581        let mut labels_by_root = HashMap::<usize, Vec<SchematicLabelDriver>>::new();
582        for label in labels.drain(..) {
583            let root = union
584                .root_for_point(label.at)
585                .ok_or_else(|| self.error("label connectivity root is missing"))?;
586            labels_by_root.entry(root).or_default().push(label);
587        }
588        let no_connects = self
589            .definition
590            .no_connects
591            .iter()
592            .map(|marker| marker.at)
593            .collect::<HashSet<_>>();
594        let mut graphical_by_root = self.index_source_graphics(union)?;
595        let groups = union.groups(usize::MAX, self.definition)?;
596        let mut out = Vec::with_capacity(groups.len());
597        for (root, mut coords) in groups {
598            coords.sort_unstable();
599            let pin_drivers = pins_by_root.remove(&root).unwrap_or_default();
600            let label_drivers = labels_by_root.remove(&root).unwrap_or_default();
601            let mut graphical = graphical_by_root.remove(&root).unwrap_or_default();
602            self.attach_driver_graphics(&mut graphical, &pin_drivers, &label_drivers)?;
603            let (chosen_name, chosen_priority, chosen_kind) =
604                resolve_driver(&label_drivers, &pin_drivers);
605            let no_connect = coords.iter().any(|point| no_connects.contains(point));
606            out.push(SchematicWireSubgraph {
607                coords,
608                pin_drivers,
609                label_drivers,
610                graphical,
611                chosen_name,
612                chosen_priority,
613                chosen_kind,
614                no_connect,
615            });
616        }
617        out.sort_by(|left, right| {
618            left.chosen_name.cmp(&right.chosen_name).then_with(|| {
619                left.coords
620                    .first()
621                    .copied()
622                    .cmp(&right.coords.first().copied())
623            })
624        });
625        Ok(out)
626    }
627
628    fn error(&self, message: &str) -> SourceBundleError {
629        SourceBundleError::new(
630            SourceBundleErrorKind::Schematic,
631            Some(&self.definition.source_path),
632            message,
633        )
634    }
635}
636
637fn classify_pin_power(
638    terminal: &crate::SchematicSymbolTerminal,
639    symbol: &crate::SchematicEffectiveSymbol,
640    library: Option<&crate::SchematicLibrarySymbol>,
641) -> PinPowerSemantics {
642    let parent_is_power = symbol.lib_id != "power:PWR_FLAG"
643        && (symbol.lib_id.starts_with("power:") || library.is_some_and(|value| value.power));
644    if implicit_hidden_power(terminal, parent_is_power) {
645        return PinPowerSemantics {
646            priority: SchematicDriverPriority::GlobalPowerPin,
647            power_value: terminal.pin_name.clone(),
648            is_power: true,
649            implicit: true,
650        };
651    }
652    let is_power = parent_is_power && terminal.electrical_type == "power_in";
653    let priority =
654        if is_power && library.is_some_and(|value| value.power_kind.as_deref() == Some("local")) {
655            SchematicDriverPriority::LocalPowerPin
656        } else if is_power {
657            SchematicDriverPriority::GlobalPowerPin
658        } else {
659            SchematicDriverPriority::Pin
660        };
661    PinPowerSemantics {
662        priority,
663        power_value: if is_power {
664            symbol.value.clone()
665        } else {
666            String::new()
667        },
668        is_power,
669        implicit: false,
670    }
671}
672
673fn implicit_hidden_power(terminal: &crate::SchematicSymbolTerminal, parent_is_power: bool) -> bool {
674    !parent_is_power
675        && terminal.electrical_type == "power_in"
676        && terminal.hidden
677        && !terminal.pin_name.is_empty()
678        && terminal.pin_name != "~"
679        && terminal.pin_name != terminal.pin_number
680}
681
682fn merge_named_point<'a>(
683    by_name: &mut HashMap<&'a str, usize>,
684    union: &mut WirePointUnion,
685    name: &'a str,
686    point: SchematicPoint,
687) {
688    if name.is_empty() {
689        return;
690    }
691    let Some(root) = union.root_for_point(point) else {
692        return;
693    };
694    if let Some(first) = by_name.get(name) {
695        union.union(*first, root);
696    } else {
697        by_name.insert(name, root);
698    }
699}
700
701fn union_roots_bounded(
702    union: &mut WirePointUnion,
703    roots: impl Iterator<Item = usize>,
704    work: &mut usize,
705    _max_work: usize,
706    definition: &crate::SchematicDefinition,
707) -> Result<(), SourceBundleError> {
708    let mut first = None;
709    for root in roots {
710        *work = work.checked_add(1).ok_or_else(|| {
711            SourceBundleError::new(
712                SourceBundleErrorKind::ResourceLimit,
713                Some(&definition.source_path),
714                "schematic jumper union work overflows",
715            )
716        })?;
717        if let Some(first) = first {
718            union.union(first, root);
719        } else {
720            first = Some(root);
721        }
722    }
723    Ok(())
724}
725
726use driver_types::{label_type, pin_kind};