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
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)
    }
}

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 })
    }
}