From ca8e8c06220f858424c8c1b799b1f00bd89e9bb2 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 13 Apr 2015 17:08:50 +0200 Subject: [PATCH] feat(engine): infrastructure * allow usage of cmn.rs for common types (like Error types) * instantiate an engine and handle errors, in an initial quick and dirty way. Fixes #52 --- src/mako/cli/lib/docopt.mako | 2 +- src/mako/cli/lib/engine.mako | 24 ++++++++++++++++++++++++ src/mako/cli/main.rs.mako | 23 +++++++++++++++++++---- src/rust/cli/cmn.rs | 12 ++++++++++++ 4 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 src/mako/cli/lib/engine.mako diff --git a/src/mako/cli/lib/docopt.mako b/src/mako/cli/lib/docopt.mako index 709fdf0335..5bf9a7804f 100644 --- a/src/mako/cli/lib/docopt.mako +++ b/src/mako/cli/lib/docopt.mako @@ -13,7 +13,7 @@ upload_protocols_used = set() output_used = False %>\ -docopt!(Args derive Debug, " +docopt!(Options derive Debug, " Usage: % for resource in sorted(c.rta_map.keys()): % for method in sorted(c.rta_map[resource]): diff --git a/src/mako/cli/lib/engine.mako b/src/mako/cli/lib/engine.mako new file mode 100644 index 0000000000..937a14621a --- /dev/null +++ b/src/mako/cli/lib/engine.mako @@ -0,0 +1,24 @@ +<%namespace name="util" file="../../lib/util.mako"/>\ +<%! + from cli import (mangle_subcommand, new_method_context, PARAM_FLAG, STRUCT_FLAG, UPLOAD_FLAG, OUTPUT_FLAG, VALUE_ARG, + CONFIG_DIR, SCOPE_FLAG, is_request_value_property, FIELD_SEP, docopt_mode, FILE_ARG, MIME_ARG, OUT_ARG) + + v_arg = '<%s>' % VALUE_ARG +%>\ +<%def name="new(c)">\ +mod cmn; +use cmn::{InvalidOptionsError, ArgumentError}; + +struct Engine { + opts: Options, +} + + +impl Engine { + fn new(options: Options) -> Result { + Ok(Engine { + opts: options, + }) + } +} + \ No newline at end of file diff --git a/src/mako/cli/main.rs.mako b/src/mako/cli/main.rs.mako index 1551f21827..06396c8e13 100644 --- a/src/mako/cli/main.rs.mako +++ b/src/mako/cli/main.rs.mako @@ -1,4 +1,5 @@ <%namespace name="docopt" file="lib/docopt.mako"/>\ +<%namespace name="engine" file="lib/engine.mako"/>\ <%namespace name="util" file="../lib/util.mako"/>\ <% from util import (new_context, rust_comment) @@ -9,16 +10,30 @@ <%block filter="rust_comment">\ <%util:gen_info source="${self.uri}" />\ -#![feature(plugin)] +#![feature(plugin, exit_status)] #![plugin(docopt_macros)] extern crate docopt; extern crate rustc_serialize; +use std::io; +use std::env; +use std::io::Write; + ${docopt.new(c)}\ +${engine.new(c)}\ + fn main() { - let args: Args = Args::docopt().decode().unwrap_or_else(|e| e.exit()); - println!("{:?}", args); - println!("Hello, ${id} !"); + let opts: Options = Options::docopt().decode().unwrap_or_else(|e| e.exit()); + println!("{:?}", opts); + match Engine::new(opts) { + Err(e) => { + write!(io::stderr(), "{:?}", e).ok(); + env::set_exit_status(e.exit_code); + }, + Ok(mut engine) => { + + } + } } \ No newline at end of file diff --git a/src/rust/cli/cmn.rs b/src/rust/cli/cmn.rs index e69de29bb2..4f3e9ffe87 100644 --- a/src/rust/cli/cmn.rs +++ b/src/rust/cli/cmn.rs @@ -0,0 +1,12 @@ + + +#[derive(Debug)] +pub enum ArgumentError { + ConfigurationDirectoryInaccessible(String), +} + +#[derive(Debug)] +pub struct InvalidOptionsError { + pub issues: Vec, + pub exit_code: i32, +} \ No newline at end of file