Struct Node

Source
pub struct Node {
    pub name: String,
    pub label: Option<String>,
    pub properties: Vec<Arc<Mutex<Property>>>,
    pub sub_nodes: Vec<Arc<Mutex<Node>>>,
}
Expand description

A node that is used to describe a device.

A node has a list of properties that are represented with a vector of Property.

Node can also contain other nodes.

Fields§

§name: String§label: Option<String>§properties: Vec<Arc<Mutex<Property>>>§sub_nodes: Vec<Arc<Mutex<Node>>>

Implementations§

Source§

impl Node

Source

pub fn new(name: &str) -> Self

Create an empty Node with name.

§Example
use devicetree_tool::Node;

let node = Node::new("node");

assert_eq!(format!("{}", node), "node {\n};\n");
Source

pub fn new_with_label(name: &str, label: &str) -> Self

Create an empty Node with name and label.

§Example
use devicetree_tool::Node;

let node = Node::new_with_label("node", "label");

assert_eq!(format!("{}", node), "label: node {\n};\n");
Source

pub fn add_property(&mut self, prop: Property)

Add a Property to the Node.

§Example
use devicetree_tool::Node;
use devicetree_tool::Property;

let mut node = Node::new("node");

node.add_property(Property::new_u32("prop1", 42));
node.add_property(Property::new_str("prop2", "hello"));

assert_eq!(node.properties.len(), 2);

assert_eq!(format!("{}", node),
           "node {\n\tprop1 = <0x0 0x0 0x0 0x2a>;\n\t\
           prop2 = <0x68 0x65 0x6c 0x6c 0x6f 0x0>;\n};\n");
Source

pub fn add_sub_node(&mut self, sub_node: Node)

Add a sub node to the Node.

§Example
use devicetree_tool::Node;
use devicetree_tool::Property;

let mut node = Node::new("node");

// Create a sub node
let mut sub_node = Node::new("sub_node");
sub_node.add_property(Property::new_u32("prop", 42));

// Add the sub node
node.add_sub_node(sub_node);

assert_eq!(node.sub_nodes.len(), 1);
assert_eq!(format!("{}", node),
           "node {\n\n\tsub_node {\n\t\tprop = <0x0 0x0 0x0 0x2a>;\n\t};\n};\n");
Source

pub fn find_property(&self, name: &str) -> Option<Arc<Mutex<Property>>>

Find Property from a Node by name.

§Example
use devicetree_tool::Node;
use devicetree_tool::Property;

let mut node = Node::new("node");

// Now the node hasn't any property
assert_eq!(node.find_property("prop").is_none(), true);

// Add a property
node.add_property(Property::new_u32("prop", 42));

// Find the property from the node
let prop = node.find_property("prop").unwrap();

assert_eq!(prop.lock().unwrap().value, vec![0u8, 0u8, 0u8, 42u8]);
Source

pub fn find_subnode_by_name(&self, name: &str) -> Option<Arc<Mutex<Node>>>

Find sub node from a Node by name.

§Example
use devicetree_tool::Node;
use devicetree_tool::Property;

let mut node = Node::new("node");

// Now the node hasn't any sub node
assert_eq!(node.find_subnode_by_name("subnode").is_none(), true);

// Add a sub node
node.add_sub_node(Node::new("subnode"));

// Find the sub node from the node
let sub_node = node.find_subnode_by_name("subnode").unwrap();

assert_eq!(sub_node.lock().unwrap().name, "subnode");
Source

pub fn find_subnode_by_label(&self, label: &str) -> Option<Arc<Mutex<Node>>>

Find sub node from a Node by label.

§Example
use devicetree_tool::Node;
use devicetree_tool::Property;

let mut node = Node::new("node");

// Now the node hasn't any sub node
assert_eq!(node.find_subnode_by_label("label").is_none(), true);

// Add a sub node
node.add_sub_node(Node::new_with_label("subnode", "label"));

// Find the sub node from the node
let sub_node = node.find_subnode_by_label("label").unwrap();

assert_eq!(sub_node.lock().unwrap().name, "subnode");
Source

pub fn find_subnode_by_path(&self, path: Vec<&str>) -> Option<Arc<Mutex<Node>>>

Find sub node from a Node by path.

§Example
use devicetree_tool::Node;
use devicetree_tool::Property;

let mut node_layer_1 = Node::new("node_layer_1");

// Now the node hasn't any sub node
assert_eq!(node_layer_1.find_subnode_by_path(vec![ "node_layer_2", "node_layer_3"]).is_none(), true);

// Create a layer-2 sub node
let mut node_layer_2 = Node::new("node_layer_2");

// Add a layer-3 sub node
node_layer_2.add_sub_node(Node::new("node_layer_3"));

node_layer_1.add_sub_node(node_layer_2);

// Find the layer-3 sub node
let sub_node = node_layer_1.find_subnode_by_path(vec!["node_layer_2", "node_layer_3"]).unwrap();

assert_eq!(sub_node.lock().unwrap().name, "node_layer_3");

Trait Implementations§

Source§

impl Display for Node

Source§

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

Print a Property in the format of DTS

Auto Trait Implementations§

§

impl Freeze for Node

§

impl RefUnwindSafe for Node

§

impl Send for Node

§

impl Sync for Node

§

impl Unpin for Node

§

impl UnwindSafe for Node

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.