derive_wizard/
field_path.rs

1/// A field path for accessing nested wizard fields.
2///
3/// This can be created from a simple string (for flat fields) or from
4/// a path array (for nested fields). Paths use dot (`.`) as the separator.
5#[derive(Debug, Clone, PartialEq, Eq, Hash)]
6pub struct FieldPath {
7    segments: Vec<String>,
8}
9
10impl FieldPath {
11    /// Create a new field path from segments.
12    pub const fn new(segments: Vec<String>) -> Self {
13        Self { segments }
14    }
15
16    /// Create a field path from a dot-separated string.
17    pub fn from_path(path: &str) -> Self {
18        Self {
19            segments: path.split('.').map(|s| s.to_string()).collect(),
20        }
21    }
22
23    /// Get the segments of this path.
24    pub fn segments(&self) -> &[String] {
25        &self.segments
26    }
27
28    /// Convert this path to a dot-separated string.
29    pub fn to_path(&self) -> String {
30        self.segments.join(".")
31    }
32
33    /// Get the depth of this path (number of segments).
34    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        // Check if it contains a dot - if so, treat as path
42        if s.contains('.') {
43            Self::from_path(s)
44        } else {
45            // Single segment
46            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 for creating type-safe field paths.
70///
71/// # Examples
72///
73/// ```rust
74/// use derive_wizard::field;
75/// // Single field (top-level)
76/// field!(name); // expands to FieldPath from "name"
77///
78/// // Nested field
79/// field!(Person::contact::email); // expands to FieldPath from "contact.email"
80///
81/// // Multiple levels
82/// field!(Company::address::location::city); // expands to FieldPath from "address.location.city"
83/// ```
84#[macro_export]
85macro_rules! field {
86    // Single identifier - top level field
87    ($field:ident) => {
88        $crate::field_path::FieldPath::from(stringify!($field))
89    };
90
91    // Type::field - one level nesting
92    ($ty:ident :: $field:ident) => {
93        $crate::field_path::FieldPath::from(stringify!($field))
94    };
95
96    // Type::nested::field - two level nesting (dot-separated for namespace prefixes)
97    ($ty:ident :: $nested:ident :: $field:ident) => {
98        $crate::field_path::FieldPath::from(concat!(stringify!($nested), ".", stringify!($field)))
99    };
100
101    // Type::nested1::nested2::field - three level nesting (dot-separated)
102    ($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    // Type::nested1::nested2::nested3::field - four level nesting (dot-separated)
113    ($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}