Skip to content

Commit 84b98d9

Browse files
committed
Merge branch 'config-comfort'
2 parents 20e188f + 7f67b23 commit 84b98d9

File tree

51 files changed

+794
-537
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+794
-537
lines changed

git-config/src/file/access/comfort.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ impl<'event> File<'event> {
99
/// Like [`value()`][File::value()], but returning an `None` if the string wasn't found.
1010
///
1111
/// As strings perform no conversions, this will never fail.
12-
pub fn string(&self, section_name: &str, subsection_name: Option<&str>, key: &str) -> Option<Cow<'_, BStr>> {
12+
pub fn string(
13+
&self,
14+
section_name: impl AsRef<str>,
15+
subsection_name: Option<&str>,
16+
key: impl AsRef<str>,
17+
) -> Option<Cow<'_, BStr>> {
1318
self.raw_value(section_name, subsection_name, key).ok()
1419
}
1520

@@ -22,7 +27,12 @@ impl<'event> File<'event> {
2227
// TODO: add `secure_path()` or similar to make use of our knowledge of the trust associated with each configuration
2328
// file, maybe even remove the insecure version to force every caller to ask themselves if the resource can
2429
// be used securely or not.
25-
pub fn path(&self, section_name: &str, subsection_name: Option<&str>, key: &str) -> Option<crate::Path<'_>> {
30+
pub fn path(
31+
&self,
32+
section_name: impl AsRef<str>,
33+
subsection_name: Option<&str>,
34+
key: impl AsRef<str>,
35+
) -> Option<crate::Path<'_>> {
2636
self.raw_value(section_name, subsection_name, key)
2737
.ok()
2838
.map(crate::Path::from)
@@ -31,9 +41,9 @@ impl<'event> File<'event> {
3141
/// Like [`value()`][File::value()], but returning `None` if the boolean value wasn't found.
3242
pub fn boolean(
3343
&self,
34-
section_name: &str,
44+
section_name: impl AsRef<str>,
3545
subsection_name: Option<&str>,
36-
key: &str,
46+
key: impl AsRef<str>,
3747
) -> Option<Result<bool, value::Error>> {
3848
self.raw_value(section_name, subsection_name, key)
3949
.ok()
@@ -43,9 +53,9 @@ impl<'event> File<'event> {
4353
/// Like [`value()`][File::value()], but returning an `Option` if the integer wasn't found.
4454
pub fn integer(
4555
&self,
46-
section_name: &str,
56+
section_name: impl AsRef<str>,
4757
subsection_name: Option<&str>,
48-
key: &str,
58+
key: impl AsRef<str>,
4959
) -> Option<Result<i64, value::Error>> {
5060
let int = self.raw_value(section_name, subsection_name, key).ok()?;
5161
Some(crate::Integer::try_from(int.as_ref()).and_then(|b| {
@@ -55,17 +65,22 @@ impl<'event> File<'event> {
5565
}
5666

5767
/// Similar to [`values(…)`][File::values()] but returning strings if at least one of them was found.
58-
pub fn strings(&self, section_name: &str, subsection_name: Option<&str>, key: &str) -> Option<Vec<Cow<'_, BStr>>> {
68+
pub fn strings(
69+
&self,
70+
section_name: impl AsRef<str>,
71+
subsection_name: Option<&str>,
72+
key: impl AsRef<str>,
73+
) -> Option<Vec<Cow<'_, BStr>>> {
5974
self.raw_values(section_name, subsection_name, key).ok()
6075
}
6176

6277
/// Similar to [`values(…)`][File::values()] but returning integers if at least one of them was found
6378
/// and if none of them overflows.
6479
pub fn integers(
6580
&self,
66-
section_name: &str,
81+
section_name: impl AsRef<str>,
6782
subsection_name: Option<&str>,
68-
key: &str,
83+
key: impl AsRef<str>,
6984
) -> Option<Result<Vec<i64>, value::Error>> {
7085
self.raw_values(section_name, subsection_name, key).ok().map(|values| {
7186
values

git-config/src/file/access/mutate.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::borrow::Cow;
22

3-
use crate::file::rename_section;
43
use crate::{
5-
file::{MutableSection, SectionBody},
4+
file::{rename_section, SectionBody, SectionMut},
65
lookup,
76
parse::section,
87
File,
@@ -13,15 +12,15 @@ impl<'event> File<'event> {
1312
/// Returns an mutable section with a given name and optional subsection.
1413
pub fn section_mut<'a>(
1514
&'a mut self,
16-
section_name: &str,
15+
section_name: impl AsRef<str>,
1716
subsection_name: Option<&str>,
18-
) -> Result<MutableSection<'a, 'event>, lookup::existing::Error> {
17+
) -> Result<SectionMut<'a, 'event>, lookup::existing::Error> {
1918
let id = self
20-
.section_ids_by_name_and_subname(section_name, subsection_name)?
19+
.section_ids_by_name_and_subname(section_name.as_ref(), subsection_name)?
2120
.rev()
2221
.next()
2322
.expect("BUG: Section lookup vec was empty");
24-
Ok(MutableSection::new(
23+
Ok(SectionMut::new(
2524
self.sections
2625
.get_mut(&id)
2726
.expect("BUG: Section did not have id from lookup"),
@@ -53,7 +52,7 @@ impl<'event> File<'event> {
5352
/// # use git_config::parse::section;
5453
/// let mut git_config = git_config::File::default();
5554
/// let mut section = git_config.new_section("hello", Some("world".into()))?;
56-
/// section.push(section::Key::try_from("a")?, b"b".as_bstr().into());
55+
/// section.push(section::Key::try_from("a")?, "b");
5756
/// assert_eq!(git_config.to_string(), "[hello \"world\"]\n\ta = b\n");
5857
/// let _section = git_config.new_section("core", None);
5958
/// assert_eq!(git_config.to_string(), "[hello \"world\"]\n\ta = b\n[core]\n");
@@ -63,7 +62,7 @@ impl<'event> File<'event> {
6362
&mut self,
6463
section_name: impl Into<Cow<'event, str>>,
6564
subsection_name: impl Into<Option<Cow<'event, str>>>,
66-
) -> Result<MutableSection<'_, 'event>, section::header::Error> {
65+
) -> Result<SectionMut<'_, 'event>, section::header::Error> {
6766
let mut section = self.push_section(section_name, subsection_name, SectionBody::default())?;
6867
section.push_newline();
6968
Ok(section)
@@ -132,20 +131,20 @@ impl<'event> File<'event> {
132131
section_name: impl Into<Cow<'event, str>>,
133132
subsection_name: impl Into<Option<Cow<'event, str>>>,
134133
section: SectionBody<'event>,
135-
) -> Result<MutableSection<'_, 'event>, section::header::Error> {
134+
) -> Result<SectionMut<'_, 'event>, section::header::Error> {
136135
Ok(self.push_section_internal(section::Header::new(section_name, subsection_name)?, section))
137136
}
138137

139138
/// Renames a section, modifying the last matching section.
140139
pub fn rename_section<'a>(
141140
&mut self,
142-
section_name: &str,
141+
section_name: impl AsRef<str>,
143142
subsection_name: impl Into<Option<&'a str>>,
144143
new_section_name: impl Into<Cow<'event, str>>,
145144
new_subsection_name: impl Into<Option<Cow<'event, str>>>,
146145
) -> Result<(), rename_section::Error> {
147146
let id = self
148-
.section_ids_by_name_and_subname(section_name, subsection_name.into())?
147+
.section_ids_by_name_and_subname(section_name.as_ref(), subsection_name.into())?
149148
.rev()
150149
.next()
151150
.expect("list of sections were empty, which violates invariant");

git-config/src/file/access/raw.rs

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ use std::{borrow::Cow, collections::HashMap};
22

33
use bstr::BStr;
44

5-
use crate::file::mutable::value::EntryData;
65
use crate::{
7-
file::{Index, MutableMultiValue, MutableSection, MutableValue, Size},
6+
file::{mutable::multi_value::EntryData, Index, MultiValueMut, SectionMut, Size, ValueMut},
87
lookup,
98
parse::{section, Event},
109
File,
@@ -21,11 +20,12 @@ impl<'event> File<'event> {
2120
/// a multivar instead.
2221
pub fn raw_value(
2322
&self,
24-
section_name: &str,
23+
section_name: impl AsRef<str>,
2524
subsection_name: Option<&str>,
26-
key: &str,
25+
key: impl AsRef<str>,
2726
) -> Result<Cow<'_, BStr>, lookup::existing::Error> {
28-
let section_ids = self.section_ids_by_name_and_subname(section_name, subsection_name)?;
27+
let section_ids = self.section_ids_by_name_and_subname(section_name.as_ref(), subsection_name)?;
28+
let key = key.as_ref();
2929
for section_id in section_ids.rev() {
3030
if let Some(v) = self.sections.get(&section_id).expect("known section id").value(key) {
3131
return Ok(v);
@@ -42,12 +42,12 @@ impl<'event> File<'event> {
4242
/// references to all values of a multivar instead.
4343
pub fn raw_value_mut<'lookup>(
4444
&mut self,
45-
section_name: &'lookup str,
45+
section_name: impl AsRef<str>,
4646
subsection_name: Option<&'lookup str>,
4747
key: &'lookup str,
48-
) -> Result<MutableValue<'_, 'lookup, 'event>, lookup::existing::Error> {
48+
) -> Result<ValueMut<'_, 'lookup, 'event>, lookup::existing::Error> {
4949
let mut section_ids = self
50-
.section_ids_by_name_and_subname(section_name, subsection_name)?
50+
.section_ids_by_name_and_subname(section_name.as_ref(), subsection_name)?
5151
.rev();
5252
let key = section::Key(Cow::<BStr>::Borrowed(key.into()));
5353

@@ -88,8 +88,8 @@ impl<'event> File<'event> {
8888
}
8989

9090
drop(section_ids);
91-
return Ok(MutableValue {
92-
section: MutableSection::new(self.sections.get_mut(&section_id).expect("known section-id")),
91+
return Ok(ValueMut {
92+
section: SectionMut::new(self.sections.get_mut(&section_id).expect("known section-id")),
9393
key,
9494
index: Index(index),
9595
size: Size(size),
@@ -136,12 +136,13 @@ impl<'event> File<'event> {
136136
/// value for a given key, if your key does not support multi-valued values.
137137
pub fn raw_values(
138138
&self,
139-
section_name: &str,
139+
section_name: impl AsRef<str>,
140140
subsection_name: Option<&str>,
141-
key: &str,
141+
key: impl AsRef<str>,
142142
) -> Result<Vec<Cow<'_, BStr>>, lookup::existing::Error> {
143143
let mut values = Vec::new();
144-
let section_ids = self.section_ids_by_name_and_subname(section_name, subsection_name)?;
144+
let section_ids = self.section_ids_by_name_and_subname(section_name.as_ref(), subsection_name)?;
145+
let key = key.as_ref();
145146
for section_id in section_ids {
146147
values.extend(self.sections.get(&section_id).expect("known section id").values(key));
147148
}
@@ -205,11 +206,11 @@ impl<'event> File<'event> {
205206
/// traversal of the config.
206207
pub fn raw_values_mut<'lookup>(
207208
&mut self,
208-
section_name: &'lookup str,
209+
section_name: impl AsRef<str>,
209210
subsection_name: Option<&'lookup str>,
210211
key: &'lookup str,
211-
) -> Result<MutableMultiValue<'_, 'lookup, 'event>, lookup::existing::Error> {
212-
let section_ids = self.section_ids_by_name_and_subname(section_name, subsection_name)?;
212+
) -> Result<MultiValueMut<'_, 'lookup, 'event>, lookup::existing::Error> {
213+
let section_ids = self.section_ids_by_name_and_subname(section_name.as_ref(), subsection_name)?;
213214
let key = section::Key(Cow::<BStr>::Borrowed(key.into()));
214215

215216
let mut offsets = HashMap::new();
@@ -255,7 +256,7 @@ impl<'event> File<'event> {
255256
if entries.is_empty() {
256257
Err(lookup::existing::Error::KeyMissing)
257258
} else {
258-
Ok(MutableMultiValue {
259+
Ok(MultiValueMut {
259260
section: &mut self.sections,
260261
key,
261262
indices_and_sizes: entries,
@@ -300,12 +301,12 @@ impl<'event> File<'event> {
300301
/// ```
301302
pub fn set_raw_value(
302303
&mut self,
303-
section_name: &str,
304+
section_name: impl AsRef<str>,
304305
subsection_name: Option<&str>,
305-
key: &str,
306+
key: impl AsRef<str>,
306307
new_value: &BStr,
307308
) -> Result<(), lookup::existing::Error> {
308-
self.raw_value_mut(section_name, subsection_name, key)
309+
self.raw_value_mut(section_name, subsection_name, key.as_ref())
309310
.map(|mut entry| entry.set(new_value))
310311
}
311312

@@ -344,9 +345,9 @@ impl<'event> File<'event> {
344345
/// # use bstr::BStr;
345346
/// # let mut git_config = git_config::File::try_from("[core]a=b\n[core]\na=c\na=d").unwrap();
346347
/// let new_values = vec![
347-
/// "x".into(),
348-
/// "y".into(),
349-
/// "z".into(),
348+
/// "x",
349+
/// "y",
350+
/// "z",
350351
/// ];
351352
/// git_config.set_raw_multi_value("core", None, "a", new_values.into_iter())?;
352353
/// let fetched_config = git_config.raw_values("core", None, "a")?;
@@ -365,8 +366,8 @@ impl<'event> File<'event> {
365366
/// # use bstr::BStr;
366367
/// # let mut git_config = git_config::File::try_from("[core]a=b\n[core]\na=c\na=d").unwrap();
367368
/// let new_values = vec![
368-
/// "x".into(),
369-
/// "y".into(),
369+
/// "x",
370+
/// "y",
370371
/// ];
371372
/// git_config.set_raw_multi_value("core", None, "a", new_values.into_iter())?;
372373
/// let fetched_config = git_config.raw_values("core", None, "a")?;
@@ -384,23 +385,27 @@ impl<'event> File<'event> {
384385
/// # use bstr::BStr;
385386
/// # let mut git_config = git_config::File::try_from("[core]a=b\n[core]\na=c\na=d").unwrap();
386387
/// let new_values = vec![
387-
/// "x".into(),
388-
/// "y".into(),
389-
/// "z".into(),
390-
/// "discarded".into(),
388+
/// "x",
389+
/// "y",
390+
/// "z",
391+
/// "discarded",
391392
/// ];
392393
/// git_config.set_raw_multi_value("core", None, "a", new_values)?;
393394
/// assert!(!git_config.raw_values("core", None, "a")?.contains(&Cow::<BStr>::Borrowed("discarded".into())));
394395
/// # Ok::<(), git_config::lookup::existing::Error>(())
395396
/// ```
396-
pub fn set_raw_multi_value<'a>(
397+
pub fn set_raw_multi_value<'a, Iter, Item>(
397398
&mut self,
398-
section_name: &str,
399+
section_name: impl AsRef<str>,
399400
subsection_name: Option<&str>,
400-
key: &str,
401-
new_values: impl IntoIterator<Item = &'a BStr>,
402-
) -> Result<(), lookup::existing::Error> {
403-
self.raw_values_mut(section_name, subsection_name, key)
401+
key: impl AsRef<str>,
402+
new_values: Iter,
403+
) -> Result<(), lookup::existing::Error>
404+
where
405+
Iter: IntoIterator<Item = Item>,
406+
Item: Into<&'a BStr>,
407+
{
408+
self.raw_values_mut(section_name, subsection_name, key.as_ref())
404409
.map(|mut v| v.set_values(new_values))
405410
}
406411
}

git-config/src/file/access/read_only.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@ impl<'event> File<'event> {
120120
/// Returns the last found immutable section with a given name and optional subsection name.
121121
pub fn section(
122122
&mut self,
123-
section_name: &str,
123+
section_name: impl AsRef<str>,
124124
subsection_name: Option<&str>,
125125
) -> Result<&SectionBody<'event>, lookup::existing::Error> {
126126
let id = self
127-
.section_ids_by_name_and_subname(section_name, subsection_name)?
127+
.section_ids_by_name_and_subname(section_name.as_ref(), subsection_name)?
128128
.rev()
129129
.next()
130130
.expect("BUG: Section lookup vec was empty");

git-config/src/file/access/write.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use crate::File;
21
use bstr::BString;
32

3+
use crate::File;
4+
45
impl File<'_> {
56
/// Serialize this type into a `BString` for convenience.
67
///

git-config/src/file/impls.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use std::str::FromStr;
2-
use std::{convert::TryFrom, fmt::Display};
1+
use std::{convert::TryFrom, fmt::Display, str::FromStr};
32

43
use bstr::{BStr, BString};
54

git-config/src/file/init/from_env.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ use std::{borrow::Cow, path::PathBuf};
22

33
use bstr::BString;
44

5-
use crate::file::from_paths;
6-
use crate::{file::init::resolve_includes, parse::section, path::interpolate, File};
5+
use crate::{
6+
file::{from_paths, init::resolve_includes},
7+
parse::section,
8+
path::interpolate,
9+
File,
10+
};
711

812
/// Represents the errors that may occur when calling [`File::from_env`][crate::File::from_env()].
913
#[derive(Debug, thiserror::Error)]
@@ -109,7 +113,7 @@ impl File<'static> {
109113

110114
section.push(
111115
section::Key(BString::from(key).into()),
112-
git_path::into_bstr(PathBuf::from(value)).into_owned().into(),
116+
git_path::into_bstr(PathBuf::from(value)).as_ref(),
113117
);
114118
}
115119
None => {

git-config/src/file/init/resolve_includes.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ use std::{
66
use bstr::{BStr, BString, ByteSlice, ByteVec};
77
use git_ref::Category;
88

9-
use crate::file::init::from_paths;
10-
use crate::file::init::from_paths::Options;
11-
use crate::{file::SectionBodyId, File};
9+
use crate::{
10+
file::{
11+
init::{from_paths, from_paths::Options},
12+
SectionBodyId,
13+
},
14+
File,
15+
};
1216

1317
pub(crate) fn resolve_includes(
1418
conf: &mut File<'static>,

0 commit comments

Comments
 (0)