From 9f5a33f962b0af59f4832002d4dfae34862d5a99 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 18 Feb 2016 17:19:56 +0100 Subject: [PATCH 01/15] imag-view: init --- imag-view/Cargo.toml | 6 ++++++ imag-view/src/main.rs | 3 +++ 2 files changed, 9 insertions(+) create mode 100644 imag-view/Cargo.toml create mode 100644 imag-view/src/main.rs diff --git a/imag-view/Cargo.toml b/imag-view/Cargo.toml new file mode 100644 index 00000000..9ef38a71 --- /dev/null +++ b/imag-view/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "imag-view" +version = "0.1.0" +authors = ["Matthias Beyer "] + +[dependencies] diff --git a/imag-view/src/main.rs b/imag-view/src/main.rs new file mode 100644 index 00000000..e7a11a96 --- /dev/null +++ b/imag-view/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} From 17eb59ae29eb275c2e961cce7e982b72efa79b7f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 18 Feb 2016 17:25:20 +0100 Subject: [PATCH 02/15] Add depdendencies --- imag-view/Cargo.toml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/imag-view/Cargo.toml b/imag-view/Cargo.toml index 9ef38a71..5dd2504d 100644 --- a/imag-view/Cargo.toml +++ b/imag-view/Cargo.toml @@ -4,3 +4,20 @@ version = "0.1.0" authors = ["Matthias Beyer "] [dependencies] +clap = "2.1.1" +glob = "0.2.11" +log = "0.3.5" +rustbox = "0.8.1" +semver = "0.2.1" +toml = "0.1.25" +version = "2.0.1" + +[dependencies.libimagstore] +path = "../libimagstore" + +[dependencies.libimagrt] +path = "../libimagrt" + +[dependencies.libimagutil] +path = "../libimagutil" + From 2e6b6b50faed750cc51687d9a3f166ed67cad0e3 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 20 Feb 2016 15:46:06 +0100 Subject: [PATCH 03/15] Add ui::build_ui() --- imag-view/src/main.rs | 4 ++ imag-view/src/ui.rs | 110 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 imag-view/src/ui.rs diff --git a/imag-view/src/main.rs b/imag-view/src/main.rs index e7a11a96..2352bb00 100644 --- a/imag-view/src/main.rs +++ b/imag-view/src/main.rs @@ -1,3 +1,7 @@ +extern crate clap; + +mod ui; + fn main() { println!("Hello, world!"); } diff --git a/imag-view/src/ui.rs b/imag-view/src/ui.rs new file mode 100644 index 00000000..2c5818de --- /dev/null +++ b/imag-view/src/ui.rs @@ -0,0 +1,110 @@ +use clap::{Arg, App, SubCommand}; + +pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { + app + .arg(Arg::with_name("id") + .long("id") + .short("i") + .takes_value(true) + .required(true) + .help("View this entry at this store path")) + + .arg(Arg::with_name("version") + .long("version") + .short("V") + .takes_value(true) + .required(false) + .help("View this version (youngest if not specified)")) + + .arg(Arg::with_name("versions") + .long("versions") + .takes_value(false) + .required(false) + .help("Only print available versions for this file")) + + .arg(Arg::with_name("view-header") + .long("header") + .short("h") + .takes_value(false) + .required(false) + .help("View header")) + .arg(Arg::with_name("view-content") + .long("content") + .short("C") + .takes_value(false) + .required(false) + .help("View content")) + + .arg(Arg::with_name("view-copy") + .long("copy") + .takes_value(false) + .required(false) + .help("Copy before opening (copies to /tmp/) and removes the file after viewing.")) + + .arg(Arg::with_name("keep-copy") + .long("keep-copy") + .short("k") + .takes_value(false) + .required(false) + .help("If --copy was passed, keep the copy after viewing.")) + + .subcommand(SubCommand::with_name("view-in") + .about("View the entry in ...") + .version("0.1") + + .arg(Arg::with_name("view-in-stdout") + .long("stdout") + .short("s") + .takes_value(false) + .required(false) + .help("View by printing to stdout")) + + .arg(Arg::with_name("view-in-ui") + .long("ui") + .short("u") + .takes_value(false) + .required(false) + .help("View by opening own curses-like UI (default)")) + + .arg(Arg::with_name("view-in-browser") + .long("browser") + .short("b") + .takes_value(true) // optional, which browser + .required(false) + .help("View content in $BROWSER (fails if no env variable $BROWSER)")) + + .arg(Arg::with_name("view-in-texteditor") + .long("editor") + .short("e") + .takes_value(true) // optional, which editor + .required(false) + .help("View content in $EDITOR")) + + .arg(Arg::with_name("view-in-custom") + .long("custom") + .short("c") + .takes_value(true) // non-optional, call-string + .required(false) + .help("View content in custom program, for example 'libreoffice %e', replace '%e' with entry path")) + ) + + .subcommand(SubCommand::with_name("compile") + .about("Compile content to other format for viewing, implies that the entry gets copied to /tmp") + .version("0.1") + .arg(Arg::with_name("from") + .long("from") + .short("f") + .takes_value(true) // "markdown" or "textile" or "restructuredtex" + .required(true) + .help("Compile from")) + + .arg(Arg::with_name("to") + .long("to") + .short("t") + .takes_value(true) // "html" or "HTML" or ... json maybe? + .required(true) + .help("Compile to")) + ) +} + + From 43d27ab68943ee005bd54ddc4488447d85aef8ed Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 20 Feb 2016 20:51:32 +0100 Subject: [PATCH 04/15] Add main() --- imag-view/src/main.rs | 92 ++++++++++++++++++++++++++++++++++++- imag-view/src/viewer/mod.rs | 15 ++++++ 2 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 imag-view/src/viewer/mod.rs diff --git a/imag-view/src/main.rs b/imag-view/src/main.rs index 2352bb00..49856f6e 100644 --- a/imag-view/src/main.rs +++ b/imag-view/src/main.rs @@ -1,7 +1,97 @@ extern crate clap; +extern crate glob; +#[macro_use] extern crate log; +extern crate semver; +extern crate toml; +#[macro_use] extern crate version; + +extern crate libimagrt; +extern crate libimagstore; +extern crate libimagutil; + +use std::process::exit; + +use clap::ArgMatches; + +use libimagrt::runtime::Runtime; +use libimagstore::store::Entry; +use libimagstore::store::Result as StoreResult; +use libimagutil::trace::trace_error; mod ui; +mod viewer; + +use ui::build_ui; +use viewer::Viewer; +use viewer::ViewInformation; fn main() { - println!("Hello, world!"); + let name = "imag-view"; + let version = &version!()[..]; + let about = "View entries (readonly)"; + let ui = build_ui(Runtime::get_default_cli_builder(name, version, about)); + let rt = { + let rt = Runtime::new(ui); + if rt.is_ok() { + rt.unwrap() + } else { + println!("Could not set up Runtime"); + println!("{:?}", rt.err().unwrap()); + exit(1); + } + }; + + rt.init_logger(); + + debug!("Hello. Logging was just enabled"); + debug!("I already set up the Runtime object and build the commandline interface parser."); + debug!("Lets get rollin' ..."); + + info!("No implementation yet"); + + let entry_id = rt.cli().value_of("id").unwrap(); // enforced by clap + + if rt.cli().is_present("versions") { + view_versions_of(entry_id, &rt); + } else { + let entry_version = rt.cli().value_of("version"); + let view_header = rt.cli().is_present("view-header"); + let view_content = rt.cli().is_present("view-content"); + let view_copy = rt.cli().is_present("view-copy"); + let keep_copy = rt.cli().is_present("keep-copy"); + + let scmd = rt.cli().subcommand_matches("view-in"); + if scmd.is_none() { + debug!("No commandline call"); + exit(1); + } + let scmd = scmd.unwrap(); + + let viewer = build_viewer(scmd); + let entry = load_entry(entry_id, entry_version); + if entry.is_err() { + trace_error(&entry.err().unwrap()); + exit(1); + } + let entry = entry.unwrap(); + + let view_info = ViewInformation { + entry: entry, + view_header: view_header, + view_content: view_content, + view_copy: view_copy, + keep_copy: keep_copy, + }; + + view_info.view(view_info); + } } + +fn load_entry(id: &str, version: Option<&str>) -> StoreResult { + unimplemented!() +} + +fn view_versions_of(id: &str, rt: &Runtime) { + unimplemented!() +} + diff --git a/imag-view/src/viewer/mod.rs b/imag-view/src/viewer/mod.rs new file mode 100644 index 00000000..07aee28c --- /dev/null +++ b/imag-view/src/viewer/mod.rs @@ -0,0 +1,15 @@ +use libimagstore::store::Entry; + +#[derive(Debug)] +pub struct ViewInformation { + pub entry: Entry, + pub view_header: bool, + pub view_content: bool, + pub view_copy: bool, + pub keep_copy: bool, +} + +pub trait Viewer { + fn view(&self, vi: ViewInformation); +} + From d02ac8bd8846507cf2c38cbfdbd3f567b8abbaec Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 24 Feb 2016 00:51:35 +0100 Subject: [PATCH 05/15] Add stdout-viewer --- imag-view/src/main.rs | 1 + imag-view/src/viewer/mod.rs | 2 ++ imag-view/src/viewer/stdout.rs | 23 +++++++++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 imag-view/src/viewer/stdout.rs diff --git a/imag-view/src/main.rs b/imag-view/src/main.rs index 49856f6e..4692337e 100644 --- a/imag-view/src/main.rs +++ b/imag-view/src/main.rs @@ -24,6 +24,7 @@ mod viewer; use ui::build_ui; use viewer::Viewer; use viewer::ViewInformation; +use viewer::stdout::StdoutViewer; fn main() { let name = "imag-view"; diff --git a/imag-view/src/viewer/mod.rs b/imag-view/src/viewer/mod.rs index 07aee28c..a12d1b36 100644 --- a/imag-view/src/viewer/mod.rs +++ b/imag-view/src/viewer/mod.rs @@ -1,3 +1,5 @@ +pub mod stdout; + use libimagstore::store::Entry; #[derive(Debug)] diff --git a/imag-view/src/viewer/stdout.rs b/imag-view/src/viewer/stdout.rs new file mode 100644 index 00000000..c36c0373 --- /dev/null +++ b/imag-view/src/viewer/stdout.rs @@ -0,0 +1,23 @@ +use std::io::{Stdout, stdout}; + +use viewer::{ViewInformation, Viewer}; + +pub struct StdoutViewer { + out: Stdout, +} + +impl StdoutViewer { + + pub fn new() -> StdoutViewer { + StdoutViewer { out: stdout() } + } + +} + +impl Viewer for StdoutViewer { + + fn view(&self, vi: ViewInformation) { + unimplemented!() + } + +} From d99d38bf8e963942280bc376399bcc1eec4468e3 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 24 Feb 2016 13:45:37 +0100 Subject: [PATCH 06/15] Implement entry loading --- imag-view/src/main.rs | 37 ++++++++++++++++++++++++++++++++++--- imag-view/src/viewer/mod.rs | 7 +++---- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/imag-view/src/main.rs b/imag-view/src/main.rs index 4692337e..137d57b6 100644 --- a/imag-view/src/main.rs +++ b/imag-view/src/main.rs @@ -15,7 +15,9 @@ use clap::ArgMatches; use libimagrt::runtime::Runtime; use libimagstore::store::Entry; +use libimagstore::store::FileLockEntry; use libimagstore::store::Result as StoreResult; +use libimagstore::storeid::StoreId; use libimagutil::trace::trace_error; mod ui; @@ -69,7 +71,7 @@ fn main() { let scmd = scmd.unwrap(); let viewer = build_viewer(scmd); - let entry = load_entry(entry_id, entry_version); + let entry = load_entry(entry_id, entry_version, &rt); if entry.is_err() { trace_error(&entry.err().unwrap()); exit(1); @@ -88,8 +90,37 @@ fn main() { } } -fn load_entry(id: &str, version: Option<&str>) -> StoreResult { - unimplemented!() +// TODO: This is a shameless adaption of imag-store/src/util.rs +fn load_entry<'a>(id: &str, + version: Option<&str>, + rt: &'a Runtime) + -> StoreResult> +{ + use std::ops::Deref; + + debug!("Checking path element for version"); + + let version = { + version.unwrap_or_else(|| { + id.split("~").last().unwrap_or_else(|| { + warn!("No version"); + exit(1); + }) + }) + }; + + debug!("Building path from {:?} and {:?}", id, version); + let mut path = rt.store().path().clone(); + + if id.chars().next() == Some('/') { + path.push(format!("{}~{}", &id[1..id.len()], version)); + } else { + path.push(format!("{}~{}", id, version)); + } + + // the above is the adaption... + + rt.store().retrieve(path) } fn view_versions_of(id: &str, rt: &Runtime) { diff --git a/imag-view/src/viewer/mod.rs b/imag-view/src/viewer/mod.rs index a12d1b36..f7e3653f 100644 --- a/imag-view/src/viewer/mod.rs +++ b/imag-view/src/viewer/mod.rs @@ -1,10 +1,9 @@ pub mod stdout; -use libimagstore::store::Entry; +use libimagstore::store::FileLockEntry; -#[derive(Debug)] -pub struct ViewInformation { - pub entry: Entry, +pub struct ViewInformation<'a> { + pub entry: FileLockEntry<'a>, pub view_header: bool, pub view_content: bool, pub view_copy: bool, From e9587ced288083bb0ff5c0b8af97746d8eb30d26 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 24 Feb 2016 14:10:31 +0100 Subject: [PATCH 07/15] Implement view_versions_of() --- imag-view/src/main.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/imag-view/src/main.rs b/imag-view/src/main.rs index 137d57b6..7a2d4bd6 100644 --- a/imag-view/src/main.rs +++ b/imag-view/src/main.rs @@ -124,6 +124,33 @@ fn load_entry<'a>(id: &str, } fn view_versions_of(id: &str, rt: &Runtime) { - unimplemented!() + use glob::glob; + + let mut path = rt.store().path().clone(); + + if id.chars().next() == Some('/') { + path.push(format!("{}~*", &id[1..id.len()])); + } else { + path.push(format!("{}~*", id)); + } + + if let Some(path) = path.to_str() { + match glob(path) { + Ok(paths) => + for entry in paths { + match entry { + Ok(path) => println!("{}", path.file_name().and_then(|s| s.to_str()).unwrap()), + Err(e) => trace_error(e.error()), + } + }, + Err(e) => { + // trace_error(&e); // error seems not to be implemented + debug!("Error in pattern"); + exit(1); + }, + } + } else { + warn!("Could not build glob() argument!"); + } } From 68b2c152e3ed6ff0f84a6761f97004b10ceed8c0 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 6 Mar 2016 11:49:04 +0100 Subject: [PATCH 08/15] Use StdoutViewer by now --- imag-view/src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/imag-view/src/main.rs b/imag-view/src/main.rs index 7a2d4bd6..ac61fd31 100644 --- a/imag-view/src/main.rs +++ b/imag-view/src/main.rs @@ -86,7 +86,8 @@ fn main() { keep_copy: keep_copy, }; - view_info.view(view_info); + let viewer = StdoutViewer::new(); + viewer.view(view_info); } } From f582ea9367be55ed116698664d53e3429c63c77a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 6 Mar 2016 11:49:19 +0100 Subject: [PATCH 09/15] StdoutViewer: Implement view() --- imag-view/src/viewer/stdout.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/imag-view/src/viewer/stdout.rs b/imag-view/src/viewer/stdout.rs index c36c0373..a42af1c1 100644 --- a/imag-view/src/viewer/stdout.rs +++ b/imag-view/src/viewer/stdout.rs @@ -1,5 +1,7 @@ use std::io::{Stdout, stdout}; +use toml::encode_str; + use viewer::{ViewInformation, Viewer}; pub struct StdoutViewer { @@ -17,7 +19,21 @@ impl StdoutViewer { impl Viewer for StdoutViewer { fn view(&self, vi: ViewInformation) { - unimplemented!() + if vi.view_copy { + unimplemented!(); + } + + if vi.view_header { + println!("{}", encode_str(vi.entry.get_header().header())); + } + + if vi.view_content { + println!("{}", vi.entry.get_content()); + } + + if vi.view_copy && !vi.keep_copy { + unimplemented!() + } } } From c415d3c9683aeb9cc0647672954373d3b0c1bac5 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 6 Mar 2016 12:19:25 +0100 Subject: [PATCH 10/15] Print error in case of GlobError --- imag-view/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/imag-view/src/main.rs b/imag-view/src/main.rs index ac61fd31..45da9918 100644 --- a/imag-view/src/main.rs +++ b/imag-view/src/main.rs @@ -145,7 +145,7 @@ fn view_versions_of(id: &str, rt: &Runtime) { } }, Err(e) => { - // trace_error(&e); // error seems not to be implemented + warn!("{}", e); // trace_error(&e); // error seems not to be implemented debug!("Error in pattern"); exit(1); }, From d464ccc9d406fc3422a4e5c7237091098e679b52 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 6 Mar 2016 12:25:15 +0100 Subject: [PATCH 11/15] Remove unused imports --- imag-view/src/main.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/imag-view/src/main.rs b/imag-view/src/main.rs index 45da9918..05ef31d6 100644 --- a/imag-view/src/main.rs +++ b/imag-view/src/main.rs @@ -11,13 +11,9 @@ extern crate libimagutil; use std::process::exit; -use clap::ArgMatches; - use libimagrt::runtime::Runtime; -use libimagstore::store::Entry; use libimagstore::store::FileLockEntry; use libimagstore::store::Result as StoreResult; -use libimagstore::storeid::StoreId; use libimagutil::trace::trace_error; mod ui; @@ -97,8 +93,6 @@ fn load_entry<'a>(id: &str, rt: &'a Runtime) -> StoreResult> { - use std::ops::Deref; - debug!("Checking path element for version"); let version = { From a61b83df07b48ec5ae0b5b57a41a906c576971a5 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 6 Mar 2016 12:32:21 +0100 Subject: [PATCH 12/15] Add viewer selection --- imag-view/src/main.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/imag-view/src/main.rs b/imag-view/src/main.rs index 05ef31d6..10efd57d 100644 --- a/imag-view/src/main.rs +++ b/imag-view/src/main.rs @@ -66,7 +66,26 @@ fn main() { } let scmd = scmd.unwrap(); - let viewer = build_viewer(scmd); + let viewer = { + if scmd.is_present("view-in-stdout") { + Box::new(StdoutViewer::new()) + } else if scmd.is_present("view-in-ui") { + warn!("Viewing in UI is currently not supported, switch to stdout"); + Box::new(StdoutViewer::new()) + } else if scmd.is_present("view-in-browser") { + warn!("Viewing in browser is currently not supported, switch to stdout"); + Box::new(StdoutViewer::new()) + } else if scmd.is_present("view-in-texteditor") { + warn!("Viewing in texteditor is currently not supported, switch to stdout"); + Box::new(StdoutViewer::new()) + } else if scmd.is_present("view-in-custom") { + warn!("Viewing in custom is currently not supported, switch to stdout"); + Box::new(StdoutViewer::new()) + } else { + Box::new(StdoutViewer::new()) + } + }; + let entry = load_entry(entry_id, entry_version, &rt); if entry.is_err() { trace_error(&entry.err().unwrap()); @@ -82,7 +101,6 @@ fn main() { keep_copy: keep_copy, }; - let viewer = StdoutViewer::new(); viewer.view(view_info); } } From c1fcf226196e700df9f6ad43dc1325f5e67f0591 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 6 Mar 2016 12:42:01 +0100 Subject: [PATCH 13/15] Group Args, so we enforce that only _one_ of them can be specified --- imag-view/src/ui.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/imag-view/src/ui.rs b/imag-view/src/ui.rs index 2c5818de..619e4f27 100644 --- a/imag-view/src/ui.rs +++ b/imag-view/src/ui.rs @@ -1,4 +1,4 @@ -use clap::{Arg, App, SubCommand}; +use clap::{Arg, App, ArgGroup, SubCommand}; pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { app @@ -86,6 +86,15 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .takes_value(true) // non-optional, call-string .required(false) .help("View content in custom program, for example 'libreoffice %e', replace '%e' with entry path")) + + .group(ArgGroup::with_name("viewer") + .args(&["view-in-stdout", + "view-in-ui", + "view-in-browser", + "view-in-texteditor", + "view-in-custom", + ]) + .required(false)) ) .subcommand(SubCommand::with_name("compile") From 34c020fa550c367cd2747393088d6c7a5e386953 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 11 Mar 2016 16:16:04 +0100 Subject: [PATCH 14/15] Introduce error module --- imag-view/src/error.rs | 89 ++++++++++++++++++++++++++++++++++++++++++ imag-view/src/main.rs | 5 +++ 2 files changed, 94 insertions(+) create mode 100644 imag-view/src/error.rs diff --git a/imag-view/src/error.rs b/imag-view/src/error.rs new file mode 100644 index 00000000..57379f43 --- /dev/null +++ b/imag-view/src/error.rs @@ -0,0 +1,89 @@ +use std::error::Error; +use std::fmt::Error as FmtError; +use std::clone::Clone; +use std::fmt::{Debug, Display, Formatter}; +use std::fmt; +use std::convert::From; + +/** + * Kind of store error + */ +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum ViewErrorKind { + StoreError, + NoVersion, + PatternError, + GlobBuildError, +} + +fn view_error_type_as_str(e: &ViewErrorKind) -> &'static str { + match e { + &ViewErrorKind::StoreError => "Store error", + &ViewErrorKind::NoVersion => "No version specified", + &ViewErrorKind::PatternError => "Error in Pattern", + &ViewErrorKind::GlobBuildError => "Could not build glob() Argument", + } +} + +impl Display for ViewErrorKind { + + fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { + try!(write!(fmt, "{}", view_error_type_as_str(self))); + Ok(()) + } + +} + +/** + * View error type + */ +#[derive(Debug)] +pub struct ViewError { + err_type: ViewErrorKind, + cause: Option>, +} + +impl ViewError { + + /** + * Build a new ViewError from an ViewErrorKind, optionally with cause + */ + pub fn new(errtype: ViewErrorKind, cause: Option>) + -> ViewError + { + ViewError { + err_type: errtype, + cause: cause, + } + } + + /** + * Get the error type of this ViewError + */ + pub fn err_type(&self) -> ViewErrorKind { + self.err_type.clone() + } + +} + +impl Display for ViewError { + + fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { + try!(write!(fmt, "[{}]", view_error_type_as_str(&self.err_type.clone()))); + Ok(()) + } + +} + +impl Error for ViewError { + + fn description(&self) -> &str { + view_error_type_as_str(&self.err_type.clone()) + } + + fn cause(&self) -> Option<&Error> { + self.cause.as_ref().map(|e| &**e) + } + +} + diff --git a/imag-view/src/main.rs b/imag-view/src/main.rs index 10efd57d..8f537b14 100644 --- a/imag-view/src/main.rs +++ b/imag-view/src/main.rs @@ -9,6 +9,7 @@ extern crate libimagrt; extern crate libimagstore; extern crate libimagutil; +use std::result::Result as RResult; use std::process::exit; use libimagrt::runtime::Runtime; @@ -16,14 +17,18 @@ use libimagstore::store::FileLockEntry; use libimagstore::store::Result as StoreResult; use libimagutil::trace::trace_error; +mod error; mod ui; mod viewer; +use error::{ViewError, ViewErrorKind}; use ui::build_ui; use viewer::Viewer; use viewer::ViewInformation; use viewer::stdout::StdoutViewer; +type Result = RResult; + fn main() { let name = "imag-view"; let version = &version!()[..]; From ca362ac23818b2eb0276612da4d7e27a5a61e91f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 11 Mar 2016 16:16:35 +0100 Subject: [PATCH 15/15] Remove exit(1) calls where possible and replace by trace() calls --- imag-view/src/main.rs | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/imag-view/src/main.rs b/imag-view/src/main.rs index 8f537b14..1b4d5222 100644 --- a/imag-view/src/main.rs +++ b/imag-view/src/main.rs @@ -41,7 +41,7 @@ fn main() { } else { println!("Could not set up Runtime"); println!("{:?}", rt.err().unwrap()); - exit(1); + exit(1); // we can afford not-executing destructors here } }; @@ -56,7 +56,10 @@ fn main() { let entry_id = rt.cli().value_of("id").unwrap(); // enforced by clap if rt.cli().is_present("versions") { - view_versions_of(entry_id, &rt); + if let Err(e) = view_versions_of(entry_id, &rt) { + trace_error(&e); + exit(1); // we can afford not-executing destructors here + } } else { let entry_version = rt.cli().value_of("version"); let view_header = rt.cli().is_present("view-header"); @@ -67,7 +70,7 @@ fn main() { let scmd = rt.cli().subcommand_matches("view-in"); if scmd.is_none() { debug!("No commandline call"); - exit(1); + exit(1); // we can afford not-executing destructors here } let scmd = scmd.unwrap(); @@ -94,7 +97,7 @@ fn main() { let entry = load_entry(entry_id, entry_version, &rt); if entry.is_err() { trace_error(&entry.err().unwrap()); - exit(1); + exit(1); // we can afford not-executing destructors here } let entry = entry.unwrap(); @@ -114,17 +117,22 @@ fn main() { fn load_entry<'a>(id: &str, version: Option<&str>, rt: &'a Runtime) - -> StoreResult> + -> Result> { debug!("Checking path element for version"); let version = { - version.unwrap_or_else(|| { - id.split("~").last().unwrap_or_else(|| { + if version.is_none() { + let r = id.split("~").last(); + if r.is_none() { warn!("No version"); - exit(1); - }) - }) + return Err(ViewError::new(ViewErrorKind::NoVersion, None)); + } else { + r.unwrap() + } + } else { + version.unwrap() + } }; debug!("Building path from {:?} and {:?}", id, version); @@ -139,9 +147,10 @@ fn load_entry<'a>(id: &str, // the above is the adaption... rt.store().retrieve(path) + .map_err(|e| ViewError::new(ViewErrorKind::StoreError, Some(Box::new(e)))) } -fn view_versions_of(id: &str, rt: &Runtime) { +fn view_versions_of(id: &str, rt: &Runtime) -> Result<()> { use glob::glob; let mut path = rt.store().path().clone(); @@ -154,21 +163,23 @@ fn view_versions_of(id: &str, rt: &Runtime) { if let Some(path) = path.to_str() { match glob(path) { - Ok(paths) => + Ok(paths) => { for entry in paths { match entry { Ok(path) => println!("{}", path.file_name().and_then(|s| s.to_str()).unwrap()), Err(e) => trace_error(e.error()), } - }, + } + Ok(()) + }, Err(e) => { - warn!("{}", e); // trace_error(&e); // error seems not to be implemented debug!("Error in pattern"); - exit(1); + Err(ViewError::new(ViewErrorKind::PatternError, Some(Box::new(e)))) }, } } else { warn!("Could not build glob() argument!"); + Err(ViewError::new(ViewErrorKind::GlobBuildError, None)) } }