diff --git a/git-config/Cargo.toml b/git-config/Cargo.toml index 48ebf432d76..4b0f57ecdc1 100644 --- a/git-config/Cargo.toml +++ b/git-config/Cargo.toml @@ -22,6 +22,7 @@ serde_crate = { version = "1", package = "serde", optional = true } pwd = "1.3.1" quick-error = "2.0.0" unicode-bom = "1.1.4" +bstr = { version = "0.2.13", default-features = false } [dev-dependencies] serial_test = "0.5.1" diff --git a/git-config/src/values.rs b/git-config/src/values.rs index 6cb093783dc..29411c59adf 100644 --- a/git-config/src/values.rs +++ b/git-config/src/values.rs @@ -1,5 +1,6 @@ //! Rust containers for valid `git-config` types. +use bstr::BStr; use std::{borrow::Cow, convert::TryFrom, fmt::Display, str::FromStr}; use quick_error::quick_error; @@ -145,6 +146,7 @@ fn b(s: &str) -> &[u8] { s.as_bytes() } +// TODO: remove bytes /// Any string value #[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] pub struct Bytes<'a> { @@ -177,6 +179,25 @@ impl<'a> From> for Bytes<'a> { } } +/// Any string value +#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] +pub struct String<'a> { + /// The string value + pub value: Cow<'a, BStr>, +} + +impl<'a> From> for String<'a> { + #[inline] + fn from(c: Cow<'a, [u8]>) -> Self { + String { + value: match c { + Cow::Borrowed(c) => Cow::Borrowed(c.into()), + Cow::Owned(c) => Cow::Owned(c.into()), + }, + } + } +} + /// pub mod path { use std::borrow::Cow; @@ -433,6 +454,13 @@ pub enum Boolean<'a> { } impl Boolean<'_> { + /// Return ourselves as plain bool. + pub fn to_bool(&self) -> bool { + match self { + Boolean::True(_) => true, + Boolean::False(_) => false, + } + } /// Generates a byte representation of the value. This should be used when /// non-UTF-8 sequences are present or a UTF-8 representation can't be /// guaranteed. diff --git a/git-repository/src/repository/config.rs b/git-repository/src/repository/config.rs deleted file mode 100644 index 84bf53ea455..00000000000 --- a/git-repository/src/repository/config.rs +++ /dev/null @@ -1,15 +0,0 @@ -/// Provide simplified access to git configuration values -impl crate::Repository { - /// Return the integer value at `key` (like `core.abbrev`) or use the given `default` value if it isn't present. - // TODO: figure out how to identify sub-sections, or how to design such an API. This is really just a first test. - // TODO: tests - // TODO: parse-errors do exist, don't dumb it down too much as to not fill in defaults for incorrect configuration. - // Fail like git does. - pub(crate) fn config_int(&self, key: &str, default: i64) -> i64 { - let (section, key) = key.split_once('.').expect("valid section.key format"); - self.config - .resolved - .value::(section, None, key) - .map_or(default, |v| v.value) - } -}