altium_format/api/generic/
value.rs1use crate::types::{Color, Coord, Layer, ParameterValue};
4
5#[derive(Debug, Clone, PartialEq, Default)]
10pub enum Value {
11 #[default]
13 Null,
14 Bool(bool),
16 Int(i64),
18 Float(f64),
20 String(String),
22 Coord(Coord),
24 Color(Color),
26 Layer(Layer),
28 List(Vec<Value>),
30 Binary(Vec<u8>),
32}
33
34impl Value {
35 pub fn is_null(&self) -> bool {
39 matches!(self, Value::Null)
40 }
41
42 pub fn is_bool(&self) -> bool {
44 matches!(self, Value::Bool(_))
45 }
46
47 pub fn is_int(&self) -> bool {
49 matches!(self, Value::Int(_))
50 }
51
52 pub fn is_float(&self) -> bool {
54 matches!(self, Value::Float(_))
55 }
56
57 pub fn is_string(&self) -> bool {
59 matches!(self, Value::String(_))
60 }
61
62 pub fn is_coord(&self) -> bool {
64 matches!(self, Value::Coord(_))
65 }
66
67 pub fn is_color(&self) -> bool {
69 matches!(self, Value::Color(_))
70 }
71
72 pub fn is_layer(&self) -> bool {
74 matches!(self, Value::Layer(_))
75 }
76
77 pub fn is_list(&self) -> bool {
79 matches!(self, Value::List(_))
80 }
81
82 pub fn is_binary(&self) -> bool {
84 matches!(self, Value::Binary(_))
85 }
86
87 pub fn as_bool(&self) -> Option<bool> {
91 match self {
92 Value::Bool(b) => Some(*b),
93 _ => None,
94 }
95 }
96
97 pub fn as_int(&self) -> Option<i64> {
99 match self {
100 Value::Int(i) => Some(*i),
101 _ => None,
102 }
103 }
104
105 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 pub fn as_str(&self) -> Option<&str> {
116 match self {
117 Value::String(s) => Some(s),
118 _ => None,
119 }
120 }
121
122 pub fn as_coord(&self) -> Option<Coord> {
124 match self {
125 Value::Coord(c) => Some(*c),
126 _ => None,
127 }
128 }
129
130 pub fn as_color(&self) -> Option<Color> {
132 match self {
133 Value::Color(c) => Some(*c),
134 _ => None,
135 }
136 }
137
138 pub fn as_layer(&self) -> Option<Layer> {
140 match self {
141 Value::Layer(l) => Some(*l),
142 _ => None,
143 }
144 }
145
146 pub fn as_list(&self) -> Option<&[Value]> {
148 match self {
149 Value::List(l) => Some(l),
150 _ => None,
151 }
152 }
153
154 pub fn as_binary(&self) -> Option<&[u8]> {
156 match self {
157 Value::Binary(b) => Some(b),
158 _ => None,
159 }
160 }
161
162 pub fn as_bool_or(&self, default: bool) -> bool {
166 self.as_bool().unwrap_or(default)
167 }
168
169 pub fn as_int_or(&self, default: i64) -> i64 {
171 self.as_int().unwrap_or(default)
172 }
173
174 pub fn as_float_or(&self, default: f64) -> f64 {
176 self.as_float().unwrap_or(default)
177 }
178
179 pub fn as_str_or<'a>(&'a self, default: &'a str) -> &'a str {
181 self.as_str().unwrap_or(default)
182 }
183
184 pub fn as_coord_or(&self, default: Coord) -> Coord {
186 self.as_coord().unwrap_or(default)
187 }
188
189 pub fn from_param_value(pv: &ParameterValue) -> Self {
193 let s = pv.as_str();
194
195 match s.to_uppercase().as_str() {
197 "T" | "TRUE" => return Value::Bool(true),
198 "F" | "FALSE" => return Value::Bool(false),
199 _ => {}
200 }
201
202 if let Ok(i) = s.parse::<i64>() {
204 return Value::Int(i);
205 }
206
207 if let Ok(f) = s.parse::<f64>() {
209 return Value::Float(f);
210 }
211
212 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 Value::String(s.to_string())
221 }
222
223 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
240impl 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}