Skip to content

Commit 451037a

Browse files
committed
refactor: 合并节点引用生成器逻辑,简化节点迭代器实现
1 parent 7134103 commit 451037a

File tree

4 files changed

+150
-143
lines changed

4 files changed

+150
-143
lines changed

fdt-edit/src/ctx.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
use alloc::{
2-
collections::BTreeMap,
3-
string::{String, ToString},
4-
vec::Vec,
5-
};
1+
use alloc::{collections::BTreeMap, string::String, vec::Vec};
2+
63
use fdt_raw::{Phandle, Status};
74

85
use crate::{Node, RangesEntry};

fdt-edit/src/node/gerneric.rs

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
use alloc::string::String;
2+
use core::{fmt::Debug, ops::Deref};
3+
4+
use crate::{Context, Node, Property};
5+
6+
#[derive(Clone)]
7+
pub struct NodeRefGen<'a> {
8+
pub node: &'a Node,
9+
pub ctx: Context<'a>,
10+
}
11+
12+
impl<'a> NodeRefGen<'a> {
13+
pub fn find_property(&self, name: &str) -> Option<&'a Property> {
14+
self.node.properties.get(name)
15+
}
16+
17+
pub fn properties(&self) -> impl Iterator<Item = &'a Property> {
18+
self.node.properties.values()
19+
}
20+
21+
fn op(&'a self) -> RefOp<'a> {
22+
RefOp {
23+
ctx: &self.ctx,
24+
node: self.node,
25+
}
26+
}
27+
28+
pub fn path(&self) -> String {
29+
self.op().path()
30+
}
31+
32+
pub fn path_eq(&self, path: &str) -> bool {
33+
self.op().ref_path_eq(path)
34+
}
35+
36+
pub fn path_eq_fuzzy(&self, path: &str) -> bool {
37+
self.op().ref_path_eq_fuzzy(path)
38+
}
39+
}
40+
41+
impl Deref for NodeRefGen<'_> {
42+
type Target = Node;
43+
44+
fn deref(&self) -> &Self::Target {
45+
self.node
46+
}
47+
}
48+
49+
pub struct NodeMutGen<'a> {
50+
pub node: &'a mut Node,
51+
pub ctx: Context<'a>,
52+
}
53+
54+
impl<'a> NodeMutGen<'a> {
55+
fn op(&'a self) -> RefOp<'a> {
56+
RefOp {
57+
ctx: &self.ctx,
58+
node: self.node,
59+
}
60+
}
61+
62+
pub fn path(&self) -> String {
63+
self.op().path()
64+
}
65+
66+
pub fn path_eq(&self, path: &str) -> bool {
67+
self.op().ref_path_eq(path)
68+
}
69+
70+
pub fn path_eq_fuzzy(&self, path: &str) -> bool {
71+
self.op().ref_path_eq_fuzzy(path)
72+
}
73+
}
74+
75+
impl Debug for NodeRefGen<'_> {
76+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
77+
write!(f, "NodeRefGen {{ name: {} }}", self.node.name())
78+
}
79+
}
80+
81+
impl Debug for NodeMutGen<'_> {
82+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
83+
write!(f, "NodeMutGen {{ name: {} }}", self.node.name())
84+
}
85+
}
86+
87+
struct RefOp<'a> {
88+
ctx: &'a Context<'a>,
89+
node: &'a Node,
90+
}
91+
92+
impl<'a> RefOp<'a> {
93+
fn path(&self) -> String {
94+
self.ctx.current_path() + "/" + self.node.name()
95+
}
96+
97+
fn ref_path_eq(&self, path: &str) -> bool {
98+
self.path() == path
99+
}
100+
101+
fn ref_path_eq_fuzzy(&self, path: &str) -> bool {
102+
let mut want = path.trim_matches('/').split("/");
103+
let got_path = self.path();
104+
let mut got = got_path.trim_matches('/').split("/");
105+
let got_count = got.clone().count();
106+
let mut current = 0;
107+
108+
loop {
109+
let w = want.next();
110+
let g = got.next();
111+
let is_last = current + 1 == got_count;
112+
113+
match (w, g) {
114+
(Some(w), Some(g)) => {
115+
if w != g && !is_last {
116+
return false;
117+
}
118+
119+
let name = g.split('@').next().unwrap_or(g);
120+
let addr = g.split('@').nth(1);
121+
122+
let want_name = w.split('@').next().unwrap_or(w);
123+
let want_addr = w.split('@').nth(1);
124+
125+
let res = match (addr, want_addr) {
126+
(Some(a), Some(wa)) => name == want_name && a == wa,
127+
(Some(_), None) => name == want_name,
128+
(None, Some(_)) => false,
129+
(None, None) => name == want_name,
130+
};
131+
if !res {
132+
return false;
133+
}
134+
}
135+
(None, _) => break,
136+
_ => return false,
137+
}
138+
current += 1;
139+
}
140+
true
141+
}
142+
}

fdt-edit/src/node/iter.rs

Lines changed: 5 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ use core::{
55
slice::Iter,
66
};
77

8-
use alloc::{string::String, vec::Vec};
8+
use alloc::vec::Vec;
99

10-
use crate::{Context, Node, Property};
10+
use crate::{
11+
Context, Node,
12+
node::gerneric::{NodeMutGen, NodeRefGen},
13+
};
1114

1215
#[derive(Clone, Debug)]
1316
pub enum NodeRef<'a> {
@@ -18,49 +21,6 @@ impl<'a> NodeRef<'a> {
1821
pub fn new(node: &'a Node, ctx: Context<'a>) -> Self {
1922
Self::Gerneric(NodeRefGen { node, ctx })
2023
}
21-
22-
fn op(&'a self) -> RefOp<'a> {
23-
RefOp {
24-
ctx: &self.ctx,
25-
node: self.node,
26-
}
27-
}
28-
29-
pub fn path(&self) -> String {
30-
self.op().path()
31-
}
32-
33-
pub fn path_eq(&self, path: &str) -> bool {
34-
self.op().ref_path_eq(path)
35-
}
36-
37-
pub fn path_eq_fuzzy(&self, path: &str) -> bool {
38-
self.op().ref_path_eq_fuzzy(path)
39-
}
40-
}
41-
42-
#[derive(Clone)]
43-
pub struct NodeRefGen<'a> {
44-
pub node: &'a Node,
45-
pub ctx: Context<'a>,
46-
}
47-
48-
impl<'a> NodeRefGen<'a> {
49-
pub fn find_property(&self, name: &str) -> Option<&'a Property> {
50-
self.node.properties.get(name)
51-
}
52-
53-
pub fn properties(&self) -> impl Iterator<Item = &'a Property> {
54-
self.node.properties.values()
55-
}
56-
}
57-
58-
impl Deref for NodeRefGen<'_> {
59-
type Target = Node;
60-
61-
fn deref(&self) -> &Self::Target {
62-
self.node
63-
}
6424
}
6525

6626
impl<'a> Deref for NodeRef<'a> {
@@ -73,11 +33,6 @@ impl<'a> Deref for NodeRef<'a> {
7333
}
7434
}
7535

76-
pub struct NodeMutGen<'a> {
77-
pub node: &'a mut Node,
78-
pub ctx: Context<'a>,
79-
}
80-
8136
#[derive(Debug)]
8237
pub enum NodeMut<'a> {
8338
Gerneric(NodeMutGen<'a>),
@@ -87,25 +42,6 @@ impl<'a> NodeMut<'a> {
8742
pub fn new(node: &'a mut Node, ctx: Context<'a>) -> Self {
8843
Self::Gerneric(NodeMutGen { node, ctx })
8944
}
90-
91-
fn op(&'a self) -> RefOp<'a> {
92-
RefOp {
93-
ctx: &self.ctx,
94-
node: self.node,
95-
}
96-
}
97-
98-
pub fn path(&self) -> String {
99-
self.op().path()
100-
}
101-
102-
pub fn path_eq(&self, path: &str) -> bool {
103-
self.op().ref_path_eq(path)
104-
}
105-
106-
pub fn path_eq_fuzzy(&self, path: &str) -> bool {
107-
self.op().ref_path_eq_fuzzy(path)
108-
}
10945
}
11046

11147
impl<'a> Deref for NodeMut<'a> {
@@ -251,72 +187,3 @@ impl<'a> Iterator for NodeIterMut<'a> {
251187
self.next()
252188
}
253189
}
254-
255-
impl Debug for NodeRefGen<'_> {
256-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
257-
write!(f, "NodeRefGen {{ name: {} }}", self.node.name())
258-
}
259-
}
260-
261-
impl Debug for NodeMutGen<'_> {
262-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
263-
write!(f, "NodeMutGen {{ name: {} }}", self.node.name())
264-
}
265-
}
266-
267-
struct RefOp<'a> {
268-
ctx: &'a Context<'a>,
269-
node: &'a Node,
270-
}
271-
272-
impl<'a> RefOp<'a> {
273-
fn path(&self) -> String {
274-
self.ctx.current_path() + "/" + self.node.name()
275-
}
276-
277-
fn ref_path_eq(&self, path: &str) -> bool {
278-
self.path() == path
279-
}
280-
281-
fn ref_path_eq_fuzzy(&self, path: &str) -> bool {
282-
let mut want = path.trim_matches('/').split("/");
283-
let got_path = self.path();
284-
let mut got = got_path.trim_matches('/').split("/");
285-
let got_count = got.clone().count();
286-
let mut current = 0;
287-
288-
loop {
289-
let w = want.next();
290-
let g = got.next();
291-
let is_last = current + 1 == got_count;
292-
293-
match (w, g) {
294-
(Some(w), Some(g)) => {
295-
if w != g && !is_last {
296-
return false;
297-
}
298-
299-
let name = g.split('@').next().unwrap_or(g);
300-
let addr = g.split('@').nth(1);
301-
302-
let want_name = w.split('@').next().unwrap_or(w);
303-
let want_addr = w.split('@').nth(1);
304-
305-
let res = match (addr, want_addr) {
306-
(Some(a), Some(wa)) => name == want_name && a == wa,
307-
(Some(_), None) => name == want_name,
308-
(None, Some(_)) => false,
309-
(None, None) => name == want_name,
310-
};
311-
if !res {
312-
return false;
313-
}
314-
}
315-
(None, _) => break,
316-
_ => return false,
317-
}
318-
current += 1;
319-
}
320-
true
321-
}
322-
}

fdt-edit/src/node/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use fdt_raw::data::StrIter;
1010
use crate::{Phandle, Property, RangesEntry, Status};
1111

1212
mod iter;
13+
mod gerneric;
1314

1415
pub use iter::*;
1516

0 commit comments

Comments
 (0)