1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
use super::{InnerNodes, Node, Nodes};
impl Nodes {
pub(crate) fn new(nodes: InnerNodes) -> Self {
// Validation should have been done prior (by builder)
Self { nodes }
}
/// Returns an iterator over the node names.
///
/// # Examples
///
/// ```rust,no_run
/// use dbc_rs::Dbc;
///
/// let dbc = Dbc::parse(r#"VERSION "1.0"
///
/// BU_: ECM TCM BCM
/// "#)?;
///
/// // Iterate over nodes
/// let mut iter = dbc.nodes().iter();
/// assert_eq!(iter.next(), Some("ECM"));
/// assert_eq!(iter.next(), Some("TCM"));
/// assert_eq!(iter.next(), Some("BCM"));
/// assert_eq!(iter.next(), None);
///
/// // Or use in a loop
/// for node in dbc.nodes().iter() {
/// println!("Node: {}", node);
/// }
/// # Ok::<(), dbc_rs::Error>(())
/// ```
#[inline]
#[must_use = "iterator is lazy and does nothing unless consumed"]
pub fn iter(&self) -> impl Iterator<Item = &str> + '_ {
self.nodes.iter().map(|node| node.name())
}
/// Returns an iterator over the node structs.
///
/// This provides access to both the node name and its comment.
///
/// # Examples
///
/// ```rust,no_run
/// use dbc_rs::Dbc;
///
/// let dbc = Dbc::parse(r#"VERSION "1.0"
///
/// BU_: ECM TCM
///
/// BO_ 256 Engine : 8 ECM
///
/// CM_ BU_ ECM "Engine Control Module";
/// "#)?;
///
/// for node in dbc.nodes().iter_nodes() {
/// println!("Node: {}", node.name());
/// if let Some(comment) = node.comment() {
/// println!(" Comment: {}", comment);
/// }
/// }
/// # Ok::<(), dbc_rs::Error>(())
/// ```
#[inline]
#[must_use = "iterator is lazy and does nothing unless consumed"]
pub fn iter_nodes(&self) -> impl Iterator<Item = &Node> + '_ {
self.nodes.iter()
}
/// Checks if a node name is in the list.
///
/// The check is case-sensitive.
///
/// # Arguments
///
/// * `node` - The node name to check
///
/// # Examples
///
/// ```rust,no_run
/// use dbc_rs::Dbc;
///
/// let dbc = Dbc::parse(r#"VERSION "1.0"
///
/// BU_: ECM TCM
/// "#)?;
///
/// assert!(dbc.nodes().contains("ECM"));
/// assert!(dbc.nodes().contains("TCM"));
/// assert!(!dbc.nodes().contains("BCM"));
/// assert!(!dbc.nodes().contains("ecm")); // Case-sensitive
/// # Ok::<(), dbc_rs::Error>(())
/// ```
#[inline]
#[must_use = "return value should be used"]
pub fn contains(&self, node: &str) -> bool {
self.iter().any(|n| n == node)
}
/// Returns the number of nodes in the collection.
///
/// # Examples
///
/// ```rust,no_run
/// use dbc_rs::Dbc;
///
/// let dbc = Dbc::parse(r#"VERSION "1.0"
///
/// BU_: ECM TCM BCM
/// "#)?;
///
/// assert_eq!(dbc.nodes().len(), 3);
/// # Ok::<(), dbc_rs::Error>(())
/// ```
#[inline]
#[must_use = "return value should be used"]
pub fn len(&self) -> usize {
self.nodes.len()
}
/// Returns `true` if there are no nodes in the collection.
///
/// # Examples
///
/// ```rust,no_run
/// use dbc_rs::Dbc;
///
/// // Empty node list
/// let dbc = Dbc::parse(r#"VERSION "1.0"
///
/// BU_:
/// "#)?;
/// assert!(dbc.nodes().is_empty());
///
/// // With nodes
/// let dbc2 = Dbc::parse(r#"VERSION "1.0"
///
/// BU_: ECM
/// "#)?;
/// assert!(!dbc2.nodes().is_empty());
/// # Ok::<(), dbc_rs::Error>(())
/// ```
#[inline]
#[must_use = "return value should be used"]
pub fn is_empty(&self) -> bool {
self.nodes.is_empty()
}
/// Gets a node name by index.
///
/// Returns `None` if the index is out of bounds.
///
/// # Arguments
///
/// * `index` - The zero-based index of the node
///
/// # Examples
///
/// ```rust,no_run
/// use dbc_rs::Dbc;
///
/// let dbc = Dbc::parse(r#"VERSION "1.0"
///
/// BU_: ECM TCM BCM
/// "#)?;
///
/// assert_eq!(dbc.nodes().at(0), Some("ECM"));
/// assert_eq!(dbc.nodes().at(1), Some("TCM"));
/// assert_eq!(dbc.nodes().at(2), Some("BCM"));
/// assert_eq!(dbc.nodes().at(3), None); // Out of bounds
/// # Ok::<(), dbc_rs::Error>(())
/// ```
#[inline]
#[must_use = "return value should be used"]
pub fn at(&self, index: usize) -> Option<&str> {
self.nodes.get(index).map(|node| node.name())
}
/// Gets a node by index.
///
/// Returns `None` if the index is out of bounds.
///
/// # Arguments
///
/// * `index` - The zero-based index of the node
///
/// # Examples
///
/// ```rust,no_run
/// use dbc_rs::Dbc;
///
/// let dbc = Dbc::parse(r#"VERSION "1.0"
///
/// BU_: ECM TCM BCM
/// "#)?;
///
/// let node = dbc.nodes().get(0).unwrap();
/// assert_eq!(node.name(), "ECM");
/// # Ok::<(), dbc_rs::Error>(())
/// ```
#[inline]
#[must_use = "return value should be used"]
pub fn get(&self, index: usize) -> Option<&Node> {
self.nodes.get(index)
}
/// Returns the comment for a specific node, if present.
///
/// # Examples
///
/// ```rust,no_run
/// use dbc_rs::Dbc;
///
/// let dbc = Dbc::parse(r#"VERSION "1.0"
///
/// BU_: ECM TCM
///
/// BO_ 256 Engine : 8 ECM
///
/// CM_ BU_ ECM "Engine Control Module";
/// "#)?;
///
/// assert_eq!(dbc.nodes().node_comment("ECM"), Some("Engine Control Module"));
/// assert_eq!(dbc.nodes().node_comment("TCM"), None);
/// # Ok::<(), dbc_rs::Error>(())
/// ```
#[inline]
#[must_use = "return value should be used"]
pub fn node_comment(&self, node_name: &str) -> Option<&str> {
self.nodes
.iter()
.find(|node| node.name() == node_name)
.and_then(|node| node.comment())
}
/// Sets the comment for a node by name.
///
/// Returns `true` if the node was found and the comment was set,
/// `false` if the node was not found.
pub(crate) fn set_node_comment(
&mut self,
node_name: &str,
comment: crate::compat::Comment,
) -> bool {
if let Some(node) = self.nodes.iter_mut().find(|n| n.name() == node_name) {
node.set_comment(comment);
true
} else {
false
}
}
}