cynic_parser/values/
const_lists.rs1use std::fmt;
2
3use crate::{common::IdRange, AstLookup, Span};
4
5use super::{const_value::ConstValue, iter::Iter, ConstValueId};
6
7#[derive(Clone, Copy)]
8pub struct ConstList<'a>(pub(super) super::Cursor<'a, ConstValueId>);
9
10impl<'a> ConstList<'a> {
11 pub fn is_empty(&self) -> bool {
12 self.len() == 0
13 }
14
15 pub fn len(&self) -> usize {
16 let store = &self.0.store;
17 store.lookup(self.0.id).kind.as_list().unwrap().len()
18 }
19
20 pub fn span(&self) -> Span {
21 let store = &self.0.store;
22 store.lookup(self.0.id).span
23 }
24
25 pub fn items(&self) -> Iter<'a, ConstValue<'a>> {
26 let store = &self.0.store;
27
28 let IdRange { start, end } = store.lookup(self.0.id).kind.as_list().unwrap();
29 let start = ConstValueId::new(start.get());
30 let end = ConstValueId::new(end.get());
31
32 Iter::new(IdRange { start, end }, store)
33 }
34
35 pub fn get(&self, index: usize) -> Option<ConstValue<'a>> {
36 self.items().nth(index)
37 }
38}
39
40impl fmt::Debug for ConstList<'_> {
41 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42 f.debug_list().entries(self.items()).finish()
43 }
44}
45
46impl<'a> IntoIterator for ConstList<'a> {
47 type Item = ConstValue<'a>;
48
49 type IntoIter = Iter<'a, ConstValue<'a>>;
50
51 fn into_iter(self) -> Self::IntoIter {
52 self.items()
53 }
54}