eure_document/
path.rs

1use core::fmt::Display;
2
3use crate::prelude_internal::*;
4
5#[derive(Debug, Clone, PartialEq, Eq, Hash, Plural)]
6pub struct EurePath(pub Vec<PathSegment>);
7
8impl EurePath {
9    /// Create an empty path representing the document root
10    pub fn root() -> Self {
11        EurePath(Vec::new())
12    }
13
14    /// Check if this is the root path
15    pub fn is_root(&self) -> bool {
16        self.0.is_empty()
17    }
18}
19
20#[derive(Debug, Clone, PartialEq, Eq, Hash)]
21pub enum PathSegment {
22    /// Regular identifiers like id, description
23    Ident(Identifier),
24    /// Extension namespace fields starting with $ like $eure, $variant
25    Extension(Identifier),
26    /// Arbitrary value used as key
27    Value(ObjectKey),
28    /// Tuple element index (0-255)
29    TupleIndex(u8),
30    /// Array element access
31    ArrayIndex(Option<usize>),
32}
33
34impl Display for EurePath {
35    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
36        for segment in &self.0 {
37            match segment {
38                PathSegment::Ident(id) => write!(f, ".{}", id)?,
39                PathSegment::Extension(id) => write!(f, ".${}", id)?,
40                PathSegment::Value(key) => write!(f, ".{}", key)?,
41                PathSegment::TupleIndex(index) => write!(f, ".#{}", index)?,
42                PathSegment::ArrayIndex(Some(index)) => write!(f, "[{}]", index)?,
43                PathSegment::ArrayIndex(None) => write!(f, "[]")?,
44            }
45        }
46        Ok(())
47    }
48}