Move imag-gps to ID provider infrastructure

This commit is contained in:
Matthias Beyer 2018-10-01 12:47:26 +02:00
parent 70014e04b1
commit 4856541f5a
2 changed files with 127 additions and 65 deletions

View file

@ -45,7 +45,6 @@ extern crate libimagstore;
use std::io::Write; use std::io::Write;
use std::process::exit; use std::process::exit;
use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
use failure::Error; use failure::Error;
@ -58,7 +57,6 @@ use libimagrt::runtime::Runtime;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
use libimagerror::exit::ExitUnwrap; use libimagerror::exit::ExitUnwrap;
use libimagerror::io::ToExitCode; use libimagerror::io::ToExitCode;
use libimagstore::storeid::IntoStoreId;
mod ui; mod ui;
@ -88,13 +86,6 @@ fn main() {
} }
fn add(rt: &Runtime) { fn add(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("add").unwrap(); // safed by main()
let entry_name = scmd.value_of("entry").unwrap(); // safed by clap
let sid = PathBuf::from(entry_name)
.into_storeid()
.map_err_trace_exit_unwrap(1);
let c = { let c = {
let parse = |value: &str| -> (i64, i64, i64) { let parse = |value: &str| -> (i64, i64, i64) {
debug!("Parsing '{}' into degree, minute and second", value); debug!("Parsing '{}' into degree, minute and second", value);
@ -120,6 +111,8 @@ fn add(rt: &Runtime) {
(*degree, *minute, *second) (*degree, *minute, *second)
}; };
let scmd = rt.cli().subcommand_matches("add").unwrap(); // safed by main()
let long = parse(scmd.value_of("longitude").unwrap()); // unwrap safed by clap let long = parse(scmd.value_of("longitude").unwrap()); // unwrap safed by clap
let lati = parse(scmd.value_of("latitude").unwrap()); // unwrap safed by clap let lati = parse(scmd.value_of("latitude").unwrap()); // unwrap safed by clap
@ -129,70 +122,78 @@ fn add(rt: &Runtime) {
Coordinates::new(long, lati) Coordinates::new(long, lati)
}; };
rt.store() rt.ids::<::ui::PathProvider>()
.get(sid)
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
.map(|mut entry| { .into_iter()
let _ = entry.set_coordinates(c).map_err_trace_exit_unwrap(1); .for_each(|id| {
}) rt.store()
.unwrap_or_else(|| { .get(id.clone())
error!("No such entry: {}", entry_name); .map_err_trace_exit_unwrap(1)
exit(1) .unwrap_or_else(|| { // if we have Ok(None)
error!("No such entry: {}", id);
exit(1)
})
.set_coordinates(c.clone())
.map_err_trace_exit_unwrap(1);
}); });
} }
fn remove(rt: &Runtime) { fn remove(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("remove").unwrap(); // safed by main() let print_removed = rt
.cli()
.subcommand_matches("remove")
.unwrap()
.is_present("print-removed"); // safed by main()
let entry_name = scmd.value_of("entry").unwrap(); // safed by clap rt.ids::<::ui::PathProvider>()
let sid = PathBuf::from(entry_name)
.into_storeid()
.map_err_trace_exit_unwrap(1);
let removed_value = rt
.store()
.get(sid)
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
.unwrap_or_else(|| { // if we have Ok(None) .into_iter()
error!("No such entry: {}", entry_name); .for_each(|id| {
exit(1) let removed_value = rt
}) .store()
.remove_coordinates() .get(id.clone())
.map_err_trace_exit_unwrap(1) // The delete action failed .map_err_trace_exit_unwrap(1)
.unwrap_or_else(|| { // if we have Ok(None) .unwrap_or_else(|| { // if we have Ok(None)
error!("Entry had no coordinates: {}", entry_name); error!("No such entry: {}", id);
exit(1) exit(1)
}) })
.map_err_trace_exit_unwrap(1); // The parsing of the deleted values failed .remove_coordinates()
.map_err_trace_exit_unwrap(1) // The delete action failed
.unwrap_or_else(|| { // if we have Ok(None)
error!("Entry had no coordinates: {}", id);
exit(1)
})
.map_err_trace_exit_unwrap(1); // The parsing of the deleted values failed
if scmd.is_present("print-removed") { if print_removed {
let _ = writeln!(rt.stdout(), "{}", removed_value).to_exit_code().unwrap_or_exit(); let _ = writeln!(rt.stdout(), "{}", removed_value).to_exit_code().unwrap_or_exit();
} }
});
} }
fn get(rt: &Runtime) { fn get(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("get").unwrap(); // safed by main() let mut stdout = rt.stdout();
rt.ids::<::ui::PathProvider>()
let entry_name = scmd.value_of("entry").unwrap(); // safed by clap
let sid = PathBuf::from(entry_name)
.into_storeid()
.map_err_trace_exit_unwrap(1);
let value = rt
.store()
.get(sid)
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
.unwrap_or_else(|| { // if we have Ok(None) .into_iter()
error!("No such entry: {}", entry_name); .for_each(|id| {
exit(1) let value = rt
}) .store()
.get_coordinates() .get(id.clone())
.map_err_trace_exit_unwrap(1) // The get action failed .map_err_trace_exit_unwrap(1)
.unwrap_or_else(|| { // if we have Ok(None) .unwrap_or_else(|| { // if we have Ok(None)
error!("Entry has no coordinates: {}", entry_name); error!("No such entry: {}", id);
exit(1) exit(1)
}); })
.get_coordinates()
.map_err_trace_exit_unwrap(1) // The get action failed
.unwrap_or_else(|| { // if we have Ok(None)
error!("Entry has no coordinates: {}", id);
exit(1)
});
let _ = writeln!(stdout, "{}", value).to_exit_code().unwrap_or_exit();
})
let _ = writeln!(rt.stdout(), "{}", value).to_exit_code().unwrap_or_exit();
} }

View file

@ -17,7 +17,14 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
use clap::{Arg, App, SubCommand}; use std::path::PathBuf;
use clap::{Arg, ArgMatches, App, SubCommand};
use libimagstore::storeid::IntoStoreId;
use libimagstore::storeid::StoreId;
use libimagrt::runtime::IdPathProvider;
use libimagerror::trace::MapErrTrace;
pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
app app
@ -42,7 +49,7 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.index(1) .index(1)
.takes_value(true) .takes_value(true)
.required(true) .required(true)
.multiple(false) .multiple(true)
.help("The entry to add the latitude/longitude to") .help("The entry to add the latitude/longitude to")
.value_name("ENTRY")) .value_name("ENTRY"))
) )
@ -60,7 +67,7 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.index(1) .index(1)
.takes_value(true) .takes_value(true)
.required(true) .required(true)
.multiple(false) .multiple(true)
.help("The entry to remove the latitude/longitude from") .help("The entry to remove the latitude/longitude from")
.value_name("ENTRY")) .value_name("ENTRY"))
) )
@ -72,7 +79,7 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.index(1) .index(1)
.takes_value(true) .takes_value(true)
.required(true) .required(true)
.multiple(false) .multiple(true)
.help("The entry to get the latitude/longitude from") .help("The entry to get the latitude/longitude from")
.value_name("ENTRY")) .value_name("ENTRY"))
.arg(Arg::with_name("format-json") .arg(Arg::with_name("format-json")
@ -89,3 +96,57 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.help("Print as <key>=<value> pairs (2 lines, default)")) .help("Print as <key>=<value> pairs (2 lines, default)"))
) )
} }
pub struct PathProvider;
impl IdPathProvider for PathProvider {
fn get_ids(matches: &ArgMatches) -> Vec<StoreId> {
match matches.subcommand() {
("add", Some(subm)) => {
subm.values_of("entry")
.ok_or_else(|| {
error!("No StoreId found");
::std::process::exit(1)
})
.unwrap()
.into_iter()
.map(PathBuf::from)
.map(|pb| pb.into_storeid())
.collect::<Result<Vec<_>, _>>()
.map_err_trace_exit_unwrap(1)
},
("remove", Some(subm)) => {
subm.values_of("entry")
.ok_or_else(|| {
error!("No StoreId found");
::std::process::exit(1)
})
.unwrap()
.into_iter()
.map(PathBuf::from)
.map(|pb| pb.into_storeid())
.collect::<Result<Vec<_>, _>>()
.map_err_trace_exit_unwrap(1)
},
("get", Some(subm)) => {
subm.values_of("get-ids")
.ok_or_else(|| {
error!("No StoreId found");
::std::process::exit(1)
})
.unwrap()
.into_iter()
.map(PathBuf::from)
.map(|pb| pb.into_storeid())
.collect::<Result<Vec<_>, _>>()
.map_err_trace_exit_unwrap(1)
},
(other, _) => {
error!("Not a known command: {}", other);
::std::process::exit(1)
}
}
}
}