11 utils::{ |
11 utils::{ |
12 files::{canonical_path, get_bytes_from_path, get_path_from_bytes}, |
12 files::{canonical_path, get_bytes_from_path, get_path_from_bytes}, |
13 hg_path::{path_to_hg_path_buf, HgPathBuf, HgPathError}, |
13 hg_path::{path_to_hg_path_buf, HgPathBuf, HgPathError}, |
14 SliceExt, |
14 SliceExt, |
15 }, |
15 }, |
16 FastHashMap, PatternError, |
16 FastHashMap, |
17 }; |
17 }; |
18 use lazy_static::lazy_static; |
18 use lazy_static::lazy_static; |
19 use regex::bytes::{NoExpand, Regex}; |
19 use regex::bytes::{NoExpand, Regex}; |
20 use std::ops::Deref; |
|
21 use std::path::{Path, PathBuf}; |
20 use std::path::{Path, PathBuf}; |
22 use std::vec::Vec; |
21 use std::vec::Vec; |
|
22 use std::{fmt, ops::Deref}; |
|
23 |
|
24 #[derive(Debug, derive_more::From)] |
|
25 pub enum PatternError { |
|
26 #[from] |
|
27 Path(HgPathError), |
|
28 UnsupportedSyntax(String), |
|
29 UnsupportedSyntaxInFile(String, String, usize), |
|
30 TooLong(usize), |
|
31 #[from] |
|
32 IO(std::io::Error), |
|
33 /// Needed a pattern that can be turned into a regex but got one that |
|
34 /// can't. This should only happen through programmer error. |
|
35 NonRegexPattern(IgnorePattern), |
|
36 } |
|
37 |
|
38 impl fmt::Display for PatternError { |
|
39 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
|
40 match self { |
|
41 PatternError::UnsupportedSyntax(syntax) => { |
|
42 write!(f, "Unsupported syntax {}", syntax) |
|
43 } |
|
44 PatternError::UnsupportedSyntaxInFile(syntax, file_path, line) => { |
|
45 write!( |
|
46 f, |
|
47 "{}:{}: unsupported syntax {}", |
|
48 file_path, line, syntax |
|
49 ) |
|
50 } |
|
51 PatternError::TooLong(size) => { |
|
52 write!(f, "matcher pattern is too long ({} bytes)", size) |
|
53 } |
|
54 PatternError::IO(error) => error.fmt(f), |
|
55 PatternError::Path(error) => error.fmt(f), |
|
56 PatternError::NonRegexPattern(pattern) => { |
|
57 write!(f, "'{:?}' cannot be turned into a regex", pattern) |
|
58 } |
|
59 } |
|
60 } |
|
61 } |
23 |
62 |
24 lazy_static! { |
63 lazy_static! { |
25 static ref RE_ESCAPE: Vec<Vec<u8>> = { |
64 static ref RE_ESCAPE: Vec<Vec<u8>> = { |
26 let mut v: Vec<Vec<u8>> = (0..=255).map(|byte| vec![byte]).collect(); |
65 let mut v: Vec<Vec<u8>> = (0..=255).map(|byte| vec![byte]).collect(); |
27 let to_escape = b"()[]{}?*+-|^$\\.&~#\t\n\r\x0b\x0c"; |
66 let to_escape = b"()[]{}?*+-|^$\\.&~#\t\n\r\x0b\x0c"; |