girt 0.7.0

An incremental, idiomatic Rust implementation of Git
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
use std::ops::Range;

use thiserror::Error;

use super::Origin;

/// Byte-oriented configuration entries from parsing or explicit layered resolution.
///
/// Supports quoted values, comments, LF/CRLF, continuations, implicit booleans and quoted
/// subsections. Deprecated dotted subsections are folded to ASCII lowercase. Parsing retains
/// include directives without I/O; [`Self::resolve`] expands them and attaches provenance. Scalar
/// lookup returns the last occurrence; [`Self::values`] preserves all occurrences.
///
/// ```
/// use girt::Config;
/// let config = Config::parse(b"[remote \"origin\"]\nurl = https://example.com/repo\n")?;
/// assert_eq!(
///     config.value("remote", Some(b"origin"), "url"),
///     Some(Some(b"https://example.com/repo".as_slice()))
/// );
/// # Ok::<(), girt::ConfigError>(())
/// ```
#[derive(Debug, Clone)]
pub struct Config {
    pub(super) entries: Vec<Entry>,
    pub(super) sections: Vec<SectionName>,
    pub(super) occurrences: Vec<SectionOccurrence>,
}

#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub(super) struct SectionName {
    pub section: Vec<u8>,
    pub subsection: Option<Vec<u8>>,
}

/// One section occurrence in a parsed or resolved configuration snapshot.
///
/// Repeated headers and repeated include visits have distinct ordinals, including empty headers.
/// Environment assignments each form a synthetic occurrence without a physical header. Membership
/// can be noncontiguous because an include inserts entries before the outer section resumes.
/// These borrowed views identify snapshot membership, not writable source sections; use
/// [`super::Document::sections`] to edit a direct file.
#[derive(Debug, Clone, Copy)]
pub struct ConfigSection<'a> {
    ordinal: usize,
    occurrence: &'a SectionOccurrence,
}

impl<'a> ConfigSection<'a> {
    /// Zero-based occurrence position within this snapshot, not a persistent source identity.
    pub fn ordinal(&self) -> usize {
        self.ordinal
    }

    /// Original section-name bytes; matching folds ASCII case.
    pub fn name(&self) -> &'a [u8] {
        &self.occurrence.name.section
    }

    /// Decoded subsection; parsing lowercases deprecated dotted subsections.
    pub fn subsection(&self) -> Option<&'a [u8]> {
        self.occurrence.name.subsection.as_deref()
    }

    /// Increasing member indices into [`Config::entries`], excluding included sections' entries.
    ///
    /// Empty headers have no members. The indices remain valid for the borrowed snapshot only.
    pub fn entry_indices(&self) -> &'a [usize] {
        &self.occurrence.entries
    }
}

#[derive(Debug, Clone)]
pub(super) struct SectionOccurrence {
    pub name: SectionName,
    // Header position before this entry, including empty headers at EOF. Together with member
    // indices this preserves interleaving when a resolved snapshot is supplied as runtime input.
    pub start: usize,
    pub entries: Vec<usize>,
}

/// One variable occurrence, retaining spelling, bytes and source location.
#[derive(Debug, Clone)]
pub struct Entry {
    /// Physical line where this variable begins, or zero for supplied decoded entries.
    pub line: usize,
    /// Source and include ancestry, absent for a purely parsed source.
    pub origin: Option<Origin>,
    /// Original section name bytes; lookup folds ASCII case.
    pub section: Vec<u8>,
    /// Decoded subsection; parsing lowercases deprecated dotted subsections.
    pub subsection: Option<Vec<u8>>,
    /// Original variable name bytes; lookup folds ASCII case.
    pub name: Vec<u8>,
    /// Decoded value; `None` is an implicit boolean.
    pub value: Option<Vec<u8>>,
}

/// Invalid syntax, decoded names, or construction limits in supplied configuration.
#[derive(Debug, Error)]
#[error("configuration line {line}: {reason}")]
pub struct ConfigError {
    /// One-based physical line, or input section ordinal for decoded construction.
    pub line: usize,
    /// Explanation of the rejected syntax.
    pub reason: &'static str,
}

impl Config {
    /// Parses one complete source without reading other files.
    ///
    /// A UTF-8 BOM is accepted only at the start. Escape sequences are `\n`, `\t`, `\b`,
    /// `\\`, and `\"`; backslash-newline joins physical lines. Unquoted surrounding whitespace
    /// is removed, while quoted whitespace and whitespace before a continuation after value text
    /// are preserved. An absent `=` is distinct from an empty value. Comments may contain
    /// uninterpreted NUL bytes; NUL in names, subsections or values and malformed syntax return
    /// an error.
    ///
    /// # Errors
    ///
    /// Returns the physical line and reason for invalid or unsupported syntax.
    pub fn parse(bytes: &[u8]) -> Result<Self, ConfigError> {
        Self::parse_internal::<false>(bytes).map(|(config, _)| config)
    }

    pub(super) fn parse_layout(bytes: &[u8]) -> Result<(Self, Layout), ConfigError> {
        Self::parse_internal::<true>(bytes)
    }

    fn parse_internal<const RETAIN_LAYOUT: bool>(
        bytes: &[u8],
    ) -> Result<(Self, Layout), ConfigError> {
        let offset = usize::from(bytes.starts_with(b"\xef\xbb\xbf")) * 3;
        let bytes = bytes.strip_prefix(b"\xef\xbb\xbf").unwrap_or(bytes);
        let mut parser = Parser {
            bytes,
            pos: 0,
            line: 1,
            value_end: 0,
        };
        let mut entries = Vec::new();
        let mut sections = Vec::new();
        let mut occurrences: Vec<SectionOccurrence> = Vec::new();
        let mut layout = Layout::default();
        let mut section = Vec::new();
        let mut subsection = None;
        while parser.pos < bytes.len() {
            parser.space();
            match parser.peek() {
                None => break,
                Some(b'\n') => {
                    parser.take();
                }
                Some(b'#' | b';') => parser.comment(),
                Some(b'[') => {
                    let start = parser.pos;
                    parser.take();
                    section = parser.name(false)?;
                    subsection = None;
                    if parser.peek() == Some(b'.') {
                        parser.take();
                        let start = parser.pos;
                        while parser
                            .peek()
                            .is_some_and(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'.'))
                        {
                            parser.take();
                        }
                        subsection = Some(parser.since(start).to_ascii_lowercase());
                    }
                    let separated = matches!(parser.peek(), Some(b' ' | b'\t'));
                    parser.space();
                    if parser.peek() == Some(b'"') {
                        if !separated {
                            return Err(parser.error("subsection requires whitespace"));
                        }
                        parser.take();
                        let mut value = Vec::new();
                        loop {
                            match parser.take() {
                                Some(b'"') => break,
                                Some(b'\\') => match parser.take() {
                                    Some(b'\n' | 0) | None => {
                                        return Err(parser.error("invalid subsection escape"));
                                    }
                                    Some(byte) => value.push(byte),
                                },
                                Some(b'\n' | 0) | None => {
                                    return Err(parser.error("unterminated subsection"));
                                }
                                Some(byte) => value.push(byte),
                            }
                        }
                        subsection = Some(value);
                        parser.space();
                    }
                    if parser.take() != Some(b']') {
                        return Err(parser.error("expected ]"));
                    }
                    let name = SectionName {
                        section: section.clone(),
                        subsection: subsection.clone(),
                    };
                    sections.push(name.clone());
                    occurrences.push(SectionOccurrence {
                        name,
                        start: entries.len(),
                        entries: Vec::new(),
                    });
                    if RETAIN_LAYOUT {
                        layout.sections.push(SectionSpan {
                            range: start + offset..parser.pos + offset,
                            section: section.clone(),
                            subsection: subsection.clone(),
                        });
                    }
                }
                Some(_) => {
                    if section.is_empty() {
                        return Err(parser.error("variable outside section"));
                    }
                    let start = parser.pos;
                    let line = parser.line;
                    let name = parser.name(true)?;
                    let name_end = parser.pos;
                    parser.space();
                    let mut value_start = name_end;
                    let value = match parser.peek() {
                        Some(b'=') => {
                            parser.take();
                            parser.space();
                            value_start = parser.pos;
                            Some(parser.value()?)
                        }
                        None | Some(b'\n' | b'#' | b';') => None,
                        _ => return Err(parser.error("expected = or end of variable")),
                    };
                    let end = if value.is_some() {
                        parser.value_end
                    } else {
                        name_end
                    };
                    if RETAIN_LAYOUT {
                        layout.entries.push(EntrySpan {
                            range: start + offset..end + offset,
                            value: value_start + offset..end + offset,
                        });
                    }
                    occurrences
                        .last_mut()
                        .ok_or_else(|| parser.error("variable outside section"))?
                        .entries
                        .push(entries.len());
                    entries.push(Entry {
                        line,
                        origin: None,
                        section: section.clone(),
                        subsection: subsection.clone(),
                        name,
                        value,
                    });
                }
            }
        }
        Ok((
            Self {
                entries,
                sections,
                occurrences,
            },
            layout,
        ))
    }

    /// Whether a section exists, including an empty parsed header.
    ///
    /// Section names use ASCII case folding; subsection bytes are exact. Resolved includes retain
    /// their empty headers. Environment assignments imply their section even though they have no
    /// physical header. This query does not identify a writable source or physical section.
    pub fn contains_section(&self, section: &str, subsection: Option<&[u8]>) -> bool {
        self.sections.iter().any(|header| {
            header.section.eq_ignore_ascii_case(section.as_bytes())
                && header.subsection.as_deref() == subsection
        }) || self.entries.iter().any(|entry| {
            entry.section.eq_ignore_ascii_case(section.as_bytes())
                && entry.subsection.as_deref() == subsection
        })
    }

    /// Lists every subsection of a section, once, sorted by exact subsection bytes.
    ///
    /// Includes empty headers and sections implied by environment assignments. Section names
    /// ignore ASCII case; subsection bytes retain case, non-UTF-8 bytes and the empty name.
    /// Headers without a subsection are excluded. This query does not expose source ownership,
    /// physical section occurrences or precedence order.
    ///
    /// ```
    /// use girt::Config;
    /// let config = Config::parse(b"[remote \"z\"]\nurl=repo\n[remote \"a\"]\n")?;
    /// assert_eq!(config.subsection_names("REMOTE"), [b"a".as_slice(), b"z"]);
    /// # Ok::<(), girt::ConfigError>(())
    /// ```
    pub fn subsection_names(&self, section: &str) -> Vec<&[u8]> {
        let headers = self
            .sections
            .iter()
            .map(|header| (header.section.as_slice(), header.subsection.as_deref()));
        let entries = self
            .entries
            .iter()
            .map(|entry| (entry.section.as_slice(), entry.subsection.as_deref()));
        let names: std::collections::BTreeSet<_> = headers
            .chain(entries)
            .filter_map(|(name, subsection)| {
                name.eq_ignore_ascii_case(section.as_bytes())
                    .then_some(subsection)
                    .flatten()
            })
            .collect();
        names.into_iter().collect()
    }

    /// Returns all occurrences, preserving order and distinguishing implicit from empty values.
    /// Names use ASCII case folding; subsection bytes are exact. No key-string splitting is done.
    pub fn values<'a>(
        &'a self,
        section: &'a str,
        subsection: Option<&'a [u8]>,
        name: &'a str,
    ) -> impl DoubleEndedIterator<Item = Option<&'a [u8]>> + 'a {
        self.entries
            .iter()
            .filter(move |entry| {
                entry.section.eq_ignore_ascii_case(section.as_bytes())
                    && entry.subsection.as_deref() == subsection
                    && entry.name.eq_ignore_ascii_case(name.as_bytes())
            })
            .map(|entry| entry.value.as_deref())
    }

    /// Returns the last occurrence; outer `None` means absent, inner `None` means implicit true.
    pub fn value(
        &self,
        section: &str,
        subsection: Option<&[u8]>,
        name: &str,
    ) -> Option<Option<&[u8]>> {
        self.entries
            .iter()
            .rev()
            .find(|entry| {
                entry.section.eq_ignore_ascii_case(section.as_bytes())
                    && entry.subsection.as_deref() == subsection
                    && entry.name.eq_ignore_ascii_case(name.as_bytes())
            })
            .map(|entry| entry.value.as_deref())
    }

    /// Returns the last explicit value; an implicit assignment reads as absent.
    ///
    /// This is Git's ordinary interpretation of a string-valued variable such as `core.editor`.
    /// Bytes are returned without path expansion or encoding checks.
    ///
    /// ```
    /// use girt::Config;
    /// let config = Config::parse(b"[core]\nexcludesFile = a\nexcludesFile = b\n")?;
    /// assert_eq!(
    ///     config.string("core", None, "excludesfile"),
    ///     Some(b"b".as_slice())
    /// );
    /// # Ok::<(), girt::ConfigError>(())
    /// ```
    pub fn string(&self, section: &str, subsection: Option<&[u8]>, name: &str) -> Option<&[u8]> {
        self.value(section, subsection, name).flatten()
    }

    /// Interprets the last occurrence with Git's boolean spelling.
    ///
    /// An implicit assignment is true. `true`/`yes`/`on`, `false`/`no`/`off`/empty and integers
    /// are accepted case-insensitively.
    ///
    /// # Errors
    ///
    /// Returns [`InvalidValue`] when the last occurrence is not a boolean.
    ///
    /// ```
    /// use girt::Config;
    /// let config = Config::parse(b"[core]\nbare\nfilemode = off\n")?;
    /// assert_eq!(config.boolean("core", None, "bare")?, Some(true));
    /// assert_eq!(config.boolean("core", None, "filemode")?, Some(false));
    /// assert_eq!(config.boolean("core", None, "missing")?, None);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn boolean(
        &self,
        section: &str,
        subsection: Option<&[u8]>,
        name: &str,
    ) -> Result<Option<bool>, InvalidValue> {
        match self.value(section, subsection, name) {
            None => Ok(None),
            Some(value) => super::values::boolean(value)
                .map(Some)
                .ok_or_else(|| InvalidValue::new(section, name, value)),
        }
    }

    /// Interprets the last occurrence with Git's integer syntax, including `k`/`m`/`g` suffixes.
    ///
    /// # Errors
    ///
    /// Returns [`InvalidValue`] for an implicit assignment or an unreadable integer.
    pub fn integer(
        &self,
        section: &str,
        subsection: Option<&[u8]>,
        name: &str,
    ) -> Result<Option<i64>, InvalidValue> {
        match self.value(section, subsection, name) {
            None => Ok(None),
            Some(value) => value
                .and_then(super::values::integer)
                .map(Some)
                .ok_or_else(|| InvalidValue::new(section, name, value)),
        }
    }

    pub(crate) fn append(&mut self, other: &Self) {
        let offset = self.entries.len();
        self.occurrences
            .extend(other.occurrences.iter().map(|occurrence| {
                SectionOccurrence {
                    name: occurrence.name.clone(),
                    start: occurrence.start + offset,
                    entries: occurrence
                        .entries
                        .iter()
                        .map(|index| index + offset)
                        .collect(),
                }
            }));
        self.entries.extend_from_slice(&other.entries);
        self.sections.extend_from_slice(&other.sections);
    }

    /// Borrows section occurrences in header encounter order, retaining empty and repeated headers.
    ///
    /// Include expansion assigns a fresh identity on every visit and retains outer membership when
    /// an included file returns. This does not change flat [`Self::value`] or [`Self::values`]
    /// semantics or apply any policy to implicit values. No I/O is performed.
    ///
    /// ```
    /// use girt::Config;
    /// let config = Config::parse(b"[core]\nx=first\nx\n[core]\nx=last\n[empty]\n")?;
    /// let sections: Vec<_> = config.section_occurrences().collect();
    /// assert_eq!(sections[0].entry_indices(), &[0, 1]);
    /// assert_eq!(sections[1].entry_indices(), &[2]);
    /// assert!(sections[2].entry_indices().is_empty());
    /// assert!(
    ///     config.entries()[sections[0].entry_indices()[1]]
    ///         .value
    ///         .is_none()
    /// );
    /// # Ok::<(), girt::ConfigError>(())
    /// ```
    pub fn section_occurrences(&self) -> impl ExactSizeIterator<Item = ConfigSection<'_>> {
        self.occurrences
            .iter()
            .enumerate()
            .map(|(ordinal, occurrence)| ConfigSection {
                ordinal,
                occurrence,
            })
    }

    /// Returns occurrences in source or resolved precedence order.
    pub fn entries(&self) -> &[Entry] {
        &self.entries
    }
}

#[derive(Debug, Clone, Default)]
pub(super) struct Layout {
    pub entries: Vec<EntrySpan>,
    pub sections: Vec<SectionSpan>,
}

#[derive(Debug, Clone)]
pub(super) struct EntrySpan {
    pub range: Range<usize>,
    pub value: Range<usize>,
}

#[derive(Debug, Clone)]
pub(super) struct SectionSpan {
    pub range: Range<usize>,
    pub section: Vec<u8>,
    pub subsection: Option<Vec<u8>>,
}

struct Parser<'a> {
    bytes: &'a [u8],
    pos: usize,
    line: usize,
    value_end: usize,
}
impl<'a> Parser<'a> {
    fn peek(&self) -> Option<u8> {
        match self.bytes.get(self.pos).copied() {
            Some(b'\r') if self.bytes.get(self.pos + 1) == Some(&b'\n') => Some(b'\n'),
            byte => byte,
        }
    }
    fn take(&mut self) -> Option<u8> {
        let byte = self.peek()?;
        if self.bytes.get(self.pos) == Some(&b'\r') && byte == b'\n' {
            self.pos += 1;
        }
        self.pos += 1;
        if byte == b'\n' {
            self.line += 1;
        }
        Some(byte)
    }
    fn error(&self, reason: &'static str) -> ConfigError {
        ConfigError {
            line: self.line,
            reason,
        }
    }
    fn space(&mut self) {
        while matches!(self.peek(), Some(b' ' | b'\t')) {
            self.take();
        }
    }
    fn comment(&mut self) {
        while !matches!(self.peek(), None | Some(b'\n')) {
            self.take();
        }
    }
    fn name(&mut self, variable: bool) -> Result<Vec<u8>, ConfigError> {
        let start = self.pos;
        if variable && !self.peek().is_some_and(|b| b.is_ascii_alphabetic()) {
            return Err(self.error("invalid variable name"));
        }
        while self
            .peek()
            .is_some_and(|b| b.is_ascii_alphanumeric() || b == b'-')
        {
            self.take();
        }
        if start == self.pos {
            return Err(self.error("empty name"));
        }
        Ok(self.since(start).to_vec())
    }
    /// Bytes consumed since `start`, a position this parser previously reached.
    fn since(&self, start: usize) -> &'a [u8] {
        self.bytes.get(start..self.pos).unwrap_or_default()
    }
    fn value(&mut self) -> Result<Vec<u8>, ConfigError> {
        self.space();
        let mut value = Vec::new();
        let mut quoted = false;
        let mut keep = 0;
        self.value_end = self.pos;
        while let Some(byte) = self.take() {
            match byte {
                0 => return Err(self.error("NUL in value")),
                b'\n' => {
                    if quoted {
                        return Err(self.error("unterminated quote"));
                    }
                    break;
                }
                b'"' => {
                    quoted = !quoted;
                    keep = value.len();
                    self.value_end = self.pos;
                }
                b'#' | b';' if !quoted => {
                    self.comment();
                    break;
                }
                b'\\' => {
                    let escaped = match self.take() {
                        Some(b'\n') => {
                            keep = value.len();
                            self.value_end = self.pos;
                            continue;
                        }
                        Some(b'n') => b'\n',
                        Some(b't') => b'\t',
                        Some(b'b') => 8,
                        Some(b'\\') => b'\\',
                        Some(b'"') => b'"',
                        _ => return Err(self.error("invalid escape")),
                    };
                    value.push(escaped);
                    keep = value.len();
                    self.value_end = self.pos;
                }
                b' ' | b'\t' if !quoted && value.is_empty() => {}
                byte => {
                    value.push(byte);
                    if quoted || !matches!(byte, b' ' | b'\t') {
                        keep = value.len();
                        self.value_end = self.pos;
                    }
                }
            }
        }
        if quoted {
            return Err(self.error("unterminated quote"));
        }
        value.truncate(keep);
        Ok(value)
    }
}

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

    use super::*;
    #[rstest]
    #[case::absent(b"", vec![])]
    #[case::bare(b"[remote]", vec![])]
    #[case::empty_header(b"[remote \"a\"]", vec![b"a".as_slice()])]
    #[case::empty_name(b"[remote \"\"]", vec![b"".as_slice()])]
    #[case::section_case(b"[REMOTE \"a\"]\n[Remote \"a\"]", vec![b"a".as_slice()])]
    #[case::subsection_case(b"[remote \"a\"]\n[remote \"A\"]", vec![b"A".as_slice(), b"a"])]
    #[case::non_utf8(b"[remote \"\xff\"]\n[remote \"a\"]", vec![b"a".as_slice(), b"\xff"])]
    #[case::entries_and_empty(b"[remote \"z\"]\nurl=repo\n[remote \"a\"]\n[remote \"z\"]", vec![b"a".as_slice(), b"z"])]
    #[case::other_section(b"[url \"a\"]\ninsteadOf=b", vec![])]
    fn complete_subsection_names(#[case] bytes: &[u8], #[case] expected: Vec<&[u8]>) {
        assert_eq!(
            Config::parse(bytes).unwrap().subsection_names("remote"),
            expected
        );
    }

    #[test]
    fn section_occurrences_preserve_headers_and_membership() {
        let config = Config::parse(b"\xef\xbb\xbf[Core] x=one\r\nx\r\n[empty]\r\n[remote.UPPER]\r\nurl=repo\r\n[remote \"UPPER\"]\r\n[Core]\r\nx=two\r\n").unwrap();
        let sections: Vec<_> = config.section_occurrences().collect();
        assert_eq!(
            sections.iter().map(|s| s.ordinal()).collect::<Vec<_>>(),
            [0, 1, 2, 3, 4]
        );
        assert_eq!(
            sections
                .iter()
                .map(|s| s.entry_indices())
                .collect::<Vec<_>>(),
            [&[0, 1][..], &[], &[2], &[], &[3]]
        );
        assert_eq!(sections[0].name(), b"Core");
        assert_eq!(sections[2].subsection(), Some(b"upper".as_slice()));
        assert_eq!(sections[3].subsection(), Some(b"UPPER".as_slice()));
        assert_eq!(
            config.values("core", None, "x").collect::<Vec<_>>(),
            [Some(b"one".as_slice()), None, Some(b"two".as_slice())]
        );
        assert_eq!(
            config.value("core", None, "x"),
            Some(Some(b"two".as_slice()))
        );
    }

    #[test]
    fn append_rebases_members_and_retains_empty_headers() {
        let mut config = Config::parse(b"[core]\nx=one\n[empty]\n").unwrap();
        config.append(&Config::parse(b"[empty]\n[core]\nx=two\n[tail]\n").unwrap());
        let members: Vec<_> = config
            .section_occurrences()
            .map(|section| section.entry_indices())
            .collect();
        assert_eq!(members, [&[0][..], &[], &[], &[1], &[]]);
        assert_eq!(
            config.entries()[1].value.as_deref(),
            Some(b"two".as_slice())
        );
    }

    #[test]
    fn append_preserves_empty_headers_and_entry_order() {
        let mut config = Config::parse(b"[remote \"z\"]\nurl=first\n").unwrap();
        config.append(&Config::parse(b"[remote \"a\"]\n[remote \"z\"]\nurl=second\n").unwrap());
        assert!(config.contains_section("remote", Some(b"a")));
        assert_eq!(config.subsection_names("remote"), [b"a".as_slice(), b"z"]);
        assert_eq!(
            config
                .values("remote", Some(b"z"), "url")
                .collect::<Vec<_>>(),
            [Some(b"first".as_slice()), Some(b"second".as_slice())]
        );
    }

    #[test]
    fn legacy_remote_names_stay_in_first_entry_order() {
        let config = Config::parse(b"[remote \"empty\"]\n[remote \"z\"]\nurl=repo\n[remote \"a\"]\nurl=repo\n[remote \"z\"]\nfetch=HEAD").unwrap();
        assert_eq!(
            crate::remote::Remote::names(&config),
            [b"z".as_slice(), b"a"]
        );
        assert_eq!(
            crate::remote::RemoteUrls::names(&config),
            [b"z".as_slice(), b"a"]
        );
        assert_eq!(
            config.subsection_names("remote"),
            [b"a".as_slice(), b"empty", b"z"]
        );
    }

    #[rstest]
    #[case::quote(b"[core]\nx = \" a # ; \" ; comment\n", b" a # ; ")]
    #[case::continuation(b"[core]\nx = ab\\\ncd\n", b"abcd")]
    #[case::escapes(b"[core]\nx = \\n\\t\\b\\\\\\\"\n", b"\n\t\x08\\\"")]
    #[case::bytes(b"[core]\nx = \xff\n", b"\xff")]
    #[case::continued_space(b"[core]\nx = first  \\\n\n", b"first  ")]
    #[case::continued_space_comment(b"[core]\nx = first\t \\\n# comment\n", b"first\t ")]
    #[case::continued_empty(b"[core]\nx = \\\n\n", b"")]
    #[case::empty_quote_prefix(b"[core]\nx = \"\"  value\n", b"value")]
    #[case::continued_prefix(b"[core]\nx = \\\n  value\n", b"value")]
    fn parses_values(#[case] input: &[u8], #[case] expected: &[u8]) {
        let config = Config::parse(input).unwrap();
        assert_eq!(config.value("CORE", None, "X"), Some(Some(expected)));
    }
    #[rstest]
    #[case::outside(b"x = y")]
    #[case::escape(b"[core]\nx = \\q")]
    #[case::quote(b"[core]\nx = \"no")]
    #[case::nul(b"[core]\nx = \0")]
    #[case::nul_quoted(b"[core]\nx = \"a\0b\"")]
    #[case::nul_key(b"[core]\nx\0 = a")]
    #[case::nul_section(b"[co\0re]\nx=a")]
    #[case::nul_subsection(b"[core \"a\0b\"]\nx=a")]
    #[case::nul_subsection_escape(b"[core \"a\\\0b\"]\nx=a")]
    #[case::nul_between(b"[core]\n\0\nx=a")]
    #[case::quoted_comment(b"[core]\nx=\"#\0\"")]
    #[case::double_cr_escape(b"[core]\nx = a\\\r\r\nb\n")]
    fn rejects_syntax(#[case] input: &[u8]) {
        assert!(Config::parse(input).is_err());
    }
    #[test]
    fn repeated_keys_and_subsections() {
        let config = Config::parse(b"[Remote \"Origin\"]\nURL\nurl=\nurl=last\n").unwrap();
        assert_eq!(
            config
                .values("remote", Some(b"Origin"), "url")
                .collect::<Vec<_>>(),
            vec![None, Some(b"".as_slice()), Some(b"last".as_slice())]
        );
        assert_eq!(config.value("remote", Some(b"origin"), "url"), None);
    }
}

/// A configuration value does not have the requested type.
#[derive(Clone, Debug, Eq, PartialEq, thiserror::Error)]
#[error("invalid value for {section}.{name}: {value:?}")]
pub struct InvalidValue {
    /// Section name as requested.
    pub section: String,
    /// Variable name as requested.
    pub name: String,
    /// Lossy rendering of the offending value; `None` for an implicit assignment.
    pub value: Option<String>,
}

impl InvalidValue {
    pub(crate) fn new(section: &str, name: &str, value: Option<&[u8]>) -> Self {
        Self {
            section: section.to_owned(),
            name: name.to_owned(),
            value: value.map(|value| String::from_utf8_lossy(value).into_owned()),
        }
    }
}