Skip to content

Commit 0a584ee

Browse files
committed
more idiomatic use of config.section_by_name()
1 parent a8bc0de commit 0a584ee

File tree

6 files changed

+60
-72
lines changed

6 files changed

+60
-72
lines changed

gix/src/config/cache/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub(crate) fn query_refupdates(
5151
) -> Result<Option<gix_ref::store::WriteReflog>, Error> {
5252
let key = "core.logAllRefUpdates";
5353
Core::LOG_ALL_REF_UPDATES
54-
.try_into_ref_updates(config.boolean_by_key(key), || config.string_by_key(key))
54+
.try_into_ref_updates(config.boolean_by_key(key))
5555
.with_leniency(lenient_config)
5656
.map_err(Into::into)
5757
}

gix/src/config/tree/sections/core.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -301,30 +301,27 @@ mod disambiguate {
301301
}
302302

303303
mod log_all_ref_updates {
304-
use std::borrow::Cow;
305-
306-
use crate::{bstr::BStr, config, config::tree::core::LogAllRefUpdates};
304+
use crate::{config, config::tree::core::LogAllRefUpdates};
307305

308306
impl LogAllRefUpdates {
309-
/// Returns the mode for ref-updates as parsed from `value`. If `value` is not a boolean, `string_on_failure` will be called
310-
/// to obtain the key `core.logAllRefUpdates` as string instead. For correctness, this two step process is necessary as
307+
/// Returns the mode for ref-updates as parsed from `value`. If `value` is not a boolean, we try
308+
/// to interpret the string value instead. For correctness, this two step process is necessary as
311309
/// the interpretation of booleans in special in `gix-config`, i.e. we can't just treat it as string.
312-
pub fn try_into_ref_updates<'a>(
310+
pub fn try_into_ref_updates(
313311
&'static self,
314312
value: Option<Result<bool, gix_config::value::Error>>,
315-
string_on_failure: impl FnOnce() -> Option<Cow<'a, BStr>>,
316313
) -> Result<Option<gix_ref::store::WriteReflog>, config::key::GenericErrorWithValue> {
317-
match value.transpose().ok().flatten() {
318-
Some(bool) => Ok(Some(if bool {
314+
match value {
315+
Some(Ok(bool)) => Ok(Some(if bool {
319316
gix_ref::store::WriteReflog::Normal
320317
} else {
321318
gix_ref::store::WriteReflog::Disable
322319
})),
323-
None => match string_on_failure() {
324-
Some(val) if val.eq_ignore_ascii_case(b"always") => Ok(Some(gix_ref::store::WriteReflog::Always)),
325-
Some(val) => Err(config::key::GenericErrorWithValue::from_value(self, val.into_owned())),
326-
None => Ok(None),
320+
Some(Err(err)) => match err.input {
321+
val if val.eq_ignore_ascii_case(b"always") => Ok(Some(gix_ref::store::WriteReflog::Always)),
322+
val => Err(config::key::GenericErrorWithValue::from_value(self, val)),
327323
},
324+
None => Ok(None),
328325
}
329326
}
330327
}
@@ -438,9 +435,7 @@ mod validate {
438435
impl keys::Validate for LogAllRefUpdates {
439436
fn validate(&self, value: &BStr) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
440437
super::Core::LOG_ALL_REF_UPDATES
441-
.try_into_ref_updates(Some(gix_config::Boolean::try_from(value).map(|b| b.0)), || {
442-
Some(value.into())
443-
})?;
438+
.try_into_ref_updates(Some(gix_config::Boolean::try_from(value).map(|b| b.0)))?;
444439
Ok(())
445440
}
446441
}

gix/src/config/tree/sections/diff.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,8 @@ mod algorithm {
6868
}
6969

7070
mod renames {
71-
use std::borrow::Cow;
72-
7371
use crate::{
74-
bstr::{BStr, ByteSlice},
72+
bstr::ByteSlice,
7573
config::{
7674
key::GenericError,
7775
tree::{keys, sections::diff::Renames, Section},
@@ -84,21 +82,20 @@ mod renames {
8482
pub const fn new_renames(name: &'static str, section: &'static dyn Section) -> Self {
8583
keys::Any::new_with_validate(name, section, super::validate::Renames)
8684
}
87-
/// Try to convert the configuration into a valid rename tracking variant. Use `value` and if it's an error, call `value_string`
88-
/// to try and interpret the key as string.
89-
pub fn try_into_renames<'a>(
85+
/// Try to convert the configuration into a valid rename tracking variant. Use `value` and if it's an error, interpret
86+
/// the boolean as string
87+
pub fn try_into_renames(
9088
&'static self,
9189
value: Result<bool, gix_config::value::Error>,
92-
value_string: impl FnOnce() -> Option<Cow<'a, BStr>>,
9390
) -> Result<Tracking, GenericError> {
9491
Ok(match value {
9592
Ok(true) => Tracking::Renames,
9693
Ok(false) => Tracking::Disabled,
9794
Err(err) => {
98-
let value = value_string().ok_or_else(|| GenericError::from(self))?;
99-
match value.as_ref().as_bytes() {
95+
let value = &err.input;
96+
match value.as_bytes() {
10097
b"copy" | b"copies" => Tracking::RenamesAndCopies,
101-
_ => return Err(GenericError::from_value(self, value.into_owned()).with_source(err)),
98+
_ => return Err(GenericError::from_value(self, value.clone()).with_source(err)),
10299
}
103100
}
104101
})
@@ -107,8 +104,6 @@ mod renames {
107104
}
108105

109106
mod validate {
110-
use std::borrow::Cow;
111-
112107
use crate::{
113108
bstr::BStr,
114109
config::tree::{keys, Diff},
@@ -126,7 +121,7 @@ mod validate {
126121
impl keys::Validate for Renames {
127122
fn validate(&self, value: &BStr) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
128123
let boolean = gix_config::Boolean::try_from(value).map(|b| b.0);
129-
Diff::RENAMES.try_into_renames(boolean, || Some(Cow::Borrowed(value)))?;
124+
Diff::RENAMES.try_into_renames(boolean)?;
130125
Ok(())
131126
}
132127
}

gix/src/filter.rs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -199,30 +199,31 @@ impl<'repo> Pipeline<'repo> {
199199

200200
/// Obtain a list of all configured driver, but ignore those in sections that we don't trust enough.
201201
fn extract_drivers(repo: &Repository) -> Result<Vec<gix_filter::Driver>, pipeline::options::Error> {
202-
Ok(match repo.config.resolved.sections_by_name("filter") {
203-
None => Vec::new(),
204-
Some(sections) => sections
205-
.filter(|s| repo.filter_config_section()(s.meta()))
206-
.filter_map(|s| {
207-
s.header().subsection_name().map(|name| {
208-
Ok(gix_filter::Driver {
209-
name: name.to_owned(),
210-
clean: s.value("clean").map(Cow::into_owned),
211-
smudge: s.value("smudge").map(Cow::into_owned),
212-
process: s.value("process").map(Cow::into_owned),
213-
required: s
214-
.value("required")
215-
.map(|value| gix_config::Boolean::try_from(value.as_ref()))
216-
.transpose()
217-
.map_err(|err| pipeline::options::Error::Driver {
218-
name: name.to_owned(),
219-
source: err,
220-
})?
221-
.unwrap_or_default()
222-
.into(),
223-
})
202+
repo.config
203+
.resolved
204+
.sections_by_name("filter")
205+
.into_iter()
206+
.flatten()
207+
.filter(|s| repo.filter_config_section()(s.meta()))
208+
.filter_map(|s| {
209+
s.header().subsection_name().map(|name| {
210+
Ok(gix_filter::Driver {
211+
name: name.to_owned(),
212+
clean: s.value("clean").map(Cow::into_owned),
213+
smudge: s.value("smudge").map(Cow::into_owned),
214+
process: s.value("process").map(Cow::into_owned),
215+
required: s
216+
.value("required")
217+
.map(|value| gix_config::Boolean::try_from(value.as_ref()))
218+
.transpose()
219+
.map_err(|err| pipeline::options::Error::Driver {
220+
name: name.to_owned(),
221+
source: err,
222+
})?
223+
.unwrap_or_default()
224+
.into(),
224225
})
225226
})
226-
.collect::<Result<Vec<_>, pipeline::options::Error>>()?,
227-
})
227+
})
228+
.collect::<Result<Vec<_>, pipeline::options::Error>>()
228229
}

gix/src/object/tree/diff/rewrites.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl Rewrites {
8080
let key = "diff.renames";
8181
let copies = match config
8282
.boolean_by_key(key)
83-
.map(|value| Diff::RENAMES.try_into_renames(value, || config.string_by_key(key)))
83+
.map(|value| Diff::RENAMES.try_into_renames(value))
8484
.transpose()
8585
.with_leniency(lenient)?
8686
{

gix/tests/config/tree.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -189,32 +189,24 @@ mod diff {
189189

190190
#[test]
191191
fn renames() -> crate::Result {
192-
assert_eq!(
193-
Diff::RENAMES.try_into_renames(Ok(true), || unreachable!())?,
194-
Tracking::Renames
195-
);
192+
assert_eq!(Diff::RENAMES.try_into_renames(Ok(true))?, Tracking::Renames);
196193
assert!(Diff::RENAMES.validate("1".into()).is_ok());
197-
assert_eq!(
198-
Diff::RENAMES.try_into_renames(Ok(false), || unreachable!())?,
199-
Tracking::Disabled
200-
);
194+
assert_eq!(Diff::RENAMES.try_into_renames(Ok(false))?, Tracking::Disabled);
201195
assert!(Diff::RENAMES.validate("0".into()).is_ok());
202196
assert_eq!(
203-
Diff::RENAMES.try_into_renames(Err(gix_config::value::Error::new("err", "err")), || Some(bcow("copy")))?,
197+
Diff::RENAMES.try_into_renames(Err(gix_config::value::Error::new("err", "copy")))?,
204198
Tracking::RenamesAndCopies
205199
);
206200
assert!(Diff::RENAMES.validate("copy".into()).is_ok());
207201
assert_eq!(
208-
Diff::RENAMES.try_into_renames(Err(gix_config::value::Error::new("err", "err")), || Some(bcow(
209-
"copies"
210-
)))?,
202+
Diff::RENAMES.try_into_renames(Err(gix_config::value::Error::new("err", "copies")))?,
211203
Tracking::RenamesAndCopies
212204
);
213205
assert!(Diff::RENAMES.validate("copies".into()).is_ok());
214206

215207
assert_eq!(
216208
Diff::RENAMES
217-
.try_into_renames(Err(gix_config::value::Error::new("err", "err")), || Some(bcow("foo")))
209+
.try_into_renames(Err(gix_config::value::Error::new("err", "foo")))
218210
.unwrap_err()
219211
.to_string(),
220212
"The value of key \"diff.renames=foo\" was invalid"
@@ -322,23 +314,28 @@ mod core {
322314
#[test]
323315
fn log_all_ref_updates() -> crate::Result {
324316
assert_eq!(
325-
Core::LOG_ALL_REF_UPDATES.try_into_ref_updates(Some(Ok(true)), || None)?,
317+
Core::LOG_ALL_REF_UPDATES.try_into_ref_updates(Some(Ok(true)),)?,
326318
Some(gix_ref::store::WriteReflog::Normal)
327319
);
328320
assert!(Core::LOG_ALL_REF_UPDATES.validate("true".into()).is_ok());
329321
assert_eq!(
330-
Core::LOG_ALL_REF_UPDATES.try_into_ref_updates(Some(Ok(false)), || None)?,
322+
Core::LOG_ALL_REF_UPDATES.try_into_ref_updates(Some(Ok(false)),)?,
331323
Some(gix_ref::store::WriteReflog::Disable)
332324
);
333325
assert!(Core::LOG_ALL_REF_UPDATES.validate("0".into()).is_ok());
326+
let boolean = |value| {
327+
gix_config::Boolean::try_from(bcow(value))
328+
.map(|b| Some(b.0))
329+
.transpose()
330+
};
334331
assert_eq!(
335-
Core::LOG_ALL_REF_UPDATES.try_into_ref_updates(None, || Some(bcow("always")))?,
332+
Core::LOG_ALL_REF_UPDATES.try_into_ref_updates(boolean("always"))?,
336333
Some(gix_ref::store::WriteReflog::Always)
337334
);
338335
assert!(Core::LOG_ALL_REF_UPDATES.validate("always".into()).is_ok());
339336
assert_eq!(
340337
Core::LOG_ALL_REF_UPDATES
341-
.try_into_ref_updates(None, || Some(bcow("invalid")))
338+
.try_into_ref_updates(boolean("invalid"))
342339
.unwrap_err()
343340
.to_string(),
344341
"The key \"core.logAllRefUpdates=invalid\" was invalid"

0 commit comments

Comments
 (0)