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