tytanic_filter/ast/
id.rs

1use std::borrow::Borrow;
2use std::fmt::Debug;
3use std::ops::Deref;
4
5use ecow::EcoString;
6use pest::iterators::Pair;
7
8use super::Error;
9use super::PairExt;
10use super::Rule;
11use crate::eval;
12use crate::eval::Context;
13use crate::eval::Eval;
14use crate::eval::Test;
15use crate::eval::Value;
16
17/// An identifier node.
18#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
19pub struct Id(pub EcoString);
20
21impl Id {
22    /// The inner string.
23    pub fn as_str(&self) -> &str {
24        self.0.as_str()
25    }
26
27    /// Unwraps the inner EcoString.
28    pub fn into_inner(self) -> EcoString {
29        self.0
30    }
31}
32
33impl Debug for Id {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        self.0.fmt(f)
36    }
37}
38
39impl Deref for Id {
40    type Target = str;
41
42    fn deref(&self) -> &Self::Target {
43        self.as_str()
44    }
45}
46
47impl AsRef<str> for Id {
48    fn as_ref(&self) -> &str {
49        self.as_str()
50    }
51}
52
53impl Borrow<str> for Id {
54    fn borrow(&self) -> &str {
55        self.as_str()
56    }
57}
58
59impl From<EcoString> for Id {
60    fn from(value: EcoString) -> Self {
61        Self(value)
62    }
63}
64
65impl From<Id> for EcoString {
66    fn from(value: Id) -> Self {
67        value.into_inner()
68    }
69}
70
71impl<T: Test> Eval<T> for Id {
72    fn eval(&self, ctx: &Context<T>) -> Result<Value<T>, eval::Error> {
73        ctx.resolve(self)
74    }
75}
76
77impl Id {
78    pub(super) fn parse(pair: Pair<'_, Rule>) -> Result<Id, Error> {
79        pair.expect_rules(&[Rule::id])?;
80        Ok(Id(pair.as_str().into()))
81    }
82}