dbc_rs/attribute/mod.rs
1//! Attribute definitions and values for DBC files.
2//!
3//! This module provides types for parsing and representing DBC attributes:
4//! - `BA_DEF_` - Attribute definitions
5//! - `BA_DEF_DEF_` - Attribute default values
6//! - `BA_` - Attribute value assignments
7//!
8//! # DBC Attribute System
9//!
10//! Attributes in DBC files provide metadata for network objects:
11//! - Network/database level (global attributes)
12//! - Node attributes (BU_)
13//! - Message attributes (BO_)
14//! - Signal attributes (SG_)
15//!
16//! # Example
17//!
18//! ```text
19//! BA_DEF_ BO_ "GenMsgCycleTime" INT 0 10000;
20//! BA_DEF_DEF_ "GenMsgCycleTime" 100;
21//! BA_ "GenMsgCycleTime" BO_ 256 50;
22//! ```
23
24mod impls;
25pub(crate) mod parse;
26#[cfg(feature = "std")]
27mod std;
28
29#[cfg(feature = "std")]
30mod builder;
31
32use crate::compat::{Name, String, Vec};
33
34#[cfg(feature = "std")]
35pub use builder::AttributeDefinitionBuilder;
36
37/// Maximum size for attribute string values.
38pub const MAX_ATTRIBUTE_STRING_SIZE: usize = 256;
39
40/// Type alias for attribute string values.
41pub type AttributeString = String<MAX_ATTRIBUTE_STRING_SIZE>;
42
43/// Type alias for enum value list in attribute definitions.
44pub type EnumValues = Vec<Name, { crate::MAX_ATTRIBUTE_ENUM_VALUES }>;
45
46/// Type alias for attribute definitions collection.
47pub type AttributeDefinitions = Vec<AttributeDefinition, { crate::MAX_ATTRIBUTE_DEFINITIONS }>;
48
49/// Object type that an attribute applies to.
50///
51/// Corresponds to the object_type in `BA_DEF_`:
52/// - Empty (no prefix) = Network/database level
53/// - `BU_` = Node
54/// - `BO_` = Message
55/// - `SG_` = Signal
56#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
57pub enum AttributeObjectType {
58 /// Network/database level attribute (no object prefix in BA_DEF_)
59 #[default]
60 Network,
61 /// Node attribute (BU_ prefix)
62 Node,
63 /// Message attribute (BO_ prefix)
64 Message,
65 /// Signal attribute (SG_ prefix)
66 Signal,
67}
68
69/// Attribute value type specification from BA_DEF_.
70///
71/// Defines the type and constraints for an attribute's values.
72#[derive(Debug, Clone, PartialEq)]
73pub enum AttributeValueType {
74 /// Integer type with min/max range: `INT min max`
75 Int {
76 /// Minimum allowed value
77 min: i64,
78 /// Maximum allowed value
79 max: i64,
80 },
81 /// Hexadecimal integer type with min/max range: `HEX min max`
82 Hex {
83 /// Minimum allowed value
84 min: i64,
85 /// Maximum allowed value
86 max: i64,
87 },
88 /// Float type with min/max range: `FLOAT min max`
89 Float {
90 /// Minimum allowed value
91 min: f64,
92 /// Maximum allowed value
93 max: f64,
94 },
95 /// String type: `STRING`
96 String,
97 /// Enumeration type: `ENUM "val1","val2",...`
98 Enum {
99 /// List of valid enum values
100 values: EnumValues,
101 },
102}
103
104/// Concrete attribute value from BA_ or BA_DEF_DEF_.
105///
106/// Represents the actual value assigned to an attribute.
107#[derive(Debug, Clone, PartialEq)]
108pub enum AttributeValue {
109 /// Integer value (for INT and HEX types)
110 Int(i64),
111 /// Floating-point value (for FLOAT type)
112 Float(f64),
113 /// String value (for STRING and ENUM types)
114 String(AttributeString),
115}
116
117/// Attribute definition from BA_DEF_.
118///
119/// Defines an attribute's name, target object type, and value type.
120///
121/// # Example
122///
123/// ```text
124/// BA_DEF_ BO_ "GenMsgCycleTime" INT 0 10000;
125/// ```
126///
127/// This defines a message attribute named "GenMsgCycleTime" with
128/// integer values in range 0-10000.
129#[derive(Debug, Clone, PartialEq)]
130pub struct AttributeDefinition {
131 /// Attribute name (quoted identifier in DBC)
132 name: Name,
133 /// Target object type (Network, Node, Message, Signal)
134 object_type: AttributeObjectType,
135 /// Value type specification
136 value_type: AttributeValueType,
137}
138
139impl AttributeDefinition {
140 /// Creates a new attribute definition.
141 #[inline]
142 pub(crate) fn new(
143 name: Name,
144 object_type: AttributeObjectType,
145 value_type: AttributeValueType,
146 ) -> Self {
147 Self {
148 name,
149 object_type,
150 value_type,
151 }
152 }
153}
154
155/// Target object reference for attribute value assignment.
156///
157/// Identifies which specific object an attribute value applies to.
158#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
159pub enum AttributeTarget {
160 /// Network/database level (global)
161 Network,
162 /// Node by name
163 Node(Name),
164 /// Message by ID
165 Message(u32),
166 /// Signal by (message_id, signal_name)
167 Signal(u32, Name),
168}