sickle 0.4.0

A robust Rust parser for CCL (Categorical Configuration Language) with Serde support
Documentation
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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
//! CCL Model - the core data structure for navigating parsed CCL documents

use crate::error::{Error, Result};
use indexmap::IndexMap;
use std::str::FromStr;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

// ============================================================================
// Type Aliases - prevent confusion between single-value and multi-value maps
// ============================================================================

/// Sentinel key used to represent an explicit blank line in a [`CclObject`].
///
/// Real CCL keys can never contain a NUL byte, so this marker is collision-free
/// with parsed content (notably the empty key `""` used by bare list items).
/// The printer renders an entry with this key as a blank line.
pub(crate) const BLANK_LINE_KEY: &str = "\u{0}blank";

/// The internal storage type for CclObject - maps keys to a Vec of values.
/// Each key can have multiple values, preserving duplicate key semantics.
pub(crate) type CclMap = IndexMap<String, Vec<CclObject>>;

/// Iterator over key-value pairs where value is the full Vec.
pub(crate) type CclMapIter<'a> = indexmap::map::Iter<'a, String, Vec<CclObject>>;

/// Options for boolean access operations
///
/// Controls how `get_bool()` interprets string values as booleans.
/// All comparisons are case-insensitive per the CCL spec.
///
/// See <https://ccl.tylerbutler.com/behavior-reference/>
#[derive(Debug, Clone, Copy, Default)]
pub struct BoolOptions {
    /// Controls boolean parsing strictness per the CCL spec.
    ///
    /// - `false` (default, `boolean_strict`): only accepts `true`/`false`
    ///   (case-insensitive).
    /// - `true` (`boolean_lenient`): also accepts `yes`/`no`, `on`/`off`,
    ///   `1`/`0` (all case-insensitive).
    pub lenient: bool,
}

impl BoolOptions {
    /// Create default options (`boolean_strict` — only `true`/`false`)
    pub fn new() -> Self {
        Self::default()
    }

    /// Enable lenient mode (`boolean_lenient` — accepts `yes`/`no`, `on`/`off`, `1`/`0`)
    pub fn with_lenient(mut self) -> Self {
        self.lenient = true;
        self
    }
}

/// Options for list access operations
///
/// Controls how `get_list()` and `get_list_typed()` handle non-list values.
///
/// See the [CCL behavior reference](https://ccl.tylerbutler.com/behavior-reference/#list-coercion)
/// for the specification of list coercion behavior.
#[derive(Debug, Clone, Copy, Default)]
pub struct ListOptions {
    /// Controls list coercion behavior per the CCL spec.
    ///
    /// - `false` (default, `list_coercion_disabled`): `get_list` only succeeds on actual
    ///   list values. Single values cause an error.
    /// - `true` (`list_coercion_enabled`): when `get_list` accesses a single value, it
    ///   wraps it in a list automatically. E.g. `single = value` → `["value"]`.
    pub coerce: bool,
}

impl ListOptions {
    /// Create default options (coerce = false, matching `list_coercion_disabled`)
    pub fn new() -> Self {
        Self::default()
    }

    /// Enable list coercion: single values are wrapped in a list automatically
    pub fn with_coerce(mut self) -> Self {
        self.coerce = true;
        self
    }
}

/// Check if a string is a scalar literal (number or boolean)
///
/// CCL distinguishes between string values and scalar literals:
/// - Numbers: integers and floats (e.g., "42", "3.14", "-17")
/// - Booleans: true/false/yes/no
#[cfg(test)]
fn is_scalar_literal(s: &str) -> bool {
    // Check if it's parseable as an integer
    if s.parse::<i64>().is_ok() {
        return true;
    }

    // Check if it's parseable as a float
    if s.parse::<f64>().is_ok() {
        return true;
    }

    // Check if it's a boolean literal
    matches!(s, "true" | "false" | "yes" | "no")
}

/// Represents a single parsed entry (key-value pair) from CCL
///
/// This is the output of the `parse()` function, representing a flat list
/// of key-value pairs before hierarchy construction.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Entry {
    /// The key (can be empty for list entries)
    pub key: String,
    /// The value (can be multiline or contain nested CCL)
    pub value: String,
}

impl Entry {
    /// Create a new entry
    pub fn new(key: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            key: key.into(),
            value: value.into(),
        }
    }
}

/// Represents a parsed CCL document as a recursive map structure
///
/// Following the OCaml implementation: `type entry_map = value_entry list KeyMap.t`
///
/// A CCL document is a fixed-point recursive structure where:
/// - Every Model is a map from String to `Vec<Model>`
/// - An empty map {} represents a leaf/terminal value
/// - String values are encoded in the recursive structure
/// - Lists are represented as multiple entries with the same key
/// - Uses IndexMap to preserve insertion order (keys ordered by first appearance)
/// - Uses Vec to preserve order of values for each key (insertion order)
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CclObject(CclMap);

impl CclObject {
    /// Create a new empty model
    pub fn new() -> Self {
        CclObject(IndexMap::new())
    }

    /// Create a configured reader over this object
    ///
    /// Returns a `CclReader` with default options. Use the builder methods
    /// to configure boolean and list behavior before reading fields.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use sickle::{load, CclObject};
    /// # use sickle::model::BoolOptions;
    /// # fn example() -> sickle::error::Result<()> {
    /// let model = load("enabled = yes\nname = Alice")?;
    /// let reader = model.reader().with_bool_options(BoolOptions::new().with_lenient());
    /// let enabled = reader.get_bool("enabled")?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn reader(&self) -> crate::reader::CclReader<'_> {
        crate::reader::CclReader::new(self)
    }

    /// Create a Model from an IndexMap
    /// This is internal-only for crate operations
    pub(crate) fn from_map(map: CclMap) -> Self {
        CclObject(map)
    }

    /// Get a value by key, returning an error if the key doesn't exist
    ///
    /// If the key has multiple values, returns the first one (matching OCaml behavior).
    /// Use `get_all()` to get all values for a key.
    pub fn get(&self, key: &str) -> Result<&CclObject> {
        self.0
            .get(key)
            .and_then(|vec| vec.first())
            .ok_or_else(|| Error::MissingKey(key.to_string()))
    }

    /// Get all values for a key, returning an error if the key doesn't exist
    pub fn get_all(&self, key: &str) -> Result<&[CclObject]> {
        self.0
            .get(key)
            .map(|vec| vec.as_slice())
            .ok_or_else(|| Error::MissingKey(key.to_string()))
    }

    /// Get an iterator over the keys in this model
    pub fn keys(&self) -> impl Iterator<Item = &String> {
        self.0.keys()
    }

    /// Get an iterator over the first value for each key
    ///
    /// This flattens the Vec structure, returning only the first value per key.
    /// Use `iter_all()` to get all values.
    pub fn values(&self) -> impl Iterator<Item = &CclObject> {
        self.0.values().filter_map(|vec| vec.first())
    }

    /// Get an iterator over key-value pairs (first value only per key)
    ///
    /// This flattens the Vec structure, returning only the first value per key.
    /// Use `iter_all()` to get all key-value pairs including duplicates.
    pub fn iter(&self) -> impl Iterator<Item = (&String, &CclObject)> {
        self.0
            .iter()
            .filter_map(|(k, vec)| vec.first().map(|v| (k, v)))
    }

    /// Get an iterator over all key-value pairs including duplicate keys
    pub fn iter_all(&self) -> impl Iterator<Item = (&String, &CclObject)> {
        self.0
            .iter()
            .flat_map(|(k, vec)| vec.iter().map(move |v| (k, v)))
    }

    /// Get the concrete IndexMap iterator for internal use (Serde)
    pub(crate) fn iter_map(&self) -> indexmap::map::Iter<'_, String, Vec<CclObject>> {
        self.0.iter()
    }

    /// Get the number of entries in this model
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Check if this model is empty
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    // ========================================================================
    // Builder API - Programmatic CCL Construction
    // ========================================================================

    /// Get mutable access to the internal IndexMap for direct manipulation
    ///
    /// This allows programmatic construction of CCL structures when you need
    /// full control over the data model.
    ///
    /// # Example
    ///
    /// ```rust
    /// use sickle::CclObject;
    ///
    /// let mut obj = CclObject::new();
    /// let map = obj.inner_mut();
    /// map.insert("key".to_string(), vec![CclObject::from_string("value")]);
    /// ```
    pub fn inner_mut(&mut self) -> &mut CclMap {
        &mut self.0
    }

    /// Create an empty CclObject (represents an empty value in CCL: `key =`)
    ///
    /// # Example
    ///
    /// ```rust
    /// use sickle::CclObject;
    ///
    /// let empty = CclObject::empty();
    /// // Represents: key =
    /// ```
    pub fn empty() -> Self {
        CclObject(IndexMap::new())
    }

    /// Create a CclObject representing a list using bare list syntax
    ///
    /// In CCL, a list is represented using the same empty key with multiple values.
    /// Now that we use `Vec<CclObject>` internally, we can properly support duplicate keys.
    ///
    /// # Example
    ///
    /// ```rust
    /// use sickle::CclObject;
    ///
    /// let list = CclObject::from_list(vec!["brew", "scoop", "pacman"]);
    /// // Represents:
    /// // packages =
    /// //   = brew
    /// //   = scoop
    /// //   = pacman
    /// ```
    pub fn from_list(items: Vec<impl Into<String>>) -> Self {
        let mut map = IndexMap::new();
        let values: Vec<CclObject> = items
            .into_iter()
            .map(|item| CclObject::from_string(item))
            .collect();
        map.insert("".to_string(), values);
        CclObject(map)
    }

    /// Add a comment entry to this CclObject
    ///
    /// CCL comments use the `/=` prefix followed by the comment text as the key,
    /// with an empty value. This method adds a comment entry directly to the object.
    ///
    /// # Example
    ///
    /// ```rust
    /// use sickle::CclObject;
    ///
    /// let mut obj = CclObject::new();
    /// obj.add_comment("Generated file - do not edit");
    /// // When printed, represents: /= Generated file - do not edit
    /// ```
    pub fn add_comment(&mut self, text: impl Into<String>) {
        let comment_key = format!("/= {}", text.into());
        self.0.insert(comment_key, vec![CclObject::empty()]);
    }

    /// Add a blank line entry to this CclObject
    ///
    /// Blank lines in CCL output are represented as entries with an empty key
    /// and empty value. This is useful for visual separation in generated files.
    ///
    /// # Example
    ///
    /// ```rust
    /// use sickle::CclObject;
    ///
    /// let mut obj = CclObject::new();
    /// obj.add_comment("Header section");
    /// obj.add_blank_line();
    /// // When printed, adds visual separation
    /// ```
    pub fn add_blank_line(&mut self) {
        // Use a sentinel key that cannot collide with real CCL keys. The empty
        // key `""` is used by bare list items (`= item`), so inserting `""` here
        // would silently overwrite existing list data (see issue #92). The NUL
        // sentinel can never appear in parsed CCL, and the printer renders it as
        // a blank line. Append (rather than insert) so multiple blank lines and
        // any pre-existing sentinel entries are preserved.
        self.0
            .entry(BLANK_LINE_KEY.to_string())
            .or_default()
            .push(CclObject::empty());
    }

    // ========================================================================
    // Composition API - CCL Monoid Operations
    // ========================================================================

    /// Compose two CCL objects together (monoid binary operation)
    ///
    /// This implements the fundamental CCL composition operation that makes CCL
    /// a monoid. When composing two objects:
    /// - Keys unique to either object are preserved
    /// - Keys present in both are recursively composed
    /// - The empty object is the identity element
    ///
    /// # Algebraic Properties
    ///
    /// - **Associativity**: `a.compose(&b).compose(&c) == a.compose(&b.compose(&c))`
    /// - **Left Identity**: `CclObject::new().compose(&x) == x`
    /// - **Right Identity**: `x.compose(&CclObject::new()) == x`
    ///
    /// # Example
    ///
    /// ```rust
    /// use sickle::{load, CclObject};
    ///
    /// let a = load("config =\n  host = localhost").unwrap();
    /// let b = load("config =\n  port = 8080").unwrap();
    /// let composed = a.compose(&b);
    /// // Result: config = { host = localhost, port = 8080 }
    /// ```
    pub fn compose(&self, other: &CclObject) -> CclObject {
        let mut result: CclMap = CclMap::new();

        // First, add all keys from self
        for (key, self_values) in &self.0 {
            if let Some(other_values) = other.0.get(key) {
                // Key exists in both - compose the values
                let composed_values = Self::compose_value_lists(self_values, other_values);
                result.insert(key.clone(), composed_values);
            } else {
                // Key only in self
                result.insert(key.clone(), self_values.clone());
            }
        }

        // Add keys only in other
        for (key, other_values) in &other.0 {
            if !self.0.contains_key(key) {
                result.insert(key.clone(), other_values.clone());
            }
        }

        CclObject(result)
    }

    /// Compose two value lists (`Vec<CclObject>`) into one
    ///
    /// When composing values for the same key, we merge them into a single
    /// composed value by recursively composing each pair.
    fn compose_value_lists(a: &[CclObject], b: &[CclObject]) -> Vec<CclObject> {
        // For composition, we merge all values from both lists into a single composed value
        // This matches OCaml's behavior: fold_left merge empty [v1, v2, v3, ...]

        // Start with empty, compose all from a, then all from b
        let mut composed = CclObject::new();

        for obj in a {
            composed = composed.compose(obj);
        }
        for obj in b {
            composed = composed.compose(obj);
        }

        vec![composed]
    }

    /// Check if composing three objects is associative
    ///
    /// Tests: `(a ∘ b) ∘ c == a ∘ (b ∘ c)`
    ///
    /// This is used for testing the algebraic properties of CCL.
    pub fn compose_associative(a: &CclObject, b: &CclObject, c: &CclObject) -> bool {
        let left = a.compose(b).compose(c);
        let right = a.compose(&b.compose(c));
        left == right
    }

    /// Check left identity property
    ///
    /// Tests: `empty ∘ x == x`
    pub fn identity_left(x: &CclObject) -> bool {
        let empty = CclObject::new();
        empty.compose(x) == *x
    }

    /// Check right identity property
    ///
    /// Tests: `x ∘ empty == x`
    pub fn identity_right(x: &CclObject) -> bool {
        let empty = CclObject::new();
        x.compose(&empty) == *x
    }

    /// Extract a string value from the model (no key lookup)
    ///
    /// A string value is represented as a map with a single key (the string) and empty value.
    /// Example: `{"Alice": [{}]}` represents the string "Alice"
    pub(crate) fn as_string(&self) -> Result<&str> {
        if self.0.len() == 1 {
            let (key, vec) = self.0.iter().next().unwrap();
            if vec.len() == 1 && vec[0].0.is_empty() {
                return Ok(key.as_str());
            }
        }
        Err(Error::ValueError(
            "expected single string value (map with one key and single empty value)".to_string(),
        ))
    }

    /// Get a string value by key
    ///
    /// Looks up the key and extracts its string representation
    pub fn get_string(&self, key: &str) -> Result<&str> {
        self.get(key)?.as_string()
    }

    /// Extract a boolean value from the model (no key lookup)
    ///
    /// Parses the string representation as a boolean using strict mode
    /// (only "true" and "false" accepted).
    pub(crate) fn as_bool(&self) -> Result<bool> {
        self.as_bool_with_options(BoolOptions::new())
    }

    /// Extract a boolean value from the model with options (no key lookup)
    ///
    /// All comparisons are case-insensitive per the CCL spec.
    /// - Strict mode (default): only "true" and "false"
    /// - Lenient mode: also accepts "yes"/"no", "on"/"off", "1"/"0"
    pub(crate) fn as_bool_with_options(&self, options: BoolOptions) -> Result<bool> {
        let s = self.as_string()?;
        let lower = s.to_lowercase();
        if options.lenient {
            match lower.as_str() {
                "true" | "yes" | "on" | "1" => Ok(true),
                "false" | "no" | "off" | "0" => Ok(false),
                _ => Err(Error::ValueError(format!(
                    "failed to parse '{}' as bool",
                    s
                ))),
            }
        } else {
            match lower.as_str() {
                "true" => Ok(true),
                "false" => Ok(false),
                _ => Err(Error::ValueError(format!(
                    "failed to parse '{}' as bool",
                    s
                ))),
            }
        }
    }

    /// Get a boolean value by key (default options, strict mode)
    ///
    /// Only accepts "true" and "false" (case-insensitive).
    /// For lenient parsing, use `get_bool_with_options()`.
    pub fn get_bool(&self, key: &str) -> Result<bool> {
        self.get(key)?.as_bool()
    }

    /// Get a boolean value by key with options
    ///
    /// Behavior depends on `options.lenient` (CCL boolean behavior):
    /// - `false` (default, `boolean_strict`): only "true"/"false" (case-insensitive)
    /// - `true` (`boolean_lenient`): also accepts "yes"/"no", "on"/"off", "1"/"0"
    pub fn get_bool_with_options(&self, key: &str, options: BoolOptions) -> Result<bool> {
        self.get(key)?.as_bool_with_options(options)
    }

    /// Extract an integer value from the model (no key lookup)
    ///
    /// Parses the string representation as an i64
    pub(crate) fn as_int(&self) -> Result<i64> {
        let s = self.as_string()?;
        s.parse::<i64>()
            .map_err(|_| Error::ValueError(format!("failed to parse '{}' as integer", s)))
    }

    /// Get an integer value by key
    pub fn get_int(&self, key: &str) -> Result<i64> {
        self.get(key)?.as_int()
    }

    /// Extract a float value from the model (no key lookup)
    ///
    /// Parses the string representation as an f64
    pub(crate) fn as_float(&self) -> Result<f64> {
        let s = self.as_string()?;
        s.parse::<f64>()
            .map_err(|_| Error::ValueError(format!("failed to parse '{}' as float", s)))
    }

    /// Get a float value by key
    pub fn get_float(&self, key: &str) -> Result<f64> {
        self.get(key)?.as_float()
    }

    /// Try to extract bare list items from the model (no key lookup)
    ///
    /// In CCL, lists are represented using **bare list syntax** with empty keys:
    /// ```text
    /// servers =
    ///   = web1
    ///   = web2
    /// ```
    ///
    /// Returns `Some(items)` if this is a bare list, `None` otherwise.
    pub(crate) fn as_bare_list(&self) -> Option<Vec<String>> {
        // Filter out comment keys (starting with '/') and blank-line sentinels
        // when checking for bare lists.
        let non_comment_keys: Vec<&String> = self
            .keys()
            .filter(|k| !k.starts_with('/') && *k != BLANK_LINE_KEY)
            .collect();

        // Handle bare list syntax: single empty-key child containing the list items
        // With Vec structure: { "": [CclObject({item1}), CclObject({item2}), ...] }
        if non_comment_keys.len() == 1 && non_comment_keys[0].is_empty() {
            if let Ok(children) = self.get_all("") {
                let items: Vec<String> = children
                    .iter()
                    .flat_map(|child| {
                        child
                            .keys()
                            .filter(|k| !k.starts_with('/') && *k != BLANK_LINE_KEY)
                            .cloned()
                    })
                    .collect();
                return Some(items);
            }
        }

        None
    }

    /// Get a list of string values by key (default options)
    ///
    /// Uses `list_coercion_disabled`: only succeeds on actual list values.
    /// For coercion or other options, use `get_list_with_options()`.
    pub fn get_list(&self, key: &str) -> Result<Vec<String>> {
        self.get_list_with_options(key, ListOptions::new())
    }

    /// Get a list of string values by key with options
    ///
    /// Behavior depends on `options.coerce` (CCL list coercion behavior):
    /// - `coerce = false` (default, `list_coercion_disabled`): `get_list` only succeeds
    ///   on actual list values. Single values cause an error.
    /// - `coerce = true` (`list_coercion_enabled`): when `get_list` accesses a single
    ///   value, it wraps it in a list automatically.
    ///
    /// See <https://ccl.tylerbutler.com/behavior-reference/#list-coercion>
    ///
    /// For typed access, use `get_list_typed()`.
    pub fn get_list_with_options(&self, key: &str, options: ListOptions) -> Result<Vec<String>> {
        let model = self.get(key)?;

        // Check if this is a bare list (= item syntax)
        if let Some(items) = model.as_bare_list() {
            return Ok(items);
        }

        // Not a bare list — behavior depends on coercion setting
        if options.coerce {
            // list_coercion_enabled: wrap single value in a list
            if let Ok(s) = model.as_string() {
                return Ok(vec![s.to_string()]);
            }
            // Not a string either — return error
            Err(Error::NotAList)
        } else {
            // list_coercion_disabled: single values cause an error
            Err(Error::NotAList)
        }
    }

    /// Get a typed list of values by key (default options)
    ///
    /// Uses `list_coercion_disabled`: only succeeds on actual list values.
    /// For coercion or other options, use `get_list_typed_with_options()`.
    pub fn get_list_typed<T>(&self, key: &str) -> Result<Vec<T>>
    where
        T: FromStr,
        T::Err: std::fmt::Display,
    {
        self.get_list_typed_with_options(key, ListOptions::new())
    }

    /// Get a typed list of values by key with options
    ///
    /// Parses list items as type T. Follows the same coercion rules as `get_list()`:
    /// - `coerce = false` (default): only bare list values succeed, single values error
    /// - `coerce = true`: single values are wrapped and parsed as one-element lists
    ///
    /// # Examples
    ///
    /// ```
    /// # use sickle::{CclObject, parse, build_hierarchy};
    /// # use sickle::model::ListOptions;
    /// # use sickle::error::Result;
    /// # fn example() -> Result<()> {
    /// let input = "numbers =\n  = 1\n  = 42\n  = -17";
    /// let entries = parse(input)?;
    /// let model = build_hierarchy(&entries)?;
    /// let numbers: Vec<i64> = model.get_list_typed("numbers")?;
    /// assert_eq!(numbers, vec![1, 42, -17]);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns `Error::NotAList` if the value is not a list and coercion is disabled.
    /// Returns `Error::ValueError` if any item cannot be parsed as type T.
    pub fn get_list_typed_with_options<T>(&self, key: &str, options: ListOptions) -> Result<Vec<T>>
    where
        T: FromStr,
        T::Err: std::fmt::Display,
    {
        let model = self.get(key)?;

        // Check if this is a bare list
        if let Some(items) = model.as_bare_list() {
            return items
                .iter()
                .map(|k| {
                    k.parse::<T>().map_err(|e| {
                        Error::ValueError(format!(
                            "Failed to parse '{}' as {}: {}",
                            k,
                            std::any::type_name::<T>(),
                            e
                        ))
                    })
                })
                .collect();
        }

        // Not a bare list — check for multi-key objects (duplicate key lists)
        if model.len() >= 2 {
            return model
                .keys()
                .map(|k| {
                    k.parse::<T>().map_err(|e| {
                        Error::ValueError(format!(
                            "Failed to parse '{}' as {}: {}",
                            k,
                            std::any::type_name::<T>(),
                            e
                        ))
                    })
                })
                .collect();
        }

        // Single value — coercion behavior
        if options.coerce {
            if let Ok(s) = model.as_string() {
                let val = s.parse::<T>().map_err(|e| {
                    Error::ValueError(format!(
                        "Failed to parse '{}' as {}: {}",
                        s,
                        std::any::type_name::<T>(),
                        e
                    ))
                })?;
                return Ok(vec![val]);
            }
        }

        Err(Error::NotAList)
    }

    /// Create a CclObject representing a string value
    ///
    /// In CCL, a string is represented as a map with a single key (the string)
    /// and an empty value: `{"string_value": [{}]}`
    ///
    /// # Example
    ///
    /// ```rust
    /// use sickle::CclObject;
    ///
    /// let val = CclObject::from_string("hello");
    /// // Represents: key = hello
    /// ```
    pub fn from_string(s: impl Into<String>) -> Self {
        let mut map = IndexMap::new();
        map.insert(s.into(), vec![CclObject::new()]);
        CclObject(map)
    }

    /// Insert a string value at the given key
    /// Creates the CCL representation: `{key: {value: {}}}`
    #[cfg(feature = "serde-serialize")]
    pub(crate) fn insert_string(&mut self, key: &str, value: String) {
        let mut inner = IndexMap::new();
        inner.insert(value, vec![CclObject::new()]);
        self.0.insert(key.to_string(), vec![CclObject(inner)]);
    }

    /// Insert a list of string values at the given key
    /// Creates the CCL representation: `{key: {item1: {}, item2: {}, ...}}`
    #[cfg(feature = "serde-serialize")]
    pub(crate) fn insert_list(&mut self, key: &str, values: Vec<String>) {
        let mut inner = IndexMap::new();
        for value in values {
            inner.insert(value, vec![CclObject::new()]);
        }
        self.0.insert(key.to_string(), vec![CclObject(inner)]);
    }

    /// Insert a nested object at the given key
    #[cfg(feature = "serde-serialize")]
    pub(crate) fn insert_object(&mut self, key: &str, obj: CclObject) {
        self.0.insert(key.to_string(), vec![obj]);
    }
}

impl Default for CclObject {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // ========================================================================
    // BoolOptions tests
    // ========================================================================

    #[test]
    fn test_bool_options_default() {
        let opts = BoolOptions::new();
        assert!(!opts.lenient);
    }

    #[test]
    fn test_bool_options_lenient() {
        let opts = BoolOptions::new().with_lenient();
        assert!(opts.lenient);
    }

    #[test]
    fn test_bool_options_default_trait() {
        let opts = BoolOptions::default();
        assert!(!opts.lenient);
    }

    // ========================================================================
    // ListOptions tests
    // ========================================================================

    #[test]
    fn test_list_options_default() {
        let opts = ListOptions::new();
        assert!(!opts.coerce);
    }

    #[test]
    fn test_list_options_with_coerce() {
        let opts = ListOptions::new().with_coerce();
        assert!(opts.coerce);
    }

    #[test]
    fn test_list_options_default_trait() {
        let opts = ListOptions::default();
        assert!(!opts.coerce);
    }

    // ========================================================================
    // is_scalar_literal tests
    // ========================================================================

    #[test]
    fn test_is_scalar_literal_integers() {
        assert!(is_scalar_literal("42"));
        assert!(is_scalar_literal("-17"));
        assert!(is_scalar_literal("0"));
        assert!(is_scalar_literal("999999"));
    }

    #[test]
    fn test_is_scalar_literal_floats() {
        assert!(is_scalar_literal("3.14"));
        assert!(is_scalar_literal("-2.5"));
        assert!(is_scalar_literal("0.0"));
        assert!(is_scalar_literal("1e10"));
    }

    #[test]
    fn test_is_scalar_literal_booleans() {
        assert!(is_scalar_literal("true"));
        assert!(is_scalar_literal("false"));
        assert!(is_scalar_literal("yes"));
        assert!(is_scalar_literal("no"));
    }

    #[test]
    fn test_is_scalar_literal_not_scalars() {
        assert!(!is_scalar_literal("hello"));
        assert!(!is_scalar_literal("web1"));
        assert!(!is_scalar_literal(""));
        assert!(!is_scalar_literal("True")); // case-sensitive
        assert!(!is_scalar_literal("YES"));
    }

    // ========================================================================
    // CclObject basic tests
    // ========================================================================

    #[test]
    fn test_empty_model() {
        let model = CclObject::new();
        assert!(model.is_empty());
    }

    #[test]
    fn test_map_navigation() {
        let mut inner = IndexMap::new();
        inner.insert("name".to_string(), vec![CclObject::new()]);
        inner.insert("version".to_string(), vec![CclObject::new()]);

        let model = CclObject(inner);
        assert!(model.get("name").is_ok());
        assert!(model.get("version").is_ok());
        assert!(model.get("nonexistent").is_err());
    }

    #[test]
    fn test_compose_disjoint_keys() {
        // Composing objects with different keys should combine them
        let a = CclObject::from_string("hello");
        let b = CclObject::from_string("world");

        let mut obj_a = CclObject::new();
        obj_a.inner_mut().insert("a".to_string(), vec![a]);

        let mut obj_b = CclObject::new();
        obj_b.inner_mut().insert("b".to_string(), vec![b]);

        let composed = obj_a.compose(&obj_b);
        assert!(composed.get("a").is_ok());
        assert!(composed.get("b").is_ok());
    }

    #[test]
    fn test_compose_overlapping_keys() {
        // Composing objects with same key should merge values
        let mut obj_a = CclObject::new();
        obj_a.inner_mut().insert(
            "config".to_string(),
            vec![{
                let mut inner = CclObject::new();
                inner.inner_mut().insert(
                    "host".to_string(),
                    vec![CclObject::from_string("localhost")],
                );
                inner
            }],
        );

        let mut obj_b = CclObject::new();
        obj_b.inner_mut().insert(
            "config".to_string(),
            vec![{
                let mut inner = CclObject::new();
                inner
                    .inner_mut()
                    .insert("port".to_string(), vec![CclObject::from_string("8080")]);
                inner
            }],
        );

        let composed = obj_a.compose(&obj_b);
        let config = composed.get("config").unwrap();
        assert!(config.get("host").is_ok());
        assert!(config.get("port").is_ok());
    }

    #[test]
    fn test_compose_left_identity() {
        let mut obj = CclObject::new();
        obj.inner_mut()
            .insert("key".to_string(), vec![CclObject::from_string("value")]);

        assert!(CclObject::identity_left(&obj));
    }

    #[test]
    fn test_compose_right_identity() {
        let mut obj = CclObject::new();
        obj.inner_mut()
            .insert("key".to_string(), vec![CclObject::from_string("value")]);

        assert!(CclObject::identity_right(&obj));
    }

    #[test]
    fn test_compose_associativity() {
        let mut a = CclObject::new();
        a.inner_mut()
            .insert("a".to_string(), vec![CclObject::from_string("1")]);

        let mut b = CclObject::new();
        b.inner_mut()
            .insert("b".to_string(), vec![CclObject::from_string("2")]);

        let mut c = CclObject::new();
        c.inner_mut()
            .insert("c".to_string(), vec![CclObject::from_string("3")]);

        assert!(CclObject::compose_associative(&a, &b, &c));
    }

    #[test]
    fn test_compose_nested_associativity() {
        // Test associativity with overlapping nested keys
        let mut a = CclObject::new();
        a.inner_mut().insert(
            "config".to_string(),
            vec![{
                let mut inner = CclObject::new();
                inner.inner_mut().insert(
                    "host".to_string(),
                    vec![CclObject::from_string("localhost")],
                );
                inner
            }],
        );

        let mut b = CclObject::new();
        b.inner_mut().insert(
            "config".to_string(),
            vec![{
                let mut inner = CclObject::new();
                inner
                    .inner_mut()
                    .insert("port".to_string(), vec![CclObject::from_string("8080")]);
                inner
            }],
        );

        let mut c = CclObject::new();
        c.inner_mut().insert(
            "db".to_string(),
            vec![{
                let mut inner = CclObject::new();
                inner
                    .inner_mut()
                    .insert("name".to_string(), vec![CclObject::from_string("test")]);
                inner
            }],
        );

        assert!(CclObject::compose_associative(&a, &b, &c));
    }

    // ========================================================================
    // get_list_typed tests
    // ========================================================================

    #[test]
    fn test_get_list_typed_single_item_coercion_disabled() {
        // list_coercion_disabled: single value returns error
        let mut model = CclObject::new();
        let mut numbers_inner = CclObject::new();
        numbers_inner
            .inner_mut()
            .insert("42".to_string(), vec![CclObject::new()]);
        model
            .inner_mut()
            .insert("numbers".to_string(), vec![numbers_inner]);

        let result: Result<Vec<i64>> = model.get_list_typed("numbers");
        assert!(result.is_err());
    }

    #[test]
    fn test_get_list_typed_single_item_coercion_enabled() {
        // list_coercion_enabled: single value wraps to vec![42]
        let mut model = CclObject::new();
        let mut numbers_inner = CclObject::new();
        numbers_inner
            .inner_mut()
            .insert("42".to_string(), vec![CclObject::new()]);
        model
            .inner_mut()
            .insert("numbers".to_string(), vec![numbers_inner]);

        let opts = ListOptions::new().with_coerce();
        let result: Vec<i64> = model.get_list_typed_with_options("numbers", opts).unwrap();
        assert_eq!(result, vec![42]);
    }

    #[test]
    fn test_get_list_typed_multiple_items() {
        let mut model = CclObject::new();
        let mut numbers_inner = CclObject::new();
        numbers_inner
            .inner_mut()
            .insert("1".to_string(), vec![CclObject::new()]);
        numbers_inner
            .inner_mut()
            .insert("2".to_string(), vec![CclObject::new()]);
        numbers_inner
            .inner_mut()
            .insert("3".to_string(), vec![CclObject::new()]);
        model
            .inner_mut()
            .insert("numbers".to_string(), vec![numbers_inner]);

        let result: Vec<i64> = model.get_list_typed("numbers").unwrap();
        assert_eq!(result, vec![1, 2, 3]);
    }
}