cradle icon indicating copy to clipboard operation
cradle copied to clipboard

Allow to easily box an Input value

Open soenkehahn opened this issue 2 years ago • 3 comments

Sometimes it's convenient to conditionally provide an input to a command or not. Currently that is difficult due to the Input values being typed. So for example this doesn't work:

cmd_unit!("ls", if true { "some-file" } else { () });

Currently this can be solved with something like:

cmd_unit!("ls", if true { vec!["some-file"] } else { vec![] });

But you could imagine situations where it's not that easy to find a workaround like this. So maybe it'd be good to have a BoxedInput type:

cmd_unit!("ls", if true { BoxedInput("some-file") } else { BoxedInput(()) });

soenkehahn avatar Jul 28 '21 02:07 soenkehahn

Another way to do this would be to have something like OptionInput, analogous to OptionFuture.

Something like:

struct OptionInput<I: Input>(Option<I>);

// With a boolean condition:
cmd_unit!("ls", OptionInput(condition.then("some-file")));

// With an option:
cmd_unit!("ls", OptionInput(Some("some-file")));

casey avatar Sep 12 '21 14:09 casey

If we'd have a BoxedInput, then users could also do something like:

let mut args: Vec<BoxedInput> = vec!["foo".boxed(), "bar".boxed(), LogCommand.boxed()];
if condition {
   args.push(Stdin("input").boxed());
}
args.run();

With OptionInput that wouldn't be possible, since the inner type would still leak out into the wrapper type. OptionInput would of course also be useful, but BoxedInput seems more general.

soenkehahn avatar Sep 12 '21 19:09 soenkehahn

I think they're probably both useful, I wouldn't mind having both.

casey avatar Sep 12 '21 20:09 casey