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
impl Node
Sourcepub fn new(name: &str) -> Self
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");
Sourcepub fn new_with_label(name: &str, label: &str) -> Self
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");
Sourcepub fn add_property(&mut self, prop: Property)
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");
Sourcepub fn add_sub_node(&mut self, sub_node: Node)
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");
Sourcepub fn find_property(&self, name: &str) -> Option<Arc<Mutex<Property>>>
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]);
Sourcepub fn find_subnode_by_name(&self, name: &str) -> Option<Arc<Mutex<Node>>>
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");
Sourcepub fn find_subnode_by_label(&self, label: &str) -> Option<Arc<Mutex<Node>>>
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");
Sourcepub fn find_subnode_by_path(&self, path: Vec<&str>) -> Option<Arc<Mutex<Node>>>
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more