| @@ -9,6 +9,8 @@ pub enum Error { · 8 unmodified lines |
| 9 | EmptyString, | 9 | EmptyString, |
| 10 | #[error("Found {:?}, which is not a valid keyword", found_keyword)] | 10 | #[error("Found {:?}, which is not a valid keyword", found_keyword)] |
| 11 | InvalidKeyword { found_keyword: BString }, | 11 | InvalidKeyword { found_keyword: BString }, |
| | 12 | #[error("Unimplemented pathspec magic {:?}", found_short_keyword)] |
| | 13 | Unimplemented { found_short_keyword: BString }, |
| 12 | #[error("Missing ')' at the end of pathspec magic in {:?}", pathspec)] | 14 | #[error("Missing ')' at the end of pathspec magic in {:?}", pathspec)] |
| 13 | MissingClosingParenthesis { pathspec: BString }, | 15 | MissingClosingParenthesis { pathspec: BString }, |
| 14 | #[error("Attribute has non-ascii characters or starts with '-': {:?}", attribute)] | 16 | #[error("Attribute has non-ascii characters or starts with '-': {:?}", attribute)] |
| @@ -33,31 +35,17 @@ impl Pattern { · 18 unmodified lines |
| 33 | search_mode: SearchMode::ShellGlob, | 35 | search_mode: SearchMode::ShellGlob, |
| 34 | attributes: Vec::new(), | 36 | attributes: Vec::new(), |
| 35 | }; | 37 | }; |
| | 38 | |
| 36 | let mut cursor = 0; | 39 | let mut cursor = 0; |
| 37 | if input.first() == Some(&b':') { | 40 | if input.first() == Some(&b':') { |
| 38 | cursor += 1; | 41 | cursor += 1; |
| 39 | while let Some(&b) = input.get(cursor) { | 42 | p.signature |= parse_short_keywords(&input, &mut cursor)?; |
| | 43 | if let Some(b'(') = input.get(cursor) { |
| 40 | cursor += 1; | 44 | cursor += 1; |
| 41 | match b { | 45 | let pat = parse_long_keywords(&input, &mut cursor)?; |
| 42 | b':' => break, | 46 | p.search_mode = pat.search_mode; |
| 43 | b'/' => p.signature |= MagicSignature::TOP, | 47 | p.attributes = pat.attributes; |
| 44 | b'^' | b'!' => p.signature |= MagicSignature::EXCLUDE, | 48 | p.signature |= pat.signature; |
| 45 | b'(' => { | | |
| 46 | let end = input.find(")").ok_or(Error::MissingClosingParenthesis { | | |
| 47 | pathspec: BString::from(input), | | |
| 48 | })?; | | |
| 49 | let pat = parse_keywords(&input[cursor..end])?; | | |
| 50 | | | |
| 51 | cursor = end + 1; | | |
| 52 | p.search_mode = pat.search_mode; | | |
| 53 | p.attributes = pat.attributes; | | |
| 54 | p.signature |= pat.signature; | | |
| 55 | } | | |
| 56 | _ => { | | |
| 57 | cursor -= 1; | | |
| 58 | break; | | |
| 59 | } | | |
| 60 | } | | |
| 61 | } | 49 | } |
| 62 | } | 50 | } |
| 63 | | 51 | |
| @@ -66,7 +54,41 @@ impl Pattern { · 2 unmodified lines |
| 66 | } | 54 | } |
| 67 | } | 55 | } |
| 68 | | 56 | |
| 69 | fn parse_keywords(input: &[u8]) -> Result<Pattern, Error> { | 57 | fn parse_short_keywords(input: &[u8], cursor: &mut usize) -> Result<MagicSignature, Error> { |
| | 58 | let unimplemented_chars = vec![ |
| | 59 | b'"', b'#', b'%', b'&', b'\'', b',', b'-', b';', b'<', b'=', b'>', b'@', b'_', b'`', b'~', |
| | 60 | ]; |
| | 61 | let mut signature = MagicSignature::empty(); |
| | 62 | |
| | 63 | while let Some(&b) = input.get(*cursor) { |
| | 64 | *cursor += 1; |
| | 65 | signature |= match b { |
| | 66 | b'/' => MagicSignature::TOP, |
| | 67 | b'^' | b'!' => MagicSignature::EXCLUDE, |
| | 68 | b':' => break, |
| | 69 | _ if unimplemented_chars.contains(&b) => { |
| | 70 | return Err(Error::Unimplemented { |
| | 71 | found_short_keyword: vec![b].into(), |
| | 72 | }); |
| | 73 | } |
| | 74 | _ => { |
| | 75 | *cursor -= 1; |
| | 76 | break; |
| | 77 | } |
| | 78 | } |
| | 79 | } |
| | 80 | |
| | 81 | return Ok(signature); |
| | 82 | } |
| | 83 | |
| | 84 | fn parse_long_keywords(input: &[u8], cursor: &mut usize) -> Result<Pattern, Error> { |
| | 85 | let end = input.find(")").ok_or(Error::MissingClosingParenthesis { |
| | 86 | pathspec: BString::from(input), |
| | 87 | })?; |
| | 88 | |
| | 89 | let input = &input[*cursor..end]; |
| | 90 | *cursor = end + 1; |
| | 91 | |
| 70 | let mut p = Pattern { | 92 | let mut p = Pattern { |
| 71 | path: BString::default(), | 93 | path: BString::default(), |
| 72 | signature: MagicSignature::empty(), | 94 | signature: MagicSignature::empty(), |