Skip to main content

dbc_rs/nodes/
node.rs

1//! [`Node`].
2
3#[allow(unused_imports)]
4use super::*;
5use crate::compat::{Comment, Name};
6
7/// Represents a single node (ECU) with its name and optional comment.
8#[derive(Debug, Default, PartialEq, Eq, Hash, Clone)]
9pub struct Node {
10    name: Name,
11    comment: Option<Comment>,
12}
13impl Node {
14    /// Creates a new node with the given name and no comment.
15    #[inline]
16    pub(crate) fn new(name: Name) -> Self {
17        Self {
18            name,
19            comment: None,
20        }
21    }
22
23    /// Creates a new node with the given name and comment.
24    #[inline]
25    #[cfg(feature = "std")]
26    pub(crate) fn with_comment(name: Name, comment: Option<Comment>) -> Self {
27        Self { name, comment }
28    }
29
30    /// Returns the node name.
31    #[inline]
32    #[must_use = "return value should be used"]
33    pub fn name(&self) -> &str {
34        self.name.as_str()
35    }
36
37    /// Returns the node comment, if any.
38    #[inline]
39    #[must_use = "return value should be used"]
40    pub fn comment(&self) -> Option<&str> {
41        self.comment.as_ref().map(|c| c.as_str())
42    }
43
44    /// Sets the comment for this node.
45    #[inline]
46    pub(crate) fn set_comment(&mut self, comment: Comment) {
47        self.comment = Some(comment);
48    }
49}