cynic_parser/values/
objects.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use std::fmt;

use crate::{AstLookup, Span};

use super::{
    const_objects::ConstObject,
    ids::{FieldId, StringId, ValueId},
    iter::{Iter, ValueStoreReader},
    value::Value,
    Cursor, ValueStoreId,
};

pub struct FieldRecord {
    pub name: StringId,
    pub name_span: Span,
    pub value: ValueId,
}

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

impl<'a> Object<'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_object().unwrap().len()
    }

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

    pub fn fields(&self) -> Iter<'a, ObjectField<'a>> {
        let store = self.0.store;
        Iter::new(store.lookup(self.0.id).kind.as_object().unwrap(), store)
    }

    pub fn get(&self, name: &str) -> Option<Value<'a>> {
        Some(self.fields().find(|field| field.name() == name)?.value())
    }
}

impl PartialEq for Object<'_> {
    fn eq(&self, other: &Self) -> bool {
        self.len() == other.len()
            && self.fields().all(|field| {
                let needle = field.name();
                let Some(b_field) = other
                    .fields()
                    .find(|other_field| other_field.name() == needle)
                else {
                    return false;
                };

                field.value() == b_field.value()
            })
    }
}

impl fmt::Debug for Object<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_map()
            .entries(self.fields().map(|field| (field.name(), field.value())))
            .finish()
    }
}

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

        let id = id.into();

        Object(Cursor { id, store })
    }
}

impl<'a> IntoIterator for Object<'a> {
    type Item = ObjectField<'a>;

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

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

// TODO: Make sure this ObjectField name has no obvious clashes
// and rename if it does
#[derive(Clone, Copy)]
pub struct ObjectField<'a>(super::Cursor<'a, FieldId>);

impl<'a> ObjectField<'a> {
    pub fn name(&self) -> &'a str {
        let store = self.0.store;
        store.lookup(store.lookup(self.0.id).name)
    }

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

    pub fn value(&self) -> Value<'a> {
        let store = self.0.store;
        store.read(store.lookup(self.0.id).value)
    }
}

impl<'a> ValueStoreReader<'a> for ObjectField<'a> {
    type Id = FieldId;
}

impl ValueStoreId for FieldId {
    type Reader<'a> = ObjectField<'a>;

    fn read(self, store: &super::ValueStore) -> Self::Reader<'_> {
        ObjectField(super::Cursor { id: self, store })
    }
}