#[macro_export]
macro_rules! make_mock_app {
{
app $appname:expr;
module $module:ident;
version $version:expr;
} => {
make_mock_app! {
app $appname;
module $module;
version $version;
with help "This is a mocking app";
}
};
{
app $appname:expr;
modulename $module:ident;
version $version:expr;
with help $help:expr;
with ui builder function $buildui:path;
}=> {
mod $module {
use clap::{App, ArgMatches};
use libimagrt::spec::CliSpec;
use libimagrt::runtime::Runtime;
use libimagrt::configuration::InternalConfiguration;
use toml::Value;
use failure::Error;
#[derive(Clone)]
struct MockLinkApp<'a> {
args: Vec<&'static str>,
inner: App<'a, 'a>,
}
impl<'a> MockLinkApp<'a> {
fn new(args: Vec<&'static str>) -> Self {
MockLinkApp {
args: args,
inner: ($buildui)(Runtime::get_default_cli_builder($appname, $version, $help)),
}
}
}
impl<'a> CliSpec<'a> for MockLinkApp<'a> {
fn name(&self) -> &str {
self.inner.get_name()
}
fn matches(self) -> ArgMatches<'a> {
self.inner.get_matches_from(self.args)
}
}
impl<'a> InternalConfiguration for MockLinkApp<'a> {
fn enable_logging(&self) -> bool {
false
}
fn use_inmemory_fs(&self) -> bool {
true
}
}
#[allow(unused)]
pub fn generate_minimal_test_config() -> Option<Value> {
::toml::de::from_str("[store]\nimplicit-create=true").ok()
}
#[allow(unused)]
pub fn generate_test_runtime<'a>(mut args: Vec<&'static str>) -> Result<Runtime<'a>, Error> {
let mut cli_args = vec![$appname];
cli_args.append(&mut args);
debug!("Generating test runtime with args = {:?}", cli_args);
let cli_app = MockLinkApp::new(cli_args);
Runtime::with_configuration(cli_app, generate_minimal_test_config())
}
#[allow(unused)]
pub fn reset_test_runtime<'a>(mut args: Vec<&'static str>, old_runtime: Runtime)
-> Result<Runtime<'a>, Error>
{
let mut cli_args = vec![$appname];
cli_args.append(&mut args);
debug!("New arguments: {:?}", cli_args);
let cli_app = MockLinkApp::new(cli_args);
Runtime::with_configuration(cli_app, generate_minimal_test_config())
.map(|rt| rt.with_store(old_runtime.extract_store()))
}
}
};
}