Debian Codegraph @

src/pattern.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#, iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#};
2
3 use bstrrust-analyzer cargo bstr 1.12.0 crate/::iorust-analyzer cargo bstr 1.12.0 io/::BufReadExtrust-analyzer cargo bstr 1.12.0 io/BufReadExt#;
4
5 use craterust-analyzer cargo grep-cli 0.1.12 crate/::escaperust-analyzer cargo grep-cli 0.1.12 escape/::{escaperust-analyzer cargo grep-cli 0.1.12 escape/escape()., escape_osrust-analyzer cargo grep-cli 0.1.12 escape/escape_os().};
6
7 /// An error that occurs when a pattern could not be converted to valid UTF-8.
8 ///
9 /// The purpose of this error is to give a more targeted failure mode for
10 /// patterns written by end users that are not valid UTF-8.
11 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
12 pub struct InvalidPatternErrorrust-analyzer cargo grep-cli 0.1.12 pattern/InvalidPatternError# {
13 originalrust-analyzer cargo grep-cli 0.1.12 pattern/InvalidPatternError#original.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
14 valid_up_torust-analyzer cargo grep-cli 0.1.12 pattern/InvalidPatternError#valid_up_to.: usize,
15 }
16
17 impl InvalidPatternErrorrust-analyzer cargo grep-cli 0.1.12 pattern/InvalidPatternError# {
18 /// Returns the index in the given string up to which valid UTF-8 was
19 /// verified.
20 pub fn valid_up_torust-analyzer cargo grep-cli 0.1.12 pattern/impl#[InvalidPatternError]valid_up_to().(&self) -> usize {
21 self.valid_up_torust-analyzer cargo grep-cli 0.1.12 pattern/InvalidPatternError#valid_up_to.
22 }
23 }
24
25 impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for InvalidPatternErrorrust-analyzer cargo grep-cli 0.1.12 pattern/InvalidPatternError# {}
26
27 impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for InvalidPatternErrorrust-analyzer cargo grep-cli 0.1.12 pattern/InvalidPatternError# {
28 fn fmtrust-analyzer cargo grep-cli 0.1.12 pattern/impl#[InvalidPatternError][Display]fmt().(&self, f: &mut stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
29 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(
30 f,
31 "found invalid UTF-8 in pattern at byte offset {}: {} \
32 (disable Unicode mode and use hex escape sequences to match \
33 arbitrary bytes in a pattern, e.g., '(?-u)\\xFF')",
34 self.valid_up_torust-analyzer cargo grep-cli 0.1.12 pattern/InvalidPatternError#valid_up_to., self.originalrust-analyzer cargo grep-cli 0.1.12 pattern/InvalidPatternError#original.,
35 )
36 }
37 }
38
39 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<InvalidPatternErrorrust-analyzer cargo grep-cli 0.1.12 pattern/InvalidPatternError#> for iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error# {
40 fn fromrust-analyzer cargo grep-cli 0.1.12 pattern/impl#[`io::Error`][`From<InvalidPatternError>`]from().(paterr: InvalidPatternErrorrust-analyzer cargo grep-cli 0.1.12 pattern/InvalidPatternError#) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error# {
41 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#, paterr)
42 }
43 }
44
45 /// Convert an OS string into a regular expression pattern.
46 ///
47 /// This conversion fails if the given pattern is not valid UTF-8, in which
48 /// case, a targeted error with more information about where the invalid UTF-8
49 /// occurs is given. The error also suggests the use of hex escape sequences,
50 /// which are supported by many regex engines.
51 pub fn pattern_from_osrust-analyzer cargo grep-cli 0.1.12 pattern/pattern_from_os().(pattern: &OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&str, InvalidPatternErrorrust-analyzer cargo grep-cli 0.1.12 pattern/InvalidPatternError#> {
52 pattern.to_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_str().().ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| {
53 let valid_up_to = pattern
54 .to_string_lossyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_string_lossy().()
55 .findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]find().('\u{FFFD}')
56 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("a Unicode replacement codepoint for invalid UTF-8");
57 InvalidPatternErrorrust-analyzer cargo grep-cli 0.1.12 pattern/InvalidPatternError# { originalrust-analyzer cargo grep-cli 0.1.12 pattern/InvalidPatternError#original.: escape_osrust-analyzer cargo grep-cli 0.1.12 escape/escape_os().(pattern), valid_up_torust-analyzer cargo grep-cli 0.1.12 pattern/InvalidPatternError#valid_up_to. }
58 })
59 }
60
61 /// Convert arbitrary bytes into a regular expression pattern.
62 ///
63 /// This conversion fails if the given pattern is not valid UTF-8, in which
64 /// case, a targeted error with more information about where the invalid UTF-8
65 /// occurs is given. The error also suggests the use of hex escape sequences,
66 /// which are supported by many regex engines.
67 pub fn pattern_from_bytesrust-analyzer cargo grep-cli 0.1.12 pattern/pattern_from_bytes().(
68 pattern: &[u8],
69 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&str, InvalidPatternErrorrust-analyzer cargo grep-cli 0.1.12 pattern/InvalidPatternError#> {
70 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::from_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(pattern).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| InvalidPatternErrorrust-analyzer cargo grep-cli 0.1.12 pattern/InvalidPatternError# {
71 originalrust-analyzer cargo grep-cli 0.1.12 pattern/InvalidPatternError#original.: escaperust-analyzer cargo grep-cli 0.1.12 escape/escape().(pattern),
72 valid_up_torust-analyzer cargo grep-cli 0.1.12 pattern/InvalidPatternError#valid_up_to.: err.valid_up_torust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/error/impl#[Utf8Error]valid_up_to().(),
73 })
74 }
75
76 /// Read patterns from a file path, one per line.
77 ///
78 /// If there was a problem reading or if any of the patterns contain invalid
79 /// UTF-8, then an error is returned. If there was a problem with a specific
80 /// pattern, then the error message will include the line number and the file
81 /// path.
82 pub fn patterns_from_pathrust-analyzer cargo grep-cli 0.1.12 pattern/patterns_from_path().<P: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>>(path: P) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>> {
83 let path = path.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().();
84 let file = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(path).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| {
85 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(
86 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#,
87 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{}: {}", path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().(), err),
88 )
89 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
90 patterns_from_readerrust-analyzer cargo grep-cli 0.1.12 pattern/patterns_from_reader().(file).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| {
91 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(
92 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#,
93 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{}:{}", path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().(), err),
94 )
95 })
96 }
97
98 /// Read patterns from stdin, one per line.
99 ///
100 /// If there was a problem reading or if any of the patterns contain invalid
101 /// UTF-8, then an error is returned. If there was a problem with a specific
102 /// pattern, then the error message will include the line number and the fact
103 /// that it came from stdin.
104 pub fn patterns_from_stdinrust-analyzer cargo grep-cli 0.1.12 pattern/patterns_from_stdin().() -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>> {
105 let stdin = iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::stdinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stdin().();
106 let locked = stdin.lockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/impl#[Stdin]lock().();
107 patterns_from_readerrust-analyzer cargo grep-cli 0.1.12 pattern/patterns_from_reader().(locked).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| {
108 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#, formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("<stdin>:{}", err))
109 })
110 }
111
112 /// Read patterns from any reader, one per line.
113 ///
114 /// If there was a problem reading or if any of the patterns contain invalid
115 /// UTF-8, then an error is returned. If there was a problem with a specific
116 /// pattern, then the error message will include the line number.
117 ///
118 /// Note that this routine uses its own internal buffer, so the caller should
119 /// not provide their own buffered reader if possible.
120 ///
121 /// # Example
122 ///
123 /// This shows how to parse patterns, one per line.
124 ///
125 /// ```
126 /// use grep_cli::patterns_from_reader;
127 ///
128 /// let patterns = "\
129 /// foo
130 /// bar\\s+foo
131 /// [a-z]{3}
132 /// ";
133 ///
134 /// assert_eq!(patterns_from_reader(patterns.as_bytes())?, vec![
135 /// r"foo",
136 /// r"bar\s+foo",
137 /// r"[a-z]{3}",
138 /// ]);
139 /// # Ok::<(), Box<dyn std::error::Error>>(())
140 /// ```
141 pub fn patterns_from_readerrust-analyzer cargo grep-cli 0.1.12 pattern/patterns_from_reader().<R: iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#>(rdr: R) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>> {
142 let mut patterns = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![];
143 let mut line_number = 0;
144 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::BufReaderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/BufReader#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/impl#[`BufReader<R>`]new().(rdr).for_byte_linerust-analyzer cargo bstr 1.12.0 io/BufReadExt#for_byte_line().(|line| {
145 line_number rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().1;
146 match pattern_from_bytesrust-analyzer cargo grep-cli 0.1.12 pattern/pattern_from_bytes().(line) {
147 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(pattern) => {
148 patterns.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(pattern.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().());
149 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(true)
150 }
151 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(
152 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#,
153 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{}: {}", line_number, err),
154 )),
155 }
156 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
157 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(patterns)
158 }
159
160 #[cfg(test)]
161 mod testsrust-analyzer cargo grep-cli 0.1.12 pattern/tests/ {
162 use superrust-analyzer cargo grep-cli 0.1.12 pattern/::*;
163
164 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
165 fn bytesrust-analyzer cargo grep-cli 0.1.12 pattern/tests/bytes().() {
166 let pat = b"abc\xFFxyz";
167 let err = pattern_from_bytesrust-analyzer cargo grep-cli 0.1.12 pattern/pattern_from_bytes().(pat).unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().();
168 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(3, err.valid_up_torust-analyzer cargo grep-cli 0.1.12 pattern/impl#[InvalidPatternError]valid_up_to().());
169 }
170
171 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
172 #[cfg(unix)]
173 fn osrust-analyzer cargo grep-cli 0.1.12 pattern/tests/os().() {
174 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#;
175 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::unixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/::OsStrExtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/OsStrExt#;
176
177 let pat = OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#::from_bytesrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/impl#[OsStr][OsStrExt]from_bytes().(b"abc\xFFxyz");
178 let err = pattern_from_osrust-analyzer cargo grep-cli 0.1.12 pattern/pattern_from_os().(pat).unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().();
179 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(3, err.valid_up_torust-analyzer cargo grep-cli 0.1.12 pattern/impl#[InvalidPatternError]valid_up_to().());
180 }
181 }