Skip to content

Commit 157fa2a

Browse files
committed
time-constrained write-down of some high-level concepts of git-config structures
Relevant for #31, but might not yet be enough to make sense of it all. Try working backwards from `git_config::File`
1 parent 9604154 commit 157fa2a

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

git-config/src/file.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ impl Token {
2222
}
2323
}
2424

25+
/// The entry point into reading and writing git config files.
2526
pub struct File {
2627
buf: Vec<u8>,
28+
/// A config file as parsed into tokens, where each [`Token`] is one of the three relevant items in git config files.
2729
tokens: Vec<Token>, // but how do we get fast lookups and proper value lookup based on decoded values?
2830
// On the fly is easier, otherwise we have to deal with a lookup cache of sorts and
2931
// many more allocations up front (which might be worth it). Cow<'a, _> would bind to

git-config/src/lib.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
use std::ops::Range;
66

7+
/// A span is a range into a set of bytes - see it as a selection into a Git config file.
8+
///
9+
/// Similar to [`std::ops::RangeInclusive`], but tailor made to work for us.
10+
/// There are various issues with std ranges, which we don't have to opt into for the simple Range-like item we need.
711
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
812
struct Span {
913
pub start: usize,
@@ -28,7 +32,8 @@ impl Span {
2832
pub mod file;
2933
pub use file::File;
3034

31-
mod value {
35+
/// A module with specialized value types as they exist within git config files.
36+
pub mod value {
3237
pub enum Color {
3338
Red,
3439
BrightRed,
@@ -48,39 +53,68 @@ mod value {
4853
}
4954
}
5055
}
56+
/// Git-config paths can contain `~` and more, see [the git documentation](https://github.com/git/git/blob/e67fbf927dfdf13d0b21dc6ea15dc3c7ef448ea0/Documentation/config.txt#L295:L295)
57+
/// on what needs to be supported.
5158
pub fn path(_value: &BStr) -> Result<PathBuf, Error> {
52-
unimplemented!("path_resolve")
59+
unimplemented!("resolve::path")
5360
}
5461
}
5562
}
5663

64+
/// Spanned items refer to their content using [`Span`]s, thus they act like a pointer into a byte buffer representing the config file.
65+
///
66+
/// These are inherently read-only, as they do not own any data but rather point to a buffer they don't even know.
5767
mod spanned {
5868
use crate::Span;
5969
// we parse leading and trailing whitespace into comments, avoiding the notion of whitespace.
6070
// This means we auto-trim whitespace otherwise, which I consider a feature
6171
pub(crate) type Comment = Span;
6272

73+
/// A section or sub-section (in case `sub_name` is `Some()`), i.e.
74+
///
75+
/// ```text
76+
/// [hello]
77+
///
78+
/// [hello.world]
79+
/// ```
6380
pub(crate) struct Section {
6481
pub(crate) name: Span,
6582
pub(crate) sub_name: Option<Span>,
6683
}
6784

85+
/// A key-value entry of a git-config file, like `name = value`
6886
pub(crate) struct Entry {
6987
pub(crate) name: Span,
7088
pub(crate) value: Option<Span>,
7189
}
7290
}
7391

92+
/// Owned versions of what can be found in `spanned`, which allows these items to be altered.
93+
///
94+
/// All of these will *may* remember their originating `span` as `Some(…)`, which is the entire region in the config file they point to. This is important
95+
/// in case of updates. New owned items thus don't have a `span`, represented by `None`.
7496
mod owned {
7597
use crate::Span;
7698
use bstr::BString;
7799

100+
/// A key-value entry of a git-config file, like `name = value`
78101
pub struct Entry {
79102
pub name: BString,
80103
pub value: Option<BString>,
81104
pub(crate) span: Option<Span>,
82105
}
83106

107+
/// A section or sub-section (in case `sub_name` is `Some()`), with all their entries.
108+
///
109+
/// For example
110+
/// ```text
111+
/// [hello]
112+
/// a = 2
113+
///
114+
/// [hello.world]
115+
/// b = c
116+
/// x = y
117+
/// ```
84118
pub struct Section {
85119
pub name: BString,
86120
pub sub_name: Option<BString>,
@@ -128,6 +162,7 @@ mod owned {
128162
}
129163
}
130164

165+
/// Borrowed items are nothing more than a fancy 'handle' to an item stored in a file, which can be made editable to make updates.
131166
mod borrowed {
132167
use crate::{file::File, owned};
133168

@@ -183,6 +218,7 @@ mod decode {
183218
}
184219
}
185220

221+
/// Decode an entry value - it can be [encoded as described in the git config documentation](https://github.com/git/git/blob/e67fbf927dfdf13d0b21dc6ea15dc3c7ef448ea0/Documentation/config.txt#L74:L80)
186222
pub fn value(_input: &BStr) -> Result<Cow<'_, BStr>, Error> {
187223
unimplemented!("decode value from bstr")
188224
}

0 commit comments

Comments
 (0)