Add CliSpec trait for Runtime initialisation

This commit is contained in:
Robert Ignat 2017-05-30 23:09:03 +02:00 committed by Matthias Beyer
parent 297eeb1bd2
commit 0def6a3d5a
3 changed files with 33 additions and 4 deletions

View File

@ -53,4 +53,5 @@ pub mod configuration;
pub mod logger;
pub mod runtime;
pub mod setup;
pub mod spec;

View File

@ -36,6 +36,7 @@ use error::MapErrInto;
use logger::ImagLogger;
use libimagstore::store::Store;
use spec::CliSpec;
/// The Runtime object
///
@ -55,7 +56,7 @@ impl<'a> Runtime<'a> {
/// and builds the Runtime object with it.
///
/// The cli_spec object should be initially build with the ::get_default_cli_builder() function.
pub fn new(mut cli_spec: App<'a, 'a>) -> Result<Runtime<'a>, RuntimeError> {
pub fn new<C: Clone + CliSpec<'a>>(mut cli_spec: C) -> Result<Runtime<'a>, RuntimeError> {
use std::env;
use std::io::stdout;
@ -66,7 +67,7 @@ impl<'a> Runtime<'a> {
use configuration::error::ConfigErrorKind;
let matches = cli_spec.clone().get_matches();
let matches = cli_spec.clone().matches();
let is_debugging = matches.is_present("debugging");
let is_verbose = matches.is_present("verbosity");
@ -78,8 +79,8 @@ impl<'a> Runtime<'a> {
Some(shell) => {
debug!("Generating shell completion script, writing to stdout");
let shell = shell.parse::<Shell>().unwrap(); // clap has our back here.
let appname = String::from(cli_spec.get_name());
cli_spec.gen_completions_to(appname, shell, &mut stdout());
let appname = String::from(cli_spec.name());
cli_spec.completions(appname, shell, &mut stdout());
},
_ => debug!("Not generating shell completion script"),
}

27
libimagrt/src/spec.rs Normal file
View File

@ -0,0 +1,27 @@
use std::io::Write;
use clap::{App, ArgMatches, Shell};
pub trait CliSpec<'a> {
fn name(&self) -> &str;
fn matches(self) -> ArgMatches<'a>;
fn completions<W: Write, S: Into<String>>(&mut self, _: S, _: Shell, _: &mut W) {}
}
impl<'a> CliSpec<'a> for App<'a, 'a> {
fn name(&self) -> &str {
self.get_name()
}
fn matches(self) -> ArgMatches<'a> {
self.get_matches()
}
fn completions<W: Write, S: Into<String>>(&mut self,
bin_name: S,
for_shell: Shell,
buf: &mut W) {
self.gen_completions_to(bin_name, for_shell, buf);
}
}