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 validatedSafeCommand<Validated>: Command has been validated and can be executed
§Examples
use crate::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>
impl SafeCommand<Unvalidated>
Sourcepub fn new<S: AsRef<str>>(command: S) -> Result<Self>
pub fn new<S: AsRef<str>>(command: S) -> Result<Self>
Create a new SafeCommand with a validated command name
§Examples
use crate::utils::safe_command::SafeCommand;
let cmd = SafeCommand::new("cargo").unwrap();
assert!(SafeCommand::new("rm").is_err());Sourcepub fn arg<S: AsRef<str>>(self, arg: S) -> Result<Self>
pub fn arg<S: AsRef<str>>(self, arg: S) -> Result<Self>
Add an argument to the command
§Examples
use crate::utils::safe_command::SafeCommand;
let cmd = SafeCommand::new("cargo")
.unwrap()
.arg("build")
.unwrap()
.arg("--release")
.unwrap();Sourcepub fn arg_path(self, path: &SafePath) -> Self
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 crate::utils::safe_command::SafeCommand;
use crate::utils::safe_path::SafePath;
let path = SafePath::new("src/generated").unwrap();
let cmd = SafeCommand::new("cargo")
.unwrap()
.arg("build")
.unwrap()
.arg_path(&path);Sourcepub fn args<I, S>(self, args: I) -> Result<Self>
pub fn args<I, S>(self, args: I) -> Result<Self>
Add multiple arguments at once
§Examples
use crate::utils::safe_command::SafeCommand;
let cmd = SafeCommand::new("cargo")
.unwrap()
.args(&["build", "--release"])
.unwrap();Sourcepub fn validate(self) -> Result<SafeCommand<Validated>>
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 crate::utils::safe_command::SafeCommand;
let cmd = SafeCommand::new("cargo")
.unwrap()
.arg("build")
.unwrap()
.validate()
.unwrap();Source§impl SafeCommand<Validated>
impl SafeCommand<Validated>
Sourcepub fn into_command(self) -> Command
pub fn into_command(self) -> Command
Convert this SafeCommand into a std::process::Command
This is only available for validated commands.
§Examples
use crate::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::CommandSourcepub fn command(&self) -> &CommandName
pub fn command(&self) -> &CommandName
Get a reference to the command name
Sourcepub fn args(&self) -> &[CommandArg]
pub fn args(&self) -> &[CommandArg]
Get a reference to the arguments
Sourcepub fn to_string_debug(&self) -> String
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>
impl<State: Clone> Clone for SafeCommand<State>
Source§fn clone(&self) -> SafeCommand<State>
fn clone(&self) -> SafeCommand<State>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<State: Debug> Debug for SafeCommand<State>
impl<State: Debug> Debug for SafeCommand<State>
Auto Trait Implementations§
impl<State> Freeze for SafeCommand<State>where
PhantomData<State>: Freeze,
impl<State> RefUnwindSafe for SafeCommand<State>where
PhantomData<State>: RefUnwindSafe,
impl<State> Send for SafeCommand<State>where
PhantomData<State>: Send,
impl<State> Sync for SafeCommand<State>where
PhantomData<State>: Sync,
impl<State> Unpin for SafeCommand<State>where
PhantomData<State>: Unpin,
impl<State> UnsafeUnpin for SafeCommand<State>where
PhantomData<State>: UnsafeUnpin,
impl<State> UnwindSafe for SafeCommand<State>where
PhantomData<State>: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> EvidenceKind for T
impl<T> EvidenceKind for T
default fn kind_label(&self) -> &'static str
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more