cynic_parser/values/
scalars.rs

1use core::fmt;
2
3use crate::{AstLookup, Span};
4
5use super::{ids::ValueId, Cursor};
6
7#[derive(Clone, Copy)]
8pub struct IntValue<'a>(pub(super) Cursor<'a, ValueId>);
9
10impl IntValue<'_> {
11    pub fn as_i64(&self) -> i64 {
12        self.value()
13    }
14
15    pub fn as_i32(&self) -> i32 {
16        self.value() as i32
17    }
18
19    pub fn value(&self) -> i64 {
20        let store = self.0.store;
21        store.lookup(self.0.id).kind.as_int().unwrap()
22    }
23
24    pub fn span(&self) -> Span {
25        let store = self.0.store;
26        store.lookup(self.0.id).span
27    }
28}
29
30impl IntValue<'_> {
31    pub fn id(&self) -> ValueId {
32        self.0.id
33    }
34}
35
36impl PartialEq for IntValue<'_> {
37    fn eq(&self, other: &Self) -> bool {
38        self.value() == other.value()
39    }
40}
41
42impl Eq for IntValue<'_> {}
43
44impl std::fmt::Debug for IntValue<'_> {
45    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46        write!(f, "{}", self.value())
47    }
48}
49
50impl std::fmt::Display for IntValue<'_> {
51    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52        write!(f, "{}", self.value())
53    }
54}
55
56#[derive(Clone, Copy)]
57pub struct FloatValue<'a>(pub(super) Cursor<'a, ValueId>);
58
59impl FloatValue<'_> {
60    pub fn value(&self) -> f64 {
61        let store = self.0.store;
62        store.lookup(self.0.id).kind.as_float().unwrap()
63    }
64
65    pub fn as_f64(&self) -> f64 {
66        self.value()
67    }
68
69    pub fn span(&self) -> Span {
70        let store = self.0.store;
71        store.lookup(self.0.id).span
72    }
73}
74
75impl FloatValue<'_> {
76    pub fn id(&self) -> ValueId {
77        self.0.id
78    }
79}
80
81impl PartialEq for FloatValue<'_> {
82    fn eq(&self, other: &Self) -> bool {
83        self.value() == other.value()
84    }
85}
86
87impl fmt::Debug for FloatValue<'_> {
88    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89        write!(f, "{}", self.value())
90    }
91}
92
93impl fmt::Display for FloatValue<'_> {
94    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95        write!(f, "{}", self.value())
96    }
97}
98
99#[derive(Clone, Copy)]
100pub struct StringValue<'a>(pub(super) Cursor<'a, ValueId>);
101
102impl<'a> StringValue<'a> {
103    pub fn value(&self) -> &'a str {
104        let store = &self.0.store;
105        store.lookup(store.lookup(self.0.id).kind.as_string().unwrap())
106    }
107
108    pub fn as_str(&self) -> &'a str {
109        self.value()
110    }
111
112    pub fn span(&self) -> Span {
113        let store = self.0.store;
114        store.lookup(self.0.id).span
115    }
116}
117
118impl StringValue<'_> {
119    pub fn id(&self) -> ValueId {
120        self.0.id
121    }
122}
123
124impl PartialEq for StringValue<'_> {
125    fn eq(&self, other: &Self) -> bool {
126        self.value() == other.value()
127    }
128}
129
130impl Eq for StringValue<'_> {}
131
132impl fmt::Debug for StringValue<'_> {
133    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134        write!(f, "{}", self.value())
135    }
136}
137
138impl fmt::Display for StringValue<'_> {
139    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
140        write!(f, "{}", self.value())
141    }
142}
143
144#[derive(Clone, Copy)]
145pub struct BooleanValue<'a>(pub(super) Cursor<'a, ValueId>);
146
147impl BooleanValue<'_> {
148    pub fn value(&self) -> bool {
149        let store = self.0.store;
150        store.lookup(self.0.id).kind.as_boolean().unwrap()
151    }
152
153    pub fn as_bool(&self) -> bool {
154        self.value()
155    }
156
157    pub fn span(&self) -> Span {
158        let store = self.0.store;
159        store.lookup(self.0.id).span
160    }
161}
162
163impl BooleanValue<'_> {
164    pub fn id(&self) -> ValueId {
165        self.0.id
166    }
167}
168
169impl PartialEq for BooleanValue<'_> {
170    fn eq(&self, other: &Self) -> bool {
171        self.value() == other.value()
172    }
173}
174
175impl Eq for BooleanValue<'_> {}
176
177impl fmt::Debug for BooleanValue<'_> {
178    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
179        write!(f, "{}", self.value())
180    }
181}
182
183impl fmt::Display for BooleanValue<'_> {
184    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
185        write!(f, "{}", self.value())
186    }
187}
188
189#[derive(Clone, Copy)]
190pub struct NullValue<'a>(pub(super) Cursor<'a, ValueId>);
191
192impl NullValue<'_> {
193    pub fn span(&self) -> Span {
194        let store = self.0.store;
195        store.lookup(self.0.id).span
196    }
197}
198
199impl NullValue<'_> {
200    pub fn id(&self) -> ValueId {
201        self.0.id
202    }
203}
204
205impl PartialEq for NullValue<'_> {
206    fn eq(&self, _: &Self) -> bool {
207        true
208    }
209}
210
211impl Eq for NullValue<'_> {}
212
213impl fmt::Debug for NullValue<'_> {
214    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
215        write!(f, "null")
216    }
217}
218
219impl fmt::Display for NullValue<'_> {
220    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
221        write!(f, "null")
222    }
223}