altium-format 0.1.7

Core altium-cli library for reading and writing Altium Designer files.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//! Selector AST types for querying schematic records.
//!
//! The selector syntax provides ergonomic shortcuts for common queries:
//!
//! | Pattern | Meaning |
//! |---------|---------|
//! | `U1` | Component by designator |
//! | `R*` | Components matching pattern |
//! | `U1:3` | Pin by number |
//! | `U1:VCC` | Pin by name |
//! | `$LM358` | Component by part number |
//! | `~VCC` | Net by name |
//! | `@10K` | Component by value |
//! | `#Power` | Sheet by name |

use super::common::FilterOp;
use super::pattern::Pattern;

/// A parsed selector, which may contain multiple alternatives (comma-separated).
#[derive(Debug, Clone)]
pub struct Selector {
    /// Union of selector chains (comma-separated alternatives).
    pub alternatives: Vec<SelectorChain>,
}

impl Selector {
    /// Create a selector with a single chain.
    pub fn single(chain: SelectorChain) -> Self {
        Self {
            alternatives: vec![chain],
        }
    }

    /// Create a selector matching any record.
    pub fn any() -> Self {
        Self::single(SelectorChain::single(SelectorSegment::any()))
    }

    /// Returns true if this selector has no segments.
    pub fn is_empty(&self) -> bool {
        self.alternatives.is_empty() || self.alternatives.iter().all(|c| c.segments.is_empty())
    }
}

/// A chain of selector segments connected by combinators.
#[derive(Debug, Clone)]
pub struct SelectorChain {
    /// Segments in this chain, each connected to the next by its combinator.
    pub segments: Vec<SelectorSegment>,
}

impl SelectorChain {
    /// Create a chain with a single segment.
    pub fn single(segment: SelectorSegment) -> Self {
        Self {
            segments: vec![segment],
        }
    }

    /// Create an empty chain.
    pub fn empty() -> Self {
        Self { segments: vec![] }
    }

    /// Add a segment to this chain.
    pub fn push(&mut self, segment: SelectorSegment) {
        self.segments.push(segment);
    }
}

/// A single segment in a selector chain.
#[derive(Debug, Clone)]
pub struct SelectorSegment {
    /// What type of record to match.
    pub matcher: RecordMatcher,
    /// Property filters `[prop op value]`.
    pub filters: Vec<PropertyFilter>,
    /// Pseudo-selectors like `:connected`, `:has()`.
    pub pseudo: Vec<PseudoSelector>,
    /// How this segment connects to the next (if any).
    pub combinator: Option<Combinator>,
}

impl SelectorSegment {
    /// Create a segment matching any record.
    pub fn any() -> Self {
        Self {
            matcher: RecordMatcher::Any,
            filters: vec![],
            pseudo: vec![],
            combinator: None,
        }
    }

    /// Create a segment from a matcher.
    pub fn from_matcher(matcher: RecordMatcher) -> Self {
        Self {
            matcher,
            filters: vec![],
            pseudo: vec![],
            combinator: None,
        }
    }

    /// Add a property filter to this segment.
    pub fn with_filter(mut self, filter: PropertyFilter) -> Self {
        self.filters.push(filter);
        self
    }

    /// Add a pseudo-selector to this segment.
    pub fn with_pseudo(mut self, pseudo: PseudoSelector) -> Self {
        self.pseudo.push(pseudo);
        self
    }

    /// Set the combinator for this segment.
    pub fn with_combinator(mut self, combinator: Combinator) -> Self {
        self.combinator = Some(combinator);
        self
    }
}

/// What records a segment matches.
#[derive(Debug, Clone)]
pub enum RecordMatcher {
    /// Match any record type: `*`
    Any,

    /// Match specific record type: `component`, `pin`, `wire`
    Type(RecordType),

    /// Match by designator pattern: `U1`, `R*`, `C??`
    Designator(Pattern),

    /// Match by part number / library reference: `$LM358`, `$STM32*`
    PartNumber(Pattern),

    /// Match by net name: `~VCC`, `~SPI_*`
    Net(Pattern),

    /// Match by value parameter: `@10K`, `@100nF`
    Value(Pattern),

    /// Match by sheet name: `#Power`, `#Analog`
    Sheet(Pattern),

    /// Pin access: `U1:3`, `U1:VCC`, `R*:1`
    Pin {
        /// Component designator pattern
        component: Pattern,
        /// Pin designator or name pattern
        pin: Pattern,
    },

    /// Net connectivity query: `~VCC:pins`, `~GND:components`
    NetConnected {
        /// Net name pattern
        net: Pattern,
        /// What to return
        target: NetConnectedTarget,
    },

    /// Combined designator + value: `R*@10K`
    DesignatorWithValue {
        /// Designator pattern
        designator: Pattern,
        /// Value pattern
        value: Pattern,
    },
}

/// Target for net connectivity queries.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NetConnectedTarget {
    /// Return pins connected to the net
    Pins,
    /// Return components connected to the net
    Components,
}

/// Record types that can be matched.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RecordType {
    Component,
    Pin,
    Wire,
    NetLabel,
    Port,
    PowerObject,
    Junction,
    Label,
    Rectangle,
    Line,
    Arc,
    Ellipse,
    Polygon,
    Polyline,
    Bezier,
    Image,
    Parameter,
    Sheet,
    Symbol,
    Designator,
    TextFrame,
}

impl RecordType {
    /// Parse a record type from a string.
    pub fn try_parse(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "component" => Some(Self::Component),
            "pin" => Some(Self::Pin),
            "wire" => Some(Self::Wire),
            "netlabel" | "net_label" => Some(Self::NetLabel),
            "port" => Some(Self::Port),
            "power" | "powerobject" | "power_object" => Some(Self::PowerObject),
            "junction" => Some(Self::Junction),
            "label" => Some(Self::Label),
            "rectangle" | "rect" => Some(Self::Rectangle),
            "line" => Some(Self::Line),
            "arc" => Some(Self::Arc),
            "ellipse" => Some(Self::Ellipse),
            "polygon" => Some(Self::Polygon),
            "polyline" => Some(Self::Polyline),
            "bezier" => Some(Self::Bezier),
            "image" => Some(Self::Image),
            "parameter" | "param" => Some(Self::Parameter),
            "sheet" => Some(Self::Sheet),
            "symbol" => Some(Self::Symbol),
            "designator" => Some(Self::Designator),
            "textframe" | "text_frame" => Some(Self::TextFrame),
            _ => None,
        }
    }

    /// Get the string name of this record type.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Component => "component",
            Self::Pin => "pin",
            Self::Wire => "wire",
            Self::NetLabel => "netlabel",
            Self::Port => "port",
            Self::PowerObject => "power",
            Self::Junction => "junction",
            Self::Label => "label",
            Self::Rectangle => "rectangle",
            Self::Line => "line",
            Self::Arc => "arc",
            Self::Ellipse => "ellipse",
            Self::Polygon => "polygon",
            Self::Polyline => "polyline",
            Self::Bezier => "bezier",
            Self::Image => "image",
            Self::Parameter => "parameter",
            Self::Sheet => "sheet",
            Self::Symbol => "symbol",
            Self::Designator => "designator",
            Self::TextFrame => "textframe",
        }
    }
}

/// A property filter like `[rotation=90]` or `[x>1000]`.
#[derive(Debug, Clone)]
pub struct PropertyFilter {
    /// Property name (case-insensitive)
    pub property: String,
    /// Comparison operator
    pub operator: FilterOperator,
    /// Value to compare against
    pub value: FilterValue,
}

impl PropertyFilter {
    /// Create a new property filter.
    pub fn new(property: impl Into<String>, operator: FilterOperator, value: FilterValue) -> Self {
        Self {
            property: property.into(),
            operator,
            value,
        }
    }

    /// Create an equality filter.
    pub fn eq(property: impl Into<String>, value: impl Into<String>) -> Self {
        Self::new(
            property,
            FilterOperator::Equal,
            FilterValue::String(value.into()),
        )
    }

    /// Create a wildcard filter.
    pub fn matches(property: impl Into<String>, pattern: Pattern) -> Self {
        Self::new(
            property,
            FilterOperator::Wildcard,
            FilterValue::Pattern(pattern),
        )
    }
}

/// Comparison operators for property filters.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FilterOperator {
    /// Exact match: `=`
    Equal,
    /// Not equal: `!=`
    NotEqual,
    /// Wildcard/pattern match: `~=`
    Wildcard,
    /// Starts with: `^=`
    StartsWith,
    /// Ends with: `$=`
    EndsWith,
    /// Contains: `*=`
    Contains,
    /// Greater than (numeric): `>`
    GreaterThan,
    /// Less than (numeric): `<`
    LessThan,
    /// Greater or equal (numeric): `>=`
    GreaterOrEqual,
    /// Less or equal (numeric): `<=`
    LessOrEqual,
}

impl FilterOperator {
    /// Parse an operator from its string representation.
    pub fn try_parse(s: &str) -> Option<Self> {
        match s {
            "=" => Some(Self::Equal),
            "!=" => Some(Self::NotEqual),
            "~=" => Some(Self::Wildcard),
            "^=" => Some(Self::StartsWith),
            "$=" => Some(Self::EndsWith),
            "*=" => Some(Self::Contains),
            ">" => Some(Self::GreaterThan),
            "<" => Some(Self::LessThan),
            ">=" => Some(Self::GreaterOrEqual),
            "<=" => Some(Self::LessOrEqual),
            _ => None,
        }
    }

    /// Convert to shared FilterOp type.
    pub fn to_filter_op(&self) -> FilterOp {
        match self {
            Self::Equal => FilterOp::Equals,
            Self::NotEqual => FilterOp::NotEquals,
            Self::Wildcard => FilterOp::WordMatch, // Closest equivalent
            Self::StartsWith => FilterOp::StartsWith,
            Self::EndsWith => FilterOp::EndsWith,
            Self::Contains => FilterOp::Contains,
            Self::GreaterThan => FilterOp::GreaterThan,
            Self::LessThan => FilterOp::LessThan,
            Self::GreaterOrEqual => FilterOp::GreaterOrEqual,
            Self::LessOrEqual => FilterOp::LessOrEqual,
        }
    }

    /// Create from shared FilterOp type.
    pub fn from_filter_op(op: FilterOp) -> Self {
        match op {
            FilterOp::Exists => Self::Equal, // No direct equivalent, use equal
            FilterOp::Equals => Self::Equal,
            FilterOp::NotEquals => Self::NotEqual,
            FilterOp::WordMatch => Self::Wildcard,
            FilterOp::StartsWith => Self::StartsWith,
            FilterOp::EndsWith => Self::EndsWith,
            FilterOp::Contains => Self::Contains,
            FilterOp::GreaterThan => Self::GreaterThan,
            FilterOp::LessThan => Self::LessThan,
            FilterOp::GreaterOrEqual => Self::GreaterOrEqual,
            FilterOp::LessOrEqual => Self::LessOrEqual,
        }
    }
}

/// Value in a property filter.
#[derive(Debug, Clone)]
pub enum FilterValue {
    /// String value
    String(String),
    /// Numeric value
    Number(f64),
    /// Boolean value
    Bool(bool),
    /// Pattern for wildcard matching
    Pattern(Pattern),
}

/// Pseudo-selectors for additional filtering.
#[derive(Debug, Clone)]
pub enum PseudoSelector {
    // === Hierarchy ===
    /// Top-level records with no parent
    Root,
    /// Records with no children
    Empty,
    /// First child of parent
    FirstChild,
    /// Last child of parent
    LastChild,
    /// Nth child (1-indexed)
    NthChild(usize),
    /// Only child of parent
    OnlyChild,

    // === Electrical state (for pins) ===
    /// Connected to a net
    Connected,
    /// Not connected to any net
    Unconnected,
    /// Input pin
    Input,
    /// Output pin
    Output,
    /// Bidirectional pin
    Bidirectional,
    /// Power pin
    Power,
    /// Passive pin
    Passive,
    /// Open collector pin
    OpenCollector,
    /// Open emitter pin
    OpenEmitter,
    /// High-Z pin
    HiZ,

    // === Visibility ===
    /// Not hidden
    Visible,
    /// Hidden records
    Hidden,
    /// Currently selected (UI state)
    Selected,

    // === Combinatorial ===
    /// Does not match the given selector
    Not(Box<Selector>),
    /// Contains a descendant matching the given selector
    Has(Box<Selector>),
    /// Matches any of the given selectors (grouping)
    Is(Box<Selector>),
}

impl PseudoSelector {
    /// Parse a pseudo-selector from its name.
    pub fn from_name(name: &str) -> Option<Self> {
        match name.to_lowercase().as_str() {
            "root" => Some(Self::Root),
            "empty" => Some(Self::Empty),
            "first-child" | "firstchild" => Some(Self::FirstChild),
            "last-child" | "lastchild" => Some(Self::LastChild),
            "only-child" | "onlychild" => Some(Self::OnlyChild),
            "connected" => Some(Self::Connected),
            "unconnected" => Some(Self::Unconnected),
            "input" => Some(Self::Input),
            "output" => Some(Self::Output),
            "bidirectional" | "bidir" => Some(Self::Bidirectional),
            "power" => Some(Self::Power),
            "passive" => Some(Self::Passive),
            "open-collector" | "opencollector" | "oc" => Some(Self::OpenCollector),
            "open-emitter" | "openemitter" | "oe" => Some(Self::OpenEmitter),
            "hiz" | "hi-z" | "high-z" => Some(Self::HiZ),
            "visible" => Some(Self::Visible),
            "hidden" => Some(Self::Hidden),
            "selected" => Some(Self::Selected),
            _ => None,
        }
    }
}

/// Combinators connecting selector segments.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Combinator {
    /// Descendant (any depth): ` ` (space)
    Descendant,
    /// Direct child only: `>` or `/`
    DirectChild,
}