Compare commits
No commits in common. "cd2f58c920a7845bd54ba887bcbf8e1dade924ce" and "5720ccd003cfdfcc2b39679072aabdec47a300ab" have entirely different histories.
cd2f58c920
...
5720ccd003
4 changed files with 519 additions and 788 deletions
|
@ -16,7 +16,6 @@ exclude = ["fuzz/**/*", ".vscode/**/*"]
|
|||
serde = ["serde_crate"]
|
||||
|
||||
[dependencies]
|
||||
memchr = "2"
|
||||
nom = { version = "6", default_features = false, features = ["std"] }
|
||||
serde_crate = { version = "1", package = "serde", optional = true }
|
||||
|
||||
|
|
1051
src/file.rs
1051
src/file.rs
File diff suppressed because it is too large
Load diff
|
@ -700,7 +700,7 @@ fn section_header(i: &[u8]) -> IResult<&[u8], ParsedSectionHeader> {
|
|||
if let Ok((i, _)) = char::<_, NomError<&[u8]>>(']')(i) {
|
||||
// Either section does not have a subsection or using deprecated
|
||||
// subsection syntax at this point.
|
||||
let header = match memchr::memrchr(b'.', name.as_bytes()) {
|
||||
let header = match find_legacy_subsection_separator(name) {
|
||||
Some(index) => ParsedSectionHeader {
|
||||
name: Cow::Borrowed(&name[..index]),
|
||||
separator: name.get(index..=index).map(|slice| Cow::Borrowed(slice)),
|
||||
|
@ -747,6 +747,16 @@ fn section_header(i: &[u8]) -> IResult<&[u8], ParsedSectionHeader> {
|
|||
))
|
||||
}
|
||||
|
||||
fn find_legacy_subsection_separator(input: &str) -> Option<usize> {
|
||||
let input = input.as_bytes();
|
||||
for i in (0..input.len()).into_iter().rev() {
|
||||
if input[i] == b'.' {
|
||||
return Some(i);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn section_body<'a, 'b, 'c>(
|
||||
i: &'a [u8],
|
||||
node: &'b mut ParserNode,
|
||||
|
|
243
src/values.rs
243
src/values.rs
|
@ -21,62 +21,53 @@ use std::str::FromStr;
|
|||
/// need to call this yourself. However, if you're directly handling events
|
||||
/// from the parser, you may want to use this to help with value interpretation.
|
||||
///
|
||||
/// Generally speaking, you'll want to use one of the variants of this function,
|
||||
/// such as [`normalize_str`] or [`normalize_vec`].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Values don't need modification are returned borrowed, without allocation.
|
||||
///
|
||||
/// ```
|
||||
/// # use std::borrow::Cow;
|
||||
/// # use git_config::values::normalize_str;
|
||||
/// assert_eq!(normalize_str("hello world"), Cow::Borrowed(b"hello world".into()));
|
||||
/// # use git_config::values::normalize;
|
||||
/// assert_eq!(normalize(b"hello world"), Cow::Borrowed(b"hello world".into()));
|
||||
/// ```
|
||||
///
|
||||
/// Fully quoted values are optimized to not need allocations.
|
||||
///
|
||||
/// ```
|
||||
/// # use std::borrow::Cow;
|
||||
/// # use git_config::values::normalize_str;
|
||||
/// assert_eq!(normalize_str("\"hello world\""), Cow::Borrowed(b"hello world".into()));
|
||||
/// # use git_config::values::normalize;
|
||||
/// assert_eq!(normalize(b"\"hello world\""), Cow::Borrowed(b"hello world".into()));
|
||||
/// ```
|
||||
///
|
||||
/// Quoted values are unwrapped as an owned variant.
|
||||
///
|
||||
/// ```
|
||||
/// # use std::borrow::Cow;
|
||||
/// # use git_config::values::normalize_str;
|
||||
/// assert_eq!(normalize_str("hello \"world\""), Cow::<[u8]>::Owned(b"hello world".to_vec()));
|
||||
/// # use git_config::values::normalize;
|
||||
/// assert_eq!(normalize(b"hello \"world\""), Cow::<[u8]>::Owned(b"hello world".to_vec()));
|
||||
/// ```
|
||||
///
|
||||
/// Escaped quotes are unescaped.
|
||||
///
|
||||
/// ```
|
||||
/// # use std::borrow::Cow;
|
||||
/// # use git_config::values::normalize_str;
|
||||
/// assert_eq!(normalize_str(r#"hello "world\"""#), Cow::<[u8]>::Owned(br#"hello world""#.to_vec()));
|
||||
/// # use git_config::values::normalize;
|
||||
/// assert_eq!(normalize(br#"hello "world\"""#), Cow::<[u8]>::Owned(br#"hello world""#.to_vec()));
|
||||
/// ```
|
||||
///
|
||||
/// [`parser`]: crate::parser::Parser
|
||||
pub fn normalize_cow(input: Cow<'_, [u8]>) -> Cow<'_, [u8]> {
|
||||
pub fn normalize(input: &[u8]) -> Cow<'_, [u8]> {
|
||||
let mut first_index = 0;
|
||||
let mut last_index = 0;
|
||||
|
||||
let size = input.len();
|
||||
if &*input == b"\"\"" {
|
||||
|
||||
if input == b"\"\"" {
|
||||
return Cow::Borrowed(&[]);
|
||||
}
|
||||
|
||||
if size >= 3 && input[0] == b'=' && input[size - 1] == b'=' && input[size - 2] != b'\\' {
|
||||
match input {
|
||||
Cow::Borrowed(input) => return normalize_bytes(&input[1..size]),
|
||||
Cow::Owned(mut input) => {
|
||||
input.pop();
|
||||
input.remove(0);
|
||||
return normalize_vec(input);
|
||||
}
|
||||
}
|
||||
return normalize(&input[1..size]);
|
||||
}
|
||||
|
||||
let mut owned = vec![];
|
||||
|
@ -113,30 +104,12 @@ pub fn normalize_cow(input: Cow<'_, [u8]>) -> Cow<'_, [u8]> {
|
|||
|
||||
owned.extend(dbg!(&input[last_index..]));
|
||||
if owned.is_empty() {
|
||||
input
|
||||
Cow::Borrowed(input)
|
||||
} else {
|
||||
Cow::Owned(owned)
|
||||
}
|
||||
}
|
||||
|
||||
/// `&[u8]` variant of [`normalize_cow`].
|
||||
#[inline]
|
||||
pub fn normalize_bytes(input: &[u8]) -> Cow<'_, [u8]> {
|
||||
normalize_cow(Cow::Borrowed(input))
|
||||
}
|
||||
|
||||
/// `Vec[u8]` variant of [`normalize_cow`].
|
||||
#[inline]
|
||||
pub fn normalize_vec(input: Vec<u8>) -> Cow<'static, [u8]> {
|
||||
normalize_cow(Cow::Owned(input))
|
||||
}
|
||||
|
||||
/// [`str`] variant of [`normalize_cow`].
|
||||
#[inline]
|
||||
pub fn normalize_str(input: &str) -> Cow<'_, [u8]> {
|
||||
normalize_bytes(input.as_bytes())
|
||||
}
|
||||
|
||||
/// Fully enumerated valid types that a `git-config` value can be.
|
||||
#[allow(missing_docs)]
|
||||
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
|
||||
|
@ -179,35 +152,6 @@ impl<'a> From<&'a [u8]> for Value<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<String> for Value<'_> {
|
||||
fn from(s: String) -> Self {
|
||||
Self::from(s.into_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<u8>> for Value<'_> {
|
||||
fn from(s: Vec<u8>) -> Self {
|
||||
if let Ok(int) = Integer::try_from(s.as_ref()) {
|
||||
return Self::Integer(int);
|
||||
}
|
||||
|
||||
if let Ok(color) = Color::try_from(s.as_ref()) {
|
||||
return Self::Color(color);
|
||||
}
|
||||
|
||||
Boolean::try_from(s).map_or_else(|v| Self::Other(Cow::Owned(v)), Self::Boolean)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<Cow<'a, [u8]>> for Value<'a> {
|
||||
fn from(c: Cow<'a, [u8]>) -> Self {
|
||||
match c {
|
||||
Cow::Borrowed(c) => Self::from(c),
|
||||
Cow::Owned(c) => Self::from(c),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// todo display for value
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
|
@ -231,11 +175,11 @@ impl Serialize for Value<'_> {
|
|||
/// documentation has a strict subset of values that may be interpreted as a
|
||||
/// boolean value, all of which are ASCII and thus UTF-8 representable.
|
||||
/// Consequently, variants hold [`str`]s rather than [`[u8]`]s.
|
||||
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
|
||||
#[allow(missing_docs)]
|
||||
pub enum Boolean<'a> {
|
||||
True(TrueVariant<'a>),
|
||||
False(Cow<'a, str>),
|
||||
False(&'a str),
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<&'a str> for Boolean<'a> {
|
||||
|
@ -260,48 +204,13 @@ impl<'a> TryFrom<&'a [u8]> for Boolean<'a> {
|
|||
|| value.eq_ignore_ascii_case(b"zero")
|
||||
|| value == b"\"\""
|
||||
{
|
||||
return Ok(Self::False(std::str::from_utf8(value).unwrap().into()));
|
||||
return Ok(Self::False(std::str::from_utf8(value).unwrap()));
|
||||
}
|
||||
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<String> for Boolean<'_> {
|
||||
type Error = String;
|
||||
|
||||
fn try_from(value: String) -> Result<Self, Self::Error> {
|
||||
Self::try_from(value.into_bytes()).map_err(|v| String::from_utf8(v).unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Vec<u8>> for Boolean<'_> {
|
||||
type Error = Vec<u8>;
|
||||
|
||||
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
|
||||
if value.eq_ignore_ascii_case(b"no")
|
||||
|| value.eq_ignore_ascii_case(b"off")
|
||||
|| value.eq_ignore_ascii_case(b"false")
|
||||
|| value.eq_ignore_ascii_case(b"zero")
|
||||
|| value == b"\"\""
|
||||
{
|
||||
return Ok(Self::False(Cow::Owned(String::from_utf8(value).unwrap())));
|
||||
}
|
||||
|
||||
TrueVariant::try_from(value).map(Self::True)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<Cow<'a, [u8]>> for Boolean<'a> {
|
||||
type Error = ();
|
||||
fn try_from(c: Cow<'a, [u8]>) -> Result<Self, Self::Error> {
|
||||
match c {
|
||||
Cow::Borrowed(c) => Self::try_from(c),
|
||||
Cow::Owned(c) => Self::try_from(c).map_err(|_| ()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Boolean<'_> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
|
@ -336,10 +245,10 @@ impl Serialize for Boolean<'_> {
|
|||
/// Discriminating enum between implicit and explicit truthy values.
|
||||
///
|
||||
/// This enum is part of the [`Boolean`] struct.
|
||||
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
|
||||
#[allow(missing_docs)]
|
||||
pub enum TrueVariant<'a> {
|
||||
Explicit(Cow<'a, str>),
|
||||
Explicit(&'a str),
|
||||
/// For values defined without a `= <value>`.
|
||||
Implicit,
|
||||
}
|
||||
|
@ -361,7 +270,7 @@ impl<'a> TryFrom<&'a [u8]> for TrueVariant<'a> {
|
|||
|| value.eq_ignore_ascii_case(b"true")
|
||||
|| value.eq_ignore_ascii_case(b"one")
|
||||
{
|
||||
Ok(Self::Explicit(std::str::from_utf8(value).unwrap().into()))
|
||||
Ok(Self::Explicit(std::str::from_utf8(value).unwrap()))
|
||||
} else if value.is_empty() {
|
||||
Ok(Self::Implicit)
|
||||
} else {
|
||||
|
@ -370,34 +279,6 @@ impl<'a> TryFrom<&'a [u8]> for TrueVariant<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl TryFrom<String> for TrueVariant<'_> {
|
||||
type Error = String;
|
||||
|
||||
fn try_from(value: String) -> Result<Self, Self::Error> {
|
||||
Self::try_from(value.into_bytes()).map_err(|v| String::from_utf8(v).unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Vec<u8>> for TrueVariant<'_> {
|
||||
type Error = Vec<u8>;
|
||||
|
||||
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
|
||||
if value.eq_ignore_ascii_case(b"yes")
|
||||
|| value.eq_ignore_ascii_case(b"on")
|
||||
|| value.eq_ignore_ascii_case(b"true")
|
||||
|| value.eq_ignore_ascii_case(b"one")
|
||||
{
|
||||
Ok(Self::Explicit(Cow::Owned(
|
||||
String::from_utf8(value).unwrap(),
|
||||
)))
|
||||
} else if value.is_empty() {
|
||||
Ok(Self::Implicit)
|
||||
} else {
|
||||
Err(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for TrueVariant<'_> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
if let Self::Explicit(v) = self {
|
||||
|
@ -497,24 +378,6 @@ impl TryFrom<&[u8]> for Integer {
|
|||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Vec<u8>> for Integer {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
|
||||
Self::try_from(value.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Cow<'_, [u8]>> for Integer {
|
||||
type Error = ();
|
||||
fn try_from(c: Cow<'_, [u8]>) -> Result<Self, Self::Error> {
|
||||
match c {
|
||||
Cow::Borrowed(c) => Self::try_from(c),
|
||||
Cow::Owned(c) => Self::try_from(c).map_err(|_| ()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Integer prefixes that are supported by `git-config`.
|
||||
///
|
||||
/// These values are base-2 unit of measurements, not the base-10 variants.
|
||||
|
@ -582,14 +445,6 @@ impl TryFrom<&[u8]> for IntegerSuffix {
|
|||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Vec<u8>> for IntegerSuffix {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
|
||||
Self::try_from(value.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
/// Any value that may contain a foreground color, background color, a
|
||||
/// collection of color (text) modifiers, or a combination of any of the
|
||||
/// aforementioned values.
|
||||
|
@ -710,24 +565,6 @@ impl TryFrom<&[u8]> for Color {
|
|||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Vec<u8>> for Color {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
|
||||
Self::try_from(value.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Cow<'_, [u8]>> for Color {
|
||||
type Error = ();
|
||||
fn try_from(c: Cow<'_, [u8]>) -> Result<Self, Self::Error> {
|
||||
match c {
|
||||
Cow::Borrowed(c) => Self::try_from(c),
|
||||
Cow::Owned(c) => Self::try_from(c).map_err(|_| ()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Discriminating enum for [`Color`] values.
|
||||
///
|
||||
/// `git-config` supports the eight standard colors, their bright variants, an
|
||||
|
@ -971,34 +808,31 @@ impl TryFrom<&[u8]> for ColorAttribute {
|
|||
|
||||
#[cfg(test)]
|
||||
mod normalize {
|
||||
use super::normalize_str;
|
||||
use super::normalize;
|
||||
use std::borrow::Cow;
|
||||
|
||||
#[test]
|
||||
fn not_modified_is_borrowed() {
|
||||
assert_eq!(normalize_str("hello world"), Cow::Borrowed(b"hello world"));
|
||||
assert_eq!(normalize(b"hello world"), Cow::Borrowed(b"hello world"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn modified_is_owned() {
|
||||
assert_eq!(
|
||||
normalize_str("hello \"world\""),
|
||||
normalize(b"hello \"world\""),
|
||||
Cow::<[u8]>::Owned(b"hello world".to_vec())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn all_quoted_is_optimized() {
|
||||
assert_eq!(
|
||||
normalize_str("\"hello world\""),
|
||||
Cow::Borrowed(b"hello world")
|
||||
);
|
||||
assert_eq!(normalize(b"\"hello world\""), Cow::Borrowed(b"hello world"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn all_quote_optimization_is_correct() {
|
||||
assert_eq!(
|
||||
normalize_str(r#""hello" world\""#),
|
||||
normalize(br#""hello" world\""#),
|
||||
Cow::Borrowed(b"hello world\"")
|
||||
);
|
||||
}
|
||||
|
@ -1006,7 +840,7 @@ mod normalize {
|
|||
#[test]
|
||||
fn quotes_right_next_to_each_other() {
|
||||
assert_eq!(
|
||||
normalize_str("\"hello\"\" world\""),
|
||||
normalize(b"\"hello\"\" world\""),
|
||||
Cow::<[u8]>::Owned(b"hello world".to_vec())
|
||||
);
|
||||
}
|
||||
|
@ -1014,19 +848,19 @@ mod normalize {
|
|||
#[test]
|
||||
fn escaped_quotes_are_kept() {
|
||||
assert_eq!(
|
||||
normalize_str(r#""hello \"\" world""#),
|
||||
normalize(br#""hello \"\" world""#),
|
||||
Cow::<[u8]>::Owned(b"hello \"\" world".to_vec())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_string() {
|
||||
assert_eq!(normalize_str(""), Cow::Borrowed(b""));
|
||||
assert_eq!(normalize(b""), Cow::Borrowed(b""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_normalized_string_is_optimized() {
|
||||
assert_eq!(normalize_str("\"\""), Cow::Borrowed(b""));
|
||||
assert_eq!(normalize(b"\"\""), Cow::Borrowed(b""));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1036,33 +870,30 @@ mod boolean {
|
|||
|
||||
#[test]
|
||||
fn from_str_false() {
|
||||
assert_eq!(Boolean::try_from("no"), Ok(Boolean::False("no".into())));
|
||||
assert_eq!(Boolean::try_from("off"), Ok(Boolean::False("off".into())));
|
||||
assert_eq!(
|
||||
Boolean::try_from("false"),
|
||||
Ok(Boolean::False("false".into()))
|
||||
);
|
||||
assert_eq!(Boolean::try_from("zero"), Ok(Boolean::False("zero".into())));
|
||||
assert_eq!(Boolean::try_from("\"\""), Ok(Boolean::False("\"\"".into())));
|
||||
assert_eq!(Boolean::try_from("no"), Ok(Boolean::False("no")));
|
||||
assert_eq!(Boolean::try_from("off"), Ok(Boolean::False("off")));
|
||||
assert_eq!(Boolean::try_from("false"), Ok(Boolean::False("false")));
|
||||
assert_eq!(Boolean::try_from("zero"), Ok(Boolean::False("zero")));
|
||||
assert_eq!(Boolean::try_from("\"\""), Ok(Boolean::False("\"\"")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_str_true() {
|
||||
assert_eq!(
|
||||
Boolean::try_from("yes"),
|
||||
Ok(Boolean::True(TrueVariant::Explicit("yes".into())))
|
||||
Ok(Boolean::True(TrueVariant::Explicit("yes")))
|
||||
);
|
||||
assert_eq!(
|
||||
Boolean::try_from("on"),
|
||||
Ok(Boolean::True(TrueVariant::Explicit("on".into())))
|
||||
Ok(Boolean::True(TrueVariant::Explicit("on")))
|
||||
);
|
||||
assert_eq!(
|
||||
Boolean::try_from("true"),
|
||||
Ok(Boolean::True(TrueVariant::Explicit("true".into())))
|
||||
Ok(Boolean::True(TrueVariant::Explicit("true")))
|
||||
);
|
||||
assert_eq!(
|
||||
Boolean::try_from("one"),
|
||||
Ok(Boolean::True(TrueVariant::Explicit("one".into())))
|
||||
Ok(Boolean::True(TrueVariant::Explicit("one")))
|
||||
);
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue