derive_wizard/
field_path.rs1#[derive(Debug, Clone, PartialEq, Eq, Hash)]
6pub struct FieldPath {
7 segments: Vec<String>,
8}
9
10impl FieldPath {
11 pub const fn new(segments: Vec<String>) -> Self {
13 Self { segments }
14 }
15
16 pub fn from_path(path: &str) -> Self {
18 Self {
19 segments: path.split('.').map(|s| s.to_string()).collect(),
20 }
21 }
22
23 pub fn segments(&self) -> &[String] {
25 &self.segments
26 }
27
28 pub fn to_path(&self) -> String {
30 self.segments.join(".")
31 }
32
33 pub const fn depth(&self) -> usize {
35 self.segments.len()
36 }
37}
38
39impl From<&str> for FieldPath {
40 fn from(s: &str) -> Self {
41 if s.contains('.') {
43 Self::from_path(s)
44 } else {
45 Self::new(vec![s.to_string()])
47 }
48 }
49}
50
51impl From<String> for FieldPath {
52 fn from(s: String) -> Self {
53 Self::from(s.as_str())
54 }
55}
56
57impl From<Vec<String>> for FieldPath {
58 fn from(segments: Vec<String>) -> Self {
59 Self::new(segments)
60 }
61}
62
63impl From<&[&str]> for FieldPath {
64 fn from(segments: &[&str]) -> Self {
65 Self::new(segments.iter().map(|s| s.to_string()).collect())
66 }
67}
68
69#[macro_export]
85macro_rules! field {
86 ($field:ident) => {
88 $crate::field_path::FieldPath::from(stringify!($field))
89 };
90
91 ($ty:ident :: $field:ident) => {
93 $crate::field_path::FieldPath::from(stringify!($field))
94 };
95
96 ($ty:ident :: $nested:ident :: $field:ident) => {
98 $crate::field_path::FieldPath::from(concat!(stringify!($nested), ".", stringify!($field)))
99 };
100
101 ($ty:ident :: $nested1:ident :: $nested2:ident :: $field:ident) => {
103 $crate::field_path::FieldPath::from(concat!(
104 stringify!($nested1),
105 ".",
106 stringify!($nested2),
107 ".",
108 stringify!($field)
109 ))
110 };
111
112 ($ty:ident :: $nested1:ident :: $nested2:ident :: $nested3:ident :: $field:ident) => {
114 $crate::field_path::FieldPath::from(concat!(
115 stringify!($nested1),
116 ".",
117 stringify!($nested2),
118 ".",
119 stringify!($nested3),
120 ".",
121 stringify!($field)
122 ))
123 };
124}