dbc_rs/attribute/
impls.rs

1//! Accessor implementations for attribute types.
2
3use super::{
4    AttributeDefinition, AttributeObjectType, AttributeValue, AttributeValueType, EnumValues,
5};
6
7// ============================================================================
8// AttributeObjectType
9// ============================================================================
10
11impl AttributeObjectType {
12    /// Returns the DBC keyword for this object type.
13    ///
14    /// Returns an empty string for Network (global) attributes.
15    #[inline]
16    #[must_use]
17    pub fn as_str(&self) -> &'static str {
18        match self {
19            Self::Network => "",
20            Self::Node => "BU_",
21            Self::Message => "BO_",
22            Self::Signal => "SG_",
23        }
24    }
25
26    /// Returns true if this is a network-level attribute.
27    #[inline]
28    #[must_use]
29    pub fn is_network(&self) -> bool {
30        matches!(self, Self::Network)
31    }
32
33    /// Returns true if this is a node attribute.
34    #[inline]
35    #[must_use]
36    pub fn is_node(&self) -> bool {
37        matches!(self, Self::Node)
38    }
39
40    /// Returns true if this is a message attribute.
41    #[inline]
42    #[must_use]
43    pub fn is_message(&self) -> bool {
44        matches!(self, Self::Message)
45    }
46
47    /// Returns true if this is a signal attribute.
48    #[inline]
49    #[must_use]
50    pub fn is_signal(&self) -> bool {
51        matches!(self, Self::Signal)
52    }
53}
54
55// ============================================================================
56// AttributeValueType
57// ============================================================================
58
59impl AttributeValueType {
60    /// Returns true if this is an integer type (INT or HEX).
61    #[inline]
62    #[must_use]
63    pub fn is_int(&self) -> bool {
64        matches!(self, Self::Int { .. } | Self::Hex { .. })
65    }
66
67    /// Returns true if this is a float type.
68    #[inline]
69    #[must_use]
70    pub fn is_float(&self) -> bool {
71        matches!(self, Self::Float { .. })
72    }
73
74    /// Returns true if this is a string type.
75    #[inline]
76    #[must_use]
77    pub fn is_string(&self) -> bool {
78        matches!(self, Self::String)
79    }
80
81    /// Returns true if this is an enum type.
82    #[inline]
83    #[must_use]
84    pub fn is_enum(&self) -> bool {
85        matches!(self, Self::Enum { .. })
86    }
87
88    /// Returns the enum values if this is an enum type.
89    #[inline]
90    #[must_use]
91    pub fn enum_values(&self) -> Option<&EnumValues> {
92        match self {
93            Self::Enum { values } => Some(values),
94            _ => None,
95        }
96    }
97
98    /// Returns the integer range if this is an INT or HEX type.
99    #[inline]
100    #[must_use]
101    pub fn int_range(&self) -> Option<(i64, i64)> {
102        match self {
103            Self::Int { min, max } | Self::Hex { min, max } => Some((*min, *max)),
104            _ => None,
105        }
106    }
107
108    /// Returns the float range if this is a FLOAT type.
109    #[inline]
110    #[must_use]
111    pub fn float_range(&self) -> Option<(f64, f64)> {
112        match self {
113            Self::Float { min, max } => Some((*min, *max)),
114            _ => None,
115        }
116    }
117}
118
119// ============================================================================
120// AttributeValue
121// ============================================================================
122
123impl AttributeValue {
124    /// Gets the value as an integer, if applicable.
125    #[inline]
126    #[must_use]
127    pub fn as_int(&self) -> Option<i64> {
128        match self {
129            Self::Int(v) => Some(*v),
130            _ => None,
131        }
132    }
133
134    /// Gets the value as a float, if applicable.
135    #[inline]
136    #[must_use]
137    pub fn as_float(&self) -> Option<f64> {
138        match self {
139            Self::Float(v) => Some(*v),
140            _ => None,
141        }
142    }
143
144    /// Gets the value as a string, if applicable.
145    ///
146    /// This returns the string for both STRING and ENUM attribute values.
147    #[inline]
148    #[must_use]
149    pub fn as_string(&self) -> Option<&str> {
150        match self {
151            Self::String(s) => Some(s.as_str()),
152            _ => None,
153        }
154    }
155
156    /// Returns true if this is an integer value.
157    #[inline]
158    #[must_use]
159    pub fn is_int(&self) -> bool {
160        matches!(self, Self::Int(_))
161    }
162
163    /// Returns true if this is a float value.
164    #[inline]
165    #[must_use]
166    pub fn is_float(&self) -> bool {
167        matches!(self, Self::Float(_))
168    }
169
170    /// Returns true if this is a string value.
171    #[inline]
172    #[must_use]
173    pub fn is_string(&self) -> bool {
174        matches!(self, Self::String(_))
175    }
176}
177
178// ============================================================================
179// AttributeDefinition
180// ============================================================================
181
182impl AttributeDefinition {
183    /// Returns the attribute name.
184    #[inline]
185    #[must_use]
186    pub fn name(&self) -> &str {
187        self.name.as_str()
188    }
189
190    /// Returns the object type this attribute applies to.
191    #[inline]
192    #[must_use]
193    pub fn object_type(&self) -> AttributeObjectType {
194        self.object_type
195    }
196
197    /// Returns the value type specification.
198    #[inline]
199    #[must_use]
200    pub fn value_type(&self) -> &AttributeValueType {
201        &self.value_type
202    }
203}