forked from oscar-project/oscar-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.rs
60 lines (51 loc) · 1.84 KB
/
cli.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//! Commands traits and base CLI parsing
use crate::error::Error;
use crate::impls::OscarDoc;
use clap::ArgMatches;
pub trait Command {
fn hook_to_clap(ctx: clap::App<'static>) -> clap::App<'static>
where
Self: Sized,
{
ctx.subcommand(Self::subcommand())
}
fn subcommand() -> clap::App<'static>
where
Self: Sized;
fn run(matches: &ArgMatches) -> Result<(), Error>
where
Self: Sized;
}
#[cfg(not(tarpaulin_include))]
pub(crate) fn build_app() -> clap::App<'static> {
use clap::AppSettings;
use crate::impls::OscarTxt;
clap::App::new("oscar-tools")
.global_setting(AppSettings::ArgRequiredElseHelp)
.subcommand(OscarDoc::subcommand())
.subcommand(OscarTxt::subcommand())
}
#[cfg(not(tarpaulin_include))]
pub(crate) fn run(matches: ArgMatches) -> Result<(), Error> {
use crate::impls::OscarTxt;
let (version, subcommand) = matches
.subcommand()
.ok_or_else(|| Error::Custom("No version provided!".to_string()))?;
match version {
//TODO: this should be automatically done by calling a version resolver
// Some struct/enum that holds OSCAR versions, and implements a from string that
// buils something that implements run and runs the correct OSCAR version
"v2" => OscarDoc::run(subcommand),
"v1" => OscarTxt::run(subcommand),
x => Err(Error::Custom(format!("Unknown version {x}"))),
}
}
/// Runnable traits have to be implemented by commands
/// in order to be executed from CLI.
// TODO: Currently, run returns (), so if the command
// actually returns something usable, it cannot pass it on.
// shall we provide flexibility to the Runnable trait by using generics
// or provide another trait like Queryable to fetch results?
pub trait Runnable {
fn run(&self) -> Result<(), Error>;
}