graphix_compiler/expr/
modpath.rs1use super::parser;
2use netidx::path::Path;
3use std::{borrow::Borrow, fmt, ops::Deref, result, str::FromStr};
4
5#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
6pub struct ModPath(pub Path);
7
8impl FromStr for ModPath {
9 type Err = anyhow::Error;
10
11 fn from_str(s: &str) -> result::Result<Self, Self::Err> {
12 parser::parse_modpath(s)
13 }
14}
15
16impl ModPath {
17 pub fn root() -> ModPath {
18 ModPath(Path::root())
19 }
20}
21
22impl Borrow<str> for ModPath {
23 fn borrow(&self) -> &str {
24 self.0.borrow()
25 }
26}
27
28impl Deref for ModPath {
29 type Target = Path;
30
31 fn deref(&self) -> &Self::Target {
32 &self.0
33 }
34}
35
36impl fmt::Display for ModPath {
37 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38 let len = Path::levels(&self.0);
39 for (i, part) in Path::parts(&self.0).enumerate() {
40 write!(f, "{part}")?;
41 if i < len - 1 {
42 write!(f, "::")?
43 }
44 }
45 Ok(())
46 }
47}
48
49impl<A> FromIterator<A> for ModPath
50where
51 A: Borrow<str>,
52{
53 fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self {
54 ModPath(Path::from_iter(iter))
55 }
56}
57
58impl<I, A> From<I> for ModPath
59where
60 A: Borrow<str>,
61 I: IntoIterator<Item = A>,
62{
63 fn from(value: I) -> Self {
64 ModPath::from_iter(value)
65 }
66}
67
68impl PartialEq<[&str]> for ModPath {
69 fn eq(&self, other: &[&str]) -> bool {
70 Path::levels(&self.0) == other.len()
71 && Path::parts(&self.0).zip(other.iter()).all(|(s0, s1)| s0 == *s1)
72 }
73}
74
75impl<const L: usize> PartialEq<[&str; L]> for ModPath {
76 fn eq(&self, other: &[&str; L]) -> bool {
77 Path::levels(&self.0) == L
78 && Path::parts(&self.0).zip(other.iter()).all(|(s0, s1)| s0 == *s1)
79 }
80}