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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use std::collections::HashMap;

use crate::sage::Idea;

use crate::book::word::Word;

use crate::model::yaml::map;
use crate::model::yaml::seq;

use std::borrow::Cow;

pub struct Volume {
    pub complete: bool,
    pub gist: Vec<(Option<String>, usize, Word)>,

    buff: Option<HashMap<usize, Idea>>,
}

impl Volume {
    pub fn new() -> Volume {
        Volume {
            complete: false,
            gist: Vec::with_capacity(0),
            buff: Some(HashMap::with_capacity(256)),
        }
    }

    pub fn complete(&mut self) {
        if self.complete {
            return;
        }

        let mut buff = self.buff.take().unwrap();

        self.gist.reserve_exact(buff.len());

        let mut border: usize = 0;

        for ix in buff.keys() {
            border = *ix;
            break;
        }

        /* 1 is always Dawn, so starting from 2 */
        for ix in 2..border {
            if buff.contains_key(&ix) {
                border = ix;
                break;
            }
        }

        let mut ix = border;

        for _ in 0..buff.len() {
            loop {
                if let Some(idea) = buff.remove(&ix) {
                    self.process(idea);
                    ix += 1;
                    break;
                } else {
                    ix += 1;
                    continue;
                }
            }
        }

        self.complete = true;
    }

    fn process(&mut self, idea: Idea) {
        match idea {
            Idea::Error(id, value) => self
                .gist
                .push((None, id.level, Word::Err(Cow::from(value)))),
            Idea::ReadError(id, _, string) => {
                self.gist
                    .push((None, id.level, Word::Err(Cow::from(string))))
            }
            Idea::ReadWarning(id, _, string) => {
                self.gist
                    .push((None, id.level, Word::Wrn(Cow::from(string))))
            }

            Idea::NodeLiteral(id, alias, value) => {
                self.gist.push((alias, id.level, Word::Str(value)))
            }
            Idea::NodeScalar(id, alias, value) => {
                self.gist
                    .push((alias, id.level, Word::extract_scalar(value)))
            }

            Idea::NodeSequence(id, alias, tag) => {
                self.gist.push((alias, id.level, Word::Seq(Cow::from(tag))))
            }
            Idea::NodeMetaSeq(id, alias, None) => {
                self.gist
                    .push((alias, id.level, Word::Seq(Cow::from(seq::TAG))))
            }
            Idea::NodeMetaSeq(id, alias, Some(tag)) => {
                self.gist.push((alias, id.level, Word::Seq(Cow::from(tag))))
            }

            Idea::NodeDictionary(id, alias, _, firstborn_id) => {
                self.gist
                    .push((alias, id.level, Word::Map(Cow::from(map::TAG))));

                if firstborn_id.is_some() {
                    // TODO: check whether it's ALWAYS the previous node?
                    let ln = self.gist.len();
                    let mut firstborn = self.gist.swap_remove(ln - 2);
                    firstborn.1 += 1; // level up
                    self.gist.push(firstborn);
                }
            }

            Idea::NodeMetaMap(id, alias, tag, firstborn_id) => {
                if let Some(tag) = tag {
                    self.gist.push((alias, id.level, Word::Map(Cow::from(tag))));
                } else {
                    self.gist
                        .push((alias, id.level, Word::Map(Cow::from(map::TAG))));
                }

                if firstborn_id.is_some() {
                    // TODO: check whether it's ALWAYS the previous node?
                    let ln = self.gist.len();
                    let mut firstborn = self.gist.swap_remove(ln - 2);
                    firstborn.1 += 1; // level up
                    self.gist.push(firstborn);
                }
            }

            Idea::Alias(id, value) => {
                let mut narr: Option<Word> = None;

                for (ix, &(ref alias, _, _)) in self.gist.iter().enumerate().rev() {
                    if let Some(ref alias) = *alias {
                        if *alias == value {
                            narr = Some(Word::Alias(ix));
                            break;
                        }
                    }
                }

                if narr.is_some() {
                    self.gist.push((None, id.level, narr.take().unwrap()));
                } else {
                    self.gist.push((None, id.level, Word::UnboundAlias(value)));
                }
            }

            Idea::Done | Idea::Dawn | Idea::Dusk => unreachable!(),
        };
    }

    pub fn stamp(&mut self, idea: Idea) {
        if self.complete {
            return;
        }

        let ix = match idea {
            Idea::Alias(ref id, _) => id.index,
            Idea::Error(ref id, _) => id.index,
            Idea::NodeMetaMap(ref id, _, _, _) => id.index,
            Idea::NodeMetaSeq(ref id, _, _) => id.index,
            Idea::NodeDictionary(ref id, _, _, _) => id.index,
            Idea::NodeSequence(ref id, _, _) => id.index,
            Idea::NodeScalar(ref id, _, _) => id.index,
            Idea::NodeLiteral(ref id, _, _) => id.index,
            Idea::ReadError(ref id, _, _) => id.index,
            Idea::ReadWarning(ref id, _, _) => id.index,

            _ => 0,
        };

        if ix > 0 {
            self.buff.as_mut().unwrap().insert(ix, idea);
        }
    }

    pub fn unalias(&self, idx: usize) -> &Word {
        let &(_, _, ref word) = &self.gist[idx];

        match *word {
            Word::Alias(idx) => self.unalias(idx),
            _ => word,
        }
    }
}