dbc_rs/version/
serialize.rs

1use super::Version;
2use crate::VERSION;
3use std::fmt::{Display, Formatter, Result};
4
5/// Display implementation for `Version`.
6///
7/// Formats the version as just the version string (without the `VERSION` keyword or quotes).
8///
9/// # Examples
10///
11/// ```rust,no_run
12/// use dbc_rs::Dbc;
13///
14/// let dbc = Dbc::parse(r#"VERSION "1.2.3"
15///
16/// BU_: ECM
17/// "#)?;
18///
19/// if let Some(version) = dbc.version() {
20///     // Display trait formats as just the version string
21///     assert_eq!(format!("{}", version), "1.2.3");
22///     // Use to_dbc_string() for full DBC format (requires std feature)
23///     #[cfg(feature = "std")]
24///     assert_eq!(version.to_dbc_string(), "VERSION \"1.2.3\"");
25/// }
26/// # Ok::<(), dbc_rs::Error>(())
27/// ```
28impl Display for Version {
29    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
30        write!(f, "{}", self.version)
31    }
32}
33
34impl Version {
35    /// Converts the version to its DBC file representation.
36    ///
37    /// Returns a string in the format: `VERSION "version_string"`
38    ///
39    /// # Examples
40    ///
41    /// ```rust,no_run
42    /// use dbc_rs::Dbc;
43    ///
44    /// let dbc = Dbc::parse(r#"VERSION "1.0"
45    ///
46    /// BU_: ECM
47    /// "#)?;
48    ///
49    /// if let Some(version) = dbc.version() {
50    ///     let dbc_string = version.to_dbc_string();
51    ///     assert_eq!(dbc_string, "VERSION \"1.0\"");
52    /// }
53    /// # Ok::<(), dbc_rs::Error>(())
54    /// ```
55    ///
56    /// # Empty Version
57    ///
58    /// Empty version strings are represented as `VERSION ""`:
59    ///
60    /// ```rust,no_run
61    /// use dbc_rs::Dbc;
62    ///
63    /// let dbc = Dbc::parse(r#"VERSION ""
64    ///
65    /// BU_: ECM
66    /// "#)?;
67    ///
68    /// if let Some(version) = dbc.version() {
69    ///     assert_eq!(version.to_dbc_string(), "VERSION \"\"");
70    /// }
71    /// # Ok::<(), dbc_rs::Error>(())
72    /// ```
73    ///
74    /// # Feature Requirements
75    ///
76    /// This method requires the `std` feature to be enabled.
77    #[must_use = "return value should be used"]
78    pub fn to_dbc_string(&self) -> String {
79        if self.version.is_empty() {
80            format!("{} \"\"", VERSION)
81        } else {
82            format!("{} \"{}\"", VERSION, &self.version)
83        }
84    }
85}
86
87#[cfg(test)]
88mod tests {
89    use super::*;
90    use crate::Parser;
91
92    #[test]
93    fn test_version_to_dbc_string() {
94        let line1 = b"VERSION \"1\"";
95        let mut parser1 = Parser::new(line1).unwrap();
96        let v1 = Version::parse(&mut parser1).unwrap();
97        assert_eq!(v1.to_dbc_string(), "VERSION \"1\"");
98
99        let line2 = b"VERSION \"1.0\"";
100        let mut parser2 = Parser::new(line2).unwrap();
101        let v2 = Version::parse(&mut parser2).unwrap();
102        assert_eq!(v2.to_dbc_string(), "VERSION \"1.0\"");
103
104        let line3 = b"VERSION \"2.3.4\"";
105        let mut parser3 = Parser::new(line3).unwrap();
106        let v3 = Version::parse(&mut parser3).unwrap();
107        assert_eq!(v3.to_dbc_string(), "VERSION \"2.3.4\"");
108    }
109
110    #[test]
111    fn test_version_empty_round_trip() {
112        let line = b"VERSION \"\"";
113        let mut parser = Parser::new(line).unwrap();
114        let version = Version::parse(&mut parser).unwrap();
115        assert_eq!(version.to_dbc_string(), "VERSION \"\"");
116    }
117}