use gix_error::ExnMessageResult;
use std::borrow::Cow;
use bstr::{BStr, BString, ByteSlice};
use crate::{Defaults, MagicSignature, Pattern, SearchMode};
impl Pattern {
pub fn from_bytes(
input: &[u8],
Defaults {
signature,
search_mode,
literal,
}: Defaults,
) -> ExnMessageResult<Self> {
if input.is_empty() {
return Err(gix_error::validation("An empty string is not a valid pathspec")
.with("input", input)
.into());
}
if literal {
return Ok(Self::from_literal(input, signature));
}
if input.as_bstr() == ":" {
return Ok(Pattern {
nil: true,
..Default::default()
});
}
let mut p = Pattern {
signature,
search_mode: SearchMode::default(),
..Default::default()
};
let mut cursor = 0;
if input.first() == Some(&b':') {
cursor += 1;
p.signature |= parse_short_keywords(input, &mut cursor)?;
if let Some(b'(') = input.get(cursor) {
cursor += 1;
parse_long_keywords(input, &mut p, &mut cursor)?;
}
}
if search_mode != Default::default() && p.search_mode == Default::default() {
p.search_mode = search_mode;
}
let mut path = &input[cursor..];
if path.last() == Some(&b'/') {
p.signature |= MagicSignature::MUST_BE_DIR;
path = &path[..path.len() - 1];
}
p.path = path.into();
Ok(p)
}
pub fn from_literal(input: &[u8], default_signature: MagicSignature) -> Self {
Pattern {
path: input.into(),
signature: default_signature,
search_mode: SearchMode::Literal,
..Default::default()
}
}
}
fn parse_short_keywords(input: &[u8], cursor: &mut usize) -> ExnMessageResult<MagicSignature> {
let unimplemented_chars = b"\"#%&'-',;<=>@_`~";
let mut signature = MagicSignature::empty();
while let Some(&b) = input.get(*cursor) {
*cursor += 1;
signature |= match b {
b'/' => MagicSignature::TOP,
b'^' | b'!' => MagicSignature::EXCLUDE,
b':' => break,
_ if unimplemented_chars.contains(&b) => {
return Err(gix_error::validation("Unimplemented short keyword")
.with("input", vec![b])
.into());
}
_ => {
*cursor -= 1;
break;
}
}
}
Ok(signature)
}
fn parse_long_keywords(input: &[u8], p: &mut Pattern, cursor: &mut usize) -> ExnMessageResult {
let end = input
.find(")")
.ok_or_else(|| gix_error::validation("Missing ')' at the end of pathspec signature").with("input", input))?;
let input = &input[*cursor..end];
*cursor = end + 1;
if input.is_empty() {
return Ok(());
}
split_on_non_escaped_char(input, b',', |keyword| {
if keyword.is_empty() {
return Ok(());
}
let attr_prefix = b"attr:";
match keyword {
b"attr" => {}
b"top" => p.signature |= MagicSignature::TOP,
b"icase" => p.signature |= MagicSignature::ICASE,
b"exclude" => p.signature |= MagicSignature::EXCLUDE,
b"literal" => match p.search_mode {
SearchMode::PathAwareGlob => {
return Err(gix_error::validation(
"'literal' and 'glob' keywords cannot be used together in the same pathspec",
)
.with("input", keyword)
.into());
}
_ => p.search_mode = SearchMode::Literal,
},
b"glob" => match p.search_mode {
SearchMode::Literal => {
return Err(gix_error::validation(
"'literal' and 'glob' keywords cannot be used together in the same pathspec",
)
.with("input", keyword)
.into());
}
_ => p.search_mode = SearchMode::PathAwareGlob,
},
_ if keyword.starts_with(attr_prefix) => {
if p.attributes.is_empty() {
p.attributes = parse_attributes(&keyword[attr_prefix.len()..])?;
} else {
return Err(gix_error::validation(
"Only one attribute specification is allowed in the same pathspec",
)
.with("input", keyword)
.into());
}
}
_ => {
return Err(gix_error::validation("Found invalid keyword in pathspec signature")
.with("input", keyword)
.into());
}
}
Ok(())
})
}
fn split_on_non_escaped_char(
input: &[u8],
split_char: u8,
mut f: impl FnMut(&[u8]) -> ExnMessageResult,
) -> ExnMessageResult {
let mut i = 0;
let mut last = 0;
while i < input.len() {
if input[i] == b'\\' {
i += 2;
continue;
}
if input[i] == split_char {
f(&input[last..i])?;
last = i + 1;
}
i += 1;
}
f(&input[last..])
}
fn parse_attributes(input: &[u8]) -> ExnMessageResult<Vec<gix_attributes::Assignment>> {
if input.is_empty() {
return Err(gix_error::validation("Attribute specification cannot be empty").into());
}
input
.split(|&b| b == b' ')
.filter(|attr| !attr.is_empty())
.map(|attr| {
let (name, state) = match attr.first() {
Some(b'!') => (&attr[1..], gix_attributes::State::Unspecified),
Some(b'-') => (&attr[1..], gix_attributes::State::Unset),
_ => match attr.find_byte(b'=') {
Some(pos) => {
let (name, value) = attr.split_at(pos);
let value = &value[1..];
let value = if value.contains(&b'\\') {
Cow::Owned(unescape_and_check_attr_value(value.into())?)
} else {
check_attribute_value(value.into())?;
Cow::Borrowed(value.as_bstr())
};
(name, gix_attributes::StateRef::from_bytes(value.as_ref()).to_owned())
}
None => (attr, gix_attributes::State::Set),
},
};
let name = gix_attributes::NameRef::try_from(name.as_bstr())?;
Ok(gix_attributes::Assignment {
name: name.to_owned(),
state,
})
})
.collect()
}
fn unescape_and_check_attr_value(value: &BStr) -> ExnMessageResult<BString> {
let mut out = BString::from(Vec::with_capacity(value.len()));
let mut bytes = value.iter();
while let Some(mut b) = bytes.next().copied() {
if b == b'\\' {
b = *bytes.next().ok_or_else(|| {
gix_error::validation(
r"Escape character '\' is not allowed as the last character in an attribute value",
)
.with("input", value)
})?;
}
out.push(validated_attr_value_byte(b)?);
}
Ok(out)
}
fn check_attribute_value(input: &BStr) -> ExnMessageResult {
match input.iter().copied().find(|b| !is_valid_attr_value(*b)) {
Some(b) => Err(gix_error::validation("Invalid character in attribute value")
.with("input", vec![b])
.into()),
None => Ok(()),
}
}
fn is_valid_attr_value(byte: u8) -> bool {
byte.is_ascii_alphanumeric() || b",-_".contains(&byte)
}
fn validated_attr_value_byte(byte: u8) -> ExnMessageResult<u8> {
if is_valid_attr_value(byte) {
Ok(byte)
} else {
Err(gix_error::validation("Invalid character in attribute value")
.with("input", vec![byte])
.into())
}
}