dbc_rs/nodes/
impls.rs

1use super::{NodeNames, Nodes};
2
3impl Nodes {
4    pub(crate) fn new(nodes: NodeNames) -> Self {
5        // Validation should have been done prior (by builder)
6        Self { nodes }
7    }
8
9    /// Returns an iterator over the node names.
10    ///
11    /// # Examples
12    ///
13    /// ```rust,no_run
14    /// use dbc_rs::Dbc;
15    ///
16    /// let dbc = Dbc::parse(r#"VERSION "1.0"
17    ///
18    /// BU_: ECM TCM BCM
19    /// "#)?;
20    ///
21    /// // Iterate over nodes
22    /// let mut iter = dbc.nodes().iter();
23    /// assert_eq!(iter.next(), Some("ECM"));
24    /// assert_eq!(iter.next(), Some("TCM"));
25    /// assert_eq!(iter.next(), Some("BCM"));
26    /// assert_eq!(iter.next(), None);
27    ///
28    /// // Or use in a loop
29    /// for node in dbc.nodes().iter() {
30    ///     println!("Node: {}", node);
31    /// }
32    /// # Ok::<(), dbc_rs::Error>(())
33    /// ```
34    #[inline]
35    #[must_use = "iterator is lazy and does nothing unless consumed"]
36    pub fn iter(&self) -> impl Iterator<Item = &str> + '_ {
37        self.nodes.iter().map(|s| s.as_str())
38    }
39
40    /// Checks if a node name is in the list.
41    ///
42    /// The check is case-sensitive.
43    ///
44    /// # Arguments
45    ///
46    /// * `node` - The node name to check
47    ///
48    /// # Examples
49    ///
50    /// ```rust,no_run
51    /// use dbc_rs::Dbc;
52    ///
53    /// let dbc = Dbc::parse(r#"VERSION "1.0"
54    ///
55    /// BU_: ECM TCM
56    /// "#)?;
57    ///
58    /// assert!(dbc.nodes().contains("ECM"));
59    /// assert!(dbc.nodes().contains("TCM"));
60    /// assert!(!dbc.nodes().contains("BCM"));
61    /// assert!(!dbc.nodes().contains("ecm")); // Case-sensitive
62    /// # Ok::<(), dbc_rs::Error>(())
63    /// ```
64    #[inline]
65    #[must_use = "return value should be used"]
66    pub fn contains(&self, node: &str) -> bool {
67        self.iter().any(|n| n == node)
68    }
69
70    /// Returns the number of nodes in the collection.
71    ///
72    /// # Examples
73    ///
74    /// ```rust,no_run
75    /// use dbc_rs::Dbc;
76    ///
77    /// let dbc = Dbc::parse(r#"VERSION "1.0"
78    ///
79    /// BU_: ECM TCM BCM
80    /// "#)?;
81    ///
82    /// assert_eq!(dbc.nodes().len(), 3);
83    /// # Ok::<(), dbc_rs::Error>(())
84    /// ```
85    #[inline]
86    #[must_use = "return value should be used"]
87    pub fn len(&self) -> usize {
88        self.nodes.len()
89    }
90
91    /// Returns `true` if there are no nodes in the collection.
92    ///
93    /// # Examples
94    ///
95    /// ```rust,no_run
96    /// use dbc_rs::Dbc;
97    ///
98    /// // Empty node list
99    /// let dbc = Dbc::parse(r#"VERSION "1.0"
100    ///
101    /// BU_:
102    /// "#)?;
103    /// assert!(dbc.nodes().is_empty());
104    ///
105    /// // With nodes
106    /// let dbc2 = Dbc::parse(r#"VERSION "1.0"
107    ///
108    /// BU_: ECM
109    /// "#)?;
110    /// assert!(!dbc2.nodes().is_empty());
111    /// # Ok::<(), dbc_rs::Error>(())
112    /// ```
113    #[inline]
114    #[must_use = "return value should be used"]
115    pub fn is_empty(&self) -> bool {
116        self.nodes.is_empty()
117    }
118
119    /// Gets a node by index.
120    ///
121    /// Returns `None` if the index is out of bounds.
122    ///
123    /// # Arguments
124    ///
125    /// * `index` - The zero-based index of the node
126    ///
127    /// # Examples
128    ///
129    /// ```rust,no_run
130    /// use dbc_rs::Dbc;
131    ///
132    /// let dbc = Dbc::parse(r#"VERSION "1.0"
133    ///
134    /// BU_: ECM TCM BCM
135    /// "#)?;
136    ///
137    /// assert_eq!(dbc.nodes().at(0), Some("ECM"));
138    /// assert_eq!(dbc.nodes().at(1), Some("TCM"));
139    /// assert_eq!(dbc.nodes().at(2), Some("BCM"));
140    /// assert_eq!(dbc.nodes().at(3), None); // Out of bounds
141    /// # Ok::<(), dbc_rs::Error>(())
142    /// ```
143    #[inline]
144    #[must_use = "return value should be used"]
145    pub fn at(&self, index: usize) -> Option<&str> {
146        self.nodes.get(index).map(|s| s.as_str())
147    }
148}