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 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>
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 ggen_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 ggen_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 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);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 ggen_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 ggen_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 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::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 more