From 9945ceb0a99c1343cb6e652e44900b36d3786e22 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel <sebastian.thiel@icloud.com> Date: Thu, 30 Jun 2022 20:47:51 +0800 Subject: [PATCH] refactor - name changes - sketch of `name::Error` --- git-attributes/src/lib.rs | 14 +++++++-- git-attributes/src/match_group.rs | 2 +- git-attributes/src/parse/attribute.rs | 44 ++++++++++----------------- git-pathspec/src/lib.rs | 4 +-- git-pathspec/src/parse.rs | 2 +- 5 files changed, 32 insertions(+), 34 deletions(-) diff --git a/git-attributes/src/lib.rs b/git-attributes/src/lib.rs index c1bcf0111df..0b3f8f57b1b 100644 --- a/git-attributes/src/lib.rs +++ b/git-attributes/src/lib.rs @@ -44,11 +44,11 @@ pub enum State { /// Holds and owns data that represent one validated attribute #[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)] -pub struct AttributeName(BString, State); +pub struct Name(BString, State); /// Holds validated attribute data as a reference #[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd)] -pub struct AttributeNameRef<'a>(&'a BStr, StateRef<'a>); +pub struct NameRef<'a>(&'a BStr, StateRef<'a>); /// Name an attribute and describe it's assigned state. #[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)] @@ -100,6 +100,16 @@ pub struct PatternMapping<T> { pub sequence_number: usize, } +mod name { + use bstr::BString; + + #[derive(Debug, thiserror::Error)] + #[error("Attribute has non-ascii characters or starts with '-': {attribute}")] + pub struct Error { + pub attribute: BString, + } +} + mod match_group; pub use match_group::{Attributes, Ignore, Match, Pattern}; diff --git a/git-attributes/src/match_group.rs b/git-attributes/src/match_group.rs index 257ff867f30..2f445f021f1 100644 --- a/git-attributes/src/match_group.rs +++ b/git-attributes/src/match_group.rs @@ -18,7 +18,7 @@ impl<'a> From<StateRef<'a>> for State { } fn attrs_to_assignments<'a>( - attrs: impl Iterator<Item = Result<crate::AttributeNameRef<'a>, crate::parse::Error>>, + attrs: impl Iterator<Item = Result<crate::NameRef<'a>, crate::parse::Error>>, ) -> Result<Vec<Assignment>, crate::parse::Error> { attrs .map(|res| { diff --git a/git-attributes/src/parse/attribute.rs b/git-attributes/src/parse/attribute.rs index eb639f06d1e..f5df007ca76 100644 --- a/git-attributes/src/parse/attribute.rs +++ b/git-attributes/src/parse/attribute.rs @@ -1,4 +1,4 @@ -use crate::{AttributeName, AttributeNameRef, State, StateRef}; +use crate::{Name, NameRef, State, StateRef}; use bstr::{BStr, BString, ByteSlice}; use std::borrow::Cow; @@ -16,23 +16,11 @@ mod error { use bstr::BString; #[derive(thiserror::Error, Debug)] pub enum Error { - #[error( - "Line {} has a negative pattern, for literal characters use \\!: {}", - line_number, - line - )] + #[error("Line {line_number} has a negative pattern, for literal characters use \\!: {line}")] PatternNegation { line_number: usize, line: BString }, - #[error( - "Attribute in line {} has non-ascii characters or starts with '-': {}", - line_number, - attribute - )] + #[error("Attribute in line {line_number} has non-ascii characters or starts with '-': {attribute}")] AttributeName { line_number: usize, attribute: BString }, - #[error( - "Macro in line {} has non-ascii characters or starts with '-': {}", - line_number, - macro_name - )] + #[error("Macro in line {line_number} has non-ascii characters or starts with '-': {macro_name}")] MacroName { line_number: usize, macro_name: BString }, #[error("Could not unquote attributes line")] Unquote(#[from] git_quote::ansi_c::undo::Error), @@ -58,14 +46,14 @@ impl<'a> Iter<'a> { } } - fn parse_attr(&self, attr: &'a [u8]) -> Result<AttributeNameRef<'a>, Error> { + fn parse_attr(&self, attr: &'a [u8]) -> Result<NameRef<'a>, Error> { let mut tokens = attr.splitn(2, |b| *b == b'='); let attr = tokens.next().expect("attr itself").as_bstr(); let possibly_value = tokens.next(); let (attr, state) = if attr.first() == Some(&b'-') { - (&attr[1..], crate::StateRef::Unset) + (&attr[1..], StateRef::Unset) } else if attr.first() == Some(&b'!') { - (&attr[1..], crate::StateRef::Unspecified) + (&attr[1..], StateRef::Unspecified) } else { ( attr, @@ -74,7 +62,7 @@ impl<'a> Iter<'a> { .unwrap_or(crate::StateRef::Set), ) }; - Ok(AttributeNameRef(check_attr(attr, self.line_no)?, state)) + Ok(NameRef(check_attr(attr, self.line_no)?, state)) } } @@ -97,7 +85,7 @@ fn check_attr(attr: &BStr, line_number: usize) -> Result<&BStr, Error> { } impl<'a> Iterator for Iter<'a> { - type Item = Result<AttributeNameRef<'a>, Error>; + type Item = Result<NameRef<'a>, Error>; fn next(&mut self) -> Option<Self::Item> { let attr = self.attrs.next().filter(|a| !a.is_empty())?; @@ -185,20 +173,20 @@ fn parse_line(line: &BStr, line_number: usize) -> Option<Result<(Kind, Iter<'_>, const BLANKS: &[u8] = b" \t\r"; -impl<'a> From<AttributeNameRef<'a>> for AttributeName { - fn from(v: AttributeNameRef<'a>) -> Self { - AttributeName(v.0.to_owned(), v.1.into()) +impl<'a> From<NameRef<'a>> for Name { + fn from(v: NameRef<'a>) -> Self { + Name(v.0.to_owned(), v.1.into()) } } -impl<'a> From<AttributeNameRef<'a>> for (&'a BStr, StateRef<'a>) { - fn from(v: AttributeNameRef<'a>) -> Self { +impl<'a> From<NameRef<'a>> for (&'a BStr, StateRef<'a>) { + fn from(v: NameRef<'a>) -> Self { (v.0, v.1) } } -impl From<AttributeName> for (BString, State) { - fn from(v: AttributeName) -> Self { +impl From<Name> for (BString, State) { + fn from(v: Name) -> Self { (v.0, v.1) } } diff --git a/git-pathspec/src/lib.rs b/git-pathspec/src/lib.rs index c60d9025cd9..3a81b0700c6 100644 --- a/git-pathspec/src/lib.rs +++ b/git-pathspec/src/lib.rs @@ -2,7 +2,7 @@ use bitflags::bitflags; use bstr::BString; -use git_attributes::AttributeName; +use git_attributes::Name; pub mod parse; @@ -16,7 +16,7 @@ pub struct Pattern { /// The search mode of the pathspec. pub search_mode: SearchMode, /// All attributes that were included in the `ATTR` part of the pathspec, if present. - pub attributes: Vec<AttributeName>, + pub attributes: Vec<Name>, } bitflags! { diff --git a/git-pathspec/src/parse.rs b/git-pathspec/src/parse.rs index d04dbbb8b4a..dc35dc8d381 100644 --- a/git-pathspec/src/parse.rs +++ b/git-pathspec/src/parse.rs @@ -142,7 +142,7 @@ fn split_on_non_escaped_char(input: &[u8], split_char: u8) -> Vec<&[u8]> { keywords } -fn parse_attributes(input: &[u8]) -> Result<Vec<git_attributes::AttributeName>, Error> { +fn parse_attributes(input: &[u8]) -> Result<Vec<git_attributes::Name>, Error> { if input.is_empty() { return Err(Error::EmptyAttribute); }