cynic_parser/values/
variables.rs

1use core::fmt;
2
3use crate::{AstLookup, Span};
4
5use super::{ids::ValueId, Cursor};
6
7#[derive(Clone, Copy)]
8pub struct VariableValue<'a>(pub(super) Cursor<'a, ValueId>);
9
10impl<'a> VariableValue<'a> {
11    pub fn name(&self) -> &'a str {
12        let store = self.0.store;
13        store.lookup(store.lookup(self.0.id).kind.as_variable().unwrap())
14    }
15
16    pub fn span(&self) -> Span {
17        let store = self.0.store;
18        store.lookup(self.0.id).span
19    }
20}
21
22impl VariableValue<'_> {
23    pub fn id(&self) -> ValueId {
24        self.0.id
25    }
26}
27
28impl PartialEq for VariableValue<'_> {
29    fn eq(&self, other: &Self) -> bool {
30        self.name() == other.name()
31    }
32}
33
34impl Eq for VariableValue<'_> {}
35
36impl fmt::Display for VariableValue<'_> {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        write!(f, "${}", self.name())
39    }
40}
41
42impl fmt::Debug for VariableValue<'_> {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        f.debug_struct("Variable")
45            .field("name", &self.name())
46            .field("span", &self.span())
47            .finish()
48    }
49}