Use Cow instead of strs
This commit is contained in:
parent
eaa0a1766b
commit
6a99b1caa0
4 changed files with 363 additions and 255 deletions
297
src/config.rs
297
src/config.rs
|
@ -1,6 +1,7 @@
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
use crate::parser::{parse_from_str, Event, Parser, ParserError};
|
use crate::parser::{parse_from_str, Event, Parser, ParserError};
|
||||||
|
use serde::Serialize;
|
||||||
|
use std::borrow::Cow;
|
||||||
|
use std::collections::{HashMap, VecDeque};
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub enum GitConfigError<'a> {
|
pub enum GitConfigError<'a> {
|
||||||
|
@ -12,25 +13,24 @@ pub enum GitConfigError<'a> {
|
||||||
KeyDoesNotExist(&'a str),
|
KeyDoesNotExist(&'a str),
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Use linked list for section ordering?
|
|
||||||
|
|
||||||
/// High level `git-config` reader and writer.
|
/// High level `git-config` reader and writer.
|
||||||
pub struct GitConfig<'a> {
|
pub struct GitConfig<'a> {
|
||||||
/// The list of events that occur before an actual section. Since 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
|
/// `git-config` file prohibits global values, this vec is limited to only
|
||||||
/// comment, newline, and whitespace events.
|
/// comment, newline, and whitespace events.
|
||||||
front_matter_events: Vec<Event<'a>>,
|
front_matter_events: Vec<Event<'a>>,
|
||||||
section_lookup_tree: HashMap<&'a str, Vec<LookupTreeNode<'a>>>,
|
section_lookup_tree: HashMap<Cow<'a, str>, Vec<LookupTreeNode<'a>>>,
|
||||||
/// SectionId to section mapping. The value of this HashMap contains actual
|
/// SectionId to section mapping. The value of this HashMap contains actual
|
||||||
/// events
|
/// events
|
||||||
sections: HashMap<SectionId, Vec<Event<'a>>>,
|
sections: HashMap<SectionId, Vec<Event<'a>>>,
|
||||||
section_header_separators: HashMap<SectionId, Option<&'a str>>,
|
section_header_separators: HashMap<SectionId, Option<Cow<'a, str>>>,
|
||||||
section_id_counter: usize,
|
section_id_counter: usize,
|
||||||
|
section_order: VecDeque<SectionId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The section ID is a monotonically increasing ID used to refer to sections,
|
/// The section ID is a monotonically increasing ID used to refer to sections.
|
||||||
/// and implies ordering between sections in the original `git-config` file,
|
/// This value does not imply any ordering between sections, as new sections
|
||||||
/// such that a section with id 0 always is before a section with id 1.
|
/// with higher section IDs may be in between lower ID sections.
|
||||||
///
|
///
|
||||||
/// We need to use a section id because `git-config` permits sections with
|
/// 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
|
/// identical names. As a result, we can't simply use the section name as a key
|
||||||
|
@ -45,7 +45,7 @@ struct SectionId(usize);
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
enum LookupTreeNode<'a> {
|
enum LookupTreeNode<'a> {
|
||||||
Terminal(Vec<SectionId>),
|
Terminal(Vec<SectionId>),
|
||||||
NonTerminal(HashMap<&'a str, Vec<SectionId>>),
|
NonTerminal(HashMap<Cow<'a, str>, Vec<SectionId>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> GitConfig<'a> {
|
impl<'a> GitConfig<'a> {
|
||||||
|
@ -62,19 +62,20 @@ impl<'a> GitConfig<'a> {
|
||||||
section_lookup_tree: HashMap::new(),
|
section_lookup_tree: HashMap::new(),
|
||||||
section_header_separators: HashMap::new(),
|
section_header_separators: HashMap::new(),
|
||||||
section_id_counter: 0,
|
section_id_counter: 0,
|
||||||
|
section_order: VecDeque::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<Cow<'a, str>> = None;
|
||||||
let mut current_subsection_name: Option<&str> = None;
|
let mut current_subsection_name: Option<Cow<'a, str>> = None;
|
||||||
let mut maybe_section: Option<Vec<Event<'a>>> = None;
|
let mut maybe_section: Option<Vec<Event<'a>>> = None;
|
||||||
|
|
||||||
for event in parser.into_iter() {
|
for event in parser.into_iter() {
|
||||||
match event {
|
match event {
|
||||||
Event::SectionHeader(header) => {
|
Event::SectionHeader(header) => {
|
||||||
new_self.push_section(
|
new_self.push_section(
|
||||||
&mut current_section_name,
|
current_section_name,
|
||||||
&mut current_subsection_name,
|
current_subsection_name,
|
||||||
&mut maybe_section,
|
&mut maybe_section,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -109,8 +110,8 @@ impl<'a> GitConfig<'a> {
|
||||||
// The last section doesn't get pushed since we only push if there's a
|
// 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 section header, so we need to call push one more time.
|
||||||
new_self.push_section(
|
new_self.push_section(
|
||||||
&mut current_section_name,
|
current_section_name,
|
||||||
&mut current_subsection_name,
|
current_subsection_name,
|
||||||
&mut maybe_section,
|
&mut maybe_section,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -119,12 +120,12 @@ impl<'a> GitConfig<'a> {
|
||||||
|
|
||||||
fn push_section(
|
fn push_section(
|
||||||
&mut self,
|
&mut self,
|
||||||
current_section_name: &mut Option<&'a str>,
|
current_section_name: Option<Cow<'a, str>>,
|
||||||
current_subsection_name: &mut Option<&'a str>,
|
current_subsection_name: Option<Cow<'a, str>>,
|
||||||
maybe_section: &mut Option<Vec<Event<'a>>>,
|
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() {
|
||||||
|
let new_section_id = SectionId(self.section_id_counter);
|
||||||
self.sections.insert(new_section_id, section);
|
self.sections.insert(new_section_id, section);
|
||||||
let lookup = self
|
let lookup = self
|
||||||
.section_lookup_tree
|
.section_lookup_tree
|
||||||
|
@ -137,7 +138,10 @@ impl<'a> GitConfig<'a> {
|
||||||
if let LookupTreeNode::NonTerminal(subsection) = node {
|
if let LookupTreeNode::NonTerminal(subsection) = node {
|
||||||
found_node = true;
|
found_node = true;
|
||||||
subsection
|
subsection
|
||||||
.entry(subsection_name)
|
// Despite the clone `push_section` is always called
|
||||||
|
// with a Cow::Borrowed, so this is effectively a
|
||||||
|
// copy.
|
||||||
|
.entry(subsection_name.clone())
|
||||||
.or_default()
|
.or_default()
|
||||||
.push(new_section_id);
|
.push(new_section_id);
|
||||||
break;
|
break;
|
||||||
|
@ -145,7 +149,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 {
|
||||||
|
@ -160,6 +164,7 @@ impl<'a> GitConfig<'a> {
|
||||||
lookup.push(LookupTreeNode::Terminal(vec![new_section_id]))
|
lookup.push(LookupTreeNode::Terminal(vec![new_section_id]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
self.section_order.push_back(new_section_id);
|
||||||
self.section_id_counter += 1;
|
self.section_id_counter += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -186,8 +191,9 @@ impl<'a> GitConfig<'a> {
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use serde_git_config::config::GitConfig;
|
/// # use serde_git_config::config::GitConfig;
|
||||||
|
/// # use std::borrow::Cow;
|
||||||
/// # let git_config = GitConfig::from_str("[core]a=b\n[core]\na=c\na=d").unwrap();
|
/// # 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"));
|
/// assert_eq!(git_config.get_raw_value("core", None, "a"), Ok(&Cow::from("d")));
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// Consider [`Self::get_raw_multi_value`] if you want to get all values for
|
/// Consider [`Self::get_raw_multi_value`] if you want to get all values for
|
||||||
|
@ -202,7 +208,7 @@ impl<'a> GitConfig<'a> {
|
||||||
section_name: &'b str,
|
section_name: &'b str,
|
||||||
subsection_name: Option<&'b str>,
|
subsection_name: Option<&'b str>,
|
||||||
key: &'b str,
|
key: &'b str,
|
||||||
) -> Result<&'a str, GitConfigError<'b>> {
|
) -> Result<&Cow<'a, str>, GitConfigError<'b>> {
|
||||||
// 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`).
|
||||||
|
@ -218,7 +224,7 @@ impl<'a> GitConfig<'a> {
|
||||||
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 => {
|
||||||
found_key = false;
|
found_key = false;
|
||||||
latest_value = Some(*v);
|
latest_value = Some(v);
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
@ -249,11 +255,12 @@ impl<'a> GitConfig<'a> {
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use serde_git_config::config::{GitConfig, GitConfigError};
|
/// # use serde_git_config::config::{GitConfig, GitConfigError};
|
||||||
|
/// # use std::borrow::Cow;
|
||||||
/// # let mut git_config = GitConfig::from_str("[core]a=b\n[core]\na=c\na=d").unwrap();
|
/// # let mut git_config = GitConfig::from_str("[core]a=b\n[core]\na=c\na=d").unwrap();
|
||||||
/// let mut mut_value = git_config.get_raw_value_mut("core", None, "a")?;
|
/// let mut mut_value = git_config.get_raw_value_mut("core", None, "a")?;
|
||||||
/// assert_eq!(mut_value, &mut "d");
|
/// assert_eq!(mut_value, &mut Cow::from("d"));
|
||||||
/// *mut_value = "hello";
|
/// *mut_value = Cow::Borrowed("hello");
|
||||||
/// assert_eq!(git_config.get_raw_value("core", None, "a"), Ok("hello"));
|
/// assert_eq!(git_config.get_raw_value("core", None, "a"), Ok(&Cow::from("hello")));
|
||||||
/// # Ok::<(), GitConfigError>(())
|
/// # Ok::<(), GitConfigError>(())
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
@ -269,7 +276,7 @@ impl<'a> GitConfig<'a> {
|
||||||
section_name: &'b str,
|
section_name: &'b str,
|
||||||
subsection_name: Option<&'b str>,
|
subsection_name: Option<&'b str>,
|
||||||
key: &'b str,
|
key: &'b str,
|
||||||
) -> Result<&mut &'a str, GitConfigError<'b>> {
|
) -> Result<&mut Cow<'a, str>, GitConfigError<'b>> {
|
||||||
// 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`).
|
||||||
|
@ -322,8 +329,12 @@ impl<'a> GitConfig<'a> {
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use serde_git_config::config::GitConfig;
|
/// # use serde_git_config::config::GitConfig;
|
||||||
|
/// # use std::borrow::Cow;
|
||||||
/// # let git_config = GitConfig::from_str("[core]a=b\n[core]\na=c\na=d").unwrap();
|
/// # let git_config = GitConfig::from_str("[core]a=b\n[core]\na=c\na=d").unwrap();
|
||||||
/// assert_eq!(git_config.get_raw_multi_value("core", None, "a"), Ok(vec!["b", "c", "d"]));
|
/// assert_eq!(
|
||||||
|
/// git_config.get_raw_multi_value("core", None, "a"),
|
||||||
|
/// Ok(vec![&Cow::from("b"), &Cow::from("c"), &Cow::from("d")]),
|
||||||
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// Consider [`Self::get_raw_value`] if you want to get the resolved single
|
/// Consider [`Self::get_raw_value`] if you want to get the resolved single
|
||||||
|
@ -339,7 +350,7 @@ impl<'a> GitConfig<'a> {
|
||||||
section_name: &'b str,
|
section_name: &'b str,
|
||||||
subsection_name: Option<&'b str>,
|
subsection_name: Option<&'b str>,
|
||||||
key: &'b str,
|
key: &'b str,
|
||||||
) -> Result<Vec<&'a str>, GitConfigError<'b>> {
|
) -> Result<Vec<&Cow<'a, str>>, GitConfigError<'b>> {
|
||||||
let mut values = vec![];
|
let mut values = vec![];
|
||||||
for section_id in self.get_section_ids_by_name_and_subname(section_name, subsection_name)? {
|
for section_id in self.get_section_ids_by_name_and_subname(section_name, subsection_name)? {
|
||||||
let mut found_key = false;
|
let mut found_key = false;
|
||||||
|
@ -349,7 +360,7 @@ impl<'a> GitConfig<'a> {
|
||||||
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 => {
|
||||||
values.push(*v);
|
values.push(v);
|
||||||
found_key = false;
|
found_key = false;
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
|
@ -379,18 +390,25 @@ impl<'a> GitConfig<'a> {
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use serde_git_config::config::{GitConfig, GitConfigError};
|
/// # use serde_git_config::config::{GitConfig, GitConfigError};
|
||||||
|
/// # use std::borrow::Cow;
|
||||||
/// # let mut git_config = GitConfig::from_str("[core]a=b\n[core]\na=c\na=d").unwrap();
|
/// # let mut git_config = GitConfig::from_str("[core]a=b\n[core]\na=c\na=d").unwrap();
|
||||||
/// assert_eq!(git_config.get_raw_multi_value("core", None, "a")?, vec!["b", "c", "d"]);
|
/// assert_eq!(git_config.get_raw_multi_value("core", None, "a")?, vec!["b", "c", "d"]);
|
||||||
/// for value in git_config.get_raw_multi_value_mut("core", None, "a")? {
|
/// for value in git_config.get_raw_multi_value_mut("core", None, "a")? {
|
||||||
/// *value = "g";
|
/// *value = Cow::from("g");
|
||||||
///}
|
///}
|
||||||
/// assert_eq!(git_config.get_raw_multi_value("core", None, "a")?, vec!["g", "g", "g"]);
|
/// assert_eq!(
|
||||||
|
/// git_config.get_raw_multi_value("core", None, "a")?,
|
||||||
|
/// vec![&Cow::from("g"), &Cow::from("g"), &Cow::from("g")],
|
||||||
|
/// );
|
||||||
/// # Ok::<(), GitConfigError>(())
|
/// # Ok::<(), GitConfigError>(())
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// Consider [`Self::get_raw_value`] if you want to get the resolved single
|
/// 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.
|
/// value for a given key, if your key does not support multi-valued values.
|
||||||
///
|
///
|
||||||
|
/// Note that this operation is relatively expensive, requiring a full
|
||||||
|
/// traversal of the config.
|
||||||
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
///
|
///
|
||||||
/// This function will return an error if the key is not in any requested
|
/// This function will return an error if the key is not in any requested
|
||||||
|
@ -401,12 +419,12 @@ impl<'a> GitConfig<'a> {
|
||||||
section_name: &'b str,
|
section_name: &'b str,
|
||||||
subsection_name: Option<&'b str>,
|
subsection_name: Option<&'b str>,
|
||||||
key: &'b str,
|
key: &'b str,
|
||||||
) -> Result<Vec<&mut &'a str>, GitConfigError<'b>> {
|
) -> Result<Vec<&mut Cow<'a, str>>, GitConfigError<'b>> {
|
||||||
let section_ids = self
|
let section_ids = self
|
||||||
.get_section_ids_by_name_and_subname(section_name, subsection_name)?
|
.get_section_ids_by_name_and_subname(section_name, subsection_name)?
|
||||||
.to_vec();
|
.to_vec();
|
||||||
let mut found_key = false;
|
let mut found_key = false;
|
||||||
let values: Vec<&mut &'a str> = self
|
let values: Vec<&mut Cow<'a, str>> = self
|
||||||
.sections
|
.sections
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
.filter_map(|(k, v)| {
|
.filter_map(|(k, v)| {
|
||||||
|
@ -469,6 +487,18 @@ impl<'a> GitConfig<'a> {
|
||||||
.map(Vec::as_slice)
|
.map(Vec::as_slice)
|
||||||
.ok_or(GitConfigError::SubSectionDoesNotExist(subsection_name))
|
.ok_or(GitConfigError::SubSectionDoesNotExist(subsection_name))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_raw_value<'b>(
|
||||||
|
&mut self,
|
||||||
|
section_name: &'b str,
|
||||||
|
subsection_name: Option<&'b str>,
|
||||||
|
key: &'b str,
|
||||||
|
new_value: impl Into<Cow<'a, str>>,
|
||||||
|
) -> Result<(), GitConfigError<'b>> {
|
||||||
|
let value = self.get_raw_value_mut(section_name, subsection_name, key)?;
|
||||||
|
*value = new_value.into();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -482,11 +512,12 @@ mod from_parser {
|
||||||
assert_eq!(config.section_id_counter, 0);
|
assert_eq!(config.section_id_counter, 0);
|
||||||
assert!(config.section_lookup_tree.is_empty());
|
assert!(config.section_lookup_tree.is_empty());
|
||||||
assert!(config.sections.is_empty());
|
assert!(config.sections.is_empty());
|
||||||
|
assert!(config.section_order.is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_single_section() {
|
fn parse_single_section() {
|
||||||
let config = GitConfig::from_str("[core]\na=b\nc=d").unwrap();
|
let mut config = GitConfig::from_str("[core]\na=b\nc=d").unwrap();
|
||||||
let expected_separators = {
|
let expected_separators = {
|
||||||
let mut map = HashMap::new();
|
let mut map = HashMap::new();
|
||||||
map.insert(SectionId(0), None);
|
map.insert(SectionId(0), None);
|
||||||
|
@ -496,7 +527,10 @@ mod from_parser {
|
||||||
assert_eq!(config.section_id_counter, 1);
|
assert_eq!(config.section_id_counter, 1);
|
||||||
let expected_lookup_tree = {
|
let expected_lookup_tree = {
|
||||||
let mut tree = HashMap::new();
|
let mut tree = HashMap::new();
|
||||||
tree.insert("core", vec![LookupTreeNode::Terminal(vec![SectionId(0)])]);
|
tree.insert(
|
||||||
|
Cow::Borrowed("core"),
|
||||||
|
vec![LookupTreeNode::Terminal(vec![SectionId(0)])],
|
||||||
|
);
|
||||||
tree
|
tree
|
||||||
};
|
};
|
||||||
assert_eq!(config.section_lookup_tree, expected_lookup_tree);
|
assert_eq!(config.section_lookup_tree, expected_lookup_tree);
|
||||||
|
@ -505,25 +539,26 @@ mod from_parser {
|
||||||
sections.insert(
|
sections.insert(
|
||||||
SectionId(0),
|
SectionId(0),
|
||||||
vec![
|
vec![
|
||||||
Event::Newline("\n"),
|
Event::Newline(Cow::Borrowed("\n")),
|
||||||
Event::Key("a"),
|
Event::Key(Cow::Borrowed("a")),
|
||||||
Event::Value("b"),
|
Event::Value(Cow::Borrowed("b")),
|
||||||
Event::Newline("\n"),
|
Event::Newline(Cow::Borrowed("\n")),
|
||||||
Event::Key("c"),
|
Event::Key(Cow::Borrowed("c")),
|
||||||
Event::Value("d"),
|
Event::Value(Cow::Borrowed("d")),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
sections
|
sections
|
||||||
};
|
};
|
||||||
assert_eq!(config.sections, expected_sections);
|
assert_eq!(config.sections, expected_sections);
|
||||||
|
assert_eq!(config.section_order.make_contiguous(), &[SectionId(0)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_single_subsection() {
|
fn parse_single_subsection() {
|
||||||
let config = GitConfig::from_str("[core.subsec]\na=b\nc=d").unwrap();
|
let mut config = GitConfig::from_str("[core.subsec]\na=b\nc=d").unwrap();
|
||||||
let expected_separators = {
|
let expected_separators = {
|
||||||
let mut map = HashMap::new();
|
let mut map = HashMap::new();
|
||||||
map.insert(SectionId(0), Some("."));
|
map.insert(SectionId(0), Some(Cow::Borrowed(".")));
|
||||||
map
|
map
|
||||||
};
|
};
|
||||||
assert_eq!(config.section_header_separators, expected_separators);
|
assert_eq!(config.section_header_separators, expected_separators);
|
||||||
|
@ -531,8 +566,11 @@ mod from_parser {
|
||||||
let expected_lookup_tree = {
|
let expected_lookup_tree = {
|
||||||
let mut tree = HashMap::new();
|
let mut tree = HashMap::new();
|
||||||
let mut inner_tree = HashMap::new();
|
let mut inner_tree = HashMap::new();
|
||||||
inner_tree.insert("subsec", vec![SectionId(0)]);
|
inner_tree.insert(Cow::Borrowed("subsec"), vec![SectionId(0)]);
|
||||||
tree.insert("core", vec![LookupTreeNode::NonTerminal(inner_tree)]);
|
tree.insert(
|
||||||
|
Cow::Borrowed("core"),
|
||||||
|
vec![LookupTreeNode::NonTerminal(inner_tree)],
|
||||||
|
);
|
||||||
tree
|
tree
|
||||||
};
|
};
|
||||||
assert_eq!(config.section_lookup_tree, expected_lookup_tree);
|
assert_eq!(config.section_lookup_tree, expected_lookup_tree);
|
||||||
|
@ -541,60 +579,23 @@ mod from_parser {
|
||||||
sections.insert(
|
sections.insert(
|
||||||
SectionId(0),
|
SectionId(0),
|
||||||
vec![
|
vec![
|
||||||
Event::Newline("\n"),
|
Event::Newline(Cow::Borrowed("\n")),
|
||||||
Event::Key("a"),
|
Event::Key(Cow::Borrowed("a")),
|
||||||
Event::Value("b"),
|
Event::Value(Cow::Borrowed("b")),
|
||||||
Event::Newline("\n"),
|
Event::Newline(Cow::Borrowed("\n")),
|
||||||
Event::Key("c"),
|
Event::Key(Cow::Borrowed("c")),
|
||||||
Event::Value("d"),
|
Event::Value(Cow::Borrowed("d")),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
sections
|
sections
|
||||||
};
|
};
|
||||||
assert_eq!(config.sections, expected_sections);
|
assert_eq!(config.sections, expected_sections);
|
||||||
|
assert_eq!(config.section_order.make_contiguous(), &[SectionId(0)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_multiple_sections() {
|
fn parse_multiple_sections() {
|
||||||
let config = GitConfig::from_str("[core]\na=b\nc=d\n[other]e=f").unwrap();
|
let mut 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 expected_separators = {
|
||||||
let mut map = HashMap::new();
|
let mut map = HashMap::new();
|
||||||
map.insert(SectionId(0), None);
|
map.insert(SectionId(0), None);
|
||||||
|
@ -606,7 +607,61 @@ mod from_parser {
|
||||||
let expected_lookup_tree = {
|
let expected_lookup_tree = {
|
||||||
let mut tree = HashMap::new();
|
let mut tree = HashMap::new();
|
||||||
tree.insert(
|
tree.insert(
|
||||||
"core",
|
Cow::Borrowed("core"),
|
||||||
|
vec![LookupTreeNode::Terminal(vec![SectionId(0)])],
|
||||||
|
);
|
||||||
|
tree.insert(
|
||||||
|
Cow::Borrowed("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(Cow::Borrowed("\n")),
|
||||||
|
Event::Key(Cow::Borrowed("a")),
|
||||||
|
Event::Value(Cow::Borrowed("b")),
|
||||||
|
Event::Newline(Cow::Borrowed("\n")),
|
||||||
|
Event::Key(Cow::Borrowed("c")),
|
||||||
|
Event::Value(Cow::Borrowed("d")),
|
||||||
|
Event::Newline(Cow::Borrowed("\n")),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
sections.insert(
|
||||||
|
SectionId(1),
|
||||||
|
vec![
|
||||||
|
Event::Key(Cow::Borrowed("e")),
|
||||||
|
Event::Value(Cow::Borrowed("f")),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
sections
|
||||||
|
};
|
||||||
|
assert_eq!(config.sections, expected_sections);
|
||||||
|
assert_eq!(
|
||||||
|
config.section_order.make_contiguous(),
|
||||||
|
&[SectionId(0), SectionId(1)]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_multiple_duplicate_sections() {
|
||||||
|
let mut 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(
|
||||||
|
Cow::Borrowed("core"),
|
||||||
vec![LookupTreeNode::Terminal(vec![SectionId(0), SectionId(1)])],
|
vec![LookupTreeNode::Terminal(vec![SectionId(0), SectionId(1)])],
|
||||||
);
|
);
|
||||||
tree
|
tree
|
||||||
|
@ -617,19 +672,29 @@ mod from_parser {
|
||||||
sections.insert(
|
sections.insert(
|
||||||
SectionId(0),
|
SectionId(0),
|
||||||
vec![
|
vec![
|
||||||
Event::Newline("\n"),
|
Event::Newline(Cow::Borrowed("\n")),
|
||||||
Event::Key("a"),
|
Event::Key(Cow::Borrowed("a")),
|
||||||
Event::Value("b"),
|
Event::Value(Cow::Borrowed("b")),
|
||||||
Event::Newline("\n"),
|
Event::Newline(Cow::Borrowed("\n")),
|
||||||
Event::Key("c"),
|
Event::Key(Cow::Borrowed("c")),
|
||||||
Event::Value("d"),
|
Event::Value(Cow::Borrowed("d")),
|
||||||
Event::Newline("\n"),
|
Event::Newline(Cow::Borrowed("\n")),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
sections.insert(
|
||||||
|
SectionId(1),
|
||||||
|
vec![
|
||||||
|
Event::Key(Cow::Borrowed("e")),
|
||||||
|
Event::Value(Cow::Borrowed("f")),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
sections.insert(SectionId(1), vec![Event::Key("e"), Event::Value("f")]);
|
|
||||||
sections
|
sections
|
||||||
};
|
};
|
||||||
assert_eq!(config.sections, expected_sections);
|
assert_eq!(config.sections, expected_sections);
|
||||||
|
assert_eq!(
|
||||||
|
config.section_order.make_contiguous(),
|
||||||
|
&[SectionId(0), SectionId(1)]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -640,20 +705,32 @@ mod get_raw_value {
|
||||||
#[test]
|
#[test]
|
||||||
fn single_section() {
|
fn single_section() {
|
||||||
let config = GitConfig::from_str("[core]\na=b\nc=d").unwrap();
|
let config = GitConfig::from_str("[core]\na=b\nc=d").unwrap();
|
||||||
assert_eq!(config.get_raw_value("core", None, "a"), Ok("b"));
|
assert_eq!(
|
||||||
assert_eq!(config.get_raw_value("core", None, "c"), Ok("d"));
|
config.get_raw_value("core", None, "a"),
|
||||||
|
Ok(&Cow::Borrowed("b"))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
config.get_raw_value("core", None, "c"),
|
||||||
|
Ok(&Cow::Borrowed("d"))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn last_one_wins_respected_in_section() {
|
fn last_one_wins_respected_in_section() {
|
||||||
let config = GitConfig::from_str("[core]\na=b\na=d").unwrap();
|
let config = GitConfig::from_str("[core]\na=b\na=d").unwrap();
|
||||||
assert_eq!(config.get_raw_value("core", None, "a"), Ok("d"));
|
assert_eq!(
|
||||||
|
config.get_raw_value("core", None, "a"),
|
||||||
|
Ok(&Cow::Borrowed("d"))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn last_one_wins_respected_across_section() {
|
fn last_one_wins_respected_across_section() {
|
||||||
let config = GitConfig::from_str("[core]\na=b\n[core]\na=d").unwrap();
|
let config = GitConfig::from_str("[core]\na=b\n[core]\na=d").unwrap();
|
||||||
assert_eq!(config.get_raw_value("core", None, "a"), Ok("d"));
|
assert_eq!(
|
||||||
|
config.get_raw_value("core", None, "a"),
|
||||||
|
Ok(&Cow::Borrowed("d"))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -686,8 +763,14 @@ mod get_raw_value {
|
||||||
#[test]
|
#[test]
|
||||||
fn subsection_must_be_respected() {
|
fn subsection_must_be_respected() {
|
||||||
let config = GitConfig::from_str("[core]a=b\n[core.a]a=c").unwrap();
|
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!(
|
||||||
assert_eq!(config.get_raw_value("core", Some("a"), "a"), Ok("c"));
|
config.get_raw_value("core", None, "a"),
|
||||||
|
Ok(&Cow::Borrowed("b"))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
config.get_raw_value("core", Some("a"), "a"),
|
||||||
|
Ok(&Cow::Borrowed("c"))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
#![forbid(unsafe_code)]
|
|
||||||
|
|
||||||
// mod de;
|
// mod de;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
mod error;
|
mod error;
|
||||||
|
|
304
src/parser.rs
304
src/parser.rs
|
@ -17,7 +17,7 @@ use nom::error::{Error as NomError, ErrorKind};
|
||||||
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};
|
||||||
use std::iter::FusedIterator;
|
use std::{borrow::Cow, iter::FusedIterator};
|
||||||
|
|
||||||
/// Syntactic events that occurs in the config.
|
/// Syntactic events that occurs in the config.
|
||||||
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
|
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
|
||||||
|
@ -30,25 +30,25 @@ pub enum Event<'a> {
|
||||||
/// exists.
|
/// exists.
|
||||||
SectionHeader(ParsedSectionHeader<'a>),
|
SectionHeader(ParsedSectionHeader<'a>),
|
||||||
/// A name to a value in a section.
|
/// A name to a value in a section.
|
||||||
Key(&'a str),
|
Key(Cow<'a, str>),
|
||||||
/// A completed value. This may be any string, including the empty string,
|
/// A completed value. This may be any string, including the empty string,
|
||||||
/// if an implicit boolean value is used. Note that these values may contain
|
/// if an implicit boolean value is used. Note that these values may contain
|
||||||
/// spaces and any special character. This value is also unprocessed, so it
|
/// spaces and any special character. This value is also unprocessed, so it
|
||||||
/// it may contain double quotes that should be replaced.
|
/// it may contain double quotes that should be replaced.
|
||||||
Value(&'a str),
|
Value(Cow<'a, str>),
|
||||||
/// Represents any token used to signify a new line character. On Unix
|
/// Represents any token used to signify a new line character. On Unix
|
||||||
/// platforms, this is typically just `\n`, but can be any valid newline
|
/// platforms, this is typically just `\n`, but can be any valid newline
|
||||||
/// sequence.
|
/// sequence.
|
||||||
Newline(&'a str),
|
Newline(Cow<'a, str>),
|
||||||
/// Any value that isn't completed. This occurs when the value is continued
|
/// Any value that isn't completed. This occurs when the value is continued
|
||||||
/// onto the next line. A Newline event is guaranteed after, followed by
|
/// onto the next line. A Newline event is guaranteed after, followed by
|
||||||
/// either a ValueDone, a Whitespace, or another ValueNotDone.
|
/// either a ValueDone, a Whitespace, or another ValueNotDone.
|
||||||
ValueNotDone(&'a str),
|
ValueNotDone(Cow<'a, str>),
|
||||||
/// The last line of a value which was continued onto another line.
|
/// The last line of a value which was continued onto another line.
|
||||||
ValueDone(&'a str),
|
ValueDone(Cow<'a, str>),
|
||||||
/// A continuous section of insignificant whitespace. Values with internal
|
/// A continuous section of insignificant whitespace. Values with internal
|
||||||
/// spaces will not be separated by this event.
|
/// spaces will not be separated by this event.
|
||||||
Whitespace(&'a str),
|
Whitespace(Cow<'a, str>),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A parsed section containing the header and the section events.
|
/// A parsed section containing the header and the section events.
|
||||||
|
@ -61,27 +61,27 @@ pub struct ParsedSection<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A parsed section header, containing a name and optionally a subsection name.
|
/// A parsed section header, containing a name and optionally a subsection name.
|
||||||
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
|
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
|
||||||
pub struct ParsedSectionHeader<'a> {
|
pub struct ParsedSectionHeader<'a> {
|
||||||
/// The name of the header.
|
/// The name of the header.
|
||||||
pub name: &'a str,
|
pub name: Cow<'a, str>,
|
||||||
/// The separator used to determine if the section contains a subsection.
|
/// The separator used to determine if the section contains a subsection.
|
||||||
/// This is either a period `.` or a string of whitespace. Note that
|
/// This is either a period `.` or a string of whitespace. Note that
|
||||||
/// reconstruction of subsection format is dependent on this value. If this
|
/// reconstruction of subsection format is dependent on this value. If this
|
||||||
/// is all whitespace, then the subsection name needs to be surrounded by
|
/// is all whitespace, then the subsection name needs to be surrounded by
|
||||||
/// quotes to have perfect reconstruction.
|
/// quotes to have perfect reconstruction.
|
||||||
pub separator: Option<&'a str>,
|
pub separator: Option<Cow<'a, str>>,
|
||||||
/// The subsection name without quotes if any exist.
|
/// The subsection name without quotes if any exist.
|
||||||
pub subsection_name: Option<&'a str>,
|
pub subsection_name: Option<Cow<'a, str>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A parsed comment event containing the comment marker and comment.
|
/// A parsed comment event containing the comment marker and comment.
|
||||||
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
|
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
|
||||||
pub struct ParsedComment<'a> {
|
pub struct ParsedComment<'a> {
|
||||||
/// The comment marker used. This is either a semicolon or octothorpe.
|
/// The comment marker used. This is either a semicolon or octothorpe.
|
||||||
pub comment_tag: char,
|
pub comment_tag: char,
|
||||||
/// The parsed comment.
|
/// The parsed comment.
|
||||||
pub comment: &'a str,
|
pub comment: Cow<'a, str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The various parsing failure reasons.
|
/// The various parsing failure reasons.
|
||||||
|
@ -176,20 +176,21 @@ impl<'a> From<nom::Err<NomError<&'a str>>> for ParserError<'a> {
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use serde_git_config::parser::{Event, ParsedSectionHeader, parse_from_str};
|
/// # use serde_git_config::parser::{Event, ParsedSectionHeader, parse_from_str};
|
||||||
|
/// # use std::borrow::Cow;
|
||||||
/// # let section_header = ParsedSectionHeader {
|
/// # let section_header = ParsedSectionHeader {
|
||||||
/// # name: "core",
|
/// # name: Cow::Borrowed("core"),
|
||||||
/// # separator: None,
|
/// # separator: None,
|
||||||
/// # subsection_name: None,
|
/// # subsection_name: None,
|
||||||
/// # };
|
/// # };
|
||||||
/// # let section_data = "[core]\n autocrlf = input";
|
/// # let section_data = "[core]\n autocrlf = input";
|
||||||
/// # assert_eq!(parse_from_str(section_data).unwrap().into_vec(), vec![
|
/// # assert_eq!(parse_from_str(section_data).unwrap().into_vec(), vec![
|
||||||
/// Event::SectionHeader(section_header),
|
/// Event::SectionHeader(section_header),
|
||||||
/// Event::Newline("\n"),
|
/// Event::Newline(Cow::Borrowed("\n")),
|
||||||
/// Event::Whitespace(" "),
|
/// Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
/// Event::Key("autocrlf"),
|
/// Event::Key(Cow::Borrowed("autocrlf")),
|
||||||
/// Event::Whitespace(" "),
|
/// Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
/// Event::Whitespace(" "),
|
/// Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
/// Event::Value("input"),
|
/// Event::Value(Cow::Borrowed("input")),
|
||||||
/// # ]);
|
/// # ]);
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
@ -216,20 +217,21 @@ impl<'a> From<nom::Err<NomError<&'a str>>> for ParserError<'a> {
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use serde_git_config::parser::{Event, ParsedSectionHeader, parse_from_str};
|
/// # use serde_git_config::parser::{Event, ParsedSectionHeader, parse_from_str};
|
||||||
|
/// # use std::borrow::Cow;
|
||||||
/// # let section_header = ParsedSectionHeader {
|
/// # let section_header = ParsedSectionHeader {
|
||||||
/// # name: "core",
|
/// # name: Cow::Borrowed("core"),
|
||||||
/// # separator: None,
|
/// # separator: None,
|
||||||
/// # subsection_name: None,
|
/// # subsection_name: None,
|
||||||
/// # };
|
/// # };
|
||||||
/// # let section_data = "[core]\nautocrlf=true\"\"\nfilemode=fa\"lse\"";
|
/// # let section_data = "[core]\nautocrlf=true\"\"\nfilemode=fa\"lse\"";
|
||||||
/// # assert_eq!(parse_from_str(section_data).unwrap().into_vec(), vec![
|
/// # assert_eq!(parse_from_str(section_data).unwrap().into_vec(), vec![
|
||||||
/// Event::SectionHeader(section_header),
|
/// Event::SectionHeader(section_header),
|
||||||
/// Event::Newline("\n"),
|
/// Event::Newline(Cow::Borrowed("\n")),
|
||||||
/// Event::Key("autocrlf"),
|
/// Event::Key(Cow::Borrowed("autocrlf")),
|
||||||
/// Event::Value(r#"true"""#),
|
/// Event::Value(Cow::Borrowed(r#"true"""#)),
|
||||||
/// Event::Newline("\n"),
|
/// Event::Newline(Cow::Borrowed("\n")),
|
||||||
/// Event::Key("filemode"),
|
/// Event::Key(Cow::Borrowed("filemode")),
|
||||||
/// Event::Value(r#"fa"lse""#),
|
/// Event::Value(Cow::Borrowed(r#"fa"lse""#)),
|
||||||
/// # ]);
|
/// # ]);
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
@ -250,19 +252,20 @@ impl<'a> From<nom::Err<NomError<&'a str>>> for ParserError<'a> {
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use serde_git_config::parser::{Event, ParsedSectionHeader, parse_from_str};
|
/// # use serde_git_config::parser::{Event, ParsedSectionHeader, parse_from_str};
|
||||||
|
/// # use std::borrow::Cow;
|
||||||
/// # let section_header = ParsedSectionHeader {
|
/// # let section_header = ParsedSectionHeader {
|
||||||
/// # name: "some-section",
|
/// # name: Cow::Borrowed("some-section"),
|
||||||
/// # separator: None,
|
/// # separator: None,
|
||||||
/// # subsection_name: None,
|
/// # subsection_name: None,
|
||||||
/// # };
|
/// # };
|
||||||
/// # let section_data = "[some-section]\nfile=a\\\n c";
|
/// # let section_data = "[some-section]\nfile=a\\\n c";
|
||||||
/// # assert_eq!(parse_from_str(section_data).unwrap().into_vec(), vec![
|
/// # assert_eq!(parse_from_str(section_data).unwrap().into_vec(), vec![
|
||||||
/// Event::SectionHeader(section_header),
|
/// Event::SectionHeader(section_header),
|
||||||
/// Event::Newline("\n"),
|
/// Event::Newline(Cow::Borrowed("\n")),
|
||||||
/// Event::Key("file"),
|
/// Event::Key(Cow::Borrowed("file")),
|
||||||
/// Event::ValueNotDone("a"),
|
/// Event::ValueNotDone(Cow::Borrowed("a")),
|
||||||
/// Event::Newline("\n"),
|
/// Event::Newline(Cow::Borrowed("\n")),
|
||||||
/// Event::ValueDone(" c"),
|
/// Event::ValueDone(Cow::Borrowed(" c")),
|
||||||
/// # ]);
|
/// # ]);
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
@ -380,7 +383,7 @@ fn comment<'a>(i: &'a str) -> IResult<&'a str, ParsedComment<'a>> {
|
||||||
i,
|
i,
|
||||||
ParsedComment {
|
ParsedComment {
|
||||||
comment_tag,
|
comment_tag,
|
||||||
comment,
|
comment: Cow::Borrowed(comment),
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -388,10 +391,14 @@ fn comment<'a>(i: &'a str) -> IResult<&'a str, ParsedComment<'a>> {
|
||||||
fn section<'a>(i: &'a str) -> IResult<&'a str, ParsedSection<'a>> {
|
fn section<'a>(i: &'a str) -> IResult<&'a str, ParsedSection<'a>> {
|
||||||
let (i, section_header) = section_header(i)?;
|
let (i, section_header) = section_header(i)?;
|
||||||
let (i, items) = many0(alt((
|
let (i, items) = many0(alt((
|
||||||
map(take_spaces, |space| vec![Event::Whitespace(space)]),
|
map(take_spaces, |space| {
|
||||||
map(take_newline, |newline| vec![Event::Newline(newline)]),
|
vec![Event::Whitespace(Cow::Borrowed(space))]
|
||||||
|
}),
|
||||||
|
map(take_newline, |newline| {
|
||||||
|
vec![Event::Newline(Cow::Borrowed(newline))]
|
||||||
|
}),
|
||||||
map(section_body, |(key, values)| {
|
map(section_body, |(key, values)| {
|
||||||
let mut vec = vec![Event::Key(key)];
|
let mut vec = vec![Event::Key(Cow::Borrowed(key))];
|
||||||
vec.extend(values);
|
vec.extend(values);
|
||||||
vec
|
vec
|
||||||
}),
|
}),
|
||||||
|
@ -416,12 +423,12 @@ fn section_header<'a>(i: &'a str) -> IResult<&'a str, ParsedSectionHeader<'a>> {
|
||||||
// subsection syntax at this point.
|
// subsection syntax at this point.
|
||||||
let header = match name.rfind('.') {
|
let header = match name.rfind('.') {
|
||||||
Some(index) => ParsedSectionHeader {
|
Some(index) => ParsedSectionHeader {
|
||||||
name: &name[..index],
|
name: Cow::Borrowed(&name[..index]),
|
||||||
separator: name.get(index..index + 1),
|
separator: name.get(index..index + 1).map(Cow::Borrowed),
|
||||||
subsection_name: name.get(index + 1..),
|
subsection_name: name.get(index + 1..).map(Cow::Borrowed),
|
||||||
},
|
},
|
||||||
None => ParsedSectionHeader {
|
None => ParsedSectionHeader {
|
||||||
name: name,
|
name: Cow::Borrowed(name),
|
||||||
separator: None,
|
separator: None,
|
||||||
subsection_name: None,
|
subsection_name: None,
|
||||||
},
|
},
|
||||||
|
@ -443,11 +450,11 @@ fn section_header<'a>(i: &'a str) -> IResult<&'a str, ParsedSectionHeader<'a>> {
|
||||||
Ok((
|
Ok((
|
||||||
i,
|
i,
|
||||||
ParsedSectionHeader {
|
ParsedSectionHeader {
|
||||||
name: name,
|
name: Cow::Borrowed(name),
|
||||||
separator: Some(whitespace),
|
separator: Some(Cow::Borrowed(whitespace)),
|
||||||
// We know that there's some section name here, so if we get an
|
// We know that there's some section name here, so if we get an
|
||||||
// empty vec here then we actually parsed an empty section name.
|
// empty vec here then we actually parsed an empty section name.
|
||||||
subsection_name: subsection_name.or(Some("")),
|
subsection_name: subsection_name.or(Some("")).map(Cow::Borrowed),
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -458,7 +465,7 @@ fn section_body<'a>(i: &'a str) -> IResult<&'a str, (&'a str, Vec<Event<'a>>)> {
|
||||||
let (i, whitespace) = opt(take_spaces)(i)?;
|
let (i, whitespace) = opt(take_spaces)(i)?;
|
||||||
let (i, value) = config_value(i)?;
|
let (i, value) = config_value(i)?;
|
||||||
if let Some(whitespace) = whitespace {
|
if let Some(whitespace) = whitespace {
|
||||||
let mut events = vec![Event::Whitespace(whitespace)];
|
let mut events = vec![Event::Whitespace(Cow::Borrowed(whitespace))];
|
||||||
events.extend(value);
|
events.extend(value);
|
||||||
Ok((i, (name, events)))
|
Ok((i, (name, events)))
|
||||||
} else {
|
} else {
|
||||||
|
@ -491,14 +498,14 @@ fn config_value<'a>(i: &'a str) -> IResult<&'a str, Vec<Event<'a>>> {
|
||||||
let (i, whitespace) = opt(take_spaces)(i)?;
|
let (i, whitespace) = opt(take_spaces)(i)?;
|
||||||
let (i, values) = value_impl(i)?;
|
let (i, values) = value_impl(i)?;
|
||||||
if let Some(whitespace) = whitespace {
|
if let Some(whitespace) = whitespace {
|
||||||
let mut events = vec![Event::Whitespace(whitespace)];
|
let mut events = vec![Event::Whitespace(Cow::Borrowed(whitespace))];
|
||||||
events.extend(values);
|
events.extend(values);
|
||||||
Ok((i, events))
|
Ok((i, events))
|
||||||
} else {
|
} else {
|
||||||
Ok((i, values))
|
Ok((i, values))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Ok((i, vec![Event::Value("")]))
|
Ok((i, vec![Event::Value(Cow::Borrowed(""))]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -521,8 +528,8 @@ fn value_impl<'a>(i: &'a str) -> IResult<&'a str, Vec<Event<'a>>> {
|
||||||
// continuation.
|
// continuation.
|
||||||
b'\n' => {
|
b'\n' => {
|
||||||
partial_value_found = true;
|
partial_value_found = true;
|
||||||
events.push(Event::ValueNotDone(&i[offset..index - 1]));
|
events.push(Event::ValueNotDone(Cow::Borrowed(&i[offset..index - 1])));
|
||||||
events.push(Event::Newline(&i[index..index + 1]));
|
events.push(Event::Newline(Cow::Borrowed(&i[index..index + 1])));
|
||||||
offset = index + 1;
|
offset = index + 1;
|
||||||
parsed_index = 0;
|
parsed_index = 0;
|
||||||
}
|
}
|
||||||
|
@ -583,9 +590,9 @@ fn value_impl<'a>(i: &'a str) -> IResult<&'a str, Vec<Event<'a>>> {
|
||||||
};
|
};
|
||||||
|
|
||||||
if partial_value_found {
|
if partial_value_found {
|
||||||
events.push(Event::ValueDone(remainder_value));
|
events.push(Event::ValueDone(Cow::Borrowed(remainder_value)));
|
||||||
} else {
|
} else {
|
||||||
events.push(Event::Value(remainder_value));
|
events.push(Event::Value(Cow::Borrowed(remainder_value)));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok((i, events))
|
Ok((i, events))
|
||||||
|
@ -625,11 +632,12 @@ 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<'_> {
|
||||||
|
let name = Cow::Borrowed(name);
|
||||||
if let Some((separator, subsection_name)) = subsection.into() {
|
if let Some((separator, subsection_name)) = subsection.into() {
|
||||||
ParsedSectionHeader {
|
ParsedSectionHeader {
|
||||||
name,
|
name,
|
||||||
separator: Some(separator),
|
separator: Some(separator).map(Cow::Borrowed),
|
||||||
subsection_name: Some(subsection_name),
|
subsection_name: Some(subsection_name).map(Cow::Borrowed),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ParsedSectionHeader {
|
ParsedSectionHeader {
|
||||||
|
@ -650,7 +658,7 @@ mod comments {
|
||||||
comment("; this is a semicolon comment").unwrap(),
|
comment("; this is a semicolon comment").unwrap(),
|
||||||
fully_consumed(ParsedComment {
|
fully_consumed(ParsedComment {
|
||||||
comment_tag: ';',
|
comment_tag: ';',
|
||||||
comment: " this is a semicolon comment",
|
comment: Cow::Borrowed(" this is a semicolon comment"),
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -661,7 +669,7 @@ mod comments {
|
||||||
comment("# this is an octothorpe comment").unwrap(),
|
comment("# this is an octothorpe comment").unwrap(),
|
||||||
fully_consumed(ParsedComment {
|
fully_consumed(ParsedComment {
|
||||||
comment_tag: '#',
|
comment_tag: '#',
|
||||||
comment: " this is an octothorpe comment",
|
comment: Cow::Borrowed(" this is an octothorpe comment"),
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -672,7 +680,7 @@ mod comments {
|
||||||
comment("###### this is an octothorpe comment").unwrap(),
|
comment("###### this is an octothorpe comment").unwrap(),
|
||||||
fully_consumed(ParsedComment {
|
fully_consumed(ParsedComment {
|
||||||
comment_tag: '#',
|
comment_tag: '#',
|
||||||
comment: "##### this is an octothorpe comment",
|
comment: Cow::Borrowed("##### this is an octothorpe comment"),
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -778,7 +786,7 @@ mod value_no_continuation {
|
||||||
fn no_comment() {
|
fn no_comment() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
value_impl("hello").unwrap(),
|
value_impl("hello").unwrap(),
|
||||||
fully_consumed(vec![Event::Value("hello")])
|
fully_consumed(vec![Event::Value(Cow::Borrowed("hello"))])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -786,7 +794,7 @@ mod value_no_continuation {
|
||||||
fn no_comment_newline() {
|
fn no_comment_newline() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
value_impl("hello\na").unwrap(),
|
value_impl("hello\na").unwrap(),
|
||||||
("\na", vec![Event::Value("hello")])
|
("\na", vec![Event::Value(Cow::Borrowed("hello"))])
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -794,7 +802,7 @@ mod value_no_continuation {
|
||||||
fn semicolon_comment_not_consumed() {
|
fn semicolon_comment_not_consumed() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
value_impl("hello;world").unwrap(),
|
value_impl("hello;world").unwrap(),
|
||||||
(";world", vec![Event::Value("hello"),])
|
(";world", vec![Event::Value(Cow::Borrowed("hello")),])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -802,7 +810,7 @@ mod value_no_continuation {
|
||||||
fn octothorpe_comment_not_consumed() {
|
fn octothorpe_comment_not_consumed() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
value_impl("hello#world").unwrap(),
|
value_impl("hello#world").unwrap(),
|
||||||
("#world", vec![Event::Value("hello"),])
|
("#world", vec![Event::Value(Cow::Borrowed("hello")),])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -810,7 +818,10 @@ mod value_no_continuation {
|
||||||
fn values_with_extraneous_whitespace_without_comment() {
|
fn values_with_extraneous_whitespace_without_comment() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
value_impl("hello ").unwrap(),
|
value_impl("hello ").unwrap(),
|
||||||
(" ", vec![Event::Value("hello")])
|
(
|
||||||
|
" ",
|
||||||
|
vec![Event::Value(Cow::Borrowed("hello"))]
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -818,11 +829,17 @@ mod value_no_continuation {
|
||||||
fn values_with_extraneous_whitespace_before_comment() {
|
fn values_with_extraneous_whitespace_before_comment() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
value_impl("hello #world").unwrap(),
|
value_impl("hello #world").unwrap(),
|
||||||
(" #world", vec![Event::Value("hello"),])
|
(
|
||||||
|
" #world",
|
||||||
|
vec![Event::Value(Cow::Borrowed("hello"))]
|
||||||
|
)
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
value_impl("hello ;world").unwrap(),
|
value_impl("hello ;world").unwrap(),
|
||||||
(" ;world", vec![Event::Value("hello"),])
|
(
|
||||||
|
" ;world",
|
||||||
|
vec![Event::Value(Cow::Borrowed("hello"))]
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -830,7 +847,10 @@ mod value_no_continuation {
|
||||||
fn trans_escaped_comment_marker_not_consumed() {
|
fn trans_escaped_comment_marker_not_consumed() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
value_impl(r##"hello"#"world; a"##).unwrap(),
|
value_impl(r##"hello"#"world; a"##).unwrap(),
|
||||||
("; a", vec![Event::Value(r##"hello"#"world"##)])
|
(
|
||||||
|
"; a",
|
||||||
|
vec![Event::Value(Cow::Borrowed(r##"hello"#"world"##))]
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -838,7 +858,7 @@ mod value_no_continuation {
|
||||||
fn complex_test() {
|
fn complex_test() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
value_impl(r#"value";";ahhhh"#).unwrap(),
|
value_impl(r#"value";";ahhhh"#).unwrap(),
|
||||||
(";ahhhh", vec![Event::Value(r#"value";""#)])
|
(";ahhhh", vec![Event::Value(Cow::Borrowed(r#"value";""#))])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -857,9 +877,9 @@ mod value_continuation {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
value_impl("hello\\\nworld").unwrap(),
|
value_impl("hello\\\nworld").unwrap(),
|
||||||
fully_consumed(vec![
|
fully_consumed(vec![
|
||||||
Event::ValueNotDone("hello"),
|
Event::ValueNotDone(Cow::Borrowed("hello")),
|
||||||
Event::Newline("\n"),
|
Event::Newline(Cow::Borrowed("\n")),
|
||||||
Event::ValueDone("world")
|
Event::ValueDone(Cow::Borrowed("world"))
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -869,9 +889,9 @@ mod value_continuation {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
value_impl("hello\\\n world").unwrap(),
|
value_impl("hello\\\n world").unwrap(),
|
||||||
fully_consumed(vec![
|
fully_consumed(vec![
|
||||||
Event::ValueNotDone("hello"),
|
Event::ValueNotDone(Cow::Borrowed("hello")),
|
||||||
Event::Newline("\n"),
|
Event::Newline(Cow::Borrowed("\n")),
|
||||||
Event::ValueDone(" world")
|
Event::ValueDone(Cow::Borrowed(" world"))
|
||||||
])
|
])
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -883,11 +903,11 @@ mod value_continuation {
|
||||||
(
|
(
|
||||||
" # \"b\t ; c",
|
" # \"b\t ; c",
|
||||||
vec![
|
vec![
|
||||||
Event::ValueNotDone(r#"1 "\""#),
|
Event::ValueNotDone(Cow::Borrowed(r#"1 "\""#)),
|
||||||
Event::Newline("\n"),
|
Event::Newline(Cow::Borrowed("\n")),
|
||||||
Event::ValueNotDone(r#"a ; e "\""#),
|
Event::ValueNotDone(Cow::Borrowed(r#"a ; e "\""#)),
|
||||||
Event::Newline("\n"),
|
Event::Newline(Cow::Borrowed("\n")),
|
||||||
Event::ValueDone("d"),
|
Event::ValueDone(Cow::Borrowed("d")),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
@ -900,9 +920,9 @@ mod value_continuation {
|
||||||
(
|
(
|
||||||
";a",
|
";a",
|
||||||
vec![
|
vec![
|
||||||
Event::ValueNotDone("\""),
|
Event::ValueNotDone(Cow::Borrowed("\"")),
|
||||||
Event::Newline("\n"),
|
Event::Newline(Cow::Borrowed("\n")),
|
||||||
Event::ValueDone(";\""),
|
Event::ValueDone(Cow::Borrowed(";\"")),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -935,22 +955,22 @@ mod section {
|
||||||
fully_consumed(ParsedSection {
|
fully_consumed(ParsedSection {
|
||||||
section_header: gen_section_header("hello", None),
|
section_header: gen_section_header("hello", None),
|
||||||
events: vec![
|
events: vec![
|
||||||
Event::Newline("\n"),
|
Event::Newline(Cow::Borrowed("\n")),
|
||||||
Event::Whitespace(" "),
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
Event::Key("a"),
|
Event::Key(Cow::Borrowed("a")),
|
||||||
Event::Whitespace(" "),
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
Event::Whitespace(" "),
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
Event::Value("b"),
|
Event::Value(Cow::Borrowed("b")),
|
||||||
Event::Newline("\n"),
|
Event::Newline(Cow::Borrowed("\n")),
|
||||||
Event::Whitespace(" "),
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
Event::Key("c"),
|
Event::Key(Cow::Borrowed("c")),
|
||||||
Event::Value(""),
|
Event::Value(Cow::Borrowed("")),
|
||||||
Event::Newline("\n"),
|
Event::Newline(Cow::Borrowed("\n")),
|
||||||
Event::Whitespace(" "),
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
Event::Key("d"),
|
Event::Key(Cow::Borrowed("d")),
|
||||||
Event::Whitespace(" "),
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
Event::Whitespace(" "),
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
Event::Value("\"lol\"")
|
Event::Value(Cow::Borrowed("\"lol\""))
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
@ -962,7 +982,11 @@ mod section {
|
||||||
section("[hello] c").unwrap(),
|
section("[hello] c").unwrap(),
|
||||||
fully_consumed(ParsedSection {
|
fully_consumed(ParsedSection {
|
||||||
section_header: gen_section_header("hello", None),
|
section_header: gen_section_header("hello", None),
|
||||||
events: vec![Event::Whitespace(" "), Event::Key("c"), Event::Value("")]
|
events: vec![
|
||||||
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
|
Event::Key(Cow::Borrowed("c")),
|
||||||
|
Event::Value(Cow::Borrowed(""))
|
||||||
|
]
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -979,40 +1003,40 @@ mod section {
|
||||||
fully_consumed(ParsedSection {
|
fully_consumed(ParsedSection {
|
||||||
section_header: gen_section_header("hello", None),
|
section_header: gen_section_header("hello", None),
|
||||||
events: vec![
|
events: vec![
|
||||||
Event::Whitespace(" "),
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
Event::Comment(ParsedComment {
|
Event::Comment(ParsedComment {
|
||||||
comment_tag: ';',
|
comment_tag: ';',
|
||||||
comment: " commentA",
|
comment: Cow::Borrowed(" commentA"),
|
||||||
}),
|
}),
|
||||||
Event::Newline("\n"),
|
Event::Newline(Cow::Borrowed("\n")),
|
||||||
Event::Whitespace(" "),
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
Event::Key("a"),
|
Event::Key(Cow::Borrowed("a")),
|
||||||
Event::Whitespace(" "),
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
Event::Whitespace(" "),
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
Event::Value("b"),
|
Event::Value(Cow::Borrowed("b")),
|
||||||
Event::Whitespace(" "),
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
Event::Comment(ParsedComment {
|
Event::Comment(ParsedComment {
|
||||||
comment_tag: '#',
|
comment_tag: '#',
|
||||||
comment: " commentB",
|
comment: Cow::Borrowed(" commentB"),
|
||||||
}),
|
}),
|
||||||
Event::Newline("\n"),
|
Event::Newline(Cow::Borrowed("\n")),
|
||||||
Event::Whitespace(" "),
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
Event::Comment(ParsedComment {
|
Event::Comment(ParsedComment {
|
||||||
comment_tag: ';',
|
comment_tag: ';',
|
||||||
comment: " commentC",
|
comment: Cow::Borrowed(" commentC"),
|
||||||
}),
|
}),
|
||||||
Event::Newline("\n"),
|
Event::Newline(Cow::Borrowed("\n")),
|
||||||
Event::Whitespace(" "),
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
Event::Comment(ParsedComment {
|
Event::Comment(ParsedComment {
|
||||||
comment_tag: ';',
|
comment_tag: ';',
|
||||||
comment: " commentD",
|
comment: Cow::Borrowed(" commentD"),
|
||||||
}),
|
}),
|
||||||
Event::Newline("\n"),
|
Event::Newline(Cow::Borrowed("\n")),
|
||||||
Event::Whitespace(" "),
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
Event::Key("c"),
|
Event::Key(Cow::Borrowed("c")),
|
||||||
Event::Whitespace(" "),
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
Event::Whitespace(" "),
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
Event::Value("d"),
|
Event::Value(Cow::Borrowed("d")),
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
@ -1026,19 +1050,19 @@ mod section {
|
||||||
fully_consumed(ParsedSection {
|
fully_consumed(ParsedSection {
|
||||||
section_header: gen_section_header("section", None),
|
section_header: gen_section_header("section", None),
|
||||||
events: vec![
|
events: vec![
|
||||||
Event::Whitespace(" "),
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
Event::Key("a"),
|
Event::Key(Cow::Borrowed("a")),
|
||||||
Event::Whitespace(" "),
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
Event::Whitespace(" "),
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
Event::ValueNotDone(r#"1 "\""#),
|
Event::ValueNotDone(Cow::Borrowed(r#"1 "\""#)),
|
||||||
Event::Newline("\n"),
|
Event::Newline(Cow::Borrowed("\n")),
|
||||||
Event::ValueNotDone(r#"a ; e "\""#),
|
Event::ValueNotDone(Cow::Borrowed(r#"a ; e "\""#)),
|
||||||
Event::Newline("\n"),
|
Event::Newline(Cow::Borrowed("\n")),
|
||||||
Event::ValueDone("d"),
|
Event::ValueDone(Cow::Borrowed("d")),
|
||||||
Event::Whitespace(" "),
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
Event::Comment(ParsedComment {
|
Event::Comment(ParsedComment {
|
||||||
comment_tag: '#',
|
comment_tag: '#',
|
||||||
comment: " \"b\t ; c"
|
comment: Cow::Borrowed(" \"b\t ; c")
|
||||||
})
|
})
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
@ -1052,15 +1076,15 @@ mod section {
|
||||||
fully_consumed(ParsedSection {
|
fully_consumed(ParsedSection {
|
||||||
section_header: gen_section_header("section", (" ", "a")),
|
section_header: gen_section_header("section", (" ", "a")),
|
||||||
events: vec![
|
events: vec![
|
||||||
Event::Whitespace(" "),
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
Event::Key("b"),
|
Event::Key(Cow::Borrowed("b")),
|
||||||
Event::Whitespace(" "),
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
Event::ValueNotDone("\""),
|
Event::ValueNotDone(Cow::Borrowed("\"")),
|
||||||
Event::Newline("\n"),
|
Event::Newline(Cow::Borrowed("\n")),
|
||||||
Event::ValueDone(";\""),
|
Event::ValueDone(Cow::Borrowed(";\"")),
|
||||||
Event::Comment(ParsedComment {
|
Event::Comment(ParsedComment {
|
||||||
comment_tag: ';',
|
comment_tag: ';',
|
||||||
comment: "a",
|
comment: Cow::Borrowed("a"),
|
||||||
})
|
})
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
@ -1074,12 +1098,12 @@ mod section {
|
||||||
fully_consumed(ParsedSection {
|
fully_consumed(ParsedSection {
|
||||||
section_header: gen_section_header("s", None),
|
section_header: gen_section_header("s", None),
|
||||||
events: vec![
|
events: vec![
|
||||||
Event::Key("hello"),
|
Event::Key(Cow::Borrowed("hello")),
|
||||||
Event::Whitespace(" "),
|
Event::Whitespace(Cow::Borrowed(" ")),
|
||||||
Event::Value(""),
|
Event::Value(Cow::Borrowed("")),
|
||||||
Event::Comment(ParsedComment {
|
Event::Comment(ParsedComment {
|
||||||
comment_tag: '#',
|
comment_tag: '#',
|
||||||
comment: "world",
|
comment: Cow::Borrowed("world"),
|
||||||
}),
|
}),
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,15 +1,18 @@
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use serde_git_config::parser::{parse_from_str, Event, ParsedSectionHeader};
|
use serde_git_config::parser::{parse_from_str, Event, ParsedSectionHeader};
|
||||||
|
|
||||||
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)>>,
|
||||||
) -> Event<'_> {
|
) -> Event<'_> {
|
||||||
|
let name = Cow::Borrowed(name);
|
||||||
Event::SectionHeader(
|
Event::SectionHeader(
|
||||||
if let Some((separator, subsection_name)) = subsection.into() {
|
if let Some((separator, subsection_name)) = subsection.into() {
|
||||||
ParsedSectionHeader {
|
ParsedSectionHeader {
|
||||||
name,
|
name,
|
||||||
separator: Some(separator),
|
separator: Some(Cow::Borrowed(separator)),
|
||||||
subsection_name: Some(subsection_name),
|
subsection_name: Some(Cow::Borrowed(subsection_name)),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ParsedSectionHeader {
|
ParsedSectionHeader {
|
||||||
|
@ -21,19 +24,19 @@ fn gen_section_header(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
fn name(name: &'static str) -> Event<'static> {
|
fn name(name: &'static str) -> Event<'static> {
|
||||||
Event::Key(name)
|
Event::Key(Cow::Borrowed(name))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn value(value: &'static str) -> Event<'static> {
|
fn value(value: &'static str) -> Event<'static> {
|
||||||
Event::Value(value)
|
Event::Value(Cow::Borrowed(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn newline() -> Event<'static> {
|
fn newline() -> Event<'static> {
|
||||||
Event::Newline("\n")
|
Event::Newline(Cow::Borrowed("\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn whitespace(value: &'static str) -> Event<'static> {
|
fn whitespace(value: &'static str) -> Event<'static> {
|
||||||
Event::Whitespace(value)
|
Event::Whitespace(Cow::Borrowed(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Reference in a new issue