dbc_rs/attribute/builder/
impls.rs

1//! Builder method implementations for AttributeDefinitionBuilder.
2
3use super::AttributeDefinitionBuilder;
4use crate::attribute::{AttributeObjectType, AttributeValueType, EnumValues};
5use crate::compat::Name;
6
7impl AttributeDefinitionBuilder {
8    /// Creates a new attribute definition builder with default values.
9    #[must_use]
10    pub fn new() -> Self {
11        Self::default()
12    }
13
14    /// Sets the attribute name.
15    #[must_use]
16    pub fn name(mut self, name: &str) -> Self {
17        self.name = Some(name.to_string());
18        self
19    }
20
21    /// Sets the object type to Network (global/database level).
22    #[must_use]
23    pub fn object_type_network(mut self) -> Self {
24        self.object_type = AttributeObjectType::Network;
25        self
26    }
27
28    /// Sets the object type to Node (BU_).
29    #[must_use]
30    pub fn object_type_node(mut self) -> Self {
31        self.object_type = AttributeObjectType::Node;
32        self
33    }
34
35    /// Sets the object type to Message (BO_).
36    #[must_use]
37    pub fn object_type_message(mut self) -> Self {
38        self.object_type = AttributeObjectType::Message;
39        self
40    }
41
42    /// Sets the object type to Signal (SG_).
43    #[must_use]
44    pub fn object_type_signal(mut self) -> Self {
45        self.object_type = AttributeObjectType::Signal;
46        self
47    }
48
49    /// Sets the object type directly.
50    #[must_use]
51    pub fn object_type(mut self, object_type: AttributeObjectType) -> Self {
52        self.object_type = object_type;
53        self
54    }
55
56    /// Sets the value type to INT with the given range.
57    #[must_use]
58    pub fn int_type(mut self, min: i64, max: i64) -> Self {
59        self.value_type = Some(AttributeValueType::Int { min, max });
60        self
61    }
62
63    /// Sets the value type to HEX with the given range.
64    #[must_use]
65    pub fn hex_type(mut self, min: i64, max: i64) -> Self {
66        self.value_type = Some(AttributeValueType::Hex { min, max });
67        self
68    }
69
70    /// Sets the value type to FLOAT with the given range.
71    #[must_use]
72    pub fn float_type(mut self, min: f64, max: f64) -> Self {
73        self.value_type = Some(AttributeValueType::Float { min, max });
74        self
75    }
76
77    /// Sets the value type to STRING.
78    #[must_use]
79    pub fn string_type(mut self) -> Self {
80        self.value_type = Some(AttributeValueType::String);
81        self
82    }
83
84    /// Sets the value type to ENUM with the given values.
85    ///
86    /// # Errors
87    ///
88    /// Returns an error if any enum value exceeds the maximum name size.
89    pub fn enum_type(mut self, values: &[&str]) -> crate::Result<Self> {
90        let mut enum_values = EnumValues::new();
91        for &value in values {
92            let name = Name::try_from(value)
93                .map_err(|_| crate::Error::expected(crate::Error::MAX_NAME_SIZE_EXCEEDED))?;
94            enum_values.push(name).map_err(|_| {
95                crate::Error::expected(crate::Error::ATTRIBUTE_ENUM_VALUES_TOO_MANY)
96            })?;
97        }
98        self.value_type = Some(AttributeValueType::Enum {
99            values: enum_values,
100        });
101        Ok(self)
102    }
103
104    /// Sets the value type directly.
105    #[must_use]
106    pub fn value_type(mut self, value_type: AttributeValueType) -> Self {
107        self.value_type = Some(value_type);
108        self
109    }
110}