Skip to content

Commit

Permalink
fix!: all config::Snapshot access now uses the new Key trait.
Browse files Browse the repository at this point in the history
That way one can officially use "section.name" strings or `&Section::NAME`.
  • Loading branch information
Byron committed Dec 30, 2024
1 parent 30d69d3 commit 38e3c50
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 28 deletions.
26 changes: 13 additions & 13 deletions gix/src/config/snapshot/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ impl<'repo> Snapshot<'repo> {
/// For a non-degenerating version, use [`try_boolean(…)`][Self::try_boolean()].
///
/// Note that this method takes the most recent value at `key` even if it is from a file with reduced trust.
pub fn boolean<'a>(&self, key: impl Into<&'a BStr>) -> Option<bool> {
pub fn boolean(&self, key: impl gix_config::AsKey) -> Option<bool> {
self.try_boolean(key).and_then(Result::ok)
}

/// Like [`boolean()`][Self::boolean()], but it will report an error if the value couldn't be interpreted as boolean.
pub fn try_boolean<'a>(&self, key: impl Into<&'a BStr>) -> Option<Result<bool, gix_config::value::Error>> {
self.repo.config.resolved.boolean(key.into())
pub fn try_boolean(&self, key: impl gix_config::AsKey) -> Option<Result<bool, gix_config::value::Error>> {
self.repo.config.resolved.boolean(key)
}

/// Return the resolved integer at `key`, or `None` if there is no such value or if the value can't be interpreted as
Expand All @@ -37,40 +37,40 @@ impl<'repo> Snapshot<'repo> {
/// For a non-degenerating version, use [`try_integer(…)`][Self::try_integer()].
///
/// Note that this method takes the most recent value at `key` even if it is from a file with reduced trust.
pub fn integer<'a>(&self, key: impl Into<&'a BStr>) -> Option<i64> {
pub fn integer(&self, key: impl gix_config::AsKey) -> Option<i64> {
self.try_integer(key).and_then(Result::ok)
}

/// Like [`integer()`][Self::integer()], but it will report an error if the value couldn't be interpreted as boolean.
pub fn try_integer<'a>(&self, key: impl Into<&'a BStr>) -> Option<Result<i64, gix_config::value::Error>> {
self.repo.config.resolved.integer(key.into())
pub fn try_integer(&self, key: impl gix_config::AsKey) -> Option<Result<i64, gix_config::value::Error>> {
self.repo.config.resolved.integer(key)
}

/// Return the string at `key`, or `None` if there is no such value.
///
/// Note that this method takes the most recent value at `key` even if it is from a file with reduced trust.
pub fn string<'a>(&self, key: impl Into<&'a BStr>) -> Option<Cow<'repo, BStr>> {
self.repo.config.resolved.string(key.into())
pub fn string(&self, key: impl gix_config::AsKey) -> Option<Cow<'repo, BStr>> {
self.repo.config.resolved.string(key)
}

/// Return the trusted and fully interpolated path at `key`, or `None` if there is no such value
/// or if no value was found in a trusted file.
/// An error occurs if the path could not be interpolated to its final value.
pub fn trusted_path<'a>(
pub fn trusted_path(
&self,
key: impl Into<&'a BStr>,
key: impl gix_config::AsKey,
) -> Option<Result<Cow<'repo, std::path::Path>, gix_config::path::interpolate::Error>> {
self.repo.config.trusted_file_path(key.into())
self.repo.config.trusted_file_path(key)
}

/// Return the trusted string at `key` for launching using [command::prepare()](gix_command::prepare()),
/// or `None` if there is no such value or if no value was found in a trusted file.
pub fn trusted_program<'a>(&self, key: impl Into<&'a BStr>) -> Option<Cow<'repo, OsStr>> {
pub fn trusted_program(&self, key: impl gix_config::AsKey) -> Option<Cow<'repo, OsStr>> {
let value = self
.repo
.config
.resolved
.string_filter(key.into(), &mut self.repo.config.filter_config_section.clone())?;
.string_filter(key, &mut self.repo.config.filter_config_section.clone())?;
Some(match gix_path::from_bstr(value) {
Cow::Borrowed(v) => Cow::Borrowed(v.as_os_str()),
Cow::Owned(v) => Cow::Owned(v.into_os_string()),
Expand Down
4 changes: 2 additions & 2 deletions gix/src/remote/connection/fetch/receive_pack.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
config::{
cache::util::ApplyLeniency,
tree::{Clone, Fetch, Key},
tree::{Clone, Fetch},
},
remote,
remote::{
Expand Down Expand Up @@ -117,7 +117,7 @@ where
let negotiator = repo
.config
.resolved
.string(Fetch::NEGOTIATION_ALGORITHM.logical_name().as_str())
.string(Fetch::NEGOTIATION_ALGORITHM)
.map(|n| Fetch::NEGOTIATION_ALGORITHM.try_into_negotiation_algorithm(n))
.transpose()
.with_leniency(repo.config.lenient_config)?
Expand Down
4 changes: 2 additions & 2 deletions gix/src/remote/url/scheme_permission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{borrow::Cow, collections::BTreeMap};
use crate::{
bstr::{BStr, BString, ByteSlice},
config,
config::tree::{gitoxide, Key, Protocol},
config::tree::{gitoxide, Protocol},
};

/// All allowed values of the `protocol.allow` key.
Expand Down Expand Up @@ -91,7 +91,7 @@ impl SchemePermission {

let user_allowed = saw_user.then(|| {
config
.string_filter(gitoxide::Allow::PROTOCOL_FROM_USER.logical_name().as_str(), &mut filter)
.string_filter(gitoxide::Allow::PROTOCOL_FROM_USER, &mut filter)
.map_or(true, |val| val.as_ref() == "1")
});
Ok(SchemePermission {
Expand Down
2 changes: 1 addition & 1 deletion gix/src/repository/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl Personas {

user_email = user_email.or_else(|| {
config
.string(gitoxide::User::EMAIL_FALLBACK.logical_name().as_str())
.string(gitoxide::User::EMAIL_FALLBACK)
.map(std::borrow::Cow::into_owned)
});
Personas {
Expand Down
4 changes: 2 additions & 2 deletions gix/src/repository/mailmap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::tree::{Key, Mailmap};
use crate::config::tree::Mailmap;
use crate::Id;

impl crate::Repository {
Expand Down Expand Up @@ -68,7 +68,7 @@ impl crate::Repository {

let configured_path = self
.config_snapshot()
.trusted_path(Mailmap::FILE.logical_name().as_str())
.trusted_path(&Mailmap::FILE)
.and_then(|res| res.map_err(|e| err.get_or_insert(e.into())).ok());

if let Some(mut file) =
Expand Down
10 changes: 2 additions & 8 deletions gix/src/repository/shallow.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::{borrow::Cow, path::PathBuf};

use crate::{
config::tree::{gitoxide, Key},
Repository,
};
use crate::{config::tree::gitoxide, Repository};

impl Repository {
/// Return `true` if the repository is a shallow clone, i.e. contains history only up to a certain depth.
Expand Down Expand Up @@ -36,10 +33,7 @@ impl Repository {
let shallow_name = self
.config
.resolved
.string_filter(
gitoxide::Core::SHALLOW_FILE.logical_name().as_str(),
&mut self.filter_config_section(),
)
.string_filter(gitoxide::Core::SHALLOW_FILE, &mut self.filter_config_section())
.unwrap_or_else(|| Cow::Borrowed("shallow".into()));
self.common_dir().join(gix_path::from_bstr(shallow_name))
}
Expand Down

0 comments on commit 38e3c50

Please sign in to comment.