gix_config/file/
mod.rs

1//! A high level wrapper around a single or multiple `git-config` file, for reading and mutation.
2use std::{
3    borrow::Cow,
4    collections::HashMap,
5    ops::{Add, AddAssign},
6    path::PathBuf,
7};
8
9use bstr::BStr;
10use gix_features::threading::OwnShared;
11
12mod mutable;
13pub use mutable::{multi_value::MultiValueMut, section::SectionMut, value::ValueMut};
14
15///
16pub mod init;
17
18mod access;
19mod impls;
20///
21pub mod includes;
22mod meta;
23mod util;
24
25///
26pub mod section;
27
28///
29pub mod rename_section {
30    /// The error returned by [`File::rename_section(…)`][crate::File::rename_section()].
31    #[derive(Debug, thiserror::Error)]
32    #[allow(missing_docs)]
33    pub enum Error {
34        #[error(transparent)]
35        Lookup(#[from] crate::lookup::existing::Error),
36        #[error(transparent)]
37        Section(#[from] crate::parse::section::header::Error),
38    }
39}
40
41///
42pub mod set_raw_value {
43    /// The error returned by [`File::set_raw_value(…)`][crate::File::set_raw_value()].
44    #[derive(Debug, thiserror::Error)]
45    #[allow(missing_docs)]
46    pub enum Error {
47        #[error(transparent)]
48        Header(#[from] crate::parse::section::header::Error),
49        #[error(transparent)]
50        ValueName(#[from] crate::parse::section::value_name::Error),
51    }
52}
53
54/// Additional information about a section.
55#[derive(Clone, Debug, PartialOrd, PartialEq, Ord, Eq, Hash)]
56pub struct Metadata {
57    /// The file path of the source, if known.
58    pub path: Option<PathBuf>,
59    /// Where the section is coming from.
60    pub source: crate::Source,
61    /// The levels of indirection of the file, with 0 being a section
62    /// that was directly loaded, and 1 being an `include.path` of a
63    /// level 0 file.
64    pub level: u8,
65    /// The trust-level for the section this meta-data is associated with.
66    pub trust: gix_sec::Trust,
67}
68
69/// A section in a git-config file, like `[core]` or `[remote "origin"]`, along with all of its keys.
70#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
71pub struct Section<'a> {
72    header: crate::parse::section::Header<'a>,
73    body: section::Body<'a>,
74    meta: OwnShared<Metadata>,
75    id: SectionId,
76}
77
78/// A strongly typed index into some range.
79#[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Clone, Copy)]
80pub(crate) struct Index(pub(crate) usize);
81
82impl Add<Size> for Index {
83    type Output = Self;
84
85    fn add(self, rhs: Size) -> Self::Output {
86        Self(self.0 + rhs.0)
87    }
88}
89
90/// A strongly typed a size.
91#[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Clone, Copy)]
92pub(crate) struct Size(pub(crate) usize);
93
94impl AddAssign<usize> for Size {
95    fn add_assign(&mut self, rhs: usize) {
96        self.0 += rhs;
97    }
98}
99
100/// The section ID is a monotonically increasing ID used to refer to section bodies.
101/// This value does not imply any ordering between sections, as new sections
102/// with higher section IDs may be in between lower ID sections after `File` mutation.
103///
104/// We need to use a section id because `git-config` permits sections with
105/// identical names, making it ambiguous when used in maps, for instance.
106///
107/// This id guaranteed to be unique, but not guaranteed to be compact. In other
108/// words, it's possible that a section may have an ID of 3 but the next section
109/// has an ID of 5 as 4 was deleted.
110#[derive(PartialEq, Eq, Hash, Copy, Clone, PartialOrd, Ord, Debug)]
111pub struct SectionId(pub(crate) usize);
112
113impl Default for SectionId {
114    fn default() -> Self {
115        SectionId(usize::MAX)
116    }
117}
118
119/// All section body ids referred to by a section name.
120///
121/// Note that order in Vec matters as it represents the order
122/// of section ids with the matched section and name, and is used for precedence
123/// management.
124#[derive(PartialEq, Eq, Clone, Debug)]
125pub(crate) enum SectionBodyIdsLut<'a> {
126    /// The list of section ids to use for obtaining the section body.
127    Terminal(Vec<SectionId>),
128    /// A hashmap from sub-section names to section ids.
129    NonTerminal(HashMap<Cow<'a, BStr>, Vec<SectionId>>),
130}
131#[cfg(test)]
132mod tests;
133mod write;