Skip to main content

SafeCommand

Struct SafeCommand 

Source
pub struct SafeCommand<State = Unvalidated> { /* private fields */ }
Expand description

A safe command builder with type-state pattern

Commands must be validated before they can be executed. The type-state pattern ensures this at compile time.

§Type States

  • SafeCommand<Unvalidated>: Command is being built, not yet validated
  • SafeCommand<Validated>: Command has been validated and can be executed

§Examples

use ggen_utils::safe_command::SafeCommand;

// Build and validate command
let cmd = SafeCommand::new("cargo")
    .unwrap()
    .arg("build")
    .unwrap()
    .arg("--release")
    .unwrap()
    .validate()
    .unwrap();

// Now cmd can be converted to std::process::Command
let process_cmd = cmd.into_command();

Implementations§

Source§

impl SafeCommand<Unvalidated>

Source

pub fn new<S: AsRef<str>>(command: S) -> Result<Self>

Create a new SafeCommand with a validated command name

§Examples
use ggen_utils::safe_command::SafeCommand;

let cmd = SafeCommand::new("cargo").unwrap();
assert!(SafeCommand::new("rm").is_err());
Source

pub fn arg<S: AsRef<str>>(self, arg: S) -> Result<Self>

Add an argument to the command

§Examples
use ggen_utils::safe_command::SafeCommand;

let cmd = SafeCommand::new("cargo")
    .unwrap()
    .arg("build")
    .unwrap()
    .arg("--release")
    .unwrap();
Source

pub fn arg_path(self, path: &SafePath) -> Self

Add a path argument to the command

This ensures path arguments are validated through SafePath.

§Examples
use ggen_utils::safe_command::SafeCommand;
use ggen_utils::safe_path::SafePath;

let path = SafePath::new("src/generated").unwrap();
let cmd = SafeCommand::new("cargo")
    .unwrap()
    .arg("build")
    .unwrap()
    .arg_path(&path);
Source

pub fn args<I, S>(self, args: I) -> Result<Self>
where I: IntoIterator<Item = S>, S: AsRef<str>,

Add multiple arguments at once

§Examples
use ggen_utils::safe_command::SafeCommand;

let cmd = SafeCommand::new("cargo")
    .unwrap()
    .args(&["build", "--release"])
    .unwrap();
Source

pub fn validate(self) -> Result<SafeCommand<Validated>>

Validate the command and transition to Validated state

This performs final validation including total command length check.

§Examples
use ggen_utils::safe_command::SafeCommand;

let cmd = SafeCommand::new("cargo")
    .unwrap()
    .arg("build")
    .unwrap()
    .validate()
    .unwrap();
Source§

impl SafeCommand<Validated>

Source

pub fn into_command(self) -> Command

Convert this SafeCommand into a std::process::Command

This is only available for validated commands.

§Examples
use ggen_utils::safe_command::SafeCommand;

let safe_cmd = SafeCommand::new("cargo")
    .unwrap()
    .arg("--version")
    .unwrap()
    .validate()
    .unwrap();

let mut cmd = safe_cmd.into_command();
// Now you can execute cmd with std::process::Command
Source

pub fn command(&self) -> &CommandName

Get a reference to the command name

Source

pub fn args(&self) -> &[CommandArg]

Get a reference to the arguments

Source

pub fn to_string_debug(&self) -> String

Get the command as a string for display purposes

This does NOT execute the command, it just formats it as a string.

Trait Implementations§

Source§

impl<State: Clone> Clone for SafeCommand<State>

Source§

fn clone(&self) -> SafeCommand<State>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<State: Debug> Debug for SafeCommand<State>

Source§

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

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

impl Display for SafeCommand<Validated>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<State> Freeze for SafeCommand<State>

§

impl<State> RefUnwindSafe for SafeCommand<State>
where State: RefUnwindSafe,

§

impl<State> Send for SafeCommand<State>
where State: Send,

§

impl<State> Sync for SafeCommand<State>
where State: Sync,

§

impl<State> Unpin for SafeCommand<State>
where State: Unpin,

§

impl<State> UnsafeUnpin for SafeCommand<State>

§

impl<State> UnwindSafe for SafeCommand<State>
where State: UnwindSafe,

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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SendSyncUnwindSafe for T
where T: Send + Sync + UnwindSafe + ?Sized,

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V