dbc_rs/attribute/
std.rs

1//! Standard library features for attributes.
2
3use super::{
4    AttributeDefinition, AttributeObjectType, AttributeTarget, AttributeValue, AttributeValueType,
5};
6use std::fmt::{Display, Formatter, Result};
7
8impl Display for AttributeObjectType {
9    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
10        match self {
11            Self::Network => Ok(()),
12            Self::Node => write!(f, "BU_ "),
13            Self::Message => write!(f, "BO_ "),
14            Self::Signal => write!(f, "SG_ "),
15        }
16    }
17}
18
19impl Display for AttributeValueType {
20    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
21        match self {
22            Self::Int { min, max } => write!(f, "INT {} {}", min, max),
23            Self::Hex { min, max } => write!(f, "HEX {} {}", min, max),
24            Self::Float { min, max } => write!(f, "FLOAT {} {}", min, max),
25            Self::String => write!(f, "STRING"),
26            Self::Enum { values } => {
27                write!(f, "ENUM ")?;
28                let mut first = true;
29                for value in values.as_slice() {
30                    if !first {
31                        write!(f, ",")?;
32                    }
33                    write!(f, "\"{}\"", value.as_str())?;
34                    first = false;
35                }
36                Ok(())
37            }
38        }
39    }
40}
41
42impl Display for AttributeValue {
43    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
44        match self {
45            Self::Int(v) => write!(f, "{}", v),
46            Self::Float(v) => write!(f, "{}", v),
47            Self::String(s) => write!(f, "\"{}\"", s.as_str()),
48        }
49    }
50}
51
52impl Display for AttributeDefinition {
53    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
54        write!(
55            f,
56            "BA_DEF_ {}\"{}\" {};",
57            self.object_type(),
58            self.name(),
59            self.value_type()
60        )
61    }
62}
63
64impl Display for AttributeTarget {
65    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
66        match self {
67            Self::Network => Ok(()),
68            Self::Node(name) => write!(f, "BU_ {} ", name.as_str()),
69            Self::Message(id) => write!(f, "BO_ {} ", id),
70            Self::Signal(msg_id, sig_name) => {
71                write!(f, "SG_ {} {} ", msg_id, sig_name.as_str())
72            }
73        }
74    }
75}
76
77impl AttributeDefinition {
78    /// Converts the attribute definition to a DBC format string.
79    pub fn to_dbc_string(&self) -> std::string::String {
80        format!("{}", self)
81    }
82}
83
84impl AttributeValue {
85    /// Converts the attribute value to a DBC format string.
86    pub fn to_dbc_string(&self) -> std::string::String {
87        format!("{}", self)
88    }
89}
90
91impl AttributeTarget {
92    /// Converts the attribute target to a DBC format string prefix.
93    pub fn to_dbc_string(&self) -> std::string::String {
94        format!("{}", self)
95    }
96}