altium_format/traits/mod.rs
1//! Core traits for Altium record serialization and deserialization.
2//!
3//! This module defines the fundamental traits used by the derive macros to
4//! generate serialization code for Altium record types.
5
6mod binary;
7mod conversion;
8mod params;
9
10pub use binary::{FromBinary, ToBinary};
11pub use conversion::{FromParamList, FromParamValue, ToParamList, ToParamValue};
12pub use params::{FromParams, ToParams};
13
14use crate::types::{CoordPoint, CoordRect, Layer};
15
16/// Marker trait for all Altium record types.
17///
18/// This trait provides common metadata about record types and is automatically
19/// implemented by the `AltiumRecord` derive macro.
20pub trait AltiumRecord: Sized + Clone + Default {
21 /// Human-readable name for this record type.
22 fn record_type_name() -> &'static str;
23
24 /// Whether this record type supports unknown field preservation.
25 fn supports_unknown_preservation() -> bool {
26 true
27 }
28}
29
30/// Trait for schematic primitives.
31///
32/// Implemented by all schematic record types that have a record ID.
33pub trait SchPrimitive: AltiumRecord + FromParams + ToParams {
34 /// The record type ID (e.g., 1 for Component, 2 for Pin).
35 const RECORD_ID: i32;
36
37 /// Get the owner index (parent reference in the hierarchy).
38 fn owner_index(&self) -> i32;
39
40 /// Set the owner index.
41 fn set_owner_index(&mut self, index: i32);
42
43 /// Calculate the bounding rectangle of this primitive.
44 fn calculate_bounds(&self) -> CoordRect;
45
46 /// Get the location of this primitive (if applicable).
47 fn location(&self) -> Option<CoordPoint> {
48 None
49 }
50
51 /// Get the human-readable record type name.
52 fn record_type_name(&self) -> &'static str;
53
54 /// Get a property value by name (for dynamic access).
55 fn get_property(&self, _name: &str) -> Option<String> {
56 None
57 }
58}
59
60/// Trait for PCB primitives.
61///
62/// Implemented by all PCB record types that have an object ID.
63pub trait PcbPrimitive: AltiumRecord + FromBinary + ToBinary {
64 /// The object ID for this primitive type.
65 const OBJECT_ID: crate::records::pcb::PcbObjectId;
66
67 /// Get the layer this primitive is on.
68 fn layer(&self) -> Layer;
69
70 /// Calculate the bounding rectangle of this primitive.
71 fn calculate_bounds(&self) -> CoordRect;
72}
73
74/// Trait for primitives that have a location.
75pub trait Locatable {
76 /// Get the location as (x, y) raw coordinate values.
77 fn location(&self) -> (i32, i32);
78
79 /// Set the location.
80 fn set_location(&mut self, x: i32, y: i32);
81}
82
83/// Trait for graphical primitives with color properties.
84pub trait Graphical: Locatable {
85 /// Get the primary color.
86 fn color(&self) -> i32;
87
88 /// Set the primary color.
89 fn set_color(&mut self, color: i32);
90
91 /// Get the area/fill color.
92 fn area_color(&self) -> i32;
93
94 /// Set the area/fill color.
95 fn set_area_color(&mut self, color: i32);
96}
97
98/// Trait for container records that can have children.
99pub trait Container: AltiumRecord {
100 /// Type of children this container can hold.
101 type Child: AltiumRecord;
102
103 /// Whether this is a root container (like SchComponent).
104 fn is_root_container() -> bool {
105 false
106 }
107}