Last active
February 3, 2019 10:44
-
-
Save RadicalZephyr/cf37eb5dd45f401768ba0ed43feb3331 to your computer and use it in GitHub Desktop.
Example syntax for mason-rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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