• Home
  • Raw
  • Download

Lines Matching +full:path +full:- +full:parse

2 // Use of this source code is governed by a BSD-style license that can be
5 //! This module implements DT path handling.
19 /// Device tree path.
21 pub struct Path(String); struct
23 impl Path { implementation
24 // Verify path and strip unneeded characters.
25 fn sanitize(path: &str) -> Result<String> { in sanitize()
26 if path.is_empty() || !path.starts_with(PATH_SEP) { in sanitize()
27 return Err(Error::InvalidPath(format!("{path} is not absolute"))); in sanitize()
28 } else if path == PATH_SEP { in sanitize()
29 return Ok(path.into()); in sanitize()
31 let path = path.trim_end_matches(PATH_SEP); in sanitize() localVariable
32 if path.is_empty() || path.split(PATH_SEP).skip(1).any(|c| c.is_empty()) { in sanitize()
33 Err(Error::InvalidPath("empty component in path".into())) in sanitize()
35 assert!(path.starts_with(PATH_SEP)); in sanitize()
36 Ok(path.into()) in sanitize()
40 // Create a new Path.
41 pub(crate) fn new(path: &str) -> Result<Self> { in new()
42 Ok(Self(Self::sanitize(path)?)) in new()
45 // Push a new path segment, creating a new path.
46 pub(crate) fn push(&self, subpath: &str) -> Result<Self> { in push()
59 // Iterate path segments.
60 pub(crate) fn iter(&self) -> impl Iterator<Item = &str> { in iter()
66 // Return `true` if the path points to a child of `other`.
67 pub(crate) fn is_child_of(&self, other: &Path) -> bool { in is_child_of()
78 impl FromStr for Path { implementation
81 fn from_str(value: &str) -> Result<Self> { in from_str()
82 Path::new(value) in from_str()
86 impl TryFrom<&str> for Path { implementation
89 fn try_from(value: &str) -> Result<Path> { in try_from() argument
90 value.parse() in try_from()
94 impl TryFrom<String> for Path { implementation
97 fn try_from(value: String) -> Result<Path> { in try_from() argument
98 value.parse() in try_from()
102 impl From<Path> for String {
103 fn from(val: Path) -> Self { in from()
104 val.0 // Return path in from()
108 impl AsRef<str> for Path { implementation
109 fn as_ref(&self) -> &str { in as_ref()
114 impl fmt::Display for Path { implementation
115 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { in fmt()
120 // Parse a DT path string containing a node path and a property location (name and offset),
121 // eg '/path/to/node:prop1:4'.
122 pub(crate) fn parse_path_with_prop(value: &str) -> Result<(Path, PhandlePin)> { in parse_path_with_prop() argument
125 let path: Path = elements.next().unwrap().parse()?; // There will always be at least one. in parse_path_with_prop() localVariable
133 .parse() in parse_path_with_prop()
134 .map_err(|_| Error::InvalidPath("cannot parse offset as u32".into()))?; in parse_path_with_prop()
135 Ok((path, PhandlePin(prop, off))) in parse_path_with_prop()
144 let l: Path = "/".parse().unwrap(); in fdt_parse_path()
147 let l: Path = "/a/b/c".parse().unwrap(); in fdt_parse_path()
150 let (path, prop) = parse_path_with_prop("/:a:0").unwrap(); in fdt_parse_path()
151 assert!(path.iter().next().is_none()); in fdt_parse_path()
155 let (path, prop) = parse_path_with_prop("/a/b/c:defg:1").unwrap(); in fdt_parse_path()
156 assert!(path.iter().eq(["a", "b", "c"])); in fdt_parse_path()
163 assert!(Path::from_str("").is_err()); in fdt_path_parse_invalid()
164 assert!(Path::from_str("/a/b//c").is_err()); in fdt_path_parse_invalid()
165 assert!(Path::from_str("a/b").is_err()); in fdt_path_parse_invalid()
166 assert!(Path::from_str("a").is_err()); in fdt_path_parse_invalid()
167 parse_path_with_prop("a").expect_err("parse error"); in fdt_path_parse_invalid()
168 parse_path_with_prop("a::").expect_err("parse error"); in fdt_path_parse_invalid()
169 parse_path_with_prop("/a/b:c:").expect_err("parse error"); in fdt_path_parse_invalid()
170 parse_path_with_prop("/a/b:c:p:w").expect_err("parse error"); in fdt_path_parse_invalid()
175 let mut path = Path::new("/").unwrap(); in fdt_path_from_empty() localVariable
176 assert!(path.iter().next().is_none()); in fdt_path_from_empty()
177 path = path.push("abc").unwrap(); in fdt_path_from_empty()
178 assert!(path.iter().eq(["abc",])); in fdt_path_from_empty()
179 path = Path::new("/").unwrap(); in fdt_path_from_empty()
180 path = path.push("a/b/c").unwrap(); in fdt_path_from_empty()
181 assert!(path.iter().eq(["a", "b", "c"])); in fdt_path_from_empty()
186 let mut path = Path::new("/a/b/c").unwrap(); in fdt_path_create() localVariable
187 path = path.push("de").unwrap(); in fdt_path_create()
188 assert!(path.iter().eq(["a", "b", "c", "de"])); in fdt_path_create()
189 path = path.push("f/g/h").unwrap(); in fdt_path_create()
190 assert!(path.iter().eq(["a", "b", "c", "de", "f", "g", "h"])); in fdt_path_create()
195 let path = Path::new("/aaa/bbb/ccc").unwrap(); in fdt_path_childof() localVariable
196 assert!(path.is_child_of(&Path::new("/aaa").unwrap())); in fdt_path_childof()
197 assert!(path.is_child_of(&Path::new("/aaa/bbb").unwrap())); in fdt_path_childof()
198 assert!(path.is_child_of(&Path::new("/aaa/bbb/ccc").unwrap())); in fdt_path_childof()
199 assert!(!path.is_child_of(&Path::new("/aaa/bbb/ccc/ddd").unwrap())); in fdt_path_childof()
200 assert!(!path.is_child_of(&Path::new("/aa").unwrap())); in fdt_path_childof()
201 assert!(!path.is_child_of(&Path::new("/aaa/bb").unwrap())); in fdt_path_childof()
202 assert!(!path.is_child_of(&Path::new("/d").unwrap())); in fdt_path_childof()
203 assert!(!path.is_child_of(&Path::new("/d/e").unwrap())); in fdt_path_childof()