Skip to content

Commit 51d0b0e

Browse files
committed
feat: 更新 Cargo.toml 版本号至 0.1.3,优化 Fdt 结构的根节点访问方法,添加 NodeMutGen 的 add_child 方法,并增强测试用例
1 parent 140c774 commit 51d0b0e

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

fdt-edit/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ description = "A high-level library for creating, editing, and encoding Flattene
55
documentation = "https://docs.rs/fdt-edit"
66
edition = "2024"
77
exclude = [".git*", "*.md", "tests/"]
8+
homepage = "https://github.com/drivercraft/fdt-parser"
89
keywords = ["device-tree", "dtb", "embedded", "no-std", "editor"]
910
license = "MIT OR Apache-2.0"
1011
name = "fdt-edit"
1112
readme = "README.md"
12-
homepage = "https://github.com/drivercraft/fdt-parser"
1313
repository = "https://github.com/drivercraft/fdt-parser"
14-
version = "0.1.2"
14+
version = "0.1.3"
1515

1616
[dependencies]
17-
log = "0.4"
18-
fdt-raw = {version = "0.1.0", path = "../fdt-raw"}
1917
enum_dispatch = "0.3.13"
18+
fdt-raw = {version = "0.1.0", path = "../fdt-raw"}
19+
log = "0.4"
2020

2121
[dev-dependencies]
2222
dtb-file = {path = "../dtb-file"}

fdt-edit/src/fdt.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,13 @@ impl Fdt {
184184
}
185185

186186
/// 获取根节点
187-
pub fn root(&self) -> &Node {
188-
&self.root
187+
pub fn root<'a>(&'a self) -> NodeRef<'a> {
188+
self.get_by_path("/").unwrap()
189189
}
190190

191191
/// 获取根节点(可变)
192-
pub fn root_mut(&mut self) -> &mut Node {
193-
&mut self.root
192+
pub fn root_mut<'a>(&'a mut self) -> NodeMut<'a> {
193+
self.get_by_path_mut("/").unwrap()
194194
}
195195

196196
/// 应用设备树覆盖 (Device Tree Overlay)
@@ -431,7 +431,7 @@ impl Fdt {
431431
.normalize_path(path)
432432
.unwrap_or_else(|| path.to_string());
433433

434-
NodeIter::new(self.root()).filter_map(move |node_ref| {
434+
NodeIter::new(&self.root).filter_map(move |node_ref| {
435435
if node_ref.path_eq_fuzzy(&path) {
436436
Some(node_ref)
437437
} else {
@@ -442,7 +442,7 @@ impl Fdt {
442442

443443
pub fn get_by_path<'a>(&'a self, path: &str) -> Option<NodeRef<'a>> {
444444
let path = self.normalize_path(path)?;
445-
NodeIter::new(self.root()).find_map(move |node_ref| {
445+
NodeIter::new(&self.root).find_map(move |node_ref| {
446446
if node_ref.path_eq(&path) {
447447
Some(node_ref)
448448
} else {
@@ -453,7 +453,7 @@ impl Fdt {
453453

454454
pub fn get_by_path_mut<'a>(&'a mut self, path: &str) -> Option<NodeMut<'a>> {
455455
let path = self.normalize_path(path)?;
456-
NodeIterMut::new(self.root_mut()).find_map(move |node_mut| {
456+
NodeIterMut::new(&mut self.root).find_map(move |node_mut| {
457457
if node_mut.path_eq(&path) {
458458
Some(node_mut)
459459
} else {

fdt-edit/src/node/gerneric.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
use alloc::{string::String, vec::Vec};
1+
use alloc::{
2+
string::{String, ToString},
3+
vec::Vec,
4+
};
25
use core::{fmt::Debug, ops::Deref};
36
use fdt_raw::RegInfo;
47

5-
use crate::{Context, Node, Property};
8+
use crate::{Context, Node, NodeMut, Property};
69

710
#[derive(Clone)]
811
pub struct NodeRefGen<'a> {
@@ -131,6 +134,23 @@ impl<'a> NodeMutGen<'a> {
131134
let prop = Property::new("reg", data);
132135
self.node.set_property(prop);
133136
}
137+
138+
pub fn add_child(&mut self, child: Node) -> NodeMut<'a> {
139+
let name = child.name().to_string();
140+
let mut ctx = self.ctx.clone();
141+
unsafe {
142+
let node_ptr = self.node as *mut Node;
143+
let node = &*node_ptr;
144+
ctx.push(node);
145+
}
146+
self.node.add_child(child);
147+
let raw = self.node.get_child_mut(&name).unwrap();
148+
unsafe {
149+
let node_ptr = raw as *mut Node;
150+
let node = &mut *node_ptr;
151+
NodeMut::new(node, ctx)
152+
}
153+
}
134154
}
135155

136156
impl Debug for NodeRefGen<'_> {

fdt-edit/tests/remove_node.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,15 @@ mod tests {
130130
#[test]
131131
fn test_remove_with_leading_slash() {
132132
let mut fdt = Fdt::new();
133-
fdt.root.add_child(Node::new("test"));
133+
let node = fdt.root_mut().add_child(Node::new("test"));
134+
assert_eq!(&node.path(), "/test");
135+
println!("Node:\n {:?}", node);
134136

135137
// 带有和不带斜杠的路径都应该工作
136138
let result = fdt.remove_node("/test");
137139
assert!(result.is_ok());
140+
141+
assert!(fdt.get_by_path("/test").is_none());
138142
}
139143

140144
#[test]

0 commit comments

Comments
 (0)