| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{ |
| 2 | 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#, OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#}, |
| 3 | 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#, |
| 4 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, |
| 5 | 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#, PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#}, |
| 6 | processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::Commandrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Command#, |
| 7 | }; |
| 8 | |
| 9 | use globsetrust-analyzer cargo globset 0.4.17 crate/::{Globrust-analyzer cargo globset 0.4.17 glob/Glob#, GlobSetrust-analyzer cargo globset 0.4.17 GlobSet#, GlobSetBuilderrust-analyzer cargo globset 0.4.17 GlobSetBuilder#}; |
| 10 | |
| 11 | use craterust-analyzer cargo grep-cli 0.1.12 crate/::processrust-analyzer cargo grep-cli 0.1.12 process/::{CommandErrorrust-analyzer cargo grep-cli 0.1.12 process/CommandError#, CommandReaderrust-analyzer cargo grep-cli 0.1.12 process/CommandReader#, CommandReaderBuilderrust-analyzer cargo grep-cli 0.1.12 process/CommandReaderBuilder#}; |
| 12 | |
| 13 | /// A builder for a matcher that determines which files get decompressed. |
| 14 | #[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:)] |
| 15 | pub struct DecompressionMatcherBuilderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcherBuilder# { |
| 16 | /// The commands for each matching glob. |
| 17 | commandsrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcherBuilder#commands.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<DecompressionCommandrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionCommand#>, |
| 18 | /// Whether to include the default matching rules. |
| 19 | defaultsrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcherBuilder#defaults.: bool, |
| 20 | } |
| 21 | |
| 22 | /// A representation of a single command for decompressing data |
| 23 | /// out-of-process. |
| 24 | #[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:)] |
| 25 | struct DecompressionCommandrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionCommand# { |
| 26 | /// The glob that matches this command. |
| 27 | globrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionCommand#glob.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 28 | /// The command or binary name. |
| 29 | binrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionCommand#bin.: PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, |
| 30 | /// The arguments to invoke with the command. |
| 31 | argsrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionCommand#args.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#>, |
| 32 | } |
| 33 | |
| 34 | impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for DecompressionMatcherBuilderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcherBuilder# { |
| 35 | fn defaultrust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionMatcherBuilder][Default]default().() -> DecompressionMatcherBuilderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcherBuilder# { |
| 36 | DecompressionMatcherBuilderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcherBuilder#::newrust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionMatcherBuilder]new().() |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | impl DecompressionMatcherBuilderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcherBuilder# { |
| 41 | /// Create a new builder for configuring a decompression matcher. |
| 42 | pub fn newrust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionMatcherBuilder]new().() -> DecompressionMatcherBuilderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcherBuilder# { |
| 43 | DecompressionMatcherBuilderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcherBuilder# { commandsrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcherBuilder#commands.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![], defaultsrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcherBuilder#defaults.: true } |
| 44 | } |
| 45 | |
| 46 | /// Build a matcher for determining how to decompress files. |
| 47 | /// |
| 48 | /// If there was a problem compiling the matcher, then an error is |
| 49 | /// returned. |
| 50 | pub fn buildrust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionMatcherBuilder]build().(&self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<DecompressionMatcherrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcher#, CommandErrorrust-analyzer cargo grep-cli 0.1.12 process/CommandError#> { |
| 51 | let defaults = if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.defaultsrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcherBuilder#defaults. { |
| 52 | vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![] |
| 53 | } else { |
| 54 | default_decompression_commandsrust-analyzer cargo grep-cli 0.1.12 decompress/default_decompression_commands().() |
| 55 | }; |
| 56 | let mut glob_builder = GlobSetBuilderrust-analyzer cargo globset 0.4.17 GlobSetBuilder#::newrust-analyzer cargo globset 0.4.17 impl#[GlobSetBuilder]new().(); |
| 57 | let mut commands = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 58 | for decomp_cmd in defaults.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().chainrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#chain().(&self.commandsrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcherBuilder#commands.) { |
| 59 | let glob = Globrust-analyzer cargo globset 0.4.17 glob/Glob#::newrust-analyzer cargo globset 0.4.17 glob/impl#[Glob]new().(&decomp_cmd.globrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionCommand#glob.).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| { |
| 60 | CommandErrorrust-analyzer cargo grep-cli 0.1.12 process/CommandError#::iorust-analyzer cargo grep-cli 0.1.12 process/impl#[CommandError]io().(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#, err)) |
| 61 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 62 | glob_builder.addrust-analyzer cargo globset 0.4.17 impl#[GlobSetBuilder]add().(glob); |
| 63 | commands.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(decomp_cmd.clonerust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionCommand][Clone]clone().()); |
| 64 | } |
| 65 | let globs = glob_builder.buildrust-analyzer cargo globset 0.4.17 impl#[GlobSetBuilder]build().().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| { |
| 66 | CommandErrorrust-analyzer cargo grep-cli 0.1.12 process/CommandError#::iorust-analyzer cargo grep-cli 0.1.12 process/impl#[CommandError]io().(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#, err)) |
| 67 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 68 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(DecompressionMatcherrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcher# { globsrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcher#globs., commandsrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcher#commands. }) |
| 69 | } |
| 70 | |
| 71 | /// When enabled, the default matching rules will be compiled into this |
| 72 | /// matcher before any other associations. When disabled, only the |
| 73 | /// rules explicitly given to this builder will be used. |
| 74 | /// |
| 75 | /// This is enabled by default. |
| 76 | pub fn defaultsrust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionMatcherBuilder]defaults().(&mut self, yes: bool) -> &mut DecompressionMatcherBuilderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcherBuilder# { |
| 77 | self.defaultsrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcherBuilder#defaults. = yes; |
| 78 | self |
| 79 | } |
| 80 | |
| 81 | /// Associates a glob with a command to decompress files matching the glob. |
| 82 | /// |
| 83 | /// If multiple globs match the same file, then the most recently added |
| 84 | /// glob takes precedence. |
| 85 | /// |
| 86 | /// The syntax for the glob is documented in the |
| 87 | /// [`globset` crate](https://docs.rs/globset/#syntax). |
| 88 | /// |
| 89 | /// The `program` given is resolved with respect to `PATH` and turned |
| 90 | /// into an absolute path internally before being executed by the current |
| 91 | /// platform. Notably, on Windows, this avoids a security problem where |
| 92 | /// passing a relative path to `CreateProcess` will automatically search |
| 93 | /// the current directory for a matching program. If the program could |
| 94 | /// not be resolved, then it is silently ignored and the association is |
| 95 | /// dropped. For this reason, callers should prefer `try_associate`. |
| 96 | pub fn associaterust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionMatcherBuilder]associate().<P, I, A>( |
| 97 | &mut self, |
| 98 | glob: &str, |
| 99 | program: P, |
| 100 | args: I, |
| 101 | ) -> &mut DecompressionMatcherBuilderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcherBuilder# |
| 102 | where |
| 103 | P: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#>, |
| 104 | I: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# = A>, |
| 105 | A: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#>, |
| 106 | { |
| 107 | let _ = self.try_associaterust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionMatcherBuilder]try_associate().(glob, program, args); |
| 108 | self |
| 109 | } |
| 110 | |
| 111 | /// Associates a glob with a command to decompress files matching the glob. |
| 112 | /// |
| 113 | /// If multiple globs match the same file, then the most recently added |
| 114 | /// glob takes precedence. |
| 115 | /// |
| 116 | /// The syntax for the glob is documented in the |
| 117 | /// [`globset` crate](https://docs.rs/globset/#syntax). |
| 118 | /// |
| 119 | /// The `program` given is resolved with respect to `PATH` and turned |
| 120 | /// into an absolute path internally before being executed by the current |
| 121 | /// platform. Notably, on Windows, this avoids a security problem where |
| 122 | /// passing a relative path to `CreateProcess` will automatically search |
| 123 | /// the current directory for a matching program. If the program could not |
| 124 | /// be resolved, then an error is returned. |
| 125 | pub fn try_associaterust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionMatcherBuilder]try_associate().<P, I, A>( |
| 126 | &mut self, |
| 127 | glob: &str, |
| 128 | program: P, |
| 129 | args: I, |
| 130 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&mut DecompressionMatcherBuilderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcherBuilder#, CommandErrorrust-analyzer cargo grep-cli 0.1.12 process/CommandError#> |
| 131 | where |
| 132 | P: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#>, |
| 133 | I: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# = A>, |
| 134 | A: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#>, |
| 135 | { |
| 136 | let glob = glob.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(); |
| 137 | let bin = try_resolve_binaryrust-analyzer cargo grep-cli 0.1.12 decompress/try_resolve_binary().(Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().(program.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 138 | let args = |
| 139 | args.into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#into_iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|a| a.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().to_os_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_os_string().()).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 140 | self.commandsrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcherBuilder#commands..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(DecompressionCommandrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionCommand# { globrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionCommand#glob., binrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionCommand#bin., argsrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionCommand#args. }); |
| 141 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(self) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /// A matcher for determining how to decompress files. |
| 146 | #[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:)] |
| 147 | pub struct DecompressionMatcherrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcher# { |
| 148 | /// The set of globs to match. Each glob has a corresponding entry in |
| 149 | /// `commands`. When a glob matches, the corresponding command should be |
| 150 | /// used to perform out-of-process decompression. |
| 151 | globsrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcher#globs.: GlobSetrust-analyzer cargo globset 0.4.17 GlobSet#, |
| 152 | /// The commands for each matching glob. |
| 153 | commandsrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcher#commands.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<DecompressionCommandrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionCommand#>, |
| 154 | } |
| 155 | |
| 156 | impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for DecompressionMatcherrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcher# { |
| 157 | fn defaultrust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionMatcher][Default]default().() -> DecompressionMatcherrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcher# { |
| 158 | DecompressionMatcherrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcher#::newrust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionMatcher]new().() |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | impl DecompressionMatcherrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcher# { |
| 163 | /// Create a new matcher with default rules. |
| 164 | /// |
| 165 | /// To add more matching rules, build a matcher with |
| 166 | /// [`DecompressionMatcherBuilder`]. |
| 167 | pub fn newrust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionMatcher]new().() -> DecompressionMatcherrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcher# { |
| 168 | DecompressionMatcherBuilderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcherBuilder#::newrust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionMatcherBuilder]new().() |
| 169 | .buildrust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionMatcherBuilder]build().() |
| 170 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("built-in matching rules should always compile") |
| 171 | } |
| 172 | |
| 173 | /// Return a pre-built command based on the given file path that can |
| 174 | /// decompress its contents. If no such decompressor is known, then this |
| 175 | /// returns `None`. |
| 176 | /// |
| 177 | /// If there are multiple possible commands matching the given path, then |
| 178 | /// the command added last takes precedence. |
| 179 | pub fn commandrust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionMatcher]command().<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#>>(&self, path: P) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Commandrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Command#> { |
| 180 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(i) = self.globsrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcher#globs..matchesrust-analyzer cargo globset 0.4.17 impl#[GlobSet]matches().(path).into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().().next_backrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/into_iter/impl#[`IntoIter<T, A>`][DoubleEndedIterator]next_back().() { |
| 181 | let decomp_cmd = &self.commandsrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcher#commands.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().i]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().; |
| 182 | let mut cmd = Commandrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Command#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]new().(&decomp_cmd.binrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionCommand#bin.); |
| 183 | cmd.argsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]args().(&decomp_cmd.argsrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionCommand#args.); |
| 184 | return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(cmd); |
| 185 | } |
| 186 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 187 | } |
| 188 | |
| 189 | /// Returns true if and only if the given file path has at least one |
| 190 | /// matching command to perform decompression on. |
| 191 | pub fn has_commandrust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionMatcher]has_command().<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#>>(&self, path: P) -> bool { |
| 192 | self.globsrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcher#globs..is_matchrust-analyzer cargo globset 0.4.17 impl#[GlobSet]is_match().(path) |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /// Configures and builds a streaming reader for decompressing data. |
| 197 | #[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:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 198 | pub struct DecompressionReaderBuilderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReaderBuilder# { |
| 199 | matcherrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReaderBuilder#matcher.: DecompressionMatcherrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcher#, |
| 200 | command_builderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReaderBuilder#command_builder.: CommandReaderBuilderrust-analyzer cargo grep-cli 0.1.12 process/CommandReaderBuilder#, |
| 201 | } |
| 202 | |
| 203 | impl DecompressionReaderBuilderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReaderBuilder# { |
| 204 | /// Create a new builder with the default configuration. |
| 205 | pub fn newrust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionReaderBuilder]new().() -> DecompressionReaderBuilderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReaderBuilder# { |
| 206 | DecompressionReaderBuilderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReaderBuilder#::defaultrust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionReaderBuilder][Default]default().() |
| 207 | } |
| 208 | |
| 209 | /// Build a new streaming reader for decompressing data. |
| 210 | /// |
| 211 | /// If decompression is done out-of-process and if there was a problem |
| 212 | /// spawning the process, then its error is logged at the debug level and a |
| 213 | /// passthru reader is returned that does no decompression. This behavior |
| 214 | /// typically occurs when the given file path matches a decompression |
| 215 | /// command, but is executing in an environment where the decompression |
| 216 | /// command is not available. |
| 217 | /// |
| 218 | /// If the given file path could not be matched with a decompression |
| 219 | /// strategy, then a passthru reader is returned that does no |
| 220 | /// decompression. |
| 221 | pub fn buildrust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionReaderBuilder]build().<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#>>( |
| 222 | &self, |
| 223 | path: P, |
| 224 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<DecompressionReaderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReader#, CommandErrorrust-analyzer cargo grep-cli 0.1.12 process/CommandError#> { |
| 225 | let path = path.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().(); |
| 226 | let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(mut cmd) = self.matcherrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReaderBuilder#matcher..commandrust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionMatcher]command().(path) else { |
| 227 | return DecompressionReaderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReader#::new_passthrurust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionReader]new_passthru().(path); |
| 228 | }; |
| 229 | cmd.argrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]arg().(path); |
| 230 | |
| 231 | match self.command_builderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReaderBuilder#command_builder..buildrust-analyzer cargo grep-cli 0.1.12 process/impl#[CommandReaderBuilder]build().(&mut cmd) { |
| 232 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(cmd_reader) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(DecompressionReaderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReader# { rdrrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReader#rdr.: Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(cmd_reader) }), |
| 233 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => { |
| 234 | logrust-analyzer cargo log 0.4.28 crate/::debugrust-analyzer cargo log 0.4.28 macros/debug!!( |
| 235 | "{}: error spawning command '{:?}': {} \ |
| 236 | (falling back to uncompressed reader)", |
| 237 | path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().(), |
| 238 | cmd, |
| 239 | err, |
| 240 | ); |
| 241 | DecompressionReaderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReader#::new_passthrurust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionReader]new_passthru().(path) |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /// Set the matcher to use to look up the decompression command for each |
| 247 | /// file path. |
| 248 | /// |
| 249 | /// A set of sensible rules is enabled by default. Setting this will |
| 250 | /// completely replace the current rules. |
| 251 | pub fn matcherrust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionReaderBuilder]matcher().( |
| 252 | &mut self, |
| 253 | matcher: DecompressionMatcherrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcher#, |
| 254 | ) -> &mut DecompressionReaderBuilderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReaderBuilder# { |
| 255 | self.matcherrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReaderBuilder#matcher. = matcher; |
| 256 | self |
| 257 | } |
| 258 | |
| 259 | /// Get the underlying matcher currently used by this builder. |
| 260 | pub fn get_matcherrust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionReaderBuilder]get_matcher().(&self) -> &DecompressionMatcherrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionMatcher# { |
| 261 | &self.matcherrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReaderBuilder#matcher. |
| 262 | } |
| 263 | |
| 264 | /// When enabled, the reader will asynchronously read the contents of the |
| 265 | /// command's stderr output. When disabled, stderr is only read after the |
| 266 | /// stdout stream has been exhausted (or if the process quits with an error |
| 267 | /// code). |
| 268 | /// |
| 269 | /// Note that when enabled, this may require launching an additional |
| 270 | /// thread in order to read stderr. This is done so that the process being |
| 271 | /// executed is never blocked from writing to stdout or stderr. If this is |
| 272 | /// disabled, then it is possible for the process to fill up the stderr |
| 273 | /// buffer and deadlock. |
| 274 | /// |
| 275 | /// This is enabled by default. |
| 276 | pub fn async_stderrrust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionReaderBuilder]async_stderr().( |
| 277 | &mut self, |
| 278 | yes: bool, |
| 279 | ) -> &mut DecompressionReaderBuilderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReaderBuilder# { |
| 280 | self.command_builderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReaderBuilder#command_builder..async_stderrrust-analyzer cargo grep-cli 0.1.12 process/impl#[CommandReaderBuilder]async_stderr().(yes); |
| 281 | self |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /// A streaming reader for decompressing the contents of a file. |
| 286 | /// |
| 287 | /// The purpose of this reader is to provide a seamless way to decompress the |
| 288 | /// contents of file using existing tools in the current environment. This is |
| 289 | /// meant to be an alternative to using decompression libraries in favor of the |
| 290 | /// simplicity and portability of using external commands such as `gzip` and |
| 291 | /// `xz`. This does impose the overhead of spawning a process, so other means |
| 292 | /// for performing decompression should be sought if this overhead isn't |
| 293 | /// acceptable. |
| 294 | /// |
| 295 | /// A decompression reader comes with a default set of matching rules that are |
| 296 | /// meant to associate file paths with the corresponding command to use to |
| 297 | /// decompress them. For example, a glob like `*.gz` matches gzip compressed |
| 298 | /// files with the command `gzip -d -c`. If a file path does not match any |
| 299 | /// existing rules, or if it matches a rule whose command does not exist in the |
| 300 | /// current environment, then the decompression reader passes through the |
| 301 | /// contents of the underlying file without doing any decompression. |
| 302 | /// |
| 303 | /// The default matching rules are probably good enough for most cases, and if |
| 304 | /// they require revision, pull requests are welcome. In cases where they must |
| 305 | /// be changed or extended, they can be customized through the use of |
| 306 | /// [`DecompressionMatcherBuilder`] and [`DecompressionReaderBuilder`]. |
| 307 | /// |
| 308 | /// By default, this reader will asynchronously read the processes' stderr. |
| 309 | /// This prevents subtle deadlocking bugs for noisy processes that write a lot |
| 310 | /// to stderr. Currently, the entire contents of stderr is read on to the heap. |
| 311 | /// |
| 312 | /// # Example |
| 313 | /// |
| 314 | /// This example shows how to read the decompressed contents of a file without |
| 315 | /// needing to explicitly choose the decompression command to run. |
| 316 | /// |
| 317 | /// Note that if you need to decompress multiple files, it is better to use |
| 318 | /// `DecompressionReaderBuilder`, which will amortize the cost of compiling the |
| 319 | /// matcher. |
| 320 | /// |
| 321 | /// ```no_run |
| 322 | /// use std::{io::Read, process::Command}; |
| 323 | /// |
| 324 | /// use grep_cli::DecompressionReader; |
| 325 | /// |
| 326 | /// let mut rdr = DecompressionReader::new("/usr/share/man/man1/ls.1.gz")?; |
| 327 | /// let mut contents = vec![]; |
| 328 | /// rdr.read_to_end(&mut contents)?; |
| 329 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 330 | /// ``` |
| 331 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 332 | pub struct DecompressionReaderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReader# { |
| 333 | rdrrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReader#rdr.: Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<CommandReaderrust-analyzer cargo grep-cli 0.1.12 process/CommandReader#, Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#>, |
| 334 | } |
| 335 | |
| 336 | impl DecompressionReaderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReader# { |
| 337 | /// Build a new streaming reader for decompressing data. |
| 338 | /// |
| 339 | /// If decompression is done out-of-process and if there was a problem |
| 340 | /// spawning the process, then its error is returned. |
| 341 | /// |
| 342 | /// If the given file path could not be matched with a decompression |
| 343 | /// strategy, then a passthru reader is returned that does no |
| 344 | /// decompression. |
| 345 | /// |
| 346 | /// This uses the default matching rules for determining how to decompress |
| 347 | /// the given file. To change those matching rules, use |
| 348 | /// [`DecompressionReaderBuilder`] and [`DecompressionMatcherBuilder`]. |
| 349 | /// |
| 350 | /// When creating readers for many paths. it is better to use the builder |
| 351 | /// since it will amortize the cost of constructing the matcher. |
| 352 | pub fn newrust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionReader]new().<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#>>( |
| 353 | path: P, |
| 354 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<DecompressionReaderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReader#, CommandErrorrust-analyzer cargo grep-cli 0.1.12 process/CommandError#> { |
| 355 | DecompressionReaderBuilderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReaderBuilder#::newrust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionReaderBuilder]new().().buildrust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionReaderBuilder]build().(path) |
| 356 | } |
| 357 | |
| 358 | /// Creates a new "passthru" decompression reader that reads from the file |
| 359 | /// corresponding to the given path without doing decompression and without |
| 360 | /// executing another process. |
| 361 | fn new_passthrurust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionReader]new_passthru().(path: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<DecompressionReaderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReader#, CommandErrorrust-analyzer cargo grep-cli 0.1.12 process/CommandError#> { |
| 362 | let file = 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)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 363 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(DecompressionReaderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReader# { rdrrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReader#rdr.: Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(file) }) |
| 364 | } |
| 365 | |
| 366 | /// Closes this reader, freeing any resources used by its underlying child |
| 367 | /// process, if one was used. If the child process exits with a nonzero |
| 368 | /// exit code, the returned Err value will include its stderr. |
| 369 | /// |
| 370 | /// `close` is idempotent, meaning it can be safely called multiple times. |
| 371 | /// The first call closes the CommandReader and any subsequent calls do |
| 372 | /// nothing. |
| 373 | /// |
| 374 | /// This method should be called after partially reading a file to prevent |
| 375 | /// resource leakage. However there is no need to call `close` explicitly |
| 376 | /// if your code always calls `read` to EOF, as `read` takes care of |
| 377 | /// calling `close` in this case. |
| 378 | /// |
| 379 | /// `close` is also called in `drop` as a last line of defense against |
| 380 | /// resource leakage. Any error from the child process is then printed as a |
| 381 | /// warning to stderr. This can be avoided by explicitly calling `close` |
| 382 | /// before the CommandReader is dropped. |
| 383 | pub fn closerust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionReader]close().(&mut self) -> 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#<()> { |
| 384 | match self.rdrrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReader#rdr. { |
| 385 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ref mut rdr) => rdr.closerust-analyzer cargo grep-cli 0.1.12 process/impl#[CommandReader]close().(), |
| 386 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()), |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | impl 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# for DecompressionReaderrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReader# { |
| 392 | fn readrust-analyzer cargo grep-cli 0.1.12 decompress/impl#[DecompressionReader][Read]read().(&mut self, buf: &mut [u8]) -> 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#<usize> { |
| 393 | match self.rdrrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionReader#rdr. { |
| 394 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ref mut rdr) => rdr.readrust-analyzer cargo grep-cli 0.1.12 process/impl#[CommandReader][Read]read().(buf), |
| 395 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ref mut rdr) => rdr.readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File][Read]read().(buf), |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /// Resolves a path to a program to a path by searching for the program in |
| 401 | /// `PATH`. |
| 402 | /// |
| 403 | /// If the program could not be resolved, then an error is returned. |
| 404 | /// |
| 405 | /// The purpose of doing this instead of passing the path to the program |
| 406 | /// directly to Command::new is that Command::new will hand relative paths |
| 407 | /// to CreateProcess on Windows, which will implicitly search the current |
| 408 | /// working directory for the executable. This could be undesirable for |
| 409 | /// security reasons. e.g., running ripgrep with the -z/--search-zip flag on an |
| 410 | /// untrusted directory tree could result in arbitrary programs executing on |
| 411 | /// Windows. |
| 412 | /// |
| 413 | /// Note that this could still return a relative path if PATH contains a |
| 414 | /// relative path. We permit this since it is assumed that the user has set |
| 415 | /// this explicitly, and thus, desires this behavior. |
| 416 | /// |
| 417 | /// # Platform behavior |
| 418 | /// |
| 419 | /// On non-Windows, this is a no-op. |
| 420 | pub fn resolve_binaryrust-analyzer cargo grep-cli 0.1.12 decompress/resolve_binary().<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#>>( |
| 421 | prog: P, |
| 422 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, CommandErrorrust-analyzer cargo grep-cli 0.1.12 process/CommandError#> { |
| 423 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(windows) { |
| 424 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(prog.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().to_path_bufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_path_buf().()); |
| 425 | } |
| 426 | try_resolve_binaryrust-analyzer cargo grep-cli 0.1.12 decompress/try_resolve_binary().(prog) |
| 427 | } |
| 428 | |
| 429 | /// Resolves a path to a program to a path by searching for the program in |
| 430 | /// `PATH`. |
| 431 | /// |
| 432 | /// If the program could not be resolved, then an error is returned. |
| 433 | /// |
| 434 | /// The purpose of doing this instead of passing the path to the program |
| 435 | /// directly to Command::new is that Command::new will hand relative paths |
| 436 | /// to CreateProcess on Windows, which will implicitly search the current |
| 437 | /// working directory for the executable. This could be undesirable for |
| 438 | /// security reasons. e.g., running ripgrep with the -z/--search-zip flag on an |
| 439 | /// untrusted directory tree could result in arbitrary programs executing on |
| 440 | /// Windows. |
| 441 | /// |
| 442 | /// Note that this could still return a relative path if PATH contains a |
| 443 | /// relative path. We permit this since it is assumed that the user has set |
| 444 | /// this explicitly, and thus, desires this behavior. |
| 445 | /// |
| 446 | /// If `check_exists` is false or the path is already an absolute path this |
| 447 | /// will return immediately. |
| 448 | fn try_resolve_binaryrust-analyzer cargo grep-cli 0.1.12 decompress/try_resolve_binary().<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#>>( |
| 449 | prog: P, |
| 450 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, CommandErrorrust-analyzer cargo grep-cli 0.1.12 process/CommandError#> { |
| 451 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/; |
| 452 | |
| 453 | fn is_exerust-analyzer cargo grep-cli 0.1.12 decompress/is_exe().(path: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#) -> bool { |
| 454 | let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(md) = path.metadatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]metadata().() else { return false }; |
| 455 | !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().md.is_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[Metadata]is_dir().() |
| 456 | } |
| 457 | |
| 458 | let prog = prog.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().(); |
| 459 | if prog.is_absoluterust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]is_absolute().() { |
| 460 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(prog.to_path_bufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_path_buf().()); |
| 461 | } |
| 462 | let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(syspaths) = envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::var_osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var_os().("PATH") else { |
| 463 | let msg = "system PATH environment variable not found"; |
| 464 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(CommandErrorrust-analyzer cargo grep-cli 0.1.12 process/CommandError#::iorust-analyzer cargo grep-cli 0.1.12 process/impl#[CommandError]io().(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().( |
| 465 | 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#, |
| 466 | msg, |
| 467 | ))); |
| 468 | }; |
| 469 | for syspath in envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::split_pathsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/split_paths().(&syspaths) { |
| 470 | if syspath.as_os_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]as_os_str().().is_emptyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]is_empty().() { |
| 471 | continue; |
| 472 | } |
| 473 | let abs_prog = syspath.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(prog); |
| 474 | if is_exerust-analyzer cargo grep-cli 0.1.12 decompress/is_exe().(&abs_prog) { |
| 475 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(abs_prog.to_path_bufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_path_buf().()); |
| 476 | } |
| 477 | if abs_prog.extensionrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]extension().().is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() { |
| 478 | for extension in ["com", "exe"] { |
| 479 | let abs_prog = abs_prog.with_extensionrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]with_extension().(extension); |
| 480 | if is_exerust-analyzer cargo grep-cli 0.1.12 decompress/is_exe().(&abs_prog) { |
| 481 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(abs_prog.to_path_bufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_path_buf().()); |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | let msg = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{}: could not find executable in PATH", prog.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().()); |
| 487 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(CommandErrorrust-analyzer cargo grep-cli 0.1.12 process/CommandError#::iorust-analyzer cargo grep-cli 0.1.12 process/impl#[CommandError]io().(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#, msg))); |
| 488 | } |
| 489 | |
| 490 | fn default_decompression_commandsrust-analyzer cargo grep-cli 0.1.12 decompress/default_decompression_commands().() -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<DecompressionCommandrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionCommand#> { |
| 491 | const ARGS_GZIPrust-analyzer cargo grep-cli 0.1.12 decompress/ARGS_GZIP.: &[&str] = &["gzip", "-d", "-c"]; |
| 492 | const ARGS_BZIPrust-analyzer cargo grep-cli 0.1.12 decompress/ARGS_BZIP.: &[&str] = &["bzip2", "-d", "-c"]; |
| 493 | const ARGS_XZrust-analyzer cargo grep-cli 0.1.12 decompress/ARGS_XZ.: &[&str] = &["xz", "-d", "-c"]; |
| 494 | const ARGS_LZ4rust-analyzer cargo grep-cli 0.1.12 decompress/ARGS_LZ4.: &[&str] = &["lz4", "-d", "-c"]; |
| 495 | const ARGS_LZMArust-analyzer cargo grep-cli 0.1.12 decompress/ARGS_LZMA.: &[&str] = &["xz", "--format=lzma", "-d", "-c"]; |
| 496 | const ARGS_BROTLIrust-analyzer cargo grep-cli 0.1.12 decompress/ARGS_BROTLI.: &[&str] = &["brotli", "-d", "-c"]; |
| 497 | const ARGS_ZSTDrust-analyzer cargo grep-cli 0.1.12 decompress/ARGS_ZSTD.: &[&str] = &["zstd", "-q", "-d", "-c"]; |
| 498 | const ARGS_UNCOMPRESSrust-analyzer cargo grep-cli 0.1.12 decompress/ARGS_UNCOMPRESS.: &[&str] = &["uncompress", "-c"]; |
| 499 | |
| 500 | fn addrust-analyzer cargo grep-cli 0.1.12 decompress/add().(glob: &str, args: &[&str], cmds: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<DecompressionCommandrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionCommand#>) { |
| 501 | let bin = match resolve_binaryrust-analyzer cargo grep-cli 0.1.12 decompress/resolve_binary().(Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().(args[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().)) { |
| 502 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(bin) => bin, |
| 503 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => { |
| 504 | logrust-analyzer cargo log 0.4.28 crate/::debugrust-analyzer cargo log 0.4.28 macros/debug!!("{}", err); |
| 505 | return; |
| 506 | } |
| 507 | }; |
| 508 | cmds.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(DecompressionCommandrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionCommand# { |
| 509 | globrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionCommand#glob.: glob.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 510 | binrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionCommand#bin., |
| 511 | argsrust-analyzer cargo grep-cli 0.1.12 decompress/DecompressionCommand#args.: args |
| 512 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 513 | .skiprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#skip().(1) |
| 514 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|s| OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]new().(s).to_os_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_os_string().()) |
| 515 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(), |
| 516 | }); |
| 517 | } |
| 518 | let mut cmds = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 519 | addrust-analyzer cargo grep-cli 0.1.12 decompress/add().("*.gz", ARGS_GZIPrust-analyzer cargo grep-cli 0.1.12 decompress/ARGS_GZIP., &mut cmds); |
| 520 | addrust-analyzer cargo grep-cli 0.1.12 decompress/add().("*.tgz", ARGS_GZIPrust-analyzer cargo grep-cli 0.1.12 decompress/ARGS_GZIP., &mut cmds); |
| 521 | addrust-analyzer cargo grep-cli 0.1.12 decompress/add().("*.bz2", ARGS_BZIPrust-analyzer cargo grep-cli 0.1.12 decompress/ARGS_BZIP., &mut cmds); |
| 522 | addrust-analyzer cargo grep-cli 0.1.12 decompress/add().("*.tbz2", ARGS_BZIPrust-analyzer cargo grep-cli 0.1.12 decompress/ARGS_BZIP., &mut cmds); |
| 523 | addrust-analyzer cargo grep-cli 0.1.12 decompress/add().("*.xz", ARGS_XZrust-analyzer cargo grep-cli 0.1.12 decompress/ARGS_XZ., &mut cmds); |
| 524 | addrust-analyzer cargo grep-cli 0.1.12 decompress/add().("*.txz", ARGS_XZrust-analyzer cargo grep-cli 0.1.12 decompress/ARGS_XZ., &mut cmds); |
| 525 | addrust-analyzer cargo grep-cli 0.1.12 decompress/add().("*.lz4", ARGS_LZ4rust-analyzer cargo grep-cli 0.1.12 decompress/ARGS_LZ4., &mut cmds); |
| 526 | addrust-analyzer cargo grep-cli 0.1.12 decompress/add().("*.lzma", ARGS_LZMArust-analyzer cargo grep-cli 0.1.12 decompress/ARGS_LZMA., &mut cmds); |
| 527 | addrust-analyzer cargo grep-cli 0.1.12 decompress/add().("*.br", ARGS_BROTLIrust-analyzer cargo grep-cli 0.1.12 decompress/ARGS_BROTLI., &mut cmds); |
| 528 | addrust-analyzer cargo grep-cli 0.1.12 decompress/add().("*.zst", ARGS_ZSTDrust-analyzer cargo grep-cli 0.1.12 decompress/ARGS_ZSTD., &mut cmds); |
| 529 | addrust-analyzer cargo grep-cli 0.1.12 decompress/add().("*.zstd", ARGS_ZSTDrust-analyzer cargo grep-cli 0.1.12 decompress/ARGS_ZSTD., &mut cmds); |
| 530 | addrust-analyzer cargo grep-cli 0.1.12 decompress/add().("*.Z", ARGS_UNCOMPRESSrust-analyzer cargo grep-cli 0.1.12 decompress/ARGS_UNCOMPRESS., &mut cmds); |
| 531 | cmds |
| 532 | } |