From 7f7ddb8d4d890dd22f5f3ed879848f411c4312c5 Mon Sep 17 00:00:00 2001 From: Mingshen Sun Date: Sat, 1 Sep 2018 00:01:19 -0700 Subject: [PATCH 1/4] Add a dummy utility to demonstrate the framework --- Cargo.toml | 6 ++++ libmesabox/Cargo.toml | 6 ++++ libmesabox/src/other/dummy.rs | 53 +++++++++++++++++++++++++++++++++++ libmesabox/src/util_list.rs | 3 ++ 4 files changed, 68 insertions(+) create mode 100644 libmesabox/src/other/dummy.rs diff --git a/Cargo.toml b/Cargo.toml index d414ed5..7e8750a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,6 +60,12 @@ sysinit = [ "init" ] +# a dummy utility to help people to understand the framework +dummy = ["libmesabox/dummy"] +other = [ + "dummy" +] + # utilities that work on Unix unix = [ "gnu", diff --git a/libmesabox/Cargo.toml b/libmesabox/Cargo.toml index e5f6da4..c9f21b4 100644 --- a/libmesabox/Cargo.toml +++ b/libmesabox/Cargo.toml @@ -58,6 +58,12 @@ sysinit = [ "init" ] +# a dummy utility to help people to understand the framework +dummy = [] +other = [ + "dummy" +] + # utilities that work on Unix unix = [ "gnu", diff --git a/libmesabox/src/other/dummy.rs b/libmesabox/src/other/dummy.rs new file mode 100644 index 0000000..3450950 --- /dev/null +++ b/libmesabox/src/other/dummy.rs @@ -0,0 +1,53 @@ +// +// Copyright (c) 2018, The MesaLock Linux Project Contributors +// All rights reserved. +// +// This work is licensed under the terms of the BSD 3-Clause License. +// For a copy, see the LICENSE file. +// + +use {UtilSetup, ArgsIter, Result, UtilRead, UtilWrite}; +use std::io::{self, Write}; + +pub const DESCRIPTION: &str = "A dummy utility to demonstrate the framework"; + +#[derive(Fail, Debug)] +enum DummyError { + #[fail(display = "something wrong")] + SomethingWrong +} + +type DummyResult = ::std::result::Result; + +struct Dummyer +where + O: Write +{ + output: O +} + +impl Dummyer +where + O: Write +{ + fn new(output: O) -> Self { + Dummyer { output } + } + + fn dummy(&mut self) -> DummyResult<()> { + writeln!(self.output, "write something to the output"); + Ok(()) + } +} + +pub fn execute(setup: &mut S, _args: T) -> Result<()> +where + S: UtilSetup, + T: ArgsIter, +{ + let output = setup.output(); + let mut output = output.lock()?; + let mut dummyer = Dummyer::new(output); + dummyer.dummy()?; + Ok(()) +} diff --git a/libmesabox/src/util_list.rs b/libmesabox/src/util_list.rs index fab9b1f..c8679bb 100644 --- a/libmesabox/src/util_list.rs +++ b/libmesabox/src/util_list.rs @@ -27,5 +27,8 @@ generate_fns! { }, sysinit { (init, "init") + }, + other { + (dummy, "dummy") } } From 076dbb2630477bb07d82b3bfd78126c25ad86340 Mon Sep 17 00:00:00 2001 From: Mingshen Sun Date: Sat, 1 Sep 2018 00:18:32 -0700 Subject: [PATCH 2/4] other/dummy: use clap to manage args --- libmesabox/src/other/dummy.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/libmesabox/src/other/dummy.rs b/libmesabox/src/other/dummy.rs index 3450950..f20402f 100644 --- a/libmesabox/src/other/dummy.rs +++ b/libmesabox/src/other/dummy.rs @@ -6,8 +6,9 @@ // For a copy, see the LICENSE file. // -use {UtilSetup, ArgsIter, Result, UtilRead, UtilWrite}; +use clap::{App, Arg, ArgMatches}; use std::io::{self, Write}; +use {UtilSetup, ArgsIter, Result, UtilRead, UtilWrite}; pub const DESCRIPTION: &str = "A dummy utility to demonstrate the framework"; @@ -40,13 +41,25 @@ where } } -pub fn execute(setup: &mut S, _args: T) -> Result<()> +fn create_app() -> App<'static, 'static> { + util_app!("dummy") + .arg(Arg::with_name("about") + .short("a") + .long("about") + .help("show about")) +} + +pub fn execute(setup: &mut S, args: T) -> Result<()> where S: UtilSetup, T: ArgsIter, { + let app = create_app(); + let matches = app.get_matches_from_safe(args)?; + let output = setup.output(); let mut output = output.lock()?; + let mut dummyer = Dummyer::new(output); dummyer.dummy()?; Ok(()) From 723dbb7ef64d10da7f6c1579a404c36cc8c5ae36 Mon Sep 17 00:00:00 2001 From: Mingshen Sun Date: Sat, 1 Sep 2018 00:23:09 -0700 Subject: [PATCH 3/4] other/dummy: write util name in the const NAME variable --- libmesabox/src/other/dummy.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libmesabox/src/other/dummy.rs b/libmesabox/src/other/dummy.rs index f20402f..3a1970a 100644 --- a/libmesabox/src/other/dummy.rs +++ b/libmesabox/src/other/dummy.rs @@ -10,6 +10,7 @@ use clap::{App, Arg, ArgMatches}; use std::io::{self, Write}; use {UtilSetup, ArgsIter, Result, UtilRead, UtilWrite}; +const NAME: &str = "dummy"; pub const DESCRIPTION: &str = "A dummy utility to demonstrate the framework"; #[derive(Fail, Debug)] @@ -42,7 +43,7 @@ where } fn create_app() -> App<'static, 'static> { - util_app!("dummy") + util_app!(NAME) .arg(Arg::with_name("about") .short("a") .long("about") From 201764c7d9def7647834371e00bd85f97c071e44 Mon Sep 17 00:00:00 2001 From: Mingshen Sun Date: Sat, 1 Sep 2018 15:16:43 -0700 Subject: [PATCH 4/4] other/dummy: add sample to handle options --- libmesabox/src/other/dummy.rs | 46 ++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/libmesabox/src/other/dummy.rs b/libmesabox/src/other/dummy.rs index 3a1970a..c69c6d2 100644 --- a/libmesabox/src/other/dummy.rs +++ b/libmesabox/src/other/dummy.rs @@ -13,13 +13,35 @@ use {UtilSetup, ArgsIter, Result, UtilRead, UtilWrite}; const NAME: &str = "dummy"; pub const DESCRIPTION: &str = "A dummy utility to demonstrate the framework"; +type DummyResult = ::std::result::Result; + #[derive(Fail, Debug)] enum DummyError { - #[fail(display = "something wrong")] + #[fail(display = "oh no, something wrong")] SomethingWrong } -type DummyResult = ::std::result::Result; +struct DummyOptions { + verbose: bool +} + +impl DummyOptions { + fn from_matches(matches: &ArgMatches) -> Self { + let mut options = Self::default(); + + options.verbose = matches.is_present("verbose"); + + options + } +} + +impl Default for DummyOptions { + fn default() -> Self { + Self { + verbose: false + } + } +} struct Dummyer where @@ -36,18 +58,23 @@ where Dummyer { output } } - fn dummy(&mut self) -> DummyResult<()> { - writeln!(self.output, "write something to the output"); + fn dummy(&mut self, options: &DummyOptions) -> DummyResult<()> { + if options.verbose { + writeln!(self.output, "Hello, world! This is a dummy utility. I am very verbose :)"); + return Err(DummyError::SomethingWrong) + } else { + writeln!(self.output, "Hello, world!"); + } Ok(()) } } fn create_app() -> App<'static, 'static> { util_app!(NAME) - .arg(Arg::with_name("about") - .short("a") - .long("about") - .help("show about")) + .arg(Arg::with_name("verbose") + .short("v") + .long("verbose") + .help("Say hello in verbose mode")) } pub fn execute(setup: &mut S, args: T) -> Result<()> @@ -57,11 +84,12 @@ where { let app = create_app(); let matches = app.get_matches_from_safe(args)?; + let options = DummyOptions::from_matches(&matches); let output = setup.output(); let mut output = output.lock()?; let mut dummyer = Dummyer::new(output); - dummyer.dummy()?; + dummyer.dummy(&options)?; Ok(()) }