cynic_parser/values/
writer.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
use std::sync::Arc;

use indexmap::IndexSet;

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

pub use super::{ids::*, storage::*};

#[derive(Default)]
pub struct ValueWriter {
    values: Vec<ValueRecord>,

    fields: Vec<FieldRecord>,
}

impl ValueWriter {
    pub fn update(store: super::ValueStore) -> Self {
        let super::ValueStore {
            strings: _,
            values,
            fields,
        } = store;

        Self { values, fields }
    }

    pub fn value(&mut self, record: ValueRecord) -> ValueId {
        let id = ValueId::new(self.values.len());
        self.values.push(record);
        id
    }

    pub fn const_value(&mut self, record: ValueRecord) -> ConstValueId {
        if let ValueKind::Variable(_) = record.kind {
            panic!("Don't pass a variable into const_value");
        }
        let value_id = self.value(record);
        ConstValueId::new(value_id.get())
    }

    pub fn list(&mut self, values: Vec<ValueRecord>) -> IdRange<ValueId> {
        let start = ValueId::new(self.values.len());

        self.values.extend(values);

        let end = ValueId::new(self.values.len());

        IdRange::new(start, end)
    }

    pub fn fields(&mut self, records: Vec<(StringId, Span, ValueId)>) -> IdRange<FieldId> {
        let start = FieldId::new(self.fields.len());

        self.fields.extend(
            records
                .into_iter()
                .map(|(name, name_span, value)| FieldRecord {
                    name,
                    name_span,
                    value,
                }),
        );

        let end = FieldId::new(self.fields.len());

        IdRange::new(start, end)
    }

    pub fn const_fields(
        &mut self,
        records: Vec<(StringId, Span, ConstValueId)>,
    ) -> IdRange<FieldId> {
        let start = FieldId::new(self.fields.len());

        self.fields.extend(
            records
                .into_iter()
                .map(|(name, name_span, value)| FieldRecord {
                    name,
                    name_span,
                    value: ValueId::new(value.get()),
                }),
        );

        let end = FieldId::new(self.fields.len());

        IdRange::new(start, end)
    }

    pub(crate) fn finish(self, strings: Arc<IndexSet<Box<str>>>) -> super::ValueStore {
        let ValueWriter { values, fields } = self;

        super::ValueStore {
            strings,
            values,
            fields,
        }
    }
}