Skip to content

Instantly share code, notes, and snippets.

@RadicalZephyr
Last active February 3, 2019 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RadicalZephyr/cf37eb5dd45f401768ba0ed43feb3331 to your computer and use it in GitHub Desktop.
Save RadicalZephyr/cf37eb5dd45f401768ba0ed43feb3331 to your computer and use it in GitHub Desktop.
Example syntax for mason-rs
use mason::generate_builder;
#[generate_builder(CommandBuilder)]
pub struct Command {
#[builder(entrypoint, into, required)]
program: String,
args: Vec<String>,
cwd: Option<String>,
}
fn main() {
let cmd = Command::program("echo")
.args(vec!["Hello", "World!"])
.build();
}
pub struct Command {
program: String,
args: Vec<String>,
cwd: Option<String>,
}
impl Command {
pub fn program(program: impl Into<String>) -> CommandBuilderWithProgram {
CommandBuilderWithProgram {
program: program.into(),
optionals: CommandBuilderOptional::default(),
}
}
}
#[derive(Default)]
pub struct CommandBuilderOptional {
args: Option<Vec<String>>,
cwd: Option<String>,
}
pub struct CommandBuilderWithProgram {
program: String,
optionals: CommandBuilderOptional,
}
impl CommandBuilderWithProgram {
pub fn args(mut self, args: Vec<String>) -> CommandBuilderWithProgram {
self.optionals.args = Some(args);
self
}
pub fn cwd(mut self, cwd: String) -> CommandBuilderWithProgram {
self.optionals.cwd = Some(cwd);
self
}
pub fn build(self) -> Command {
let CommandBuilderWithProgram { program, optionals } = self;
let CommandBuilderOptional { args, cwd } = optionals;
Command {
program: program,
args: args.unwrap_or_else(Default::default),
cwd: cwd,
}
}
}
fn main() {
let cmd = Command::program("echo")
.args(
vec!["Hello", "World!"]
.into_iter()
.map(String::from)
.collect(),
)
.build();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment