Skip to main content

altium_format/api/generic/
value.rs

1//! Dynamic value type for Altium data.
2
3use crate::types::{Color, Coord, Layer, ParameterValue};
4
5/// Dynamic value type for Altium data, similar to `serde_json::Value`.
6///
7/// This enum represents any value that can appear in an Altium record,
8/// enabling dynamic access without compile-time type knowledge.
9#[derive(Debug, Clone, PartialEq, Default)]
10pub enum Value {
11    /// Null or missing value
12    #[default]
13    Null,
14    /// Boolean value
15    Bool(bool),
16    /// Integer value
17    Int(i64),
18    /// Floating-point value
19    Float(f64),
20    /// String value
21    String(String),
22    /// Coordinate value (preserves fractional precision)
23    Coord(Coord),
24    /// Color value (Win32 COLORREF)
25    Color(Color),
26    /// Layer value
27    Layer(Layer),
28    /// List of values (comma-separated in params)
29    List(Vec<Value>),
30    /// Raw binary data
31    Binary(Vec<u8>),
32}
33
34impl Value {
35    // --- Type checking ---
36
37    /// Returns true if this value is null.
38    pub fn is_null(&self) -> bool {
39        matches!(self, Value::Null)
40    }
41
42    /// Returns true if this value is a boolean.
43    pub fn is_bool(&self) -> bool {
44        matches!(self, Value::Bool(_))
45    }
46
47    /// Returns true if this value is an integer.
48    pub fn is_int(&self) -> bool {
49        matches!(self, Value::Int(_))
50    }
51
52    /// Returns true if this value is a float.
53    pub fn is_float(&self) -> bool {
54        matches!(self, Value::Float(_))
55    }
56
57    /// Returns true if this value is a string.
58    pub fn is_string(&self) -> bool {
59        matches!(self, Value::String(_))
60    }
61
62    /// Returns true if this value is a coordinate.
63    pub fn is_coord(&self) -> bool {
64        matches!(self, Value::Coord(_))
65    }
66
67    /// Returns true if this value is a color.
68    pub fn is_color(&self) -> bool {
69        matches!(self, Value::Color(_))
70    }
71
72    /// Returns true if this value is a layer.
73    pub fn is_layer(&self) -> bool {
74        matches!(self, Value::Layer(_))
75    }
76
77    /// Returns true if this value is a list.
78    pub fn is_list(&self) -> bool {
79        matches!(self, Value::List(_))
80    }
81
82    /// Returns true if this value is binary data.
83    pub fn is_binary(&self) -> bool {
84        matches!(self, Value::Binary(_))
85    }
86
87    // --- Conversion with Option ---
88
89    /// Returns the boolean value, if this is a Bool.
90    pub fn as_bool(&self) -> Option<bool> {
91        match self {
92            Value::Bool(b) => Some(*b),
93            _ => None,
94        }
95    }
96
97    /// Returns the integer value, if this is an Int.
98    pub fn as_int(&self) -> Option<i64> {
99        match self {
100            Value::Int(i) => Some(*i),
101            _ => None,
102        }
103    }
104
105    /// Returns the float value, if this is a Float.
106    pub fn as_float(&self) -> Option<f64> {
107        match self {
108            Value::Float(f) => Some(*f),
109            Value::Int(i) => Some(*i as f64),
110            _ => None,
111        }
112    }
113
114    /// Returns the string value, if this is a String.
115    pub fn as_str(&self) -> Option<&str> {
116        match self {
117            Value::String(s) => Some(s),
118            _ => None,
119        }
120    }
121
122    /// Returns the coordinate value, if this is a Coord.
123    pub fn as_coord(&self) -> Option<Coord> {
124        match self {
125            Value::Coord(c) => Some(*c),
126            _ => None,
127        }
128    }
129
130    /// Returns the color value, if this is a Color.
131    pub fn as_color(&self) -> Option<Color> {
132        match self {
133            Value::Color(c) => Some(*c),
134            _ => None,
135        }
136    }
137
138    /// Returns the layer value, if this is a Layer.
139    pub fn as_layer(&self) -> Option<Layer> {
140        match self {
141            Value::Layer(l) => Some(*l),
142            _ => None,
143        }
144    }
145
146    /// Returns the list value, if this is a List.
147    pub fn as_list(&self) -> Option<&[Value]> {
148        match self {
149            Value::List(l) => Some(l),
150            _ => None,
151        }
152    }
153
154    /// Returns the binary data, if this is Binary.
155    pub fn as_binary(&self) -> Option<&[u8]> {
156        match self {
157            Value::Binary(b) => Some(b),
158            _ => None,
159        }
160    }
161
162    // --- Conversion with defaults ---
163
164    /// Returns the boolean value, or a default.
165    pub fn as_bool_or(&self, default: bool) -> bool {
166        self.as_bool().unwrap_or(default)
167    }
168
169    /// Returns the integer value, or a default.
170    pub fn as_int_or(&self, default: i64) -> i64 {
171        self.as_int().unwrap_or(default)
172    }
173
174    /// Returns the float value, or a default.
175    pub fn as_float_or(&self, default: f64) -> f64 {
176        self.as_float().unwrap_or(default)
177    }
178
179    /// Returns the string value, or a default.
180    pub fn as_str_or<'a>(&'a self, default: &'a str) -> &'a str {
181        self.as_str().unwrap_or(default)
182    }
183
184    /// Returns the coordinate value, or a default.
185    pub fn as_coord_or(&self, default: Coord) -> Coord {
186        self.as_coord().unwrap_or(default)
187    }
188
189    // --- Construction from ParameterValue ---
190
191    /// Creates a Value from a ParameterValue, inferring the type.
192    pub fn from_param_value(pv: &ParameterValue) -> Self {
193        let s = pv.as_str();
194
195        // Try boolean
196        match s.to_uppercase().as_str() {
197            "T" | "TRUE" => return Value::Bool(true),
198            "F" | "FALSE" => return Value::Bool(false),
199            _ => {}
200        }
201
202        // Try integer
203        if let Ok(i) = s.parse::<i64>() {
204            return Value::Int(i);
205        }
206
207        // Try float
208        if let Ok(f) = s.parse::<f64>() {
209            return Value::Float(f);
210        }
211
212        // Try coordinate (ends with unit)
213        if s.ends_with("mil") || s.ends_with("mm") || s.ends_with("in") {
214            if let Ok(c) = pv.as_coord() {
215                return Value::Coord(c);
216            }
217        }
218
219        // Default to string
220        Value::String(s.to_string())
221    }
222
223    /// Returns a human-readable type name.
224    pub fn type_name(&self) -> &'static str {
225        match self {
226            Value::Null => "null",
227            Value::Bool(_) => "bool",
228            Value::Int(_) => "int",
229            Value::Float(_) => "float",
230            Value::String(_) => "string",
231            Value::Coord(_) => "coord",
232            Value::Color(_) => "color",
233            Value::Layer(_) => "layer",
234            Value::List(_) => "list",
235            Value::Binary(_) => "binary",
236        }
237    }
238}
239
240// --- From implementations ---
241
242impl From<bool> for Value {
243    fn from(v: bool) -> Self {
244        Value::Bool(v)
245    }
246}
247
248impl From<i32> for Value {
249    fn from(v: i32) -> Self {
250        Value::Int(v as i64)
251    }
252}
253
254impl From<i64> for Value {
255    fn from(v: i64) -> Self {
256        Value::Int(v)
257    }
258}
259
260impl From<f64> for Value {
261    fn from(v: f64) -> Self {
262        Value::Float(v)
263    }
264}
265
266impl From<String> for Value {
267    fn from(v: String) -> Self {
268        Value::String(v)
269    }
270}
271
272impl From<&str> for Value {
273    fn from(v: &str) -> Self {
274        Value::String(v.to_string())
275    }
276}
277
278impl From<Coord> for Value {
279    fn from(v: Coord) -> Self {
280        Value::Coord(v)
281    }
282}
283
284impl From<Color> for Value {
285    fn from(v: Color) -> Self {
286        Value::Color(v)
287    }
288}
289
290impl From<Layer> for Value {
291    fn from(v: Layer) -> Self {
292        Value::Layer(v)
293    }
294}
295
296impl<T: Into<Value>> From<Vec<T>> for Value {
297    fn from(v: Vec<T>) -> Self {
298        Value::List(v.into_iter().map(Into::into).collect())
299    }
300}
301
302impl std::fmt::Display for Value {
303    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
304        match self {
305            Value::Null => write!(f, "null"),
306            Value::Bool(b) => write!(f, "{}", if *b { "T" } else { "F" }),
307            Value::Int(i) => write!(f, "{}", i),
308            Value::Float(v) => write!(f, "{}", v),
309            Value::String(s) => write!(f, "{}", s),
310            Value::Coord(c) => write!(f, "{}mil", c.to_mils()),
311            Value::Color(c) => write!(f, "{}", c.to_win32()),
312            Value::Layer(l) => write!(f, "{}", l.0),
313            Value::List(l) => {
314                let items: Vec<String> = l.iter().map(|v| v.to_string()).collect();
315                write!(f, "{}", items.join(","))
316            }
317            Value::Binary(b) => write!(f, "<{} bytes>", b.len()),
318        }
319    }
320}
321
322#[cfg(test)]
323mod tests {
324    use super::*;
325
326    #[test]
327    fn test_value_type_checks() {
328        assert!(Value::Null.is_null());
329        assert!(Value::Bool(true).is_bool());
330        assert!(Value::Int(42).is_int());
331        assert!(Value::String("test".into()).is_string());
332    }
333
334    #[test]
335    fn test_value_conversions() {
336        let v = Value::Int(42);
337        assert_eq!(v.as_int(), Some(42));
338        assert_eq!(v.as_float(), Some(42.0));
339        assert_eq!(v.as_str(), None);
340    }
341
342    #[test]
343    fn test_value_from() {
344        let v: Value = 42i32.into();
345        assert_eq!(v, Value::Int(42));
346
347        let v: Value = "test".into();
348        assert_eq!(v, Value::String("test".into()));
349    }
350}