2021-02-26 18:44:58 -08:00
|
|
|
use crate::parser::{parse_from_bytes, Event, ParsedSectionHeader, Parser, ParserError};
|
2021-02-27 19:18:44 -08:00
|
|
|
use std::collections::{HashMap, VecDeque};
|
2021-02-26 18:44:58 -08:00
|
|
|
use std::convert::TryFrom;
|
2021-02-27 19:18:44 -08:00
|
|
|
use std::error::Error;
|
2021-02-21 18:16:08 -08:00
|
|
|
use std::{borrow::Cow, fmt::Display};
|
2021-02-18 21:12:59 -08:00
|
|
|
|
2021-02-26 18:44:58 -08:00
|
|
|
#[derive(PartialEq, Eq, Hash, Copy, Clone, PartialOrd, Ord, Debug)]
|
2021-02-20 09:01:38 -08:00
|
|
|
pub enum GitConfigError<'a> {
|
2021-02-20 10:49:01 -08:00
|
|
|
/// The requested section does not exist.
|
2021-02-27 19:18:44 -08:00
|
|
|
SectionDoesNotExist(&'a str),
|
2021-02-20 10:49:01 -08:00
|
|
|
/// The requested subsection does not exist.
|
2021-02-27 19:18:44 -08:00
|
|
|
SubSectionDoesNotExist(Option<&'a str>),
|
2021-02-20 10:49:01 -08:00
|
|
|
/// The key does not exist in the requested section.
|
2021-02-27 19:18:44 -08:00
|
|
|
KeyDoesNotExist(&'a str),
|
2021-02-26 20:33:38 -08:00
|
|
|
FailedConversion,
|
2021-02-20 09:01:38 -08:00
|
|
|
}
|
|
|
|
|
2021-02-26 20:33:38 -08:00
|
|
|
impl Display for GitConfigError<'_> {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
// Todo, try parse as utf8 first for better looking errors
|
2021-02-27 19:18:44 -08:00
|
|
|
Self::SectionDoesNotExist(s) => write!(f, "Section '{}' does not exist.", s),
|
|
|
|
Self::SubSectionDoesNotExist(s) => match s {
|
|
|
|
Some(s) => write!(f, "Subsection '{}' does not exist.", s),
|
|
|
|
None => write!(f, "Top level section does not exist."),
|
|
|
|
},
|
2021-02-26 20:33:38 -08:00
|
|
|
Self::KeyDoesNotExist(k) => write!(f, "Name '{}' does not exist.", k),
|
|
|
|
Self::FailedConversion => write!(f, "Failed to convert to specified type."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for GitConfigError<'_> {}
|
|
|
|
|
2021-02-21 11:43:41 -08:00
|
|
|
/// The section ID is a monotonically increasing ID used to refer to sections.
|
|
|
|
/// This value does not imply any ordering between sections, as new sections
|
|
|
|
/// with higher section IDs may be in between lower ID sections.
|
2021-02-20 17:41:34 -08:00
|
|
|
///
|
|
|
|
/// We need to use a section id because `git-config` permits sections with
|
|
|
|
/// identical names. As a result, we can't simply use the section name as a key
|
|
|
|
/// in a map.
|
|
|
|
///
|
|
|
|
/// This id guaranteed to be unique, but not guaranteed to be compact. In other
|
|
|
|
/// words, it's possible that a section may have an ID of 3 but the next section
|
|
|
|
/// has an ID of 5.
|
|
|
|
#[derive(PartialEq, Eq, Hash, Copy, Clone, PartialOrd, Ord, Debug)]
|
|
|
|
struct SectionId(usize);
|
|
|
|
|
2021-02-26 18:44:58 -08:00
|
|
|
#[derive(PartialEq, Eq, Clone, Debug)]
|
2021-02-20 17:41:34 -08:00
|
|
|
enum LookupTreeNode<'a> {
|
|
|
|
Terminal(Vec<SectionId>),
|
2021-02-27 19:18:44 -08:00
|
|
|
NonTerminal(HashMap<Cow<'a, str>, Vec<SectionId>>),
|
2021-02-20 17:41:34 -08:00
|
|
|
}
|
2021-02-26 18:44:58 -08:00
|
|
|
/// High level `git-config` reader and writer.
|
2021-02-27 08:13:20 -08:00
|
|
|
///
|
|
|
|
/// Internally, this uses various acceleration data structures to improve
|
|
|
|
/// performance.
|
|
|
|
///
|
|
|
|
/// # Multivar behavior
|
|
|
|
///
|
|
|
|
/// `git` is flexible enough to allow users to set a key multiple times in
|
|
|
|
/// any number of identically named sections. When this is the case, the key
|
|
|
|
/// is known as a "multivar". In this case, `get_raw_value` follows the
|
|
|
|
/// "last one wins" approach that `git-config` internally uses for multivar
|
|
|
|
/// resolution.
|
|
|
|
///
|
|
|
|
/// Concretely, the following config has a multivar, `a`, with the values
|
|
|
|
/// of `b`, `c`, and `d`, while `e` is a single variable with the value
|
|
|
|
/// `f g h`.
|
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// [core]
|
|
|
|
/// a = b
|
|
|
|
/// a = c
|
|
|
|
/// [core]
|
|
|
|
/// a = d
|
|
|
|
/// e = f g h
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Calling methods that only one fetch value (such as [`get_raw_value`]) to
|
|
|
|
/// fetch `a` with the above config will return `d`, since the last valid config
|
|
|
|
/// value is `a = d`:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use git_config::config::GitConfig;
|
|
|
|
/// # use std::borrow::Cow;
|
|
|
|
/// # use std::convert::TryFrom;
|
|
|
|
/// # let git_config = GitConfig::try_from("[core]a=b\n[core]\na=c\na=d").unwrap();
|
2021-02-27 19:18:44 -08:00
|
|
|
/// assert_eq!(git_config.get_raw_value("core", None, "a"), Ok(&Cow::Borrowed("d".as_bytes())));
|
2021-02-27 08:13:20 -08:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Consider the `multi` variants of the methods instead, if you want to work
|
|
|
|
/// with all values instead.
|
|
|
|
///
|
2021-02-27 19:18:44 -08:00
|
|
|
/// [`get_raw_value`]: Self::get_raw_value
|
2021-02-26 18:44:58 -08:00
|
|
|
#[derive(PartialEq, Eq, Clone, Debug)]
|
|
|
|
pub struct GitConfig<'a> {
|
|
|
|
/// The list of events that occur before an actual section. Since a
|
|
|
|
/// `git-config` file prohibits global values, this vec is limited to only
|
|
|
|
/// comment, newline, and whitespace events.
|
|
|
|
front_matter_events: Vec<Event<'a>>,
|
2021-02-27 19:18:44 -08:00
|
|
|
section_lookup_tree: HashMap<Cow<'a, str>, Vec<LookupTreeNode<'a>>>,
|
2021-02-26 18:44:58 -08:00
|
|
|
/// SectionId to section mapping. The value of this HashMap contains actual
|
|
|
|
/// events
|
|
|
|
sections: HashMap<SectionId, Vec<Event<'a>>>,
|
|
|
|
section_headers: HashMap<SectionId, ParsedSectionHeader<'a>>,
|
|
|
|
section_id_counter: usize,
|
|
|
|
section_order: VecDeque<SectionId>,
|
|
|
|
}
|
2021-02-20 17:41:34 -08:00
|
|
|
|
2021-02-18 21:12:59 -08:00
|
|
|
impl<'a> GitConfig<'a> {
|
2021-02-20 09:01:38 -08:00
|
|
|
fn push_section(
|
|
|
|
&mut self,
|
2021-02-27 19:18:44 -08:00
|
|
|
current_section_name: Option<Cow<'a, str>>,
|
|
|
|
current_subsection_name: Option<Cow<'a, str>>,
|
2021-02-20 09:01:38 -08:00
|
|
|
maybe_section: &mut Option<Vec<Event<'a>>>,
|
|
|
|
) {
|
|
|
|
if let Some(section) = maybe_section.take() {
|
2021-02-21 11:43:41 -08:00
|
|
|
let new_section_id = SectionId(self.section_id_counter);
|
2021-02-20 09:01:38 -08:00
|
|
|
self.sections.insert(new_section_id, section);
|
|
|
|
let lookup = self
|
|
|
|
.section_lookup_tree
|
|
|
|
.entry(current_section_name.unwrap())
|
|
|
|
.or_default();
|
|
|
|
|
|
|
|
let mut found_node = false;
|
|
|
|
if let Some(subsection_name) = current_subsection_name {
|
|
|
|
for node in lookup.iter_mut() {
|
|
|
|
if let LookupTreeNode::NonTerminal(subsection) = node {
|
|
|
|
found_node = true;
|
|
|
|
subsection
|
2021-02-21 11:43:41 -08:00
|
|
|
// Despite the clone `push_section` is always called
|
|
|
|
// with a Cow::Borrowed, so this is effectively a
|
|
|
|
// copy.
|
|
|
|
.entry(subsection_name.clone())
|
2021-02-20 09:01:38 -08:00
|
|
|
.or_default()
|
|
|
|
.push(new_section_id);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found_node {
|
|
|
|
let mut map = HashMap::new();
|
2021-02-21 11:43:41 -08:00
|
|
|
map.insert(subsection_name, vec![new_section_id]);
|
2021-02-20 09:01:38 -08:00
|
|
|
lookup.push(LookupTreeNode::NonTerminal(map));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for node in lookup.iter_mut() {
|
|
|
|
if let LookupTreeNode::Terminal(vec) = node {
|
|
|
|
found_node = true;
|
|
|
|
vec.push(new_section_id);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found_node {
|
|
|
|
lookup.push(LookupTreeNode::Terminal(vec![new_section_id]))
|
|
|
|
}
|
|
|
|
}
|
2021-02-21 11:43:41 -08:00
|
|
|
self.section_order.push_back(new_section_id);
|
2021-02-20 09:01:38 -08:00
|
|
|
self.section_id_counter += 1;
|
2021-02-19 21:33:17 -08:00
|
|
|
}
|
2021-02-18 21:12:59 -08:00
|
|
|
}
|
|
|
|
|
2021-02-26 20:33:38 -08:00
|
|
|
/// Returns an interpreted value given a section, an optional subsection and
|
|
|
|
/// key.
|
|
|
|
///
|
|
|
|
/// It's recommended to use one of the values in the [`values`] module as
|
|
|
|
/// the conversion is already implemented, but this function is flexible and
|
|
|
|
/// will accept any type that implements [`TryFrom<&[u8]>`][`TryFrom`].
|
2021-02-20 09:01:38 -08:00
|
|
|
///
|
2021-02-27 08:13:20 -08:00
|
|
|
/// Consider [`Self::get_multi_value`] if you want to get all values of a
|
|
|
|
/// multivar instead.
|
2021-02-20 11:10:23 -08:00
|
|
|
///
|
2021-02-26 20:33:38 -08:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use git_config::config::{GitConfig, GitConfigError};
|
|
|
|
/// # use git_config::values::{Integer, Value, Boolean};
|
|
|
|
/// # use std::borrow::Cow;
|
|
|
|
/// # use std::convert::TryFrom;
|
|
|
|
/// let config = r#"
|
|
|
|
/// [core]
|
|
|
|
/// a = 10k
|
|
|
|
/// c
|
|
|
|
/// "#;
|
|
|
|
/// let git_config = GitConfig::try_from(config).unwrap();
|
|
|
|
/// // You can either use the turbofish to determine the type...
|
2021-02-27 19:18:44 -08:00
|
|
|
/// let a_value = git_config.get_value::<Integer>("core", None, "a")?;
|
2021-02-26 20:33:38 -08:00
|
|
|
/// // ... or explicitly declare the type to avoid the turbofish
|
|
|
|
/// let c_value: Boolean = git_config.get_value("core", None, "c")?;
|
|
|
|
/// # Ok::<(), GitConfigError>(())
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function will return an error if the key is not in the requested
|
|
|
|
/// section and subsection, if the section and subsection do not exist, or
|
|
|
|
/// if there was an issue converting the type into the requested variant.
|
|
|
|
///
|
|
|
|
/// [`values`]: crate::values
|
|
|
|
/// [`TryFrom`]: std::convert::TryFrom
|
2021-02-27 19:18:44 -08:00
|
|
|
pub fn get_value<'b, 'c, T: TryFrom<&'c [u8]>>(
|
2021-02-26 20:33:38 -08:00
|
|
|
&'c self,
|
2021-02-27 19:18:44 -08:00
|
|
|
section_name: &'b str,
|
|
|
|
subsection_name: Option<&'b str>,
|
|
|
|
key: &'b str,
|
2021-02-26 20:33:38 -08:00
|
|
|
) -> Result<T, GitConfigError<'b>> {
|
|
|
|
T::try_from(self.get_raw_value(section_name, subsection_name, key)?)
|
|
|
|
.map_err(|_| GitConfigError::FailedConversion)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_section_id_by_name_and_subname<'b>(
|
|
|
|
&'a self,
|
2021-02-27 19:18:44 -08:00
|
|
|
section_name: &'b str,
|
|
|
|
subsection_name: Option<&'b str>,
|
2021-02-26 20:33:38 -08:00
|
|
|
) -> Result<SectionId, GitConfigError<'b>> {
|
|
|
|
self.get_section_ids_by_name_and_subname(section_name, subsection_name)
|
|
|
|
.map(|vec| {
|
|
|
|
// get_section_ids_by_name_and_subname is guaranteed to return
|
|
|
|
// a non-empty vec, so max can never return empty.
|
|
|
|
*vec.iter().max().unwrap()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an uninterpreted value given a section, an optional subsection
|
|
|
|
/// and key.
|
|
|
|
///
|
|
|
|
/// Consider [`Self::get_raw_multi_value`] if you want to get all values of
|
|
|
|
/// a multivar instead.
|
|
|
|
///
|
2021-02-20 10:49:01 -08:00
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function will return an error if the key is not in the requested
|
2021-02-20 11:10:23 -08:00
|
|
|
/// section and subsection, or if the section and subsection do not exist.
|
2021-02-27 19:18:44 -08:00
|
|
|
pub fn get_raw_value<'b>(
|
2021-02-19 21:33:17 -08:00
|
|
|
&self,
|
2021-02-27 19:18:44 -08:00
|
|
|
section_name: &'b str,
|
|
|
|
subsection_name: Option<&'b str>,
|
|
|
|
key: &'b str,
|
|
|
|
) -> Result<&Cow<'a, [u8]>, GitConfigError<'b>> {
|
|
|
|
let key = key;
|
2021-02-19 21:33:17 -08:00
|
|
|
// Note: cannot wrap around the raw_multi_value method because we need
|
|
|
|
// to guarantee that the highest section id is used (so that we follow
|
|
|
|
// the "last one wins" resolution strategy by `git-config`).
|
2021-02-27 19:18:44 -08:00
|
|
|
let section_id = self.get_section_id_by_name_and_subname(section_name, subsection_name)?;
|
2021-02-19 21:33:17 -08:00
|
|
|
|
|
|
|
// section_id is guaranteed to exist in self.sections, else we have a
|
|
|
|
// violated invariant.
|
|
|
|
let events = self.sections.get(§ion_id).unwrap();
|
|
|
|
let mut found_key = false;
|
2021-02-20 09:01:38 -08:00
|
|
|
let mut latest_value = None;
|
2021-02-19 21:33:17 -08:00
|
|
|
for event in events {
|
|
|
|
match event {
|
|
|
|
Event::Key(event_key) if *event_key == key => found_key = true,
|
2021-02-20 09:01:38 -08:00
|
|
|
Event::Value(v) if found_key => {
|
|
|
|
found_key = false;
|
2021-02-21 11:43:41 -08:00
|
|
|
latest_value = Some(v);
|
2021-02-20 09:01:38 -08:00
|
|
|
}
|
2021-02-19 21:33:17 -08:00
|
|
|
_ => (),
|
2021-02-18 21:12:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-20 09:01:38 -08:00
|
|
|
latest_value.ok_or(GitConfigError::KeyDoesNotExist(key))
|
2021-02-18 21:12:59 -08:00
|
|
|
}
|
|
|
|
|
2021-02-20 17:41:34 -08:00
|
|
|
/// Returns a mutable reference to an uninterpreted value given a section,
|
|
|
|
/// an optional subsection and key.
|
|
|
|
///
|
2021-02-21 16:08:27 -08:00
|
|
|
/// Consider [`Self::get_raw_multi_value_mut`] if you want to get mutable
|
|
|
|
/// references to all values of a multivar instead.
|
2021-02-20 17:41:34 -08:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function will return an error if the key is not in the requested
|
|
|
|
/// section and subsection, or if the section and subsection do not exist.
|
2021-02-27 19:18:44 -08:00
|
|
|
pub fn get_raw_value_mut<'b>(
|
2021-02-20 17:41:34 -08:00
|
|
|
&mut self,
|
2021-02-27 19:18:44 -08:00
|
|
|
section_name: &'b str,
|
|
|
|
subsection_name: Option<&'b str>,
|
|
|
|
key: &'b str,
|
|
|
|
) -> Result<&mut Cow<'a, [u8]>, GitConfigError<'b>> {
|
|
|
|
let key = key;
|
2021-02-20 17:41:34 -08:00
|
|
|
// Note: cannot wrap around the raw_multi_value method because we need
|
|
|
|
// to guarantee that the highest section id is used (so that we follow
|
|
|
|
// the "last one wins" resolution strategy by `git-config`).
|
2021-02-27 19:18:44 -08:00
|
|
|
let section_id = self.get_section_id_by_name_and_subname(section_name, subsection_name)?;
|
2021-02-20 17:41:34 -08:00
|
|
|
|
|
|
|
// section_id is guaranteed to exist in self.sections, else we have a
|
|
|
|
// violated invariant.
|
|
|
|
let events = self.sections.get_mut(§ion_id).unwrap();
|
|
|
|
let mut found_key = false;
|
|
|
|
let mut latest_value = None;
|
|
|
|
for event in events {
|
|
|
|
match event {
|
|
|
|
Event::Key(event_key) if *event_key == key => found_key = true,
|
|
|
|
Event::Value(v) if found_key => {
|
|
|
|
found_key = false;
|
|
|
|
latest_value = Some(v);
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
latest_value.ok_or(GitConfigError::KeyDoesNotExist(key))
|
|
|
|
}
|
|
|
|
|
2021-02-20 11:10:23 -08:00
|
|
|
/// Returns all uninterpreted values given a section, an optional subsection
|
|
|
|
/// and key. If you have the following config:
|
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// [core]
|
|
|
|
/// a = b
|
|
|
|
/// [core]
|
|
|
|
/// a = c
|
|
|
|
/// a = d
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Attempting to get all values of `a` yields the following:
|
|
|
|
///
|
|
|
|
/// ```
|
2021-02-24 12:47:35 -08:00
|
|
|
/// # use git_config::config::GitConfig;
|
2021-02-21 11:43:41 -08:00
|
|
|
/// # use std::borrow::Cow;
|
2021-02-26 20:33:38 -08:00
|
|
|
/// # use std::convert::TryFrom;
|
|
|
|
/// # let git_config = GitConfig::try_from("[core]a=b\n[core]\na=c\na=d").unwrap();
|
2021-02-21 11:43:41 -08:00
|
|
|
/// assert_eq!(
|
|
|
|
/// git_config.get_raw_multi_value("core", None, "a"),
|
2021-02-27 19:18:44 -08:00
|
|
|
/// Ok(vec![
|
|
|
|
/// &Cow::<[u8]>::Borrowed(b"b"),
|
|
|
|
/// &Cow::<[u8]>::Borrowed(b"c"),
|
|
|
|
/// &Cow::<[u8]>::Borrowed(b"d"),
|
|
|
|
/// ]),
|
2021-02-21 11:43:41 -08:00
|
|
|
/// );
|
2021-02-20 11:10:23 -08:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Consider [`Self::get_raw_value`] if you want to get the resolved single
|
|
|
|
/// value for a given key, if your key does not support multi-valued values.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function will return an error if the key is not in any requested
|
|
|
|
/// section and subsection, or if no instance of the section and subsections
|
|
|
|
/// exist.
|
2021-02-27 19:18:44 -08:00
|
|
|
pub fn get_raw_multi_value<'b>(
|
2021-02-19 21:33:17 -08:00
|
|
|
&'a self,
|
2021-02-27 19:18:44 -08:00
|
|
|
section_name: &'b str,
|
|
|
|
subsection_name: Option<&'b str>,
|
|
|
|
key: &'b str,
|
|
|
|
) -> Result<Vec<&Cow<'a, [u8]>>, GitConfigError<'b>> {
|
|
|
|
let key = key;
|
2021-02-20 17:41:34 -08:00
|
|
|
let mut values = vec![];
|
2021-02-27 19:18:44 -08:00
|
|
|
for section_id in self.get_section_ids_by_name_and_subname(section_name, subsection_name)? {
|
2021-02-20 17:41:34 -08:00
|
|
|
let mut found_key = false;
|
|
|
|
// section_id is guaranteed to exist in self.sections, else we
|
|
|
|
// have a violated invariant.
|
|
|
|
for event in self.sections.get(section_id).unwrap() {
|
|
|
|
match event {
|
|
|
|
Event::Key(event_key) if *event_key == key => found_key = true,
|
|
|
|
Event::Value(v) if found_key => {
|
2021-02-21 11:43:41 -08:00
|
|
|
values.push(v);
|
2021-02-20 17:41:34 -08:00
|
|
|
found_key = false;
|
2021-02-19 21:33:17 -08:00
|
|
|
}
|
2021-02-20 17:41:34 -08:00
|
|
|
_ => (),
|
2021-02-19 21:33:17 -08:00
|
|
|
}
|
2021-02-20 17:41:34 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if values.is_empty() {
|
|
|
|
Err(GitConfigError::KeyDoesNotExist(key))
|
|
|
|
} else {
|
|
|
|
Ok(values)
|
|
|
|
}
|
|
|
|
}
|
2021-02-18 21:12:59 -08:00
|
|
|
|
2021-02-20 17:41:34 -08:00
|
|
|
/// Returns mutable references to all uninterpreted values given a section,
|
|
|
|
/// an optional subsection and key. If you have the following config:
|
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// [core]
|
|
|
|
/// a = b
|
|
|
|
/// [core]
|
|
|
|
/// a = c
|
|
|
|
/// a = d
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Attempting to get all values of `a` yields the following:
|
|
|
|
///
|
|
|
|
/// ```
|
2021-02-24 12:47:35 -08:00
|
|
|
/// # use git_config::config::{GitConfig, GitConfigError};
|
2021-02-21 11:43:41 -08:00
|
|
|
/// # use std::borrow::Cow;
|
2021-02-26 20:33:38 -08:00
|
|
|
/// # use std::convert::TryFrom;
|
|
|
|
/// # let mut git_config = GitConfig::try_from("[core]a=b\n[core]\na=c\na=d").unwrap();
|
2021-02-23 16:47:24 -08:00
|
|
|
/// assert_eq!(
|
|
|
|
/// git_config.get_raw_multi_value("core", None, "a")?,
|
|
|
|
/// vec![
|
2021-02-27 19:18:44 -08:00
|
|
|
/// &Cow::Borrowed(b"b"),
|
|
|
|
/// &Cow::Borrowed(b"c"),
|
|
|
|
/// &Cow::Borrowed(b"d")
|
2021-02-23 16:47:24 -08:00
|
|
|
/// ]
|
|
|
|
/// );
|
2021-02-20 17:41:34 -08:00
|
|
|
/// for value in git_config.get_raw_multi_value_mut("core", None, "a")? {
|
2021-02-27 19:18:44 -08:00
|
|
|
/// *value = Cow::Borrowed(b"g");
|
2021-02-20 17:41:34 -08:00
|
|
|
///}
|
2021-02-21 11:43:41 -08:00
|
|
|
/// assert_eq!(
|
|
|
|
/// git_config.get_raw_multi_value("core", None, "a")?,
|
2021-02-23 16:47:24 -08:00
|
|
|
/// vec![
|
2021-02-27 19:18:44 -08:00
|
|
|
/// &Cow::Borrowed(b"g"),
|
|
|
|
/// &Cow::Borrowed(b"g"),
|
|
|
|
/// &Cow::Borrowed(b"g")
|
2021-02-23 16:47:24 -08:00
|
|
|
/// ],
|
2021-02-21 11:43:41 -08:00
|
|
|
/// );
|
2021-02-20 17:41:34 -08:00
|
|
|
/// # Ok::<(), GitConfigError>(())
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Consider [`Self::get_raw_value`] if you want to get the resolved single
|
|
|
|
/// value for a given key, if your key does not support multi-valued values.
|
|
|
|
///
|
2021-02-21 11:43:41 -08:00
|
|
|
/// Note that this operation is relatively expensive, requiring a full
|
|
|
|
/// traversal of the config.
|
|
|
|
///
|
2021-02-20 17:41:34 -08:00
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function will return an error if the key is not in any requested
|
|
|
|
/// section and subsection, or if no instance of the section and subsections
|
|
|
|
/// exist.
|
2021-02-27 19:18:44 -08:00
|
|
|
pub fn get_raw_multi_value_mut<'b>(
|
2021-02-20 17:41:34 -08:00
|
|
|
&mut self,
|
2021-02-27 19:18:44 -08:00
|
|
|
section_name: &'b str,
|
|
|
|
subsection_name: Option<&'b str>,
|
|
|
|
key: &'b str,
|
|
|
|
) -> Result<Vec<&mut Cow<'a, [u8]>>, GitConfigError<'b>> {
|
|
|
|
let key = key;
|
2021-02-20 17:41:34 -08:00
|
|
|
let section_ids = self
|
2021-02-27 19:18:44 -08:00
|
|
|
.get_section_ids_by_name_and_subname(section_name, subsection_name)?
|
2021-02-20 17:41:34 -08:00
|
|
|
.to_vec();
|
|
|
|
let mut found_key = false;
|
2021-02-27 19:18:44 -08:00
|
|
|
let values: Vec<&mut Cow<'a, [u8]>> = self
|
2021-02-20 17:41:34 -08:00
|
|
|
.sections
|
|
|
|
.iter_mut()
|
|
|
|
.filter_map(|(k, v)| {
|
|
|
|
if section_ids.contains(k) {
|
|
|
|
let mut values = vec![];
|
|
|
|
for event in v {
|
|
|
|
match event {
|
|
|
|
Event::Key(event_key) if *event_key == key => found_key = true,
|
|
|
|
Event::Value(v) if found_key => {
|
|
|
|
values.push(v);
|
|
|
|
found_key = false;
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(values)
|
2021-02-20 10:49:01 -08:00
|
|
|
} else {
|
2021-02-20 17:41:34 -08:00
|
|
|
None
|
2021-02-20 10:49:01 -08:00
|
|
|
}
|
2021-02-19 21:33:17 -08:00
|
|
|
})
|
2021-02-20 10:49:01 -08:00
|
|
|
.flatten()
|
2021-02-20 17:41:34 -08:00
|
|
|
.collect();
|
|
|
|
|
2021-02-19 21:33:17 -08:00
|
|
|
if values.is_empty() {
|
|
|
|
Err(GitConfigError::KeyDoesNotExist(key))
|
|
|
|
} else {
|
|
|
|
Ok(values)
|
|
|
|
}
|
2021-02-18 21:12:59 -08:00
|
|
|
}
|
|
|
|
|
2021-02-19 21:33:17 -08:00
|
|
|
fn get_section_ids_by_name_and_subname<'b>(
|
|
|
|
&'a self,
|
2021-02-27 19:18:44 -08:00
|
|
|
section_name: &'b str,
|
|
|
|
subsection_name: Option<&'b str>,
|
2021-02-20 10:49:01 -08:00
|
|
|
) -> Result<&[SectionId], GitConfigError<'b>> {
|
|
|
|
let section_ids = self
|
|
|
|
.section_lookup_tree
|
|
|
|
.get(section_name)
|
|
|
|
.ok_or(GitConfigError::SectionDoesNotExist(section_name))?;
|
|
|
|
let mut maybe_ids = None;
|
|
|
|
// Don't simplify if and matches here -- the for loop currently needs
|
|
|
|
// `n + 1` checks, while the if and matches will result in the for loop
|
|
|
|
// needing `2n` checks.
|
2021-02-19 21:33:17 -08:00
|
|
|
if let Some(subsect_name) = subsection_name {
|
|
|
|
for node in section_ids {
|
|
|
|
if let LookupTreeNode::NonTerminal(subsection_lookup) = node {
|
2021-02-24 09:11:18 -08:00
|
|
|
maybe_ids = subsection_lookup.get(subsect_name);
|
2021-02-19 21:33:17 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for node in section_ids {
|
|
|
|
if let LookupTreeNode::Terminal(subsection_lookup) = node {
|
2021-02-20 10:49:01 -08:00
|
|
|
maybe_ids = Some(subsection_lookup);
|
2021-02-19 21:33:17 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-20 10:49:01 -08:00
|
|
|
maybe_ids
|
|
|
|
.map(Vec::as_slice)
|
|
|
|
.ok_or(GitConfigError::SubSectionDoesNotExist(subsection_name))
|
2021-02-18 21:12:59 -08:00
|
|
|
}
|
2021-02-21 11:43:41 -08:00
|
|
|
|
2021-02-27 19:18:44 -08:00
|
|
|
pub fn set_raw_value<'b>(
|
2021-02-21 11:43:41 -08:00
|
|
|
&mut self,
|
2021-02-27 19:18:44 -08:00
|
|
|
section_name: &'b str,
|
|
|
|
subsection_name: Option<&'b str>,
|
|
|
|
key: &'b str,
|
|
|
|
new_value: impl Into<Cow<'a, [u8]>>,
|
2021-02-21 11:43:41 -08:00
|
|
|
) -> Result<(), GitConfigError<'b>> {
|
|
|
|
let value = self.get_raw_value_mut(section_name, subsection_name, key)?;
|
|
|
|
*value = new_value.into();
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-02-21 18:16:08 -08:00
|
|
|
|
|
|
|
/// Sets a multivar in a given section, optional subsection, and key value.
|
|
|
|
///
|
|
|
|
/// This internally zips together the new values and the existing values.
|
|
|
|
/// As a result, if more new values are provided than the current amount of
|
|
|
|
/// multivars, then the latter values are not applied. If there are less
|
|
|
|
/// new values than old ones then the remaining old values are unmodified.
|
|
|
|
///
|
|
|
|
/// If you need finer control over which values of the multivar are set,
|
|
|
|
/// consider using [`get_raw_multi_value_mut`].
|
|
|
|
///
|
|
|
|
/// todo: examples and errors
|
|
|
|
///
|
|
|
|
/// [`get_raw_multi_value_mut`]: Self::get_raw_multi_value_mut
|
2021-02-27 19:18:44 -08:00
|
|
|
pub fn set_raw_multi_value<'b>(
|
2021-02-21 18:16:08 -08:00
|
|
|
&mut self,
|
2021-02-27 19:18:44 -08:00
|
|
|
section_name: &'b str,
|
|
|
|
subsection_name: Option<&'b str>,
|
|
|
|
key: &'b str,
|
|
|
|
new_values: Vec<Cow<'a, [u8]>>,
|
2021-02-21 18:16:08 -08:00
|
|
|
) -> Result<(), GitConfigError<'b>> {
|
|
|
|
let values = self.get_raw_multi_value_mut(section_name, subsection_name, key)?;
|
|
|
|
for (old, new) in values.into_iter().zip(new_values) {
|
|
|
|
*old = new;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 18:44:58 -08:00
|
|
|
impl<'a> TryFrom<&'a str> for GitConfig<'a> {
|
|
|
|
type Error = ParserError<'a>;
|
|
|
|
|
|
|
|
/// Convenience constructor. Attempts to parse the provided string into a
|
|
|
|
/// [`GitConfig`]. See [`parse_from_str`] for more information.
|
|
|
|
///
|
|
|
|
/// [`parse_from_str`]: crate::parser::parse_from_str
|
|
|
|
fn try_from(s: &'a str) -> Result<Self, Self::Error> {
|
|
|
|
parse_from_bytes(s.as_bytes()).map(Self::from)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-27 19:18:44 -08:00
|
|
|
impl<'a> TryFrom<&'a [u8]> for GitConfig<'a> {
|
2021-02-26 18:44:58 -08:00
|
|
|
type Error = ParserError<'a>;
|
|
|
|
|
|
|
|
/// Convenience constructor. Attempts to parse the provided byte string into
|
|
|
|
//// a [`GitConfig`]. See [`parse_from_bytes`] for more information.
|
|
|
|
///
|
|
|
|
/// [`parse_from_bytes`]: crate::parser::parse_from_bytes
|
2021-02-27 19:18:44 -08:00
|
|
|
fn try_from(value: &'a [u8]) -> Result<Self, Self::Error> {
|
2021-02-26 18:44:58 -08:00
|
|
|
parse_from_bytes(value).map(Self::from)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 08:30:48 -08:00
|
|
|
impl<'a> From<Parser<'a>> for GitConfig<'a> {
|
2021-02-26 18:44:58 -08:00
|
|
|
fn from(parser: Parser<'a>) -> Self {
|
|
|
|
let mut new_self = Self {
|
|
|
|
front_matter_events: vec![],
|
|
|
|
sections: HashMap::new(),
|
|
|
|
section_lookup_tree: HashMap::new(),
|
|
|
|
section_headers: HashMap::new(),
|
|
|
|
section_id_counter: 0,
|
|
|
|
section_order: VecDeque::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Current section that we're building
|
2021-02-27 19:18:44 -08:00
|
|
|
let mut current_section_name: Option<Cow<'a, str>> = None;
|
|
|
|
let mut current_subsection_name: Option<Cow<'a, str>> = None;
|
2021-02-26 18:44:58 -08:00
|
|
|
let mut maybe_section: Option<Vec<Event<'a>>> = None;
|
|
|
|
|
|
|
|
for event in parser.into_iter() {
|
|
|
|
match event {
|
|
|
|
Event::SectionHeader(header) => {
|
|
|
|
new_self.push_section(
|
|
|
|
current_section_name,
|
|
|
|
current_subsection_name,
|
|
|
|
&mut maybe_section,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Initialize new section
|
|
|
|
// We need to store the new, current id counter, so don't
|
|
|
|
// use new_section_id here and use the already incremented
|
|
|
|
// section id value.
|
|
|
|
new_self
|
|
|
|
.section_headers
|
|
|
|
.insert(SectionId(new_self.section_id_counter), header.clone());
|
|
|
|
let (name, subname) = (header.name, header.subsection_name);
|
|
|
|
maybe_section = Some(vec![]);
|
|
|
|
current_section_name = Some(name);
|
|
|
|
current_subsection_name = subname;
|
|
|
|
}
|
|
|
|
e @ Event::Key(_)
|
|
|
|
| e @ Event::Value(_)
|
|
|
|
| e @ Event::ValueNotDone(_)
|
|
|
|
| e @ Event::ValueDone(_)
|
|
|
|
| e @ Event::KeyValueSeparator => maybe_section
|
|
|
|
.as_mut()
|
|
|
|
.expect("Got a section-only event before a section")
|
|
|
|
.push(e),
|
|
|
|
e @ Event::Comment(_) | e @ Event::Newline(_) | e @ Event::Whitespace(_) => {
|
|
|
|
match maybe_section {
|
|
|
|
Some(ref mut section) => section.push(e),
|
|
|
|
None => new_self.front_matter_events.push(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The last section doesn't get pushed since we only push if there's a
|
|
|
|
// new section header, so we need to call push one more time.
|
|
|
|
new_self.push_section(
|
|
|
|
current_section_name,
|
|
|
|
current_subsection_name,
|
|
|
|
&mut maybe_section,
|
|
|
|
);
|
|
|
|
|
|
|
|
new_self
|
2021-02-23 08:30:48 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-21 18:16:08 -08:00
|
|
|
impl Display for GitConfig<'_> {
|
2021-02-27 19:18:44 -08:00
|
|
|
/// Note that this is a best-effort attempt at printing a `GitConfig`. If
|
|
|
|
/// there are non UTF-8 values in your config, this will _NOT_ render as
|
|
|
|
/// read.
|
2021-02-21 18:16:08 -08:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
for front_matter in &self.front_matter_events {
|
|
|
|
front_matter.fmt(f)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
for section_id in &self.section_order {
|
|
|
|
self.section_headers.get(section_id).unwrap().fmt(f)?;
|
|
|
|
for event in self.sections.get(section_id).unwrap() {
|
2021-02-23 08:30:48 -08:00
|
|
|
event.fmt(f)?;
|
2021-02-21 18:16:08 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-02-18 21:12:59 -08:00
|
|
|
}
|
|
|
|
|
2021-02-23 08:30:48 -08:00
|
|
|
// todo impl serialize
|
|
|
|
|
2021-02-20 09:01:38 -08:00
|
|
|
#[cfg(test)]
|
2021-02-20 10:49:01 -08:00
|
|
|
mod from_parser {
|
|
|
|
use super::*;
|
2021-02-23 16:47:24 -08:00
|
|
|
use crate::test_util::*;
|
2021-02-20 09:01:38 -08:00
|
|
|
|
2021-02-20 10:49:01 -08:00
|
|
|
#[test]
|
|
|
|
fn parse_empty() {
|
2021-02-26 18:44:58 -08:00
|
|
|
let config = GitConfig::try_from("").unwrap();
|
2021-02-21 18:16:08 -08:00
|
|
|
assert!(config.section_headers.is_empty());
|
2021-02-20 10:49:01 -08:00
|
|
|
assert_eq!(config.section_id_counter, 0);
|
|
|
|
assert!(config.section_lookup_tree.is_empty());
|
|
|
|
assert!(config.sections.is_empty());
|
2021-02-21 11:43:41 -08:00
|
|
|
assert!(config.section_order.is_empty());
|
2021-02-20 10:49:01 -08:00
|
|
|
}
|
2021-02-20 09:01:38 -08:00
|
|
|
|
2021-02-20 10:49:01 -08:00
|
|
|
#[test]
|
|
|
|
fn parse_single_section() {
|
2021-02-26 18:44:58 -08:00
|
|
|
let mut config = GitConfig::try_from("[core]\na=b\nc=d").unwrap();
|
2021-02-20 10:49:01 -08:00
|
|
|
let expected_separators = {
|
|
|
|
let mut map = HashMap::new();
|
2021-02-23 16:47:24 -08:00
|
|
|
map.insert(SectionId(0), section_header("core", None));
|
2021-02-20 10:49:01 -08:00
|
|
|
map
|
|
|
|
};
|
2021-02-21 18:16:08 -08:00
|
|
|
assert_eq!(config.section_headers, expected_separators);
|
2021-02-20 10:49:01 -08:00
|
|
|
assert_eq!(config.section_id_counter, 1);
|
|
|
|
let expected_lookup_tree = {
|
|
|
|
let mut tree = HashMap::new();
|
2021-02-21 11:43:41 -08:00
|
|
|
tree.insert(
|
2021-02-27 19:18:44 -08:00
|
|
|
Cow::Borrowed("core"),
|
2021-02-21 11:43:41 -08:00
|
|
|
vec![LookupTreeNode::Terminal(vec![SectionId(0)])],
|
|
|
|
);
|
2021-02-20 10:49:01 -08:00
|
|
|
tree
|
|
|
|
};
|
|
|
|
assert_eq!(config.section_lookup_tree, expected_lookup_tree);
|
|
|
|
let expected_sections = {
|
|
|
|
let mut sections = HashMap::new();
|
|
|
|
sections.insert(
|
|
|
|
SectionId(0),
|
|
|
|
vec![
|
2021-02-23 16:47:24 -08:00
|
|
|
newline_event(),
|
|
|
|
name_event("a"),
|
2021-02-23 08:30:48 -08:00
|
|
|
Event::KeyValueSeparator,
|
2021-02-23 16:47:24 -08:00
|
|
|
value_event("b"),
|
|
|
|
newline_event(),
|
|
|
|
name_event("c"),
|
2021-02-23 08:30:48 -08:00
|
|
|
Event::KeyValueSeparator,
|
2021-02-23 16:47:24 -08:00
|
|
|
value_event("d"),
|
2021-02-20 10:49:01 -08:00
|
|
|
],
|
|
|
|
);
|
|
|
|
sections
|
|
|
|
};
|
|
|
|
assert_eq!(config.sections, expected_sections);
|
2021-02-21 11:43:41 -08:00
|
|
|
assert_eq!(config.section_order.make_contiguous(), &[SectionId(0)]);
|
2021-02-20 10:49:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_single_subsection() {
|
2021-02-26 18:44:58 -08:00
|
|
|
let mut config = GitConfig::try_from("[core.subsec]\na=b\nc=d").unwrap();
|
2021-02-20 10:49:01 -08:00
|
|
|
let expected_separators = {
|
|
|
|
let mut map = HashMap::new();
|
2021-02-23 16:47:24 -08:00
|
|
|
map.insert(SectionId(0), section_header("core", (".", "subsec")));
|
2021-02-20 10:49:01 -08:00
|
|
|
map
|
|
|
|
};
|
2021-02-21 18:16:08 -08:00
|
|
|
assert_eq!(config.section_headers, expected_separators);
|
2021-02-20 10:49:01 -08:00
|
|
|
assert_eq!(config.section_id_counter, 1);
|
|
|
|
let expected_lookup_tree = {
|
|
|
|
let mut tree = HashMap::new();
|
|
|
|
let mut inner_tree = HashMap::new();
|
2021-02-27 19:18:44 -08:00
|
|
|
inner_tree.insert(Cow::Borrowed("subsec"), vec![SectionId(0)]);
|
2021-02-21 11:43:41 -08:00
|
|
|
tree.insert(
|
2021-02-27 19:18:44 -08:00
|
|
|
Cow::Borrowed("core"),
|
2021-02-21 11:43:41 -08:00
|
|
|
vec![LookupTreeNode::NonTerminal(inner_tree)],
|
|
|
|
);
|
2021-02-20 10:49:01 -08:00
|
|
|
tree
|
|
|
|
};
|
|
|
|
assert_eq!(config.section_lookup_tree, expected_lookup_tree);
|
|
|
|
let expected_sections = {
|
|
|
|
let mut sections = HashMap::new();
|
|
|
|
sections.insert(
|
|
|
|
SectionId(0),
|
|
|
|
vec![
|
2021-02-23 16:47:24 -08:00
|
|
|
newline_event(),
|
|
|
|
name_event("a"),
|
2021-02-23 08:30:48 -08:00
|
|
|
Event::KeyValueSeparator,
|
2021-02-23 16:47:24 -08:00
|
|
|
value_event("b"),
|
|
|
|
newline_event(),
|
|
|
|
name_event("c"),
|
2021-02-23 08:30:48 -08:00
|
|
|
Event::KeyValueSeparator,
|
2021-02-23 16:47:24 -08:00
|
|
|
value_event("d"),
|
2021-02-20 10:49:01 -08:00
|
|
|
],
|
|
|
|
);
|
|
|
|
sections
|
|
|
|
};
|
|
|
|
assert_eq!(config.sections, expected_sections);
|
2021-02-21 11:43:41 -08:00
|
|
|
assert_eq!(config.section_order.make_contiguous(), &[SectionId(0)]);
|
2021-02-20 10:49:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_multiple_sections() {
|
2021-02-26 18:44:58 -08:00
|
|
|
let mut config = GitConfig::try_from("[core]\na=b\nc=d\n[other]e=f").unwrap();
|
2021-02-20 10:49:01 -08:00
|
|
|
let expected_separators = {
|
|
|
|
let mut map = HashMap::new();
|
2021-02-23 16:47:24 -08:00
|
|
|
map.insert(SectionId(0), section_header("core", None));
|
|
|
|
map.insert(SectionId(1), section_header("other", None));
|
2021-02-20 10:49:01 -08:00
|
|
|
map
|
|
|
|
};
|
2021-02-21 18:16:08 -08:00
|
|
|
assert_eq!(config.section_headers, expected_separators);
|
2021-02-20 10:49:01 -08:00
|
|
|
assert_eq!(config.section_id_counter, 2);
|
|
|
|
let expected_lookup_tree = {
|
|
|
|
let mut tree = HashMap::new();
|
2021-02-21 11:43:41 -08:00
|
|
|
tree.insert(
|
2021-02-27 19:18:44 -08:00
|
|
|
Cow::Borrowed("core"),
|
2021-02-21 11:43:41 -08:00
|
|
|
vec![LookupTreeNode::Terminal(vec![SectionId(0)])],
|
|
|
|
);
|
|
|
|
tree.insert(
|
2021-02-27 19:18:44 -08:00
|
|
|
Cow::Borrowed("other"),
|
2021-02-21 11:43:41 -08:00
|
|
|
vec![LookupTreeNode::Terminal(vec![SectionId(1)])],
|
|
|
|
);
|
2021-02-20 10:49:01 -08:00
|
|
|
tree
|
|
|
|
};
|
|
|
|
assert_eq!(config.section_lookup_tree, expected_lookup_tree);
|
|
|
|
let expected_sections = {
|
|
|
|
let mut sections = HashMap::new();
|
|
|
|
sections.insert(
|
|
|
|
SectionId(0),
|
|
|
|
vec![
|
2021-02-23 16:47:24 -08:00
|
|
|
newline_event(),
|
|
|
|
name_event("a"),
|
2021-02-23 08:30:48 -08:00
|
|
|
Event::KeyValueSeparator,
|
2021-02-23 16:47:24 -08:00
|
|
|
value_event("b"),
|
|
|
|
newline_event(),
|
|
|
|
name_event("c"),
|
2021-02-23 08:30:48 -08:00
|
|
|
Event::KeyValueSeparator,
|
2021-02-23 16:47:24 -08:00
|
|
|
value_event("d"),
|
|
|
|
newline_event(),
|
2021-02-21 11:43:41 -08:00
|
|
|
],
|
|
|
|
);
|
|
|
|
sections.insert(
|
|
|
|
SectionId(1),
|
2021-02-23 16:47:24 -08:00
|
|
|
vec![name_event("e"), Event::KeyValueSeparator, value_event("f")],
|
2021-02-20 10:49:01 -08:00
|
|
|
);
|
|
|
|
sections
|
|
|
|
};
|
|
|
|
assert_eq!(config.sections, expected_sections);
|
2021-02-21 11:43:41 -08:00
|
|
|
assert_eq!(
|
|
|
|
config.section_order.make_contiguous(),
|
|
|
|
&[SectionId(0), SectionId(1)]
|
|
|
|
);
|
2021-02-20 10:49:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_multiple_duplicate_sections() {
|
2021-02-26 18:44:58 -08:00
|
|
|
let mut config = GitConfig::try_from("[core]\na=b\nc=d\n[core]e=f").unwrap();
|
2021-02-20 10:49:01 -08:00
|
|
|
let expected_separators = {
|
|
|
|
let mut map = HashMap::new();
|
2021-02-23 16:47:24 -08:00
|
|
|
map.insert(SectionId(0), section_header("core", None));
|
|
|
|
map.insert(SectionId(1), section_header("core", None));
|
2021-02-20 10:49:01 -08:00
|
|
|
map
|
|
|
|
};
|
2021-02-21 18:16:08 -08:00
|
|
|
assert_eq!(config.section_headers, expected_separators);
|
2021-02-20 10:49:01 -08:00
|
|
|
assert_eq!(config.section_id_counter, 2);
|
|
|
|
let expected_lookup_tree = {
|
|
|
|
let mut tree = HashMap::new();
|
|
|
|
tree.insert(
|
2021-02-27 19:18:44 -08:00
|
|
|
Cow::Borrowed("core"),
|
2021-02-20 10:49:01 -08:00
|
|
|
vec![LookupTreeNode::Terminal(vec![SectionId(0), SectionId(1)])],
|
|
|
|
);
|
|
|
|
tree
|
|
|
|
};
|
|
|
|
assert_eq!(config.section_lookup_tree, expected_lookup_tree);
|
|
|
|
let expected_sections = {
|
|
|
|
let mut sections = HashMap::new();
|
|
|
|
sections.insert(
|
|
|
|
SectionId(0),
|
|
|
|
vec![
|
2021-02-23 16:47:24 -08:00
|
|
|
newline_event(),
|
|
|
|
name_event("a"),
|
2021-02-23 08:30:48 -08:00
|
|
|
Event::KeyValueSeparator,
|
2021-02-23 16:47:24 -08:00
|
|
|
value_event("b"),
|
|
|
|
newline_event(),
|
|
|
|
name_event("c"),
|
2021-02-23 08:30:48 -08:00
|
|
|
Event::KeyValueSeparator,
|
2021-02-23 16:47:24 -08:00
|
|
|
value_event("d"),
|
|
|
|
newline_event(),
|
2021-02-21 11:43:41 -08:00
|
|
|
],
|
|
|
|
);
|
|
|
|
sections.insert(
|
|
|
|
SectionId(1),
|
2021-02-23 16:47:24 -08:00
|
|
|
vec![name_event("e"), Event::KeyValueSeparator, value_event("f")],
|
2021-02-20 10:49:01 -08:00
|
|
|
);
|
|
|
|
sections
|
|
|
|
};
|
|
|
|
assert_eq!(config.sections, expected_sections);
|
2021-02-21 11:43:41 -08:00
|
|
|
assert_eq!(
|
|
|
|
config.section_order.make_contiguous(),
|
|
|
|
&[SectionId(0), SectionId(1)]
|
|
|
|
);
|
2021-02-20 10:49:01 -08:00
|
|
|
}
|
|
|
|
}
|
2021-02-20 09:01:38 -08:00
|
|
|
|
2021-02-20 10:49:01 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod get_raw_value {
|
|
|
|
use super::*;
|
2021-02-20 09:01:38 -08:00
|
|
|
|
2021-02-20 10:49:01 -08:00
|
|
|
#[test]
|
|
|
|
fn single_section() {
|
2021-02-26 18:44:58 -08:00
|
|
|
let config = GitConfig::try_from("[core]\na=b\nc=d").unwrap();
|
2021-02-21 11:43:41 -08:00
|
|
|
assert_eq!(
|
|
|
|
config.get_raw_value("core", None, "a"),
|
2021-02-27 19:18:44 -08:00
|
|
|
Ok(&Cow::<[u8]>::Borrowed(b"b"))
|
2021-02-21 11:43:41 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
config.get_raw_value("core", None, "c"),
|
2021-02-27 19:18:44 -08:00
|
|
|
Ok(&Cow::<[u8]>::Borrowed(b"d"))
|
2021-02-21 11:43:41 -08:00
|
|
|
);
|
2021-02-20 10:49:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn last_one_wins_respected_in_section() {
|
2021-02-26 18:44:58 -08:00
|
|
|
let config = GitConfig::try_from("[core]\na=b\na=d").unwrap();
|
2021-02-21 11:43:41 -08:00
|
|
|
assert_eq!(
|
|
|
|
config.get_raw_value("core", None, "a"),
|
2021-02-27 19:18:44 -08:00
|
|
|
Ok(&Cow::<[u8]>::Borrowed(b"d"))
|
2021-02-21 11:43:41 -08:00
|
|
|
);
|
2021-02-20 10:49:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn last_one_wins_respected_across_section() {
|
2021-02-26 18:44:58 -08:00
|
|
|
let config = GitConfig::try_from("[core]\na=b\n[core]\na=d").unwrap();
|
2021-02-21 11:43:41 -08:00
|
|
|
assert_eq!(
|
|
|
|
config.get_raw_value("core", None, "a"),
|
2021-02-27 19:18:44 -08:00
|
|
|
Ok(&Cow::<[u8]>::Borrowed(b"d"))
|
2021-02-21 11:43:41 -08:00
|
|
|
);
|
2021-02-20 10:49:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn section_not_found() {
|
2021-02-26 18:44:58 -08:00
|
|
|
let config = GitConfig::try_from("[core]\na=b\nc=d").unwrap();
|
2021-02-20 10:49:01 -08:00
|
|
|
assert_eq!(
|
|
|
|
config.get_raw_value("foo", None, "a"),
|
2021-02-27 19:18:44 -08:00
|
|
|
Err(GitConfigError::SectionDoesNotExist("foo"))
|
2021-02-20 10:49:01 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn subsection_not_found() {
|
2021-02-26 18:44:58 -08:00
|
|
|
let config = GitConfig::try_from("[core]\na=b\nc=d").unwrap();
|
2021-02-20 10:49:01 -08:00
|
|
|
assert_eq!(
|
|
|
|
config.get_raw_value("core", Some("a"), "a"),
|
2021-02-27 19:18:44 -08:00
|
|
|
Err(GitConfigError::SubSectionDoesNotExist(Some("a")))
|
2021-02-20 10:49:01 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn key_not_found() {
|
2021-02-26 18:44:58 -08:00
|
|
|
let config = GitConfig::try_from("[core]\na=b\nc=d").unwrap();
|
2021-02-20 10:49:01 -08:00
|
|
|
assert_eq!(
|
|
|
|
config.get_raw_value("core", None, "aaaaaa"),
|
2021-02-27 19:18:44 -08:00
|
|
|
Err(GitConfigError::KeyDoesNotExist("aaaaaa"))
|
2021-02-20 10:49:01 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn subsection_must_be_respected() {
|
2021-02-26 18:44:58 -08:00
|
|
|
let config = GitConfig::try_from("[core]a=b\n[core.a]a=c").unwrap();
|
2021-02-21 11:43:41 -08:00
|
|
|
assert_eq!(
|
|
|
|
config.get_raw_value("core", None, "a"),
|
2021-02-27 19:18:44 -08:00
|
|
|
Ok(&Cow::<[u8]>::Borrowed(b"b"))
|
2021-02-21 11:43:41 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
config.get_raw_value("core", Some("a"), "a"),
|
2021-02-27 19:18:44 -08:00
|
|
|
Ok(&Cow::<[u8]>::Borrowed(b"c"))
|
2021-02-21 11:43:41 -08:00
|
|
|
);
|
2021-02-20 10:49:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 20:33:38 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod get_value {
|
|
|
|
use super::*;
|
|
|
|
use crate::values::{Boolean, TrueVariant, Value};
|
|
|
|
use std::error::Error;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_section() -> Result<(), Box<dyn Error>> {
|
|
|
|
let config = GitConfig::try_from("[core]\na=b\nc").unwrap();
|
|
|
|
let first_value: Value = config.get_value("core", None, "a")?;
|
|
|
|
let second_value: Boolean = config.get_value("core", None, "c")?;
|
|
|
|
|
2021-02-27 19:18:44 -08:00
|
|
|
assert_eq!(first_value, Value::Other(Cow::Borrowed(b"b")));
|
2021-02-26 20:33:38 -08:00
|
|
|
assert_eq!(second_value, Boolean::True(TrueVariant::Implicit));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-20 10:49:01 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod get_raw_multi_value {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_value_is_identical_to_single_value_query() {
|
2021-02-26 18:44:58 -08:00
|
|
|
let config = GitConfig::try_from("[core]\na=b\nc=d").unwrap();
|
2021-02-20 10:49:01 -08:00
|
|
|
assert_eq!(
|
|
|
|
vec![config.get_raw_value("core", None, "a").unwrap()],
|
|
|
|
config.get_raw_multi_value("core", None, "a").unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multi_value_in_section() {
|
2021-02-26 18:44:58 -08:00
|
|
|
let config = GitConfig::try_from("[core]\na=b\na=c").unwrap();
|
2021-02-20 10:49:01 -08:00
|
|
|
assert_eq!(
|
|
|
|
config.get_raw_multi_value("core", None, "a").unwrap(),
|
2021-02-27 19:18:44 -08:00
|
|
|
vec![&Cow::Borrowed(b"b"), &Cow::Borrowed(b"c")]
|
2021-02-20 10:49:01 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multi_value_across_sections() {
|
2021-02-26 18:44:58 -08:00
|
|
|
let config = GitConfig::try_from("[core]\na=b\na=c\n[core]a=d").unwrap();
|
2021-02-20 10:49:01 -08:00
|
|
|
assert_eq!(
|
|
|
|
config.get_raw_multi_value("core", None, "a").unwrap(),
|
2021-02-23 16:47:24 -08:00
|
|
|
vec![
|
2021-02-27 19:18:44 -08:00
|
|
|
&Cow::Borrowed(b"b"),
|
|
|
|
&Cow::Borrowed(b"c"),
|
|
|
|
&Cow::Borrowed(b"d")
|
2021-02-23 16:47:24 -08:00
|
|
|
]
|
2021-02-20 10:49:01 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn section_not_found() {
|
2021-02-26 18:44:58 -08:00
|
|
|
let config = GitConfig::try_from("[core]\na=b\nc=d").unwrap();
|
2021-02-20 10:49:01 -08:00
|
|
|
assert_eq!(
|
|
|
|
config.get_raw_multi_value("foo", None, "a"),
|
2021-02-27 19:18:44 -08:00
|
|
|
Err(GitConfigError::SectionDoesNotExist("foo"))
|
2021-02-20 10:49:01 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn subsection_not_found() {
|
2021-02-26 18:44:58 -08:00
|
|
|
let config = GitConfig::try_from("[core]\na=b\nc=d").unwrap();
|
2021-02-20 10:49:01 -08:00
|
|
|
assert_eq!(
|
|
|
|
config.get_raw_multi_value("core", Some("a"), "a"),
|
2021-02-27 19:18:44 -08:00
|
|
|
Err(GitConfigError::SubSectionDoesNotExist(Some("a")))
|
2021-02-20 10:49:01 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn key_not_found() {
|
2021-02-26 18:44:58 -08:00
|
|
|
let config = GitConfig::try_from("[core]\na=b\nc=d").unwrap();
|
2021-02-20 10:49:01 -08:00
|
|
|
assert_eq!(
|
|
|
|
config.get_raw_multi_value("core", None, "aaaaaa"),
|
2021-02-27 19:18:44 -08:00
|
|
|
Err(GitConfigError::KeyDoesNotExist("aaaaaa"))
|
2021-02-20 10:49:01 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn subsection_must_be_respected() {
|
2021-02-26 18:44:58 -08:00
|
|
|
let config = GitConfig::try_from("[core]a=b\n[core.a]a=c").unwrap();
|
2021-02-20 10:49:01 -08:00
|
|
|
assert_eq!(
|
|
|
|
config.get_raw_multi_value("core", None, "a").unwrap(),
|
2021-02-27 19:18:44 -08:00
|
|
|
vec![&Cow::Borrowed(b"b")]
|
2021-02-20 10:49:01 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
config.get_raw_multi_value("core", Some("a"), "a").unwrap(),
|
2021-02-27 19:18:44 -08:00
|
|
|
vec![&Cow::Borrowed(b"c")]
|
2021-02-20 10:49:01 -08:00
|
|
|
);
|
2021-02-20 09:01:38 -08:00
|
|
|
}
|
2021-02-20 11:10:23 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn non_relevant_subsection_is_ignored() {
|
2021-02-26 18:44:58 -08:00
|
|
|
let config = GitConfig::try_from("[core]\na=b\na=c\n[core]a=d\n[core]g=g").unwrap();
|
2021-02-20 11:10:23 -08:00
|
|
|
assert_eq!(
|
|
|
|
config.get_raw_multi_value("core", None, "a").unwrap(),
|
2021-02-23 16:47:24 -08:00
|
|
|
vec![
|
2021-02-27 19:18:44 -08:00
|
|
|
&Cow::Borrowed(b"b"),
|
|
|
|
&Cow::Borrowed(b"c"),
|
|
|
|
&Cow::Borrowed(b"d")
|
2021-02-23 16:47:24 -08:00
|
|
|
]
|
2021-02-20 11:10:23 -08:00
|
|
|
);
|
|
|
|
}
|
2021-02-18 21:12:59 -08:00
|
|
|
}
|
2021-02-21 18:16:08 -08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod display {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_reconstruct_empty_config() {
|
|
|
|
let config = r#"
|
|
|
|
|
|
|
|
"#;
|
2021-02-26 18:44:58 -08:00
|
|
|
assert_eq!(GitConfig::try_from(config).unwrap().to_string(), config);
|
2021-02-21 18:16:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_reconstruct_non_empty_config() {
|
|
|
|
let config = r#"[user]
|
|
|
|
email = code@eddie.sh
|
|
|
|
[core]
|
|
|
|
autocrlf = input
|
|
|
|
[push]
|
|
|
|
default = simple
|
|
|
|
[commit]
|
|
|
|
gpgsign = true
|
|
|
|
[gpg]
|
|
|
|
program = gpg
|
|
|
|
[url "ssh://git@github.com/"]
|
|
|
|
insteadOf = "github://"
|
|
|
|
[url "ssh://git@git.eddie.sh/edward/"]
|
|
|
|
insteadOf = "gitea://"
|
|
|
|
[pull]
|
|
|
|
ff = only
|
|
|
|
[init]
|
|
|
|
defaultBranch = master"#;
|
|
|
|
|
2021-02-26 18:44:58 -08:00
|
|
|
assert_eq!(GitConfig::try_from(config).unwrap().to_string(), config);
|
2021-02-21 18:16:08 -08:00
|
|
|
}
|
2021-02-23 08:30:48 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_reconstruct_configs_with_implicits() {
|
|
|
|
let config = r#"[user]
|
|
|
|
email
|
|
|
|
name
|
|
|
|
[core]
|
|
|
|
autocrlf
|
|
|
|
[push]
|
|
|
|
default
|
|
|
|
[commit]
|
|
|
|
gpgsign"#;
|
|
|
|
|
2021-02-26 18:44:58 -08:00
|
|
|
assert_eq!(GitConfig::try_from(config).unwrap().to_string(), config);
|
2021-02-23 08:30:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_reconstruct_configs_without_whitespace_in_middle() {
|
|
|
|
let config = r#"[core]
|
|
|
|
autocrlf=input
|
|
|
|
[push]
|
|
|
|
default=simple
|
|
|
|
[commit]
|
|
|
|
gpgsign=true
|
|
|
|
[pull]
|
|
|
|
ff = only
|
|
|
|
[init]
|
|
|
|
defaultBranch = master"#;
|
|
|
|
|
2021-02-26 18:44:58 -08:00
|
|
|
assert_eq!(GitConfig::try_from(config).unwrap().to_string(), config);
|
2021-02-23 08:30:48 -08:00
|
|
|
}
|
2021-02-21 18:16:08 -08:00
|
|
|
}
|