Trait cradle::input::Input[][src]

pub trait Input {
    fn run<O>(self) -> O
    where
        Self: Sized,
        O: Output
, { ... }
fn run_unit(self)
    where
        Self: Sized
, { ... }
fn run_result<O>(self) -> Result<O, Error>
    where
        Self: Sized,
        O: Output
, { ... } }
Expand description

All types that are possible arguments to cmd!, cmd_unit! or cmd_result! must implement this trait. This makes cradle very flexible. For example you can pass in an executable as a String, and a variable number of arguments as a Vec:

use cradle::prelude::*;

let executable = "echo";
let arguments = vec!["foo", "bar"];
let StdoutUntrimmed(output) = cmd!(executable, arguments);
assert_eq!(output, "foo bar\n");

For more documentation on all possible input types, see the documentation for the individual impls of Input. Here’s a non-exhaustive list of the most commonly used types to get you started:

Tuples

cradle also implements Input for tuples of types that themselves implement Input. Instead of passing multiple arguments to cmd!, they can be passed in a single tuple:

use cradle::prelude::*;

let args = ("echo", "foo");
let StdoutTrimmed(output) = cmd!(args);
assert_eq!(output, "foo");

This can be used to group arguments:

use cradle::prelude::*;

let to_hex_command = ("xxd", "-ps", "-u", LogCommand);
let StdoutTrimmed(output) = cmd!(to_hex_command, Stdin(&[14, 15, 16]));
assert_eq!(output, "0E0F10");

Also, tuples make it possible to write wrappers around cmd! without requiring the use of macros:

use cradle::prelude::*;

fn to_hex<I: Input>(input: I) -> String {
  let StdoutTrimmed(hex) = cmd!(%"xxd -ps -u", input);
  hex
}

// It works for slices:
let hex = to_hex(Stdin(&[14, 15, 16]));
assert_eq!(hex, "0E0F10");

// Vectors:
let hex = to_hex(Stdin(vec![14, 15, 16]));
assert_eq!(hex, "0E0F10");

// And multiple arguments using tuples:
let hex = to_hex((Stdin(&[14, 15, 16]), Stdin(&[17, 18, 19])));
assert_eq!(hex, "0E0F10111213");

Provided methods

input.run() runs input as a child process. It’s equivalent to cmd!(input).

use cradle::prelude::*;

let StdoutTrimmed(output) = ("echo", "foo").run();
assert_eq!(output, "foo");

input.run_unit() runs input as a child process. It’s equivalent to cmd_unit!(input).

use cradle::prelude::*;

("touch", "foo").run_unit();

input.run_result() runs input as a child process. It’s equivalent to cmd_result!(input).

use cradle::prelude::*;

// make sure build tools are installed
cmd_result!(%"which make")?;
cmd_result!(%"which gcc")?;
cmd_result!(%"which ld")?;
cmd_result!(%"make build")?;

Implementations on Foreign Types

Blanket implementation for &_.

Arguments of type OsString are passed to the child process as arguments.

use cradle::prelude::*;

cmd_unit!("ls", std::env::var_os("HOME").unwrap());

Arguments of type &OsStr are passed to the child process as arguments.

use cradle::prelude::*;

cmd_unit!("echo", std::env::current_dir().unwrap().file_name().unwrap());

Arguments of type &str are passed to the child process as arguments. This is especially useful because it allows you to use string literals:

use cradle::prelude::*;

let StdoutTrimmed(output) = cmd!("echo", "foo");
assert_eq!(output, "foo");

Arguments of type String are passed to the child process as arguments. Executables can also be passed as Strings:

use cradle::prelude::*;

let executable: String = "echo".to_string();
let argument: String = "foo".to_string();
let StdoutTrimmed(output) = cmd!(executable, argument);
assert_eq!(output, "foo");

Allows to use split to split your argument into words:

use cradle::prelude::*;

let StdoutTrimmed(output) = cmd!("echo foo".split(' '));
assert_eq!(output, "foo");

Arguments to split must be of type char.

Allows to use split_whitespace to split your argument into words:

use cradle::prelude::*;

let StdoutTrimmed(output) = cmd!("echo foo".split_whitespace());
assert_eq!(output, "foo");

Allows to use split_ascii_whitespace to split your argument into words:

use cradle::prelude::*;

let StdoutTrimmed(output) = cmd!("echo foo".split_ascii_whitespace());
assert_eq!(output, "foo");

All elements of the given Vec are used as arguments to cmd!. Same as passing in the elements separately.

use cradle::prelude::*;

let StdoutTrimmed(output) = cmd!(vec!["echo", "foo"]);
assert_eq!(output, "foo");

Similar to the implementation for Vec<T>. All elements of the array will be used as arguments.

use cradle::prelude::*;

let StdoutTrimmed(output) = cmd!(["echo", "foo"]);
assert_eq!(output, "foo");

Only works on rust version 1.51 and up.

Similar to the implementation for Vec<T>. All elements of the slice will be used as arguments.

Arguments of type PathBuf are passed to the child process as arguments.

use cradle::prelude::*;
use std::path::PathBuf;

let current_dir: PathBuf = std::env::current_dir().unwrap();
cmd_unit!("ls", current_dir);

Arguments of type &Path are passed to the child process as arguments.

use cradle::prelude::*;
use std::path::Path;

let file: &Path = Path::new("./foo");
cmd_unit!("touch", file);

Implementors