Quoted

Struct Quoted 

Source
pub struct Quoted<'a> { /* private fields */ }
Expand description

A wrapper around string types for displaying with quoting and escaping applied.

Implementations§

Source§

impl<'a> Quoted<'a>

Source

pub fn native(text: &'a str) -> Self

Quote a string with the default style for the platform.

On Windows this is PowerShell syntax, on all other platforms this is bash/ksh syntax.

Source

pub fn native_raw(text: &'a OsStr) -> Self

Quote an OsStr with the default style for the platform.

On platforms other than Windows and Unix, if the encoding is invalid, the Debug representation will be used.

Source

pub fn unix(text: &'a str) -> Self

Quote a string using bash/ksh syntax.

§Optional

This requires the optional unix feature.

Source

pub fn unix_raw(bytes: &'a [u8]) -> Self

Quote possibly invalid UTF-8 using bash/ksh syntax.

§Optional

This requires the optional unix feature.

Source

pub fn windows(text: &'a str) -> Self

Quote a string using PowerShell syntax.

§Optional

This requires the optional windows feature.

Examples found in repository?
examples/echo.rs (line 15)
3fn main() {
4    for arg in std::env::args_os().skip(1) {
5        println!("Native: {}", arg.maybe_quote());
6        #[cfg(all(windows, feature = "unix"))]
7        {
8            if let Some(arg) = arg.to_str() {
9                println!("Unix: {}", os_display::Quoted::unix(arg).force(false));
10            }
11        }
12        #[cfg(all(not(windows), feature = "windows"))]
13        {
14            if let Some(arg) = arg.to_str() {
15                println!("Windows: {}", os_display::Quoted::windows(arg).force(false));
16            }
17        }
18    }
19}
Source

pub fn windows_raw(units: &'a [u16]) -> Self

Quote possibly invalid UTF-16 using PowerShell syntax.

§Optional

This requires the optional windows feature and the (default) alloc feature.

Source

pub fn force(self, force: bool) -> Self

Toggle forced quoting. If true, quotes are added even if no special characters are present.

Defaults to true.

Examples found in repository?
examples/echo.rs (line 15)
3fn main() {
4    for arg in std::env::args_os().skip(1) {
5        println!("Native: {}", arg.maybe_quote());
6        #[cfg(all(windows, feature = "unix"))]
7        {
8            if let Some(arg) = arg.to_str() {
9                println!("Unix: {}", os_display::Quoted::unix(arg).force(false));
10            }
11        }
12        #[cfg(all(not(windows), feature = "windows"))]
13        {
14            if let Some(arg) = arg.to_str() {
15                println!("Windows: {}", os_display::Quoted::windows(arg).force(false));
16            }
17        }
18    }
19}
Source

pub fn external(self, external: bool) -> Self

When quoting for PowerShell, toggle whether to use legacy quoting for external programs.

If enabled, double quotes (and sometimes backslashes) will be escaped so that they can be passed to external programs in PowerShell versions before 7.3, or with $PSNativeCommandArgumentPassing set to 'Legacy'.

If disabled, quoting will suit modern argument passing (always used for internal commandlets and .NET functions). Strings that look like options or numbers will be quoted.

It is sadly impossible to quote a string such that it’s suitable for both modern and legacy argument passing.

Defaults to false.

§Optional

This requires either the windows or the native feature. It has no effect on Unix-style quoting.

Trait Implementations§

Source§

impl<'a> Clone for Quoted<'a>

Source§

fn clone(&self) -> Quoted<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Quoted<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Quoted<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T: Quotable + ?Sized> From<&'a T> for Quoted<'a>

Source§

fn from(val: &'a T) -> Self

Converts to this type from the input type.
Source§

impl<'a> Copy for Quoted<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Quoted<'a>

§

impl<'a> RefUnwindSafe for Quoted<'a>

§

impl<'a> Send for Quoted<'a>

§

impl<'a> Sync for Quoted<'a>

§

impl<'a> Unpin for Quoted<'a>

§

impl<'a> UnwindSafe for Quoted<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.