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

kicad_monkey_core/pcb/
selection.rs

1//! Dependency-aware PCB family selection.
2
3/// One independently requestable native PCB model family.
4#[derive(Clone, Copy, Debug, Eq, PartialEq)]
5#[repr(u8)]
6pub enum PcbFamily {
7    Layers,
8    Setup,
9    Nets,
10    Properties,
11    Footprints,
12    FootprintProperties,
13    FootprintGraphics,
14    FootprintTexts,
15    FootprintTextBoxes,
16    Pads,
17    Models,
18    Segments,
19    Vias,
20    Zones,
21    Arcs,
22    Graphics,
23    Dimensions,
24    Groups,
25    GeneratedItems,
26    EmbeddedFiles,
27    Variants,
28    Images,
29    Barcodes,
30    Tables,
31    Holes,
32    FootprintTransforms,
33    Profile,
34    /// Embedded resource declarations owned by board footprints.
35    FootprintEmbeddedFiles,
36    /// Zones and rule areas owned by board footprints.
37    FootprintZones,
38}
39
40/// A compact, allocation-free set of requested PCB families.
41#[derive(Clone, Copy, Debug, Eq, PartialEq)]
42pub struct PcbSelection(u32);
43
44impl PcbSelection {
45    const FAMILY_COUNT: u32 = PcbFamily::FootprintZones as u32 + 1;
46    const ALL_MASK: u32 = (1 << Self::FAMILY_COUNT) - 1;
47
48    pub const fn none() -> Self {
49        Self(0)
50    }
51
52    pub const fn all() -> Self {
53        Self(Self::ALL_MASK)
54    }
55
56    pub const fn only(family: PcbFamily) -> Self {
57        Self(family_mask(family))
58    }
59
60    #[must_use]
61    pub const fn with(self, family: PcbFamily) -> Self {
62        Self(self.0 | family_mask(family))
63    }
64
65    pub const fn contains(self, family: PcbFamily) -> bool {
66        self.0 & family_mask(family) != 0
67    }
68
69    pub(super) const fn dependencies(self) -> Self {
70        let mut result = self;
71        if self.intersects(Self::FOOTPRINT_DEPENDENTS) {
72            result = result.with(PcbFamily::Footprints);
73        }
74        if self.contains(PcbFamily::Holes) {
75            result = result.with(PcbFamily::Pads).with(PcbFamily::Vias);
76        }
77        if self.contains(PcbFamily::Profile) {
78            result = result.with(PcbFamily::Graphics);
79        }
80        if self.intersects(Self::NET_DEPENDENTS) {
81            result = result.with(PcbFamily::Nets);
82        }
83        result
84    }
85
86    const FOOTPRINT_DEPENDENTS: Self = Self(
87        family_mask(PcbFamily::Pads)
88            | family_mask(PcbFamily::Models)
89            | family_mask(PcbFamily::FootprintProperties)
90            | family_mask(PcbFamily::FootprintGraphics)
91            | family_mask(PcbFamily::FootprintTexts)
92            | family_mask(PcbFamily::FootprintTextBoxes)
93            | family_mask(PcbFamily::Holes)
94            | family_mask(PcbFamily::FootprintTransforms)
95            | family_mask(PcbFamily::FootprintEmbeddedFiles)
96            | family_mask(PcbFamily::FootprintZones)
97            | family_mask(PcbFamily::Profile),
98    );
99
100    const NET_DEPENDENTS: Self = Self(
101        family_mask(PcbFamily::Pads)
102            | family_mask(PcbFamily::Segments)
103            | family_mask(PcbFamily::Vias)
104            | family_mask(PcbFamily::Zones)
105            | family_mask(PcbFamily::FootprintZones)
106            | family_mask(PcbFamily::Arcs),
107    );
108
109    const fn intersects(self, other: Self) -> bool {
110        self.0 & other.0 != 0
111    }
112}
113
114impl Default for PcbSelection {
115    fn default() -> Self {
116        Self::all()
117    }
118}
119
120const fn family_mask(family: PcbFamily) -> u32 {
121    1 << family as u32
122}