Compare commits
2 commits
6ce2c1d89d
...
7c29e44f20
Author | SHA1 | Date | |
---|---|---|---|
7c29e44f20 | |||
1354d931b2 |
5 changed files with 45 additions and 35 deletions
16
src/file.rs
16
src/file.rs
|
@ -122,7 +122,7 @@ impl MutableValue<'_, '_, '_> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An imtermediate representation of a mutable multivar obtained from
|
/// An intermediate representation of a mutable multivar obtained from
|
||||||
/// [`GitConfig`].
|
/// [`GitConfig`].
|
||||||
pub struct MutableMultiValue<'borrow, 'lookup, 'event> {
|
pub struct MutableMultiValue<'borrow, 'lookup, 'event> {
|
||||||
section: &'borrow mut HashMap<SectionId, Vec<Event<'event>>>,
|
section: &'borrow mut HashMap<SectionId, Vec<Event<'event>>>,
|
||||||
|
@ -169,6 +169,7 @@ impl<'event> MutableMultiValue<'_, '_, 'event> {
|
||||||
|
|
||||||
/// Returns the size of values the multivar has.
|
/// Returns the size of values the multivar has.
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
pub fn len(&self) -> usize {
|
pub fn len(&self) -> usize {
|
||||||
self.indices_and_sizes.len()
|
self.indices_and_sizes.len()
|
||||||
}
|
}
|
||||||
|
@ -176,6 +177,7 @@ impl<'event> MutableMultiValue<'_, '_, 'event> {
|
||||||
/// Returns if the multivar has any values. This might occur if the value
|
/// Returns if the multivar has any values. This might occur if the value
|
||||||
/// was deleted but not set with a new value.
|
/// was deleted but not set with a new value.
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
self.indices_and_sizes.is_empty()
|
self.indices_and_sizes.is_empty()
|
||||||
}
|
}
|
||||||
|
@ -1131,8 +1133,8 @@ impl Display for GitConfig<'_> {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod from_parser {
|
mod from_parser {
|
||||||
use super::*;
|
use super::{Cow, Event, GitConfig, HashMap, LookupTreeNode, SectionId, TryFrom};
|
||||||
use crate::test_util::*;
|
use crate::test_util::{name_event, newline_event, section_header, value_event};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_empty() {
|
fn parse_empty() {
|
||||||
|
@ -1331,7 +1333,7 @@ mod from_parser {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod get_raw_value {
|
mod get_raw_value {
|
||||||
use super::*;
|
use super::{Cow, GitConfig, GitConfigError, TryFrom};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn single_section() {
|
fn single_section() {
|
||||||
|
@ -1407,7 +1409,7 @@ mod get_raw_value {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod get_value {
|
mod get_value {
|
||||||
use super::*;
|
use super::{Cow, GitConfig, TryFrom};
|
||||||
use crate::values::{Boolean, TrueVariant, Value};
|
use crate::values::{Boolean, TrueVariant, Value};
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
|
@ -1426,7 +1428,7 @@ mod get_value {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod get_raw_multi_value {
|
mod get_raw_multi_value {
|
||||||
use super::*;
|
use super::{Cow, GitConfig, GitConfigError, TryFrom};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn single_value_is_identical_to_single_value_query() {
|
fn single_value_is_identical_to_single_value_query() {
|
||||||
|
@ -1515,7 +1517,7 @@ mod get_raw_multi_value {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod display {
|
mod display {
|
||||||
use super::*;
|
use super::{GitConfig, TryFrom};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn can_reconstruct_empty_config() {
|
fn can_reconstruct_empty_config() {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#![forbid(unsafe_code)]
|
#![forbid(unsafe_code)]
|
||||||
// #![deny(missing_docs)]
|
// #![deny(missing_docs)]
|
||||||
#![warn(clippy::pedantic, clippy::nursery, clippy::cargo)]
|
#![warn(clippy::pedantic, clippy::nursery, clippy::cargo)]
|
||||||
#![allow(clippy::must_use_candidate, clippy::shadow_unrelated)]
|
#![allow(clippy::shadow_unrelated)]
|
||||||
|
|
||||||
//! # `git_config`
|
//! # `git_config`
|
||||||
//!
|
//!
|
||||||
|
|
|
@ -69,6 +69,7 @@ pub enum Event<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Event<'_> {
|
impl Event<'_> {
|
||||||
|
#[must_use]
|
||||||
pub fn to_vec(&self) -> Vec<u8> {
|
pub fn to_vec(&self) -> Vec<u8> {
|
||||||
self.into()
|
self.into()
|
||||||
}
|
}
|
||||||
|
@ -96,13 +97,7 @@ impl Display for Event<'_> {
|
||||||
|
|
||||||
impl Into<Vec<u8>> for Event<'_> {
|
impl Into<Vec<u8>> for Event<'_> {
|
||||||
fn into(self) -> Vec<u8> {
|
fn into(self) -> Vec<u8> {
|
||||||
match self {
|
(&self).into()
|
||||||
Self::Value(e) | Self::ValueNotDone(e) | Self::ValueDone(e) => e.to_vec(),
|
|
||||||
Self::Comment(e) => e.into(),
|
|
||||||
Self::SectionHeader(e) => e.into(),
|
|
||||||
Self::Key(e) | Self::Newline(e) | Self::Whitespace(e) => e.as_bytes().to_vec(),
|
|
||||||
Self::KeyValueSeparator => vec![b'='],
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,6 +152,7 @@ pub struct ParsedSectionHeader<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ParsedSectionHeader<'_> {
|
impl ParsedSectionHeader<'_> {
|
||||||
|
#[must_use]
|
||||||
pub fn to_vec(&self) -> Vec<u8> {
|
pub fn to_vec(&self) -> Vec<u8> {
|
||||||
self.into()
|
self.into()
|
||||||
}
|
}
|
||||||
|
@ -249,11 +245,13 @@ pub struct Error<'a> {
|
||||||
impl Error<'_> {
|
impl Error<'_> {
|
||||||
/// The one-indexed line number where the error occurred. This is determined
|
/// The one-indexed line number where the error occurred. This is determined
|
||||||
/// by the number of newlines that were successfully parsed.
|
/// by the number of newlines that were successfully parsed.
|
||||||
|
#[must_use]
|
||||||
pub const fn line_number(&self) -> usize {
|
pub const fn line_number(&self) -> usize {
|
||||||
self.line_number + 1
|
self.line_number + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The remaining data that was left unparsed.
|
/// The remaining data that was left unparsed.
|
||||||
|
#[must_use]
|
||||||
pub const fn remaining_data(&self) -> &[u8] {
|
pub const fn remaining_data(&self) -> &[u8] {
|
||||||
self.parsed_until
|
self.parsed_until
|
||||||
}
|
}
|
||||||
|
@ -527,6 +525,7 @@ impl<'a> Parser<'a> {
|
||||||
/// a section) from the parser. Consider [`Parser::take_frontmatter`] if
|
/// a section) from the parser. Consider [`Parser::take_frontmatter`] if
|
||||||
/// you need an owned copy only once. If that function was called, then this
|
/// you need an owned copy only once. If that function was called, then this
|
||||||
/// will always return an empty slice.
|
/// will always return an empty slice.
|
||||||
|
#[must_use]
|
||||||
pub fn frontmatter(&self) -> &[Event<'a>] {
|
pub fn frontmatter(&self) -> &[Event<'a>] {
|
||||||
&self.frontmatter
|
&self.frontmatter
|
||||||
}
|
}
|
||||||
|
@ -544,6 +543,7 @@ impl<'a> Parser<'a> {
|
||||||
/// Returns the parsed sections from the parser. Consider
|
/// Returns the parsed sections from the parser. Consider
|
||||||
/// [`Parser::take_sections`] if you need an owned copy only once. If that
|
/// [`Parser::take_sections`] if you need an owned copy only once. If that
|
||||||
/// function was called, then this will always return an empty slice.
|
/// function was called, then this will always return an empty slice.
|
||||||
|
#[must_use]
|
||||||
pub fn sections(&self) -> &[ParsedSection<'a>] {
|
pub fn sections(&self) -> &[ParsedSection<'a>] {
|
||||||
&self.sections
|
&self.sections
|
||||||
}
|
}
|
||||||
|
@ -558,6 +558,7 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Consumes the parser to produce a Vec of Events.
|
/// Consumes the parser to produce a Vec of Events.
|
||||||
|
#[must_use]
|
||||||
pub fn into_vec(self) -> Vec<Event<'a>> {
|
pub fn into_vec(self) -> Vec<Event<'a>> {
|
||||||
self.into_iter().collect()
|
self.into_iter().collect()
|
||||||
}
|
}
|
||||||
|
@ -1000,7 +1001,7 @@ fn take_newline(i: &[u8]) -> IResult<&[u8], (&str, usize)> {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod comments {
|
mod comments {
|
||||||
use super::*;
|
use super::comment;
|
||||||
use crate::test_util::{comment as parsed_comment, fully_consumed};
|
use crate::test_util::{comment as parsed_comment, fully_consumed};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -1030,7 +1031,7 @@ mod comments {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod section_headers {
|
mod section_headers {
|
||||||
use super::*;
|
use super::section_header;
|
||||||
use crate::test_util::{fully_consumed, section_header as parsed_section_header};
|
use crate::test_util::{fully_consumed, section_header as parsed_section_header};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -1102,7 +1103,7 @@ mod section_headers {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod config_name {
|
mod config_name {
|
||||||
use super::*;
|
use super::config_name;
|
||||||
use crate::test_util::fully_consumed;
|
use crate::test_util::fully_consumed;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -1124,7 +1125,7 @@ mod config_name {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod section_body {
|
mod section_body {
|
||||||
use super::*;
|
use super::{section_body, Event, ParserNode};
|
||||||
use crate::test_util::{name_event, value_event, whitespace_event};
|
use crate::test_util::{name_event, value_event, whitespace_event};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -1158,7 +1159,7 @@ mod section_body {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod value_no_continuation {
|
mod value_no_continuation {
|
||||||
use super::*;
|
use super::value_impl;
|
||||||
use crate::test_util::value_event;
|
use crate::test_util::value_event;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -1264,7 +1265,7 @@ mod value_no_continuation {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod value_continuation {
|
mod value_continuation {
|
||||||
use super::*;
|
use super::value_impl;
|
||||||
use crate::test_util::{newline_event, value_done_event, value_not_done_event};
|
use crate::test_util::{newline_event, value_done_event, value_not_done_event};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -1324,10 +1325,7 @@ mod value_continuation {
|
||||||
#[test]
|
#[test]
|
||||||
fn quote_split_over_two_lines_with_leftover_comment() {
|
fn quote_split_over_two_lines_with_leftover_comment() {
|
||||||
let mut events = vec![];
|
let mut events = vec![];
|
||||||
assert_eq!(
|
assert_eq!(value_impl(b"\"\\\n;\";a", &mut events).unwrap().0, b";a");
|
||||||
value_impl(b"\"\\\n;\";a", &mut events).unwrap().0,
|
|
||||||
";a".as_bytes()
|
|
||||||
);
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
events,
|
events,
|
||||||
vec![
|
vec![
|
||||||
|
@ -1341,7 +1339,7 @@ mod value_continuation {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod section {
|
mod section {
|
||||||
use super::*;
|
use super::{section, Event, ParsedSection, ParserNode};
|
||||||
use crate::test_util::{
|
use crate::test_util::{
|
||||||
comment_event, fully_consumed, name_event, newline_event,
|
comment_event, fully_consumed, name_event, newline_event,
|
||||||
section_header as parsed_section_header, value_done_event, value_event,
|
section_header as parsed_section_header, value_done_event, value_event,
|
||||||
|
@ -1541,7 +1539,7 @@ mod section {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod error {
|
mod error {
|
||||||
use super::*;
|
use super::parse_from_str;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn line_no_is_one_indexed() {
|
fn line_no_is_one_indexed() {
|
||||||
|
|
|
@ -59,6 +59,7 @@ use std::str::FromStr;
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [`parser`]: crate::parser::Parser
|
/// [`parser`]: crate::parser::Parser
|
||||||
|
#[must_use]
|
||||||
pub fn normalize_cow(input: Cow<'_, [u8]>) -> Cow<'_, [u8]> {
|
pub fn normalize_cow(input: Cow<'_, [u8]>) -> Cow<'_, [u8]> {
|
||||||
let size = input.len();
|
let size = input.len();
|
||||||
if &*input == b"\"\"" {
|
if &*input == b"\"\"" {
|
||||||
|
@ -120,18 +121,21 @@ pub fn normalize_cow(input: Cow<'_, [u8]>) -> Cow<'_, [u8]> {
|
||||||
|
|
||||||
/// `&[u8]` variant of [`normalize_cow`].
|
/// `&[u8]` variant of [`normalize_cow`].
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
pub fn normalize_bytes(input: &[u8]) -> Cow<'_, [u8]> {
|
pub fn normalize_bytes(input: &[u8]) -> Cow<'_, [u8]> {
|
||||||
normalize_cow(Cow::Borrowed(input))
|
normalize_cow(Cow::Borrowed(input))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `Vec[u8]` variant of [`normalize_cow`].
|
/// `Vec[u8]` variant of [`normalize_cow`].
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
pub fn normalize_vec(input: Vec<u8>) -> Cow<'static, [u8]> {
|
pub fn normalize_vec(input: Vec<u8>) -> Cow<'static, [u8]> {
|
||||||
normalize_cow(Cow::Owned(input))
|
normalize_cow(Cow::Owned(input))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// [`str`] variant of [`normalize_cow`].
|
/// [`str`] variant of [`normalize_cow`].
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
pub fn normalize_str(input: &str) -> Cow<'_, [u8]> {
|
pub fn normalize_str(input: &str) -> Cow<'_, [u8]> {
|
||||||
normalize_bytes(input.as_bytes())
|
normalize_bytes(input.as_bytes())
|
||||||
}
|
}
|
||||||
|
@ -150,6 +154,7 @@ pub enum Value<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Value<'_> {
|
impl Value<'_> {
|
||||||
|
#[must_use]
|
||||||
pub fn to_vec(&self) -> Vec<u8> {
|
pub fn to_vec(&self) -> Vec<u8> {
|
||||||
self.into()
|
self.into()
|
||||||
}
|
}
|
||||||
|
@ -261,10 +266,12 @@ pub enum Boolean<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Boolean<'_> {
|
impl Boolean<'_> {
|
||||||
|
#[must_use]
|
||||||
pub fn to_vec(&self) -> Vec<u8> {
|
pub fn to_vec(&self) -> Vec<u8> {
|
||||||
self.into()
|
self.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn as_bytes(&self) -> &[u8] {
|
pub fn as_bytes(&self) -> &[u8] {
|
||||||
self.into()
|
self.into()
|
||||||
}
|
}
|
||||||
|
@ -500,6 +507,7 @@ pub struct Integer {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Integer {
|
impl Integer {
|
||||||
|
#[must_use]
|
||||||
pub fn to_vec(&self) -> Vec<u8> {
|
pub fn to_vec(&self) -> Vec<u8> {
|
||||||
self.into()
|
self.into()
|
||||||
}
|
}
|
||||||
|
@ -610,6 +618,7 @@ pub enum IntegerSuffix {
|
||||||
|
|
||||||
impl IntegerSuffix {
|
impl IntegerSuffix {
|
||||||
/// Returns the number of bits that the suffix shifts left by.
|
/// Returns the number of bits that the suffix shifts left by.
|
||||||
|
#[must_use]
|
||||||
pub const fn bitwise_offset(self) -> usize {
|
pub const fn bitwise_offset(self) -> usize {
|
||||||
match self {
|
match self {
|
||||||
Self::Kibi => 10,
|
Self::Kibi => 10,
|
||||||
|
@ -690,6 +699,7 @@ pub struct Color {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Color {
|
impl Color {
|
||||||
|
#[must_use]
|
||||||
pub fn to_vec(&self) -> Vec<u8> {
|
pub fn to_vec(&self) -> Vec<u8> {
|
||||||
self.into()
|
self.into()
|
||||||
}
|
}
|
||||||
|
@ -1118,7 +1128,7 @@ mod normalize {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod boolean {
|
mod boolean {
|
||||||
use super::*;
|
use super::{Boolean, TrueVariant, TryFrom};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn from_str_false() {
|
fn from_str_false() {
|
||||||
|
@ -1171,7 +1181,7 @@ mod boolean {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod integer {
|
mod integer {
|
||||||
use super::*;
|
use super::{FromStr, Integer, IntegerSuffix};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn from_str_no_suffix() {
|
fn from_str_no_suffix() {
|
||||||
|
|
|
@ -13,12 +13,12 @@ pub fn 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.into());
|
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(Cow::Borrowed(separator.into())),
|
separator: Some(Cow::Borrowed(separator)),
|
||||||
subsection_name: Some(Cow::Borrowed(subsection_name.into())),
|
subsection_name: Some(Cow::Borrowed(subsection_name)),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ParsedSectionHeader {
|
ParsedSectionHeader {
|
||||||
|
@ -30,7 +30,7 @@ pub fn section_header(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn name(name: &'static str) -> Event<'static> {
|
fn name(name: &'static str) -> Event<'static> {
|
||||||
Event::Key(Cow::Borrowed(name.into()))
|
Event::Key(Cow::Borrowed(name))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn value(value: &'static str) -> Event<'static> {
|
fn value(value: &'static str) -> Event<'static> {
|
||||||
|
@ -42,11 +42,11 @@ fn newline() -> Event<'static> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn newline_custom(value: &'static str) -> Event<'static> {
|
fn newline_custom(value: &'static str) -> Event<'static> {
|
||||||
Event::Newline(Cow::Borrowed(value.into()))
|
Event::Newline(Cow::Borrowed(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn whitespace(value: &'static str) -> Event<'static> {
|
fn whitespace(value: &'static str) -> Event<'static> {
|
||||||
Event::Whitespace(Cow::Borrowed(value.into()))
|
Event::Whitespace(Cow::Borrowed(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn separator() -> Event<'static> {
|
fn separator() -> Event<'static> {
|
||||||
|
|
Reference in a new issue