| 1 | //! <!-- Override references in README.md back to `cargo doc`-generated links. --> |
| 2 | //! |
| 3 | //! [`&str`]: `&str` |
| 4 | //! [`String`]: `String` |
| 5 | //! [`bstr::BStr`]: `bstr::BStr` |
| 6 | //! [`bstr::BString`]: `bstr::BString` |
| 7 | //! [`slice`]: `slice` |
| 8 | //! [`Vec<u8>`]: `std::vec::Vec<u8>` |
| 9 | //! [`OsStr`]: `std::ffi::OsStr` |
| 10 | //! [`OsString`]: `std::ffi::OsString` |
| 11 | //! [`Path`]: `std::path::Path` |
| 12 | //! [`PathBuf`]: `std::path::PathBuf` |
| 13 | //! |
| 14 | //! [`Sh`]: `Sh` |
| 15 | //! [`Dash`]: `Dash` |
| 16 | //! [`Bash`]: `Bash` |
| 17 | //! [`Fish`]: `Fish` |
| 18 | //! [`Zsh`]: `Zsh` |
| 19 | //! |
| 20 | //! [`QuoteRefExt`]: `QuoteRefExt` |
| 21 | //! [`QuoteRefExt::quoted`]: `QuoteRefExt::quoted` |
| 22 | //! [`QuoteExt`]: `QuoteExt` |
| 23 | //! |
| 24 | //! <style> |
| 25 | //! .readme-only { display: none; } |
| 26 | //! </style> |
| 27 | //! |
| 28 | |
| 29 | #![cfg_attr( |
| 30 | all( |
| 31 | feature = "bstr", |
| 32 | feature = "bash", |
| 33 | feature = "fish", |
| 34 | feature = "sh", |
| 35 | ), |
| 36 | doc = include_str!("../README.md") |
| 37 | )] |
| 38 | |
| 39 | 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#, OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#}; |
| 40 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::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#}; |
| 41 | |
| 42 | mod asciirust-analyzer cargo shell-quote 0.7.2 ascii/; |
| 43 | mod bashrust-analyzer cargo shell-quote 0.7.2 bash/; |
| 44 | mod fishrust-analyzer cargo shell-quote 0.7.2 fish/; |
| 45 | mod shrust-analyzer cargo shell-quote 0.7.2 sh/; |
| 46 | mod utf8rust-analyzer cargo shell-quote 0.7.2 utf8/; |
| 47 | |
| 48 | #[cfg(feature = "bash")] |
| 49 | pub use bashrust-analyzer cargo shell-quote 0.7.2 bash/::Bashrust-analyzer cargo shell-quote 0.7.2 bash/Bash#; |
| 50 | #[cfg(feature = "fish")] |
| 51 | pub use fishrust-analyzer cargo shell-quote 0.7.2 fish/::Fishrust-analyzer cargo shell-quote 0.7.2 fish/Fish#; |
| 52 | #[cfg(feature = "sh")] |
| 53 | pub use shrust-analyzer cargo shell-quote 0.7.2 sh/::Shrust-analyzer cargo shell-quote 0.7.2 sh/Sh#; |
| 54 | |
| 55 | /// Dash accepts the same quoted/escaped strings as `/bin/sh` – indeed, on many |
| 56 | /// systems, `dash` _is_ `/bin/sh` – hence this is an alias for [`Sh`]. |
| 57 | #[cfg(feature = "sh")] |
| 58 | pub type Dashrust-analyzer cargo shell-quote 0.7.2 Dash# = shrust-analyzer cargo shell-quote 0.7.2 sh/::Shrust-analyzer cargo shell-quote 0.7.2 sh/Sh#; |
| 59 | |
| 60 | /// Zsh accepts the same quoted/escaped strings as Bash, hence this is an alias |
| 61 | /// for [`Bash`]. |
| 62 | #[cfg(feature = "bash")] |
| 63 | pub type Zshrust-analyzer cargo shell-quote 0.7.2 Zsh# = bashrust-analyzer cargo shell-quote 0.7.2 bash/::Bashrust-analyzer cargo shell-quote 0.7.2 bash/Bash#; |
| 64 | |
| 65 | // ---------------------------------------------------------------------------- |
| 66 | |
| 67 | /// Quoting/escaping a string of bytes into a shell-safe form. |
| 68 | pub trait QuoteIntorust-analyzer cargo shell-quote 0.7.2 QuoteInto#<OUT: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#> { |
| 69 | /// Quote/escape a string of bytes into an existing container. |
| 70 | fn quote_intorust-analyzer cargo shell-quote 0.7.2 QuoteInto#quote_into().<'q, S: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'q>>>(s: S, out: &mut OUT); |
| 71 | } |
| 72 | |
| 73 | /// Quoting/escaping a string of bytes into a shell-safe form. |
| 74 | pub trait Quoterust-analyzer cargo shell-quote 0.7.2 Quote#<OUT: Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#>: QuoteIntorust-analyzer cargo shell-quote 0.7.2 QuoteInto#<OUT> { |
| 75 | /// Quote/escape a string of bytes into a new container. |
| 76 | fn quoterust-analyzer cargo shell-quote 0.7.2 Quote#quote().<'q, S: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'q>>>(s: S) -> OUT { |
| 77 | let mut out = OUT::defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#default().(); |
| 78 | Self::quote_intorust-analyzer cargo shell-quote 0.7.2 QuoteInto#quote_into().(s, &mut out); |
| 79 | out |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /// Blanket [`Quote`] impl for anything that has a [`QuoteInto`] impl. |
| 84 | impl<T: QuoteIntorust-analyzer cargo shell-quote 0.7.2 QuoteInto#<OUT>, OUT: Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#> Quoterust-analyzer cargo shell-quote 0.7.2 Quote#<OUT> for T {} |
| 85 | |
| 86 | // ---------------------------------------------------------------------------- |
| 87 | |
| 88 | /// Extension trait for pushing shell quoted byte slices, e.g. `&[u8]`, [`&str`] |
| 89 | /// – anything that's [`Quotable`] – into container types like [`Vec<u8>`], |
| 90 | /// [`String`], [`OsString`] on Unix, and [`bstr::BString`] if it's enabled. |
| 91 | pub trait QuoteExtrust-analyzer cargo shell-quote 0.7.2 QuoteExt# { |
| 92 | fn push_quotedrust-analyzer cargo shell-quote 0.7.2 QuoteExt#push_quoted().<'q, Q, S>(&mut self, _q: Q, s: S) |
| 93 | where |
| 94 | Q: QuoteIntorust-analyzer cargo shell-quote 0.7.2 QuoteInto#<Self>, |
| 95 | S: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'q>>; |
| 96 | } |
| 97 | |
| 98 | impl<T: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#> QuoteExtrust-analyzer cargo shell-quote 0.7.2 QuoteExt# for T { |
| 99 | fn push_quotedrust-analyzer cargo shell-quote 0.7.2 impl#[T][QuoteExt]push_quoted().<'q, Q, S>(&mut self, _q: Q, s: S) |
| 100 | where |
| 101 | Q: QuoteIntorust-analyzer cargo shell-quote 0.7.2 QuoteInto#<Selfrust-analyzer cargo shell-quote 0.7.2 impl#[T][QuoteExt]>, |
| 102 | S: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'q>>, |
| 103 | { |
| 104 | Q::quote_intorust-analyzer cargo shell-quote 0.7.2 QuoteInto#quote_into().(s, self); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // ---------------------------------------------------------------------------- |
| 109 | |
| 110 | /// Extension trait for shell quoting many different owned and reference types, |
| 111 | /// e.g. `&[u8]`, [`&str`] – anything that's [`Quotable`] – into owned container |
| 112 | /// types like [`Vec<u8>`], [`String`], [`OsString`] on Unix, and |
| 113 | /// [`bstr::BString`] if it's enabled. |
| 114 | pub trait QuoteRefExtrust-analyzer cargo shell-quote 0.7.2 QuoteRefExt#<Output: Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#> { |
| 115 | fn quotedrust-analyzer cargo shell-quote 0.7.2 QuoteRefExt#quoted().<Q: Quoterust-analyzer cargo shell-quote 0.7.2 Quote#<Output>>(self, q: Q) -> Output; |
| 116 | } |
| 117 | |
| 118 | impl<'a, S, OUT: Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#> QuoteRefExtrust-analyzer cargo shell-quote 0.7.2 QuoteRefExt#<OUT> for S |
| 119 | where |
| 120 | S: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a>>, |
| 121 | { |
| 122 | fn quotedrust-analyzer cargo shell-quote 0.7.2 impl#[S][`QuoteRefExt<OUT>`]quoted().<Q: Quoterust-analyzer cargo shell-quote 0.7.2 Quote#<OUT>>(self, _q: Q) -> OUT { |
| 123 | Q::quoterust-analyzer cargo shell-quote 0.7.2 Quote#quote().(self) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // ---------------------------------------------------------------------------- |
| 128 | |
| 129 | /// A string of bytes that can be quoted/escaped. |
| 130 | /// |
| 131 | /// This is used by many methods in this crate as a generic |
| 132 | /// [`Into<Quotable>`][`Into`] constraint. Why not accept |
| 133 | /// [`AsRef<[u8]>`][`AsRef`] instead? The ergonomics of that approach were not |
| 134 | /// so good. For example, quoting [`OsString`]/[`OsStr`] and |
| 135 | /// [`PathBuf`]/[`Path`] didn't work in a natural way. |
| 136 | pub enum Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a> { |
| 137 | #[cfg_attr( |
| 138 | not(any(feature = "bash", feature = "fish", feature = "sh")), |
| 139 | allow(unused) |
| 140 | )] |
| 141 | Bytesrust-analyzer cargo shell-quote 0.7.2 Quotable#Bytes#(&'a [u8]), |
| 142 | #[cfg_attr( |
| 143 | not(any(feature = "bash", feature = "fish", feature = "sh")), |
| 144 | allow(unused) |
| 145 | )] |
| 146 | Textrust-analyzer cargo shell-quote 0.7.2 Quotable#Text#(&'a str), |
| 147 | } |
| 148 | |
| 149 | impl<'a> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<&'a [u8]> for Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a> { |
| 150 | fn fromrust-analyzer cargo shell-quote 0.7.2 impl#[`Quotable<'a>`][`From<&'a [u8]>`]from().(source: &'a [u8]) -> Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a> { |
| 151 | Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#::Bytesrust-analyzer cargo shell-quote 0.7.2 Quotable#Bytes#(source) |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | impl<'a, const N: usize> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<&'a [u8; N]> for Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a> { |
| 156 | fn fromrust-analyzer cargo shell-quote 0.7.2 impl#[`Quotable<'a>`][`From<&'a [u8; N]>`]from().(source: &'a [u8; N]) -> Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a> { |
| 157 | Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#::Bytesrust-analyzer cargo shell-quote 0.7.2 Quotable#Bytes#(&source[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | impl<'a> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<&'a Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>> for Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a> { |
| 162 | fn fromrust-analyzer cargo shell-quote 0.7.2 impl#[`Quotable<'a>`][`From<&'a Vec<u8>>`]from().(source: &'a Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>) -> Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a> { |
| 163 | Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#::Bytesrust-analyzer cargo shell-quote 0.7.2 Quotable#Bytes#(source) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | impl<'a> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<&'a str> for Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a> { |
| 168 | fn fromrust-analyzer cargo shell-quote 0.7.2 impl#[`Quotable<'a>`][`From<&'a str>`]from().(source: &'a str) -> Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a> { |
| 169 | Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#::Textrust-analyzer cargo shell-quote 0.7.2 Quotable#Text#(source) |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | impl<'a> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<&'a Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> for Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a> { |
| 174 | fn fromrust-analyzer cargo shell-quote 0.7.2 impl#[`Quotable<'a>`][`From<&'a String>`]from().(source: &'a Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a> { |
| 175 | Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#::Textrust-analyzer cargo shell-quote 0.7.2 Quotable#Text#(source) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | #[cfg(unix)] |
| 180 | impl<'a> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<&'a OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#> for Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a> { |
| 181 | fn fromrust-analyzer cargo shell-quote 0.7.2 impl#[`Quotable<'a>`][`From<&'a OsStr>`]from().(source: &'a OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#) -> Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a> { |
| 182 | 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#; |
| 183 | source.as_bytesrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/impl#[OsStr][OsStrExt]as_bytes().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | #[cfg(unix)] |
| 188 | impl<'a> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<&'a OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#> for Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a> { |
| 189 | fn fromrust-analyzer cargo shell-quote 0.7.2 impl#[`Quotable<'a>`][`From<&'a OsString>`]from().(source: &'a OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#) -> Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a> { |
| 190 | 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#; |
| 191 | source.as_bytesrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/impl#[OsStr][OsStrExt]as_bytes().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | #[cfg(feature = "bstr")] |
| 196 | impl<'a> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<&'a bstrrust-analyzer cargo bstr 1.13.1 crate/::BStrrust-analyzer cargo bstr 1.13.1 bstr/BStr#> for Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a> { |
| 197 | fn fromrust-analyzer cargo shell-quote 0.7.2 impl#[`Quotable<'a>`][`From<&'a bstr::BStr>`]from().(source: &'a bstrrust-analyzer cargo bstr 1.13.1 crate/::BStrrust-analyzer cargo bstr 1.13.1 bstr/BStr#) -> Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a> { |
| 198 | let bytes: &[u8] = source.as_refrust-analyzer cargo bstr 1.13.1 impls/bstr/impl#[BStr][`AsRef<[u8]>`]as_ref().(); |
| 199 | bytes.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | #[cfg(feature = "bstr")] |
| 204 | impl<'a> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<&'a bstrrust-analyzer cargo bstr 1.13.1 crate/::BStringrust-analyzer cargo bstr 1.13.1 bstring/BString#> for Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a> { |
| 205 | fn fromrust-analyzer cargo shell-quote 0.7.2 impl#[`Quotable<'a>`][`From<&'a bstr::BString>`]from().(source: &'a bstrrust-analyzer cargo bstr 1.13.1 crate/::BStringrust-analyzer cargo bstr 1.13.1 bstring/BString#) -> Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a> { |
| 206 | let bytes: &[u8] = source.as_refrust-analyzer cargo bstr 1.13.1 impls/bstring/impl#[BString][`AsRef<[u8]>`]as_ref().(); |
| 207 | bytes.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | #[cfg(unix)] |
| 212 | impl<'a> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<&'a Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#> for Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a> { |
| 213 | fn fromrust-analyzer cargo shell-quote 0.7.2 impl#[`Quotable<'a>`][`From<&'a Path>`]from().(source: &'a Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#) -> Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a> { |
| 214 | source.as_os_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]as_os_str().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | #[cfg(unix)] |
| 219 | impl<'a> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<&'a PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#> for Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a> { |
| 220 | fn fromrust-analyzer cargo shell-quote 0.7.2 impl#[`Quotable<'a>`][`From<&'a PathBuf>`]from().(source: &'a PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#) -> Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a> { |
| 221 | source.as_os_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]as_os_str().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 222 | } |
| 223 | } |