1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::fmt::Debug;

use super::Ident;

/// An accessor represents a dotted path
///
/// Probably it would be a good idea to intern accessors, like idents
#[derive(Clone, Eq, PartialEq, Hash)]
pub enum Accessor {
    /// A path like a.b.c
    Path(Vec<Ident>),
}

impl Accessor {
    pub fn equals_ident(&self, value: impl AsRef<str>) -> bool {
        match self {
            Accessor::Path(path) => match path.as_slice() {
                [ident] => match ident {
                    Ident::Value(name) => name == value.as_ref(),
                    Ident::Special(_) | Ident::Index(_) => false,
                },
                _ => false,
            },
        }
    }
}

impl From<Ident> for Accessor {
    fn from(other: Ident) -> Self {
        Accessor::Path(vec![other])
    }
}

impl Debug for Accessor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Accessor::Path(path) => write!(
                f,
                "Path({})",
                path.iter()
                    .map(Ident::to_string)
                    .collect::<Vec<String>>()
                    .join(".")
            ),
        }
    }
}