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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
use std::iter::FromIterator;
use indexmap::map::IndexMap;
use crate::key::Key;
use crate::repr::Decor;
use crate::value::DEFAULT_VALUE_DECOR;
use crate::{InlineTable, InternalString, Item, KeyMut, Value};
#[derive(Clone, Debug, Default)]
pub struct Table {
pub(crate) decor: Decor,
pub(crate) implicit: bool,
pub(crate) dotted: bool,
doc_position: Option<usize>,
pub(crate) items: KeyValuePairs,
}
impl Table {
pub fn new() -> Self {
Default::default()
}
pub(crate) fn with_pos(doc_position: Option<usize>) -> Self {
Self {
doc_position,
..Default::default()
}
}
pub(crate) fn with_pairs(items: KeyValuePairs) -> Self {
Self {
items,
..Default::default()
}
}
pub fn into_inline_table(mut self) -> InlineTable {
for (_, kv) in self.items.iter_mut() {
kv.value.make_value();
}
let mut t = InlineTable::with_pairs(self.items);
t.fmt();
t
}
}
impl Table {
pub fn get_values(&self) -> Vec<(Vec<&Key>, &Value)> {
let mut values = Vec::new();
let root = Vec::new();
self.append_values(&root, &mut values);
values
}
fn append_values<'s, 'c>(
&'s self,
parent: &[&'s Key],
values: &'c mut Vec<(Vec<&'s Key>, &'s Value)>,
) {
for value in self.items.values() {
let mut path = parent.to_vec();
path.push(&value.key);
match &value.value {
Item::Table(table) if table.is_dotted() => {
table.append_values(&path, values);
}
Item::Value(value) => {
if let Some(table) = value.as_inline_table() {
if table.is_dotted() {
table.append_values(&path, values);
} else {
values.push((path, value));
}
} else {
values.push((path, value));
}
}
_ => {}
}
}
}
pub fn fmt(&mut self) {
decorate_table(self);
}
pub fn sort_values(&mut self) {
self.items.sort_keys();
for kv in self.items.values_mut() {
match &mut kv.value {
Item::Table(table) if table.is_dotted() => {
table.sort_values();
}
_ => {}
}
}
}
pub fn sort_values_by<F>(&mut self, mut compare: F)
where
F: FnMut(&Key, &Item, &Key, &Item) -> std::cmp::Ordering,
{
self.sort_values_by_internal(&mut compare);
}
fn sort_values_by_internal<F>(&mut self, compare: &mut F)
where
F: FnMut(&Key, &Item, &Key, &Item) -> std::cmp::Ordering,
{
let modified_cmp = |_: &InternalString,
val1: &TableKeyValue,
_: &InternalString,
val2: &TableKeyValue|
-> std::cmp::Ordering {
compare(&val1.key, &val1.value, &val2.key, &val2.value)
};
self.items.sort_by(modified_cmp);
for kv in self.items.values_mut() {
match &mut kv.value {
Item::Table(table) if table.is_dotted() => {
table.sort_values_by_internal(compare);
}
_ => {}
}
}
}
pub fn set_implicit(&mut self, implicit: bool) {
self.implicit = implicit;
}
pub fn is_implicit(&self) -> bool {
self.implicit
}
pub fn set_dotted(&mut self, yes: bool) {
self.dotted = yes;
}
pub fn is_dotted(&self) -> bool {
self.dotted
}
pub fn set_position(&mut self, doc_position: usize) {
self.doc_position = Some(doc_position);
}
pub fn position(&self) -> Option<usize> {
self.doc_position
}
pub fn decor_mut(&mut self) -> &mut Decor {
&mut self.decor
}
pub fn decor(&self) -> &Decor {
&self.decor
}
pub fn key_decor_mut(&mut self, key: &str) -> Option<&mut Decor> {
self.items.get_mut(key).map(|kv| &mut kv.key.decor)
}
pub fn key_decor(&self, key: &str) -> Option<&Decor> {
self.items.get(key).map(|kv| &kv.key.decor)
}
}
impl Table {
pub fn iter(&self) -> Iter<'_> {
Box::new(
self.items
.iter()
.filter(|(_, kv)| !kv.value.is_none())
.map(|(key, kv)| (&key[..], &kv.value)),
)
}
pub fn iter_mut(&mut self) -> IterMut<'_> {
Box::new(
self.items
.iter_mut()
.filter(|(_, kv)| !kv.value.is_none())
.map(|(_, kv)| (kv.key.as_mut(), &mut kv.value)),
)
}
pub fn len(&self) -> usize {
self.items.iter().filter(|i| !(i.1).value.is_none()).count()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn clear(&mut self) {
self.items.clear()
}
pub fn entry<'a>(&'a mut self, key: &str) -> Entry<'a> {
match self.items.entry(key.into()) {
indexmap::map::Entry::Occupied(entry) => Entry::Occupied(OccupiedEntry { entry }),
indexmap::map::Entry::Vacant(entry) => Entry::Vacant(VacantEntry { entry, key: None }),
}
}
pub fn entry_format<'a>(&'a mut self, key: &Key) -> Entry<'a> {
match self.items.entry(key.get().into()) {
indexmap::map::Entry::Occupied(entry) => Entry::Occupied(OccupiedEntry { entry }),
indexmap::map::Entry::Vacant(entry) => Entry::Vacant(VacantEntry {
entry,
key: Some(key.to_owned()),
}),
}
}
pub fn get<'a>(&'a self, key: &str) -> Option<&'a Item> {
self.items.get(key).and_then(|kv| {
if !kv.value.is_none() {
Some(&kv.value)
} else {
None
}
})
}
pub fn get_mut<'a>(&'a mut self, key: &str) -> Option<&'a mut Item> {
self.items.get_mut(key).and_then(|kv| {
if !kv.value.is_none() {
Some(&mut kv.value)
} else {
None
}
})
}
pub fn get_key_value<'a>(&'a self, key: &str) -> Option<(&'a Key, &'a Item)> {
self.items.get(key).and_then(|kv| {
if !kv.value.is_none() {
Some((&kv.key, &kv.value))
} else {
None
}
})
}
pub fn get_key_value_mut<'a>(&'a mut self, key: &str) -> Option<(KeyMut<'a>, &'a mut Item)> {
self.items.get_mut(key).and_then(|kv| {
if !kv.value.is_none() {
Some((kv.key.as_mut(), &mut kv.value))
} else {
None
}
})
}
pub fn contains_key(&self, key: &str) -> bool {
if let Some(kv) = self.items.get(key) {
!kv.value.is_none()
} else {
false
}
}
pub fn contains_table(&self, key: &str) -> bool {
if let Some(kv) = self.items.get(key) {
kv.value.is_table()
} else {
false
}
}
pub fn contains_value(&self, key: &str) -> bool {
if let Some(kv) = self.items.get(key) {
kv.value.is_value()
} else {
false
}
}
pub fn contains_array_of_tables(&self, key: &str) -> bool {
if let Some(kv) = self.items.get(key) {
kv.value.is_array_of_tables()
} else {
false
}
}
pub fn insert(&mut self, key: &str, item: Item) -> Option<Item> {
let kv = TableKeyValue::new(Key::new(key), item);
self.items.insert(key.into(), kv).map(|kv| kv.value)
}
pub fn insert_formatted(&mut self, key: &Key, item: Item) -> Option<Item> {
let kv = TableKeyValue::new(key.to_owned(), item);
self.items.insert(key.get().into(), kv).map(|kv| kv.value)
}
pub fn remove(&mut self, key: &str) -> Option<Item> {
self.items.shift_remove(key).map(|kv| kv.value)
}
pub fn remove_entry(&mut self, key: &str) -> Option<(Key, Item)> {
self.items.shift_remove(key).map(|kv| (kv.key, kv.value))
}
}
impl std::fmt::Display for Table {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use crate::encode::Encode;
let children = self.get_values();
for (key_path, value) in children {
key_path.as_slice().encode(f, DEFAULT_KEY_DECOR)?;
write!(f, "=")?;
value.encode(f, DEFAULT_VALUE_DECOR)?;
writeln!(f)?;
}
Ok(())
}
}
impl<K: Into<Key>, V: Into<Value>> Extend<(K, V)> for Table {
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
for (key, value) in iter {
let key = key.into();
let value = Item::Value(value.into());
let value = TableKeyValue::new(key, value);
self.items.insert(value.key.get().into(), value);
}
}
}
impl<K: Into<Key>, V: Into<Value>> FromIterator<(K, V)> for Table {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = (K, V)>,
{
let mut table = Table::new();
table.extend(iter);
table
}
}
impl IntoIterator for Table {
type Item = (InternalString, Item);
type IntoIter = IntoIter;
fn into_iter(self) -> Self::IntoIter {
Box::new(self.items.into_iter().map(|(k, kv)| (k, kv.value)))
}
}
impl<'s> IntoIterator for &'s Table {
type Item = (&'s str, &'s Item);
type IntoIter = Iter<'s>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
pub(crate) type KeyValuePairs = IndexMap<InternalString, TableKeyValue>;
fn decorate_table(table: &mut Table) {
for (key_decor, value) in table
.items
.iter_mut()
.filter(|&(_, ref kv)| kv.value.is_value())
.map(|(_, kv)| (&mut kv.key.decor, kv.value.as_value_mut().unwrap()))
{
key_decor.clear();
value.decor_mut().clear();
}
}
pub(crate) const DEFAULT_KEY_DECOR: (&str, &str) = ("", " ");
pub(crate) const DEFAULT_TABLE_DECOR: (&str, &str) = ("\n", "");
pub(crate) const DEFAULT_KEY_PATH_DECOR: (&str, &str) = ("", "");
#[derive(Debug, Clone)]
pub(crate) struct TableKeyValue {
pub(crate) key: Key,
pub(crate) value: Item,
}
impl TableKeyValue {
pub(crate) fn new(key: Key, value: Item) -> Self {
TableKeyValue { key, value }
}
}
pub type IntoIter = Box<dyn Iterator<Item = (InternalString, Item)>>;
pub type Iter<'a> = Box<dyn Iterator<Item = (&'a str, &'a Item)> + 'a>;
pub type IterMut<'a> = Box<dyn Iterator<Item = (KeyMut<'a>, &'a mut Item)> + 'a>;
pub trait TableLike: crate::private::Sealed {
fn iter(&self) -> Iter<'_>;
fn iter_mut(&mut self) -> IterMut<'_>;
fn len(&self) -> usize {
self.iter().filter(|&(_, v)| !v.is_none()).count()
}
fn is_empty(&self) -> bool {
self.len() == 0
}
fn clear(&mut self);
fn entry<'a>(&'a mut self, key: &str) -> Entry<'a>;
fn entry_format<'a>(&'a mut self, key: &Key) -> Entry<'a>;
fn get<'s>(&'s self, key: &str) -> Option<&'s Item>;
fn get_mut<'s>(&'s mut self, key: &str) -> Option<&'s mut Item>;
fn get_key_value<'a>(&'a self, key: &str) -> Option<(&'a Key, &'a Item)>;
fn get_key_value_mut<'a>(&'a mut self, key: &str) -> Option<(KeyMut<'a>, &'a mut Item)>;
fn contains_key(&self, key: &str) -> bool;
fn insert(&mut self, key: &str, value: Item) -> Option<Item>;
fn remove(&mut self, key: &str) -> Option<Item>;
fn get_values(&self) -> Vec<(Vec<&Key>, &Value)>;
fn fmt(&mut self);
fn sort_values(&mut self);
fn set_dotted(&mut self, yes: bool);
fn is_dotted(&self) -> bool;
fn key_decor_mut(&mut self, key: &str) -> Option<&mut Decor>;
fn key_decor(&self, key: &str) -> Option<&Decor>;
}
impl TableLike for Table {
fn iter(&self) -> Iter<'_> {
self.iter()
}
fn iter_mut(&mut self) -> IterMut<'_> {
self.iter_mut()
}
fn clear(&mut self) {
self.clear();
}
fn entry<'a>(&'a mut self, key: &str) -> Entry<'a> {
self.entry(key)
}
fn entry_format<'a>(&'a mut self, key: &Key) -> Entry<'a> {
self.entry_format(key)
}
fn get<'s>(&'s self, key: &str) -> Option<&'s Item> {
self.get(key)
}
fn get_mut<'s>(&'s mut self, key: &str) -> Option<&'s mut Item> {
self.get_mut(key)
}
fn get_key_value<'a>(&'a self, key: &str) -> Option<(&'a Key, &'a Item)> {
self.get_key_value(key)
}
fn get_key_value_mut<'a>(&'a mut self, key: &str) -> Option<(KeyMut<'a>, &'a mut Item)> {
self.get_key_value_mut(key)
}
fn contains_key(&self, key: &str) -> bool {
self.contains_key(key)
}
fn insert(&mut self, key: &str, value: Item) -> Option<Item> {
self.insert(key, value)
}
fn remove(&mut self, key: &str) -> Option<Item> {
self.remove(key)
}
fn get_values(&self) -> Vec<(Vec<&Key>, &Value)> {
self.get_values()
}
fn fmt(&mut self) {
self.fmt()
}
fn sort_values(&mut self) {
self.sort_values()
}
fn is_dotted(&self) -> bool {
self.is_dotted()
}
fn set_dotted(&mut self, yes: bool) {
self.set_dotted(yes)
}
fn key_decor_mut(&mut self, key: &str) -> Option<&mut Decor> {
self.key_decor_mut(key)
}
fn key_decor(&self, key: &str) -> Option<&Decor> {
self.key_decor(key)
}
}
pub enum Entry<'a> {
Occupied(OccupiedEntry<'a>),
Vacant(VacantEntry<'a>),
}
impl<'a> Entry<'a> {
pub fn key(&self) -> &str {
match self {
Entry::Occupied(e) => e.key(),
Entry::Vacant(e) => e.key(),
}
}
pub fn or_insert(self, default: Item) -> &'a mut Item {
match self {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => entry.insert(default),
}
}
pub fn or_insert_with<F: FnOnce() -> Item>(self, default: F) -> &'a mut Item {
match self {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => entry.insert(default()),
}
}
}
pub struct OccupiedEntry<'a> {
pub(crate) entry: indexmap::map::OccupiedEntry<'a, InternalString, TableKeyValue>,
}
impl<'a> OccupiedEntry<'a> {
pub fn key(&self) -> &str {
self.entry.key().as_str()
}
pub fn key_mut(&mut self) -> KeyMut<'_> {
self.entry.get_mut().key.as_mut()
}
pub fn get(&self) -> &Item {
&self.entry.get().value
}
pub fn get_mut(&mut self) -> &mut Item {
&mut self.entry.get_mut().value
}
pub fn into_mut(self) -> &'a mut Item {
&mut self.entry.into_mut().value
}
pub fn insert(&mut self, mut value: Item) -> Item {
std::mem::swap(&mut value, &mut self.entry.get_mut().value);
value
}
pub fn remove(self) -> Item {
self.entry.shift_remove().value
}
}
pub struct VacantEntry<'a> {
pub(crate) entry: indexmap::map::VacantEntry<'a, InternalString, TableKeyValue>,
pub(crate) key: Option<Key>,
}
impl<'a> VacantEntry<'a> {
pub fn key(&self) -> &str {
self.entry.key().as_str()
}
pub fn insert(self, value: Item) -> &'a mut Item {
let entry = self.entry;
let key = self.key.unwrap_or_else(|| Key::new(entry.key().as_str()));
&mut entry.insert(TableKeyValue::new(key, value)).value
}
}