Skip to content

Commit

Permalink
Make application of lenient configuration values way easier and nicer…
Browse files Browse the repository at this point in the history
… to read
  • Loading branch information
Byron committed Nov 15, 2022
1 parent b5ca8a6 commit a59c791
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
31 changes: 31 additions & 0 deletions git-repository/src/config/cache/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,37 @@ pub(crate) fn query_refupdates(
}
}

pub trait ApplyLeniencyOpt {
fn with_leniency(self, is_lenient: bool) -> Self;
}

pub trait ApplyLeniency {
fn with_leniency(self, is_lenient: bool) -> Self;
}

impl<T, E> ApplyLeniencyOpt for Result<Option<T>, E> {
fn with_leniency(self, is_lenient: bool) -> Self {
match self {
Ok(v) => Ok(v),
Err(_) if is_lenient => Ok(None),
Err(err) => Err(err),
}
}
}

impl<T, E> ApplyLeniency for Result<T, E>
where
T: Default,
{
fn with_leniency(self, is_lenient: bool) -> Self {
match self {
Ok(v) => Ok(v),
Err(_) if is_lenient => Ok(T::default()),
Err(err) => Err(err),
}
}
}

pub(crate) fn check_lenient<T, E>(v: Result<Option<T>, E>, lenient: bool) -> Result<Option<T>, E> {
match v {
Ok(v) => Ok(v),
Expand Down
19 changes: 8 additions & 11 deletions git-repository/src/repository/config/transport.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::bstr::BStr;
use crate::config::cache::util::ApplyLeniencyOpt;
use std::any::Any;

impl crate::Repository {
Expand Down Expand Up @@ -70,14 +71,11 @@ impl crate::Repository {
subsection_name,
value_name,
} = git_config::parse::key(key).expect("valid key statically known");
check_lenient(
check_lenient(
config
.integer_filter(section_name, subsection_name, value_name, &mut filter)
.transpose()
.map_err(|err| crate::config::transport::Error::ConfigValue { source: err, key }),
lenient,
)?
config
.integer_filter(section_name, subsection_name, value_name, &mut filter)
.transpose()
.map_err(|err| crate::config::transport::Error::ConfigValue { source: err, key })
.with_leniency(lenient)?
.map(|integer| {
integer
.try_into()
Expand All @@ -87,9 +85,8 @@ impl crate::Repository {
kind,
})
})
.transpose(),
lenient,
)
.transpose()
.with_leniency(lenient)
}
let mut opts = http::Options::default();
let config = &self.config.resolved;
Expand Down

0 comments on commit a59c791

Please sign in to comment.