cynic_parser/values/
const_lists.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use std::fmt;

use crate::{common::IdRange, AstLookup, Span};

use super::{const_value::ConstValue, iter::Iter, ConstValueId};

#[derive(Clone, Copy)]
pub struct ConstList<'a>(pub(super) super::Cursor<'a, ConstValueId>);

impl<'a> ConstList<'a> {
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn len(&self) -> usize {
        let store = &self.0.store;
        store.lookup(self.0.id).kind.as_list().unwrap().len()
    }

    pub fn span(&self) -> Span {
        let store = &self.0.store;
        store.lookup(self.0.id).span
    }

    pub fn items(&self) -> Iter<'a, ConstValue<'a>> {
        let store = &self.0.store;

        let IdRange { start, end } = store.lookup(self.0.id).kind.as_list().unwrap();
        let start = ConstValueId::new(start.get());
        let end = ConstValueId::new(end.get());

        Iter::new(IdRange { start, end }, store)
    }

    pub fn get(&self, index: usize) -> Option<ConstValue<'a>> {
        self.items().nth(index)
    }
}

impl fmt::Debug for ConstList<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list().entries(self.items()).finish()
    }
}

impl<'a> IntoIterator for ConstList<'a> {
    type Item = ConstValue<'a>;

    type IntoIter = Iter<'a, ConstValue<'a>>;

    fn into_iter(self) -> Self::IntoIter {
        self.items()
    }
}