cynic_parser/values/
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
55
56
57
58
59
60
61
62
63
64
65
use std::fmt;

use crate::{AstLookup, Span};

use super::{const_lists::ConstList, ids::ValueId, iter::Iter, value::Value, Cursor};

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

impl<'a> List<'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, Value<'a>> {
        let store = &self.0.store;
        Iter::new(store.lookup(self.0.id).kind.as_list().unwrap(), store)
    }

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

impl PartialEq for List<'_> {
    fn eq(&self, other: &Self) -> bool {
        self.len() == other.len() && self.items().zip(other.items()).all(|(lhs, rhs)| lhs == rhs)
    }
}

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

impl<'a> From<ConstList<'a>> for List<'a> {
    fn from(value: ConstList<'a>) -> Self {
        let Cursor { id, store } = value.0;

        let id = id.into();

        List(Cursor { id, store })
    }
}

impl<'a> IntoIterator for List<'a> {
    type Item = Value<'a>;

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

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