From 10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 4 May 2024 14:41:41 +0200 Subject: Merging upstream version 1.70.0+dfsg2. Signed-off-by: Daniel Baumann --- vendor/gix/src/config/overrides.rs | 49 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 vendor/gix/src/config/overrides.rs (limited to 'vendor/gix/src/config/overrides.rs') diff --git a/vendor/gix/src/config/overrides.rs b/vendor/gix/src/config/overrides.rs new file mode 100644 index 000000000..f43e8471b --- /dev/null +++ b/vendor/gix/src/config/overrides.rs @@ -0,0 +1,49 @@ +use std::convert::TryFrom; + +use crate::bstr::{BStr, BString, ByteSlice}; + +/// The error returned by [SnapshotMut::apply_cli_overrides()][crate::config::SnapshotMut::append_config()]. +#[derive(Debug, thiserror::Error)] +#[allow(missing_docs)] +pub enum Error { + #[error("{input:?} is not a valid configuration key. Examples are 'core.abbrev' or 'remote.origin.url'")] + InvalidKey { input: BString }, + #[error("Key {key:?} could not be parsed")] + SectionKey { + key: BString, + source: gix_config::parse::section::key::Error, + }, + #[error(transparent)] + SectionHeader(#[from] gix_config::parse::section::header::Error), +} + +pub(crate) fn append( + config: &mut gix_config::File<'static>, + values: impl IntoIterator>, + source: gix_config::Source, + mut make_comment: impl FnMut(&BStr) -> Option, +) -> Result<(), Error> { + let mut file = gix_config::File::new(gix_config::file::Metadata::from(source)); + for key_value in values { + let key_value = key_value.as_ref(); + let mut tokens = key_value.splitn(2, |b| *b == b'=').map(|v| v.trim()); + let key = tokens.next().expect("always one value").as_bstr(); + let value = tokens.next(); + let key = gix_config::parse::key(key.to_str().map_err(|_| Error::InvalidKey { input: key.into() })?) + .ok_or_else(|| Error::InvalidKey { input: key.into() })?; + let mut section = file.section_mut_or_create_new(key.section_name, key.subsection_name)?; + let key = + gix_config::parse::section::Key::try_from(key.value_name.to_owned()).map_err(|err| Error::SectionKey { + source: err, + key: key.value_name.into(), + })?; + let comment = make_comment(key_value); + let value = value.map(|v| v.as_bstr()); + match comment { + Some(comment) => section.push_with_comment(key, value, &**comment), + None => section.push(key, value), + }; + } + config.append(file); + Ok(()) +} -- cgit v1.2.3