NodesBuilder

Struct NodesBuilder 

Source
pub struct NodesBuilder { /* private fields */ }
Expand description

Builder for creating Nodes programmatically.

This builder allows you to construct node lists when building DBC files programmatically. It validates that node names are unique and within limits.

§Examples

use dbc_rs::NodesBuilder;

let nodes = NodesBuilder::new()
    .add_node("ECM")
    .add_node("TCM")
    .add_node("BCM")
    .build()?;

assert_eq!(nodes.len(), 3);
assert!(nodes.contains("ECM"));

§Adding Comments

You can add comments to nodes using add_node_with_comment:

use dbc_rs::NodesBuilder;

let nodes = NodesBuilder::new()
    .add_node_with_comment("ECM", "Engine Control Module")
    .add_node("TCM")
    .build()?;

assert_eq!(nodes.node_comment("ECM"), Some("Engine Control Module"));
assert_eq!(nodes.node_comment("TCM"), None);

§Validation

The builder validates:

  • Maximum of 256 nodes (DoS protection)
  • All node names must be unique (case-sensitive)
  • Maximum 32 characters per node name
  • Maximum number of nodes (implementation limit for DoS protection)

§Feature Requirements

This builder requires the std feature to be enabled.

Implementations§

Source§

impl NodesBuilder

Source

pub fn validate(self) -> Result<Self>

Validates the current builder state without building.

This is useful for checking if the configuration is valid before building. Returns a new builder with validated nodes, or an error if validation fails.

§Returns

Returns Ok(Self) if validation succeeds, or Err(Error::Validation) if:

  • Too many nodes are specified (exceeds 256 nodes limit by default)
  • Duplicate node names are found (case-sensitive)
  • Node name exceeds maximum length (32 characters by default)
§Examples
use dbc_rs::NodesBuilder;

// Valid configuration
let builder = NodesBuilder::new()
    .add_node("ECM")
    .add_node("TCM");
assert!(builder.validate().is_ok());

// Invalid: duplicate nodes
let builder2 = NodesBuilder::new()
    .add_node("ECM")
    .add_node("ECM"); // Duplicate
assert!(builder2.validate().is_err());
Source

pub fn build(self) -> Result<Nodes>

Builds the Nodes from the builder configuration.

This validates the nodes and constructs a Nodes instance.

§Returns

Returns Ok(Nodes) if successful, or Err(Error::Validation) if:

  • Too many nodes are specified (exceeds 256 nodes limit by default)
  • Duplicate node names are found (case-sensitive)
  • Node name exceeds maximum length (32 characters)
§Examples
use dbc_rs::NodesBuilder;

// Build with nodes
let nodes = NodesBuilder::new()
    .add_node("ECM")
    .add_node("TCM")
    .build()?;
assert_eq!(nodes.len(), 2);

// Build empty
let empty = NodesBuilder::new().build()?;
assert!(empty.is_empty());
§Errors
use dbc_rs::NodesBuilder;

// Duplicate nodes
let result = NodesBuilder::new()
    .add_node("ECM")
    .add_node("ECM") // Duplicate
    .build();
assert!(result.is_err());
Source§

impl NodesBuilder

Source

pub fn new() -> Self

Creates a new NodesBuilder with an empty node list.

§Examples
use dbc_rs::NodesBuilder;

let builder = NodesBuilder::new();
let nodes = builder.build()?;
assert!(nodes.is_empty());
Source

pub fn add_node(self, node: impl AsRef<str>) -> Self

Adds a single node to the list without a comment.

§Arguments
  • node - The node name (anything that implements AsRef<str>)
§Examples
use dbc_rs::NodesBuilder;

let nodes = NodesBuilder::new()
    .add_node("ECM")
    .add_node("TCM")
    .build()?;
assert_eq!(nodes.len(), 2);
Source

pub fn add_node_with_comment( self, node: impl AsRef<str>, comment: impl AsRef<str>, ) -> Self

Adds a single node to the list with an optional comment.

§Arguments
  • node - The node name (anything that implements AsRef<str>)
  • comment - The comment for this node
§Examples
use dbc_rs::NodesBuilder;

let nodes = NodesBuilder::new()
    .add_node_with_comment("ECM", "Engine Control Module")
    .add_node("TCM")
    .build()?;
assert_eq!(nodes.len(), 2);
assert_eq!(nodes.node_comment("ECM"), Some("Engine Control Module"));
assert_eq!(nodes.node_comment("TCM"), None);
Source

pub fn add_nodes<I, S>(self, nodes: I) -> Self
where I: IntoIterator<Item = S>, S: AsRef<str>,

Adds multiple nodes from an iterator.

§Arguments
  • nodes - An iterator of node names (each item must implement AsRef<str>)
§Examples
use dbc_rs::NodesBuilder;

// From a slice
let nodes = NodesBuilder::new()
    .add_nodes(&["ECM", "TCM", "BCM"])
    .build()?;
assert_eq!(nodes.len(), 3);

// From a vector
let node_vec = vec!["Node1", "Node2"];
let nodes2 = NodesBuilder::new()
    .add_nodes(node_vec.iter())
    .build()?;
assert_eq!(nodes2.len(), 2);
Source

pub fn clear(self) -> Self

Clears all nodes from the builder.

§Examples
use dbc_rs::NodesBuilder;

let nodes = NodesBuilder::new()
    .add_node("ECM")
    .add_node("TCM")
    .clear()
    .add_node("BCM")
    .build()?;
assert_eq!(nodes.len(), 1);
assert!(nodes.contains("BCM"));
assert!(!nodes.contains("ECM"));

Trait Implementations§

Source§

impl Debug for NodesBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for NodesBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.