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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
use std::cell::Cell;
use std::ops::{Range, RangeInclusive};
use std::rc::Rc;
use std::str::FromStr;

use crate::elements::{AttributeImpl, CommentImpl, DocumentInternals, ElementImpl, ItemImpl};
use crate::{Attribute, Element, Error, Item, Printer, SectionElement};

#[derive(Debug)]
pub struct Field {
    content: FieldContent,
    document_internals: Rc<DocumentInternals>,
    escape_operator_ranges: Option<(Range<usize>, Range<usize>)>,
    key_range: Range<usize>,
    line_begin_index: usize,
    pub line_number: u32,
    operator_index: usize,
    touched: Cell<bool>,
}

#[derive(Debug)]
pub enum FieldContent {
    Attributes(Vec<Attribute>),
    Items(Vec<Item>),
    None,
    Value(Range<usize>),
}

pub trait FieldImpl {
    fn get_content(&self) -> &FieldContent;
    fn get_content_mut(&mut self) -> &mut FieldContent;
    fn get_document_content(&self) -> &str;
    fn new(
        content: FieldContent,
        document_internals: Rc<DocumentInternals>,
        escape_operator_ranges: Option<(Range<usize>, Range<usize>)>,
        key_range: Range<usize>,
        line_begin_index: usize,
        line_number: u32,
        operator_index: usize,
    ) -> Field;
    fn set_content(&mut self, content: FieldContent);
}

impl Field {
    pub fn attributes(&self) -> Result<&[Attribute], Error> {
        match &self.content {
            FieldContent::Attributes(attributes) => {
                for attribute in attributes.iter() {
                    attribute.touch();
                }
                Ok(attributes.as_slice())
            }
            FieldContent::Items(_) => Err(Error::new(
                format!("Attributes expected, Items found"),
                self.line_number,
            )),
            FieldContent::Value { .. } => Err(Error::new(
                format!("Attributes expected, Value found"),
                self.line_number,
            )),
            FieldContent::None => Ok(&[]),
        }
    }

    pub fn items(&self) -> Result<&[Item], Error> {
        match &self.content {
            FieldContent::Attributes(_) => Err(Error::new(
                format!("Items expected, Attributes found"),
                self.line_number,
            )),
            FieldContent::Items(items) => {
                for item in items.iter() {
                    item.touch();
                }
                Ok(items.as_slice())
            }
            FieldContent::Value { .. } => Err(Error::new(
                format!("Items expected, Value found"),
                self.line_number,
            )),
            FieldContent::None => Ok(&[]),
        }
    }

    pub fn optional_value(&self) -> Result<Option<String>, Error> {
        match &self.content {
            FieldContent::Attributes(attributes) => Err(Error::new(
                format!("Value expected, Attributes found"),
                attributes[0].line_number,
            )),
            FieldContent::Items(items) => Err(Error::new(
                format!("Value expected, Items found"),
                items[0].line_number,
            )),
            FieldContent::Value(range) => Ok(Some(
                self.document_internals.content[range.clone()].to_owned(),
            )),
            FieldContent::None => Ok(None),
        }
    }

    pub fn required_attribute(&self, key: &str) -> Result<&Attribute, Error> {
        match &self.content {
            FieldContent::Attributes(attributes) => {
                let mut attribute_option = None;

                for attribute in attributes {
                    if attribute.key() == key {
                        match attribute_option {
                            Some(_) => {
                                return Err(Error::new(
                                    format!("Multiple attributes with key {} found", key),
                                    self.line_number,
                                ))
                            }
                            None => attribute_option = Some(attribute),
                        }
                    }
                }

                match attribute_option {
                    Some(attribute) => {
                        attribute.touch();
                        Ok(attribute)
                    }
                    None => Err(Error::new(
                        format!("Missing attribute {}", key),
                        self.line_number,
                    )),
                }
            }
            FieldContent::Items(_) => Err(Error::new(
                format!("Attributes expected, Items found"),
                self.line_number,
            )),
            FieldContent::Value { .. } => Err(Error::new(
                format!("Attributes expected, Value found"),
                self.line_number,
            )),
            FieldContent::None => Err(Error::new(
                format!("Missing attribute {}", key),
                self.line_number,
            )),
        }
    }

    pub fn required_value<T: FromStr>(&self) -> Result<T, Error>
    where
        <T as FromStr>::Err: std::fmt::Display,
    {
        match &self.content {
            FieldContent::Attributes(_) => Err(Error::new(
                format!("Value expected, Attributes found"),
                self.line_number,
            )),
            FieldContent::Items(_) => Err(Error::new(
                format!("Value expected, Items found"),
                self.line_number,
            )),
            FieldContent::Value(range) => {
                let value = &self.document_internals.content[range.clone()].to_owned();
                match value.parse::<T>() {
                    Ok(converted) => Ok(converted),
                    Err(err) => Err(Error::new(format!("{}", err), self.line_number)),
                }
            }
            FieldContent::None => Err(Error::new(format!("Missing value"), self.line_number)),
        }
    }

    pub fn untouched_elements(&self) -> Vec<&dyn Element> {
        match &self.content {
            FieldContent::Attributes(attributes) => attributes
                .iter()
                .filter(|attribute| !attribute.touched())
                .map(|attribute| attribute.as_element())
                .collect(),
            FieldContent::Items(items) => items
                .iter()
                .filter(|item| !item.touched())
                .map(|item| item.as_element())
                .collect(),
            _ => Vec::new(),
        }
    }
}

impl Element for Field {
    fn as_field(&self) -> Option<&Field> {
        Some(self)
    }

    fn is_field(&self) -> bool {
        true
    }

    fn line_number(&self) -> u32 {
        self.line_number
    }

    fn snippet(&self) -> String {
        self.snippet_with_options(&*self.document_internals.default_printer, true)
    }

    fn snippet_with_options(&self, printer: &dyn Printer, gutter: bool) -> String {
        let mut out = String::new();

        if gutter {
            out.push_str(&printer.gutter(self.line_number));
        }

        if let Some((escape_begin_range, escape_end_range)) = &self.escape_operator_ranges {
            out.push_str(
                &self.document_internals.content[self.line_begin_index..escape_begin_range.start],
            );
            out.push_str(
                &printer.operator(&self.document_internals.content[escape_begin_range.clone()]),
            );
            out.push_str(
                &self.document_internals.content[escape_begin_range.end..self.key_range.start],
            );
            out.push_str(&printer.key(&self.document_internals.content[self.key_range.clone()]));
            out.push_str(
                &self.document_internals.content[self.key_range.end..escape_end_range.start],
            );
            out.push_str(
                &printer.operator(&self.document_internals.content[escape_end_range.clone()]),
            );
            out.push_str(
                &self.document_internals.content[escape_end_range.end..self.operator_index],
            );
        } else {
            out.push_str(
                &self.document_internals.content[self.line_begin_index..self.key_range.start],
            );
            out.push_str(&printer.key(&self.document_internals.content[self.key_range.clone()]));
            out.push_str(&self.document_internals.content[self.key_range.end..self.operator_index]);
        }

        out.push_str(&self.document_internals.content[self.key_range.end..self.operator_index]);
        out.push_str(&printer.operator(":"));

        let mut line_number = self.line_number + 1;

        match &self.content {
            FieldContent::Attributes(attributes) => {
                for attribute in attributes {
                    out.push('\n');

                    let attribute_line_range = attribute.line_range();

                    while line_number < *attribute_line_range.start() {
                        if let Some(comment) = self
                            .document_internals
                            .comments
                            .borrow()
                            .iter()
                            .find(|comment| comment.line_number == line_number)
                        {
                            out.push_str(&comment.snippet_with_options(printer, gutter));
                        } else if gutter {
                            out.push_str(&printer.gutter(line_number));
                        }

                        out.push('\n');

                        line_number += 1;
                    }

                    out.push_str(&attribute.snippet_with_options(printer, gutter));

                    line_number = *attribute_line_range.end() + 1;
                }
            }
            FieldContent::Items(items) => {
                for item in items {
                    out.push('\n');

                    let item_line_range = item.line_range();

                    while line_number < *item_line_range.start() {
                        if let Some(comment) = self
                            .document_internals
                            .comments
                            .borrow()
                            .iter()
                            .find(|comment| comment.line_number == line_number)
                        {
                            out.push_str(&comment.snippet_with_options(printer, gutter));
                        } else if gutter {
                            out.push_str(&printer.gutter(line_number));
                        }

                        out.push('\n');

                        line_number += 1;
                    }

                    out.push_str(&item.snippet_with_options(printer, gutter));

                    line_number = *item_line_range.end() + 1;
                }
            }
            FieldContent::Value(value_range) => {
                out.push_str(
                    &self.document_internals.content[(self.operator_index + 1)..value_range.start],
                );
                out.push_str(&printer.value(&self.document_internals.content[value_range.clone()]));
            }
            FieldContent::None => (),
        }

        out
    }

    fn touch(&self) {
        self.touched.set(true);
    }
}

impl ElementImpl for Field {
    fn line_range(&self) -> RangeInclusive<u32> {
        let end = match &self.content {
            FieldContent::Attributes(attributes) => *attributes.last().unwrap().line_range().end(),
            FieldContent::Items(items) => *items.last().unwrap().line_range().end(),
            FieldContent::Value(_) | FieldContent::None => self.line_number,
        };

        self.line_number..=end
    }

    fn touched(&self) -> bool {
        self.touched.get()
    }
}

impl FieldImpl for Field {
    fn get_content(&self) -> &FieldContent {
        &self.content
    }

    fn get_content_mut(&mut self) -> &mut FieldContent {
        &mut self.content
    }

    fn get_document_content(&self) -> &str {
        &self.document_internals.content
    }

    fn new(
        content: FieldContent,
        document_internals: Rc<DocumentInternals>,
        escape_operator_ranges: Option<(Range<usize>, Range<usize>)>,
        key_range: Range<usize>,
        line_begin_index: usize,
        line_number: u32,
        operator_index: usize,
    ) -> Field {
        Field {
            content,
            document_internals,
            escape_operator_ranges,
            key_range,
            line_begin_index,
            line_number,
            operator_index,
            touched: Cell::new(false),
        }
    }

    fn set_content(&mut self, content: FieldContent) {
        self.content = content;
    }
}

impl SectionElement for Field {
    fn as_element(&self) -> &dyn Element {
        self
    }

    fn as_mut_field(&mut self) -> Option<&mut Field> {
        Some(self)
    }

    fn key(&self) -> &str {
        &self.document_internals.content[self.key_range.clone()]
    }
}