Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add a dummy utility to demonstrate the framework #47

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions libmesabox/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
95 changes: 95 additions & 0 deletions libmesabox/src/other/dummy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//
// 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 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";

type DummyResult<T> = ::std::result::Result<T, DummyError>;

#[derive(Fail, Debug)]
enum DummyError {
#[fail(display = "oh no, something wrong")]
SomethingWrong
}

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<O>
where
O: Write
{
output: O
}

impl<O> Dummyer<O>
where
O: Write
{
fn new(output: O) -> Self {
Dummyer { 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("verbose")
.short("v")
.long("verbose")
.help("Say hello in verbose mode"))
}

pub fn execute<S, T>(setup: &mut S, args: T) -> Result<()>
where
S: UtilSetup,
T: ArgsIter,
{
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(&options)?;
Ok(())
}
3 changes: 3 additions & 0 deletions libmesabox/src/util_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ generate_fns! {
},
sysinit {
(init, "init")
},
other {
(dummy, "dummy")
}
}