Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
- name changes
- sketch of `name::Error`
  • Loading branch information
Byron committed Jun 30, 2022
1 parent 3b2bab8 commit 9945ceb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 34 deletions.
14 changes: 12 additions & 2 deletions git-attributes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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};

Expand Down
2 changes: 1 addition & 1 deletion git-attributes/src/match_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
44 changes: 16 additions & 28 deletions git-attributes/src/parse/attribute.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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),
Expand All @@ -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,
Expand All @@ -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))
}
}

Expand All @@ -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())?;
Expand Down Expand Up @@ -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)
}
}
4 changes: 2 additions & 2 deletions git-pathspec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use bitflags::bitflags;
use bstr::BString;
use git_attributes::AttributeName;
use git_attributes::Name;

pub mod parse;

Expand All @@ -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! {
Expand Down
2 changes: 1 addition & 1 deletion git-pathspec/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 9945ceb

Please sign in to comment.