Compare commits
No commits in common. "594ee8ac0c5620be279c7520e9f4478ecf4eb582" and "93e6e2eed3f855786d1686dcfce1b2f4f21e4fb7" have entirely different histories.
594ee8ac0c
...
93e6e2eed3
3 changed files with 548 additions and 894 deletions
523
src/config.rs
523
src/config.rs
|
@ -2,25 +2,14 @@ use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::parser::{parse_from_str, Event, Parser, ParserError};
|
use crate::parser::{parse_from_str, Event, Parser, ParserError};
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Hash, Copy, Clone, PartialOrd, Ord, Debug)]
|
#[derive(PartialEq, Eq, Hash, Copy, Clone, PartialOrd, Ord)]
|
||||||
struct SectionId(usize);
|
struct SectionId(usize);
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
|
||||||
enum LookupTreeNode<'a> {
|
enum LookupTreeNode<'a> {
|
||||||
Terminal(Vec<SectionId>),
|
Terminal(Vec<SectionId>),
|
||||||
NonTerminal(HashMap<&'a str, Vec<SectionId>>),
|
NonTerminal(HashMap<&'a str, Vec<SectionId>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
|
||||||
pub enum GitConfigError<'a> {
|
|
||||||
/// The requested section does not exist.
|
|
||||||
SectionDoesNotExist(&'a str),
|
|
||||||
/// The requested subsection does not exist.
|
|
||||||
SubSectionDoesNotExist(Option<&'a str>),
|
|
||||||
/// The key does not exist in the requested section.
|
|
||||||
KeyDoesNotExist(&'a str),
|
|
||||||
}
|
|
||||||
|
|
||||||
/// High level `git-config` reader and writer.
|
/// High level `git-config` reader and writer.
|
||||||
pub struct GitConfig<'a> {
|
pub struct GitConfig<'a> {
|
||||||
front_matter_events: Vec<Event<'a>>,
|
front_matter_events: Vec<Event<'a>>,
|
||||||
|
@ -38,13 +27,14 @@ impl<'a> GitConfig<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_parser(parser: Parser<'a>) -> Self {
|
pub fn from_parser(parser: Parser<'a>) -> Self {
|
||||||
let mut new_self = Self {
|
// Monotonically increasing
|
||||||
front_matter_events: vec![],
|
let mut section_id_counter: usize = 0;
|
||||||
sections: HashMap::new(),
|
|
||||||
section_lookup_tree: HashMap::new(),
|
// Fields for the struct
|
||||||
section_header_separators: HashMap::new(),
|
let mut front_matter_events: Vec<Event<'a>> = vec![];
|
||||||
section_id_counter: 0,
|
let mut sections: HashMap<SectionId, Vec<Event<'a>>> = HashMap::new();
|
||||||
};
|
let mut section_lookup_tree: HashMap<&str, Vec<LookupTreeNode>> = HashMap::new();
|
||||||
|
let mut section_header_separators = HashMap::new();
|
||||||
|
|
||||||
// Current section that we're building
|
// Current section that we're building
|
||||||
let mut current_section_name: Option<&str> = None;
|
let mut current_section_name: Option<&str> = None;
|
||||||
|
@ -53,63 +43,16 @@ impl<'a> GitConfig<'a> {
|
||||||
|
|
||||||
for event in parser.into_iter() {
|
for event in parser.into_iter() {
|
||||||
match event {
|
match event {
|
||||||
Event::SectionHeader(header) => {
|
e @ Event::Comment(_) => match maybe_section {
|
||||||
new_self.push_section(
|
|
||||||
&mut current_section_name,
|
|
||||||
&mut current_subsection_name,
|
|
||||||
&mut maybe_section,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Initialize new section
|
|
||||||
let (name, subname) = (header.name, header.subsection_name);
|
|
||||||
maybe_section = Some(vec![]);
|
|
||||||
current_section_name = Some(name);
|
|
||||||
current_subsection_name = subname;
|
|
||||||
// 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_header_separators
|
|
||||||
.insert(SectionId(new_self.section_id_counter), header.separator);
|
|
||||||
}
|
|
||||||
e @ Event::Key(_)
|
|
||||||
| e @ Event::Value(_)
|
|
||||||
| e @ Event::ValueNotDone(_)
|
|
||||||
| e @ Event::ValueDone(_) => 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),
|
Some(ref mut section) => section.push(e),
|
||||||
None => new_self.front_matter_events.push(e),
|
None => front_matter_events.push(e),
|
||||||
}
|
},
|
||||||
}
|
Event::SectionHeader(header) => {
|
||||||
}
|
// Push current section to struct
|
||||||
}
|
let new_section_id = SectionId(section_id_counter);
|
||||||
|
|
||||||
// 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(
|
|
||||||
&mut current_section_name,
|
|
||||||
&mut current_subsection_name,
|
|
||||||
&mut maybe_section,
|
|
||||||
);
|
|
||||||
|
|
||||||
new_self
|
|
||||||
}
|
|
||||||
|
|
||||||
fn push_section(
|
|
||||||
&mut self,
|
|
||||||
current_section_name: &mut Option<&'a str>,
|
|
||||||
current_subsection_name: &mut Option<&'a str>,
|
|
||||||
maybe_section: &mut Option<Vec<Event<'a>>>,
|
|
||||||
) {
|
|
||||||
let new_section_id = SectionId(self.section_id_counter);
|
|
||||||
if let Some(section) = maybe_section.take() {
|
if let Some(section) = maybe_section.take() {
|
||||||
self.sections.insert(new_section_id, section);
|
sections.insert(new_section_id, section);
|
||||||
let lookup = self
|
let lookup = section_lookup_tree
|
||||||
.section_lookup_tree
|
|
||||||
.entry(current_section_name.unwrap())
|
.entry(current_section_name.unwrap())
|
||||||
.or_default();
|
.or_default();
|
||||||
|
|
||||||
|
@ -127,7 +70,7 @@ impl<'a> GitConfig<'a> {
|
||||||
}
|
}
|
||||||
if !found_node {
|
if !found_node {
|
||||||
let mut map = HashMap::new();
|
let mut map = HashMap::new();
|
||||||
map.insert(*subsection_name, vec![new_section_id]);
|
map.insert(subsection_name, vec![new_section_id]);
|
||||||
lookup.push(LookupTreeNode::NonTerminal(map));
|
lookup.push(LookupTreeNode::NonTerminal(map));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -142,40 +85,58 @@ impl<'a> GitConfig<'a> {
|
||||||
lookup.push(LookupTreeNode::Terminal(vec![new_section_id]))
|
lookup.push(LookupTreeNode::Terminal(vec![new_section_id]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.section_id_counter += 1;
|
|
||||||
|
section_id_counter += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize new section
|
||||||
|
let (name, subname) = (header.name, header.subsection_name);
|
||||||
|
maybe_section = Some(vec![]);
|
||||||
|
current_section_name = Some(name);
|
||||||
|
current_subsection_name = subname;
|
||||||
|
// 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.
|
||||||
|
section_header_separators
|
||||||
|
.insert(SectionId(section_id_counter), header.separator);
|
||||||
|
}
|
||||||
|
e @ Event::Key(_) => maybe_section
|
||||||
|
.as_mut()
|
||||||
|
.expect("Got a section-only event before a section")
|
||||||
|
.push(e),
|
||||||
|
e @ Event::Value(_) => maybe_section
|
||||||
|
.as_mut()
|
||||||
|
.expect("Got a section-only event before a section")
|
||||||
|
.push(e),
|
||||||
|
e @ Event::Newline(_) => match maybe_section {
|
||||||
|
Some(ref mut section) => section.push(e),
|
||||||
|
None => front_matter_events.push(e),
|
||||||
|
},
|
||||||
|
e @ Event::ValueNotDone(_) => maybe_section
|
||||||
|
.as_mut()
|
||||||
|
.expect("Got a section-only event before a section")
|
||||||
|
.push(e),
|
||||||
|
e @ Event::ValueDone(_) => maybe_section
|
||||||
|
.as_mut()
|
||||||
|
.expect("Got a section-only event before a section")
|
||||||
|
.push(e),
|
||||||
|
e @ Event::Whitespace(_) => match maybe_section {
|
||||||
|
Some(ref mut section) => section.push(e),
|
||||||
|
None => front_matter_events.push(e),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an uninterpreted value given a section and optional subsection
|
Self {
|
||||||
/// and key.
|
front_matter_events,
|
||||||
///
|
section_lookup_tree,
|
||||||
/// Note that `git-config` follows a "last-one-wins" rule for single values.
|
sections,
|
||||||
/// If multiple sections contain the same key, then the last section's last
|
section_header_separators,
|
||||||
/// key's value will be returned.
|
section_id_counter,
|
||||||
///
|
}
|
||||||
/// Concretely, if you have the following config:
|
}
|
||||||
///
|
|
||||||
/// ```text
|
pub fn get_raw_single_value<'b>(
|
||||||
/// [core]
|
|
||||||
/// a = b
|
|
||||||
/// [core]
|
|
||||||
/// a = c
|
|
||||||
/// a = d
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
/// Then this function will return `d`:
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// # use serde_git_config::config::GitConfig;
|
|
||||||
/// # let git_config = GitConfig::from_str("[core]a=b\n[core]\na=c\na=d").unwrap();
|
|
||||||
/// assert_eq!(git_config.get_raw_value("core", None, "a"), Ok("d"));
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
/// # Errors
|
|
||||||
///
|
|
||||||
/// This function will return an error if the key is not in the requested
|
|
||||||
/// section and subsection.
|
|
||||||
pub fn get_raw_value<'b>(
|
|
||||||
&self,
|
&self,
|
||||||
section_name: &'b str,
|
section_name: &'b str,
|
||||||
subsection_name: Option<&'b str>,
|
subsection_name: Option<&'b str>,
|
||||||
|
@ -184,38 +145,33 @@ impl<'a> GitConfig<'a> {
|
||||||
// Note: cannot wrap around the raw_multi_value method because we need
|
// 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
|
// to guarantee that the highest section id is used (so that we follow
|
||||||
// the "last one wins" resolution strategy by `git-config`).
|
// the "last one wins" resolution strategy by `git-config`).
|
||||||
let section_id = self.get_section_id_by_name_and_subname(section_name, subsection_name)?;
|
let section_id = self
|
||||||
|
.get_section_id_by_name_and_subname(section_name, subsection_name)
|
||||||
|
.ok_or(GitConfigError::SubSectionDoesNotExist(subsection_name))?;
|
||||||
|
|
||||||
// section_id is guaranteed to exist in self.sections, else we have a
|
// section_id is guaranteed to exist in self.sections, else we have a
|
||||||
// violated invariant.
|
// violated invariant.
|
||||||
let events = self.sections.get(§ion_id).unwrap();
|
let events = self.sections.get(§ion_id).unwrap();
|
||||||
let mut found_key = false;
|
let mut found_key = false;
|
||||||
let mut latest_value = None;
|
|
||||||
for event in events {
|
for event in events {
|
||||||
match event {
|
match event {
|
||||||
Event::Key(event_key) if *event_key == key => found_key = true,
|
Event::Key(event_key) if *event_key == key => found_key = true,
|
||||||
Event::Value(v) if found_key => {
|
Event::Value(v) if found_key => return Ok(v),
|
||||||
found_key = false;
|
|
||||||
latest_value = Some(*v);
|
|
||||||
}
|
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
latest_value.ok_or(GitConfigError::KeyDoesNotExist(key))
|
Err(GitConfigError::KeyDoesNotExist(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_section_id_by_name_and_subname<'b>(
|
fn get_section_id_by_name_and_subname<'b>(
|
||||||
&'a self,
|
&'a self,
|
||||||
section_name: &'b str,
|
section_name: &'b str,
|
||||||
subsection_name: Option<&'b str>,
|
subsection_name: Option<&'b str>,
|
||||||
) -> Result<SectionId, GitConfigError<'b>> {
|
) -> Option<SectionId> {
|
||||||
self.get_section_ids_by_name_and_subname(section_name, subsection_name)
|
self.get_section_ids_by_name_and_subname(section_name, subsection_name)
|
||||||
.map(|vec| {
|
.map(|vec| vec.into_iter().max())
|
||||||
// get_section_ids_by_name_and_subname is guaranteed to return
|
.flatten()
|
||||||
// a non-empty vec, so max can never return empty.
|
|
||||||
*vec.into_iter().max().unwrap()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_raw_multi_value<'b>(
|
pub fn get_raw_multi_value<'b>(
|
||||||
|
@ -225,33 +181,26 @@ impl<'a> GitConfig<'a> {
|
||||||
key: &'b str,
|
key: &'b str,
|
||||||
) -> Result<Vec<&'a str>, GitConfigError<'b>> {
|
) -> Result<Vec<&'a str>, GitConfigError<'b>> {
|
||||||
let values = self
|
let values = self
|
||||||
.get_section_ids_by_name_and_subname(section_name, subsection_name)?
|
.get_section_ids_by_name_and_subname(section_name, subsection_name)
|
||||||
|
.ok_or(GitConfigError::SubSectionDoesNotExist(subsection_name))?
|
||||||
.iter()
|
.iter()
|
||||||
.map(|section_id| {
|
.map(|section_id| {
|
||||||
let mut found_key = false;
|
let mut found_key = false;
|
||||||
let mut events = vec![];
|
|
||||||
// section_id is guaranteed to exist in self.sections, else we have a
|
// section_id is guaranteed to exist in self.sections, else we have a
|
||||||
// violated invariant.
|
// violated invariant.
|
||||||
for event in self.sections.get(section_id).unwrap() {
|
for event in self.sections.get(section_id).unwrap() {
|
||||||
match event {
|
match event {
|
||||||
Event::Key(event_key) if *event_key == key => found_key = true,
|
Event::Key(event_key) if *event_key == key => found_key = true,
|
||||||
Event::Value(v) if found_key => {
|
Event::Value(v) if found_key => return Ok(*v),
|
||||||
events.push(*v);
|
|
||||||
found_key = false;
|
|
||||||
}
|
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if events.is_empty() {
|
|
||||||
Err(GitConfigError::KeyDoesNotExist(key))
|
Err(GitConfigError::KeyDoesNotExist(key))
|
||||||
} else {
|
|
||||||
Ok(events)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.filter_map(Result::ok)
|
.filter_map(Result::ok)
|
||||||
.flatten()
|
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
if values.is_empty() {
|
if values.is_empty() {
|
||||||
Err(GitConfigError::KeyDoesNotExist(key))
|
Err(GitConfigError::KeyDoesNotExist(key))
|
||||||
} else {
|
} else {
|
||||||
|
@ -263,324 +212,32 @@ impl<'a> GitConfig<'a> {
|
||||||
&'a self,
|
&'a self,
|
||||||
section_name: &'b str,
|
section_name: &'b str,
|
||||||
subsection_name: Option<&'b str>,
|
subsection_name: Option<&'b str>,
|
||||||
) -> Result<&[SectionId], GitConfigError<'b>> {
|
) -> Option<Vec<SectionId>> {
|
||||||
let section_ids = self
|
let section_ids = self.section_lookup_tree.get(section_name)?;
|
||||||
.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.
|
|
||||||
if let Some(subsect_name) = subsection_name {
|
if let Some(subsect_name) = subsection_name {
|
||||||
|
let mut maybe_ids = None;
|
||||||
for node in section_ids {
|
for node in section_ids {
|
||||||
if let LookupTreeNode::NonTerminal(subsection_lookup) = node {
|
if let LookupTreeNode::NonTerminal(subsection_lookup) = node {
|
||||||
maybe_ids = subsection_lookup.get(subsect_name);
|
maybe_ids = subsection_lookup.get(subsect_name);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
maybe_ids.map(|vec| vec.clone())
|
||||||
} else {
|
} else {
|
||||||
|
let mut maybe_ids = None;
|
||||||
for node in section_ids {
|
for node in section_ids {
|
||||||
if let LookupTreeNode::Terminal(subsection_lookup) = node {
|
if let LookupTreeNode::Terminal(subsection_lookup) = node {
|
||||||
maybe_ids = Some(subsection_lookup);
|
maybe_ids = subsection_lookup.iter().max();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
maybe_ids.map(|v| vec![*v])
|
||||||
}
|
}
|
||||||
maybe_ids
|
|
||||||
.map(Vec::as_slice)
|
|
||||||
.ok_or(GitConfigError::SubSectionDoesNotExist(subsection_name))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
pub enum GitConfigError<'a> {
|
||||||
mod from_parser {
|
SectionDoesNotExist(&'a str),
|
||||||
use super::*;
|
SubSectionDoesNotExist(Option<&'a str>),
|
||||||
|
KeyDoesNotExist(&'a str),
|
||||||
#[test]
|
|
||||||
fn parse_empty() {
|
|
||||||
let config = GitConfig::from_str("").unwrap();
|
|
||||||
assert!(config.section_header_separators.is_empty());
|
|
||||||
assert_eq!(config.section_id_counter, 0);
|
|
||||||
assert!(config.section_lookup_tree.is_empty());
|
|
||||||
assert!(config.sections.is_empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn parse_single_section() {
|
|
||||||
let config = GitConfig::from_str("[core]\na=b\nc=d").unwrap();
|
|
||||||
let expected_separators = {
|
|
||||||
let mut map = HashMap::new();
|
|
||||||
map.insert(SectionId(0), None);
|
|
||||||
map
|
|
||||||
};
|
|
||||||
assert_eq!(config.section_header_separators, expected_separators);
|
|
||||||
assert_eq!(config.section_id_counter, 1);
|
|
||||||
let expected_lookup_tree = {
|
|
||||||
let mut tree = HashMap::new();
|
|
||||||
tree.insert("core", vec![LookupTreeNode::Terminal(vec![SectionId(0)])]);
|
|
||||||
tree
|
|
||||||
};
|
|
||||||
assert_eq!(config.section_lookup_tree, expected_lookup_tree);
|
|
||||||
let expected_sections = {
|
|
||||||
let mut sections = HashMap::new();
|
|
||||||
sections.insert(
|
|
||||||
SectionId(0),
|
|
||||||
vec![
|
|
||||||
Event::Newline("\n"),
|
|
||||||
Event::Key("a"),
|
|
||||||
Event::Value("b"),
|
|
||||||
Event::Newline("\n"),
|
|
||||||
Event::Key("c"),
|
|
||||||
Event::Value("d"),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
sections
|
|
||||||
};
|
|
||||||
assert_eq!(config.sections, expected_sections);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn parse_single_subsection() {
|
|
||||||
let config = GitConfig::from_str("[core.subsec]\na=b\nc=d").unwrap();
|
|
||||||
let expected_separators = {
|
|
||||||
let mut map = HashMap::new();
|
|
||||||
map.insert(SectionId(0), Some("."));
|
|
||||||
map
|
|
||||||
};
|
|
||||||
assert_eq!(config.section_header_separators, expected_separators);
|
|
||||||
assert_eq!(config.section_id_counter, 1);
|
|
||||||
let expected_lookup_tree = {
|
|
||||||
let mut tree = HashMap::new();
|
|
||||||
let mut inner_tree = HashMap::new();
|
|
||||||
inner_tree.insert("subsec", vec![SectionId(0)]);
|
|
||||||
tree.insert("core", vec![LookupTreeNode::NonTerminal(inner_tree)]);
|
|
||||||
tree
|
|
||||||
};
|
|
||||||
assert_eq!(config.section_lookup_tree, expected_lookup_tree);
|
|
||||||
let expected_sections = {
|
|
||||||
let mut sections = HashMap::new();
|
|
||||||
sections.insert(
|
|
||||||
SectionId(0),
|
|
||||||
vec![
|
|
||||||
Event::Newline("\n"),
|
|
||||||
Event::Key("a"),
|
|
||||||
Event::Value("b"),
|
|
||||||
Event::Newline("\n"),
|
|
||||||
Event::Key("c"),
|
|
||||||
Event::Value("d"),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
sections
|
|
||||||
};
|
|
||||||
assert_eq!(config.sections, expected_sections);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn parse_multiple_sections() {
|
|
||||||
let config = GitConfig::from_str("[core]\na=b\nc=d\n[other]e=f").unwrap();
|
|
||||||
let expected_separators = {
|
|
||||||
let mut map = HashMap::new();
|
|
||||||
map.insert(SectionId(0), None);
|
|
||||||
map.insert(SectionId(1), None);
|
|
||||||
map
|
|
||||||
};
|
|
||||||
assert_eq!(config.section_header_separators, expected_separators);
|
|
||||||
assert_eq!(config.section_id_counter, 2);
|
|
||||||
let expected_lookup_tree = {
|
|
||||||
let mut tree = HashMap::new();
|
|
||||||
tree.insert("core", vec![LookupTreeNode::Terminal(vec![SectionId(0)])]);
|
|
||||||
tree.insert("other", vec![LookupTreeNode::Terminal(vec![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![
|
|
||||||
Event::Newline("\n"),
|
|
||||||
Event::Key("a"),
|
|
||||||
Event::Value("b"),
|
|
||||||
Event::Newline("\n"),
|
|
||||||
Event::Key("c"),
|
|
||||||
Event::Value("d"),
|
|
||||||
Event::Newline("\n"),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
sections.insert(SectionId(1), vec![Event::Key("e"), Event::Value("f")]);
|
|
||||||
sections
|
|
||||||
};
|
|
||||||
assert_eq!(config.sections, expected_sections);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn parse_multiple_duplicate_sections() {
|
|
||||||
let config = GitConfig::from_str("[core]\na=b\nc=d\n[core]e=f").unwrap();
|
|
||||||
let expected_separators = {
|
|
||||||
let mut map = HashMap::new();
|
|
||||||
map.insert(SectionId(0), None);
|
|
||||||
map.insert(SectionId(1), None);
|
|
||||||
map
|
|
||||||
};
|
|
||||||
assert_eq!(config.section_header_separators, expected_separators);
|
|
||||||
assert_eq!(config.section_id_counter, 2);
|
|
||||||
let expected_lookup_tree = {
|
|
||||||
let mut tree = HashMap::new();
|
|
||||||
tree.insert(
|
|
||||||
"core",
|
|
||||||
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![
|
|
||||||
Event::Newline("\n"),
|
|
||||||
Event::Key("a"),
|
|
||||||
Event::Value("b"),
|
|
||||||
Event::Newline("\n"),
|
|
||||||
Event::Key("c"),
|
|
||||||
Event::Value("d"),
|
|
||||||
Event::Newline("\n"),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
sections.insert(SectionId(1), vec![Event::Key("e"), Event::Value("f")]);
|
|
||||||
sections
|
|
||||||
};
|
|
||||||
assert_eq!(config.sections, expected_sections);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod get_raw_value {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn single_section() {
|
|
||||||
let config = GitConfig::from_str("[core]\na=b\nc=d").unwrap();
|
|
||||||
assert_eq!(config.get_raw_value("core", None, "a"), Ok("b"));
|
|
||||||
assert_eq!(config.get_raw_value("core", None, "c"), Ok("d"));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn last_one_wins_respected_in_section() {
|
|
||||||
let config = GitConfig::from_str("[core]\na=b\na=d").unwrap();
|
|
||||||
assert_eq!(config.get_raw_value("core", None, "a"), Ok("d"));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn last_one_wins_respected_across_section() {
|
|
||||||
let config = GitConfig::from_str("[core]\na=b\n[core]\na=d").unwrap();
|
|
||||||
assert_eq!(config.get_raw_value("core", None, "a"), Ok("d"));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn section_not_found() {
|
|
||||||
let config = GitConfig::from_str("[core]\na=b\nc=d").unwrap();
|
|
||||||
assert_eq!(
|
|
||||||
config.get_raw_value("foo", None, "a"),
|
|
||||||
Err(GitConfigError::SectionDoesNotExist("foo"))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn subsection_not_found() {
|
|
||||||
let config = GitConfig::from_str("[core]\na=b\nc=d").unwrap();
|
|
||||||
assert_eq!(
|
|
||||||
config.get_raw_value("core", Some("a"), "a"),
|
|
||||||
Err(GitConfigError::SubSectionDoesNotExist(Some("a")))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn key_not_found() {
|
|
||||||
let config = GitConfig::from_str("[core]\na=b\nc=d").unwrap();
|
|
||||||
assert_eq!(
|
|
||||||
config.get_raw_value("core", None, "aaaaaa"),
|
|
||||||
Err(GitConfigError::KeyDoesNotExist("aaaaaa"))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn subsection_must_be_respected() {
|
|
||||||
let config = GitConfig::from_str("[core]a=b\n[core.a]a=c").unwrap();
|
|
||||||
assert_eq!(config.get_raw_value("core", None, "a"), Ok("b"));
|
|
||||||
assert_eq!(config.get_raw_value("core", Some("a"), "a"), Ok("c"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod get_raw_multi_value {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn single_value_is_identical_to_single_value_query() {
|
|
||||||
let config = GitConfig::from_str("[core]\na=b\nc=d").unwrap();
|
|
||||||
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() {
|
|
||||||
let config = GitConfig::from_str("[core]\na=b\na=c").unwrap();
|
|
||||||
assert_eq!(
|
|
||||||
config.get_raw_multi_value("core", None, "a").unwrap(),
|
|
||||||
vec!["b", "c"]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn multi_value_across_sections() {
|
|
||||||
let config = GitConfig::from_str("[core]\na=b\na=c\n[core]a=d").unwrap();
|
|
||||||
assert_eq!(
|
|
||||||
config.get_raw_multi_value("core", None, "a").unwrap(),
|
|
||||||
vec!["b", "c", "d"]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn section_not_found() {
|
|
||||||
let config = GitConfig::from_str("[core]\na=b\nc=d").unwrap();
|
|
||||||
assert_eq!(
|
|
||||||
config.get_raw_multi_value("foo", None, "a"),
|
|
||||||
Err(GitConfigError::SectionDoesNotExist("foo"))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn subsection_not_found() {
|
|
||||||
let config = GitConfig::from_str("[core]\na=b\nc=d").unwrap();
|
|
||||||
assert_eq!(
|
|
||||||
config.get_raw_multi_value("core", Some("a"), "a"),
|
|
||||||
Err(GitConfigError::SubSectionDoesNotExist(Some("a")))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn key_not_found() {
|
|
||||||
let config = GitConfig::from_str("[core]\na=b\nc=d").unwrap();
|
|
||||||
assert_eq!(
|
|
||||||
config.get_raw_multi_value("core", None, "aaaaaa"),
|
|
||||||
Err(GitConfigError::KeyDoesNotExist("aaaaaa"))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn subsection_must_be_respected() {
|
|
||||||
let config = GitConfig::from_str("[core]a=b\n[core.a]a=c").unwrap();
|
|
||||||
assert_eq!(
|
|
||||||
config.get_raw_multi_value("core", None, "a").unwrap(),
|
|
||||||
vec!["b"]
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
config.get_raw_multi_value("core", Some("a"), "a").unwrap(),
|
|
||||||
vec!["c"]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,18 @@
|
||||||
//! This module handles parsing a `git-config`. Generally speaking, you want to
|
//! This module handles parsing a `git-config`. Generally speaking, you want to
|
||||||
//! use a higher abstraction such as [`GitConfig`] unless you have some explicit
|
//! use a higher abstraction unless you have some explicit reason to work with
|
||||||
//! reason to work with events instead.
|
//! events instead.
|
||||||
//!
|
//!
|
||||||
//! The general workflow for interacting with this is to use one of the
|
//! The general workflow for interacting with this is to use one of the
|
||||||
//! `parse_from_*` function variants. These will return a [`Parser`] on success,
|
//! `parse_from_*` function variants. These will return a [`Parser`] on success,
|
||||||
//! which can be converted into an [`Event`] iterator. The [`Parser`] also has
|
//! which can be converted into an [`Event`] iterator. The [`Parser`] also has
|
||||||
//! additional methods for accessing leading comments or events by section.
|
//! additional methods for accessing leading comments or events by section.
|
||||||
//!
|
|
||||||
//! [`GitConfig`]: crate::config::GitConfig
|
|
||||||
|
|
||||||
use nom::bytes::complete::{escaped, tag, take_till, take_while};
|
use nom::bytes::complete::{escaped, tag, take_till, take_while};
|
||||||
use nom::character::complete::{char, none_of, one_of};
|
use nom::character::complete::{char, none_of, one_of};
|
||||||
use nom::character::{is_newline, is_space};
|
use nom::character::{is_newline, is_space};
|
||||||
use nom::combinator::{map, opt};
|
use nom::combinator::{map, opt};
|
||||||
use nom::error::{Error as NomError, ErrorKind};
|
use nom::error::{Error as NomError, ErrorKind};
|
||||||
|
use nom::multi::many1;
|
||||||
use nom::sequence::delimited;
|
use nom::sequence::delimited;
|
||||||
use nom::IResult;
|
use nom::IResult;
|
||||||
use nom::{branch::alt, multi::many0};
|
use nom::{branch::alt, multi::many0};
|
||||||
|
@ -361,7 +360,7 @@ impl<'a> Parser<'a> {
|
||||||
/// data succeeding valid `git-config` data.
|
/// data succeeding valid `git-config` data.
|
||||||
pub fn parse_from_str(input: &str) -> Result<Parser<'_>, ParserError> {
|
pub fn parse_from_str(input: &str) -> Result<Parser<'_>, ParserError> {
|
||||||
let (i, comments) = many0(comment)(input)?;
|
let (i, comments) = many0(comment)(input)?;
|
||||||
let (i, sections) = many0(section)(i)?;
|
let (i, sections) = many1(section)(i)?;
|
||||||
|
|
||||||
if !i.is_empty() {
|
if !i.is_empty() {
|
||||||
return Err(ParserError::ConfigHasExtraData(i));
|
return Err(ParserError::ConfigHasExtraData(i));
|
||||||
|
@ -618,17 +617,18 @@ fn take_common<'a, F: Fn(char) -> bool>(i: &'a str, f: F) -> IResult<&'a str, &'
|
||||||
Ok((i, v))
|
Ok((i, v))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
fn fully_consumed<T>(t: T) -> (&'static str, T) {
|
mod parse {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn fully_consumed<T>(t: T) -> (&'static str, T) {
|
||||||
("", t)
|
("", t)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
fn gen_section_header(
|
||||||
fn gen_section_header(
|
|
||||||
name: &str,
|
name: &str,
|
||||||
subsection: impl Into<Option<(&'static str, &'static str)>>,
|
subsection: impl Into<Option<(&'static str, &'static str)>>,
|
||||||
) -> ParsedSectionHeader<'_> {
|
) -> ParsedSectionHeader<'_> {
|
||||||
if let Some((separator, subsection_name)) = subsection.into() {
|
if let Some((separator, subsection_name)) = subsection.into() {
|
||||||
ParsedSectionHeader {
|
ParsedSectionHeader {
|
||||||
name,
|
name,
|
||||||
|
@ -642,10 +642,10 @@ fn gen_section_header(
|
||||||
subsection_name: None,
|
subsection_name: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
mod comments {
|
||||||
mod comments {
|
use super::super::*;
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -680,10 +680,10 @@ mod comments {
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
mod section_headers {
|
||||||
mod section_headers {
|
use super::super::*;
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -751,10 +751,10 @@ mod section_headers {
|
||||||
fully_consumed(gen_section_header("hello", (" ", "]")))
|
fully_consumed(gen_section_header("hello", (" ", "]")))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
mod config_name {
|
||||||
mod config_name {
|
use super::super::*;
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -772,10 +772,10 @@ mod config_name {
|
||||||
fn cannot_be_empty() {
|
fn cannot_be_empty() {
|
||||||
assert!(config_name("").is_err())
|
assert!(config_name("").is_err())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
mod value_no_continuation {
|
||||||
mod value_no_continuation {
|
use super::super::*;
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -850,10 +850,10 @@ mod value_no_continuation {
|
||||||
fn garbage_after_continution_is_err() {
|
fn garbage_after_continution_is_err() {
|
||||||
assert!(value_impl("hello \\afwjdls").is_err());
|
assert!(value_impl("hello \\afwjdls").is_err());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
mod value_continuation {
|
||||||
mod value_continuation {
|
use super::super::*;
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -911,10 +911,10 @@ mod value_continuation {
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
mod section {
|
||||||
mod section {
|
use super::super::*;
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -1089,4 +1089,5 @@ mod section {
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,7 +62,8 @@ fn personal_config() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse_from_str(config)
|
parse_from_str(config)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.into_vec(),
|
.into_iter()
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
vec![
|
vec![
|
||||||
gen_section_header("user", None),
|
gen_section_header("user", None),
|
||||||
newline(),
|
newline(),
|
||||||
|
@ -162,8 +163,3 @@ fn personal_config() {
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn parse_empty() {
|
|
||||||
assert_eq!(parse_from_str("").unwrap().into_vec(), vec![]);
|
|
||||||
}
|
|
||||||
|
|
Reference in a new issue