Move imag-gps to ID provider infrastructure
This commit is contained in:
parent
70014e04b1
commit
4856541f5a
2 changed files with 127 additions and 65 deletions
|
@ -45,7 +45,6 @@ extern crate libimagstore;
|
|||
|
||||
use std::io::Write;
|
||||
use std::process::exit;
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
|
||||
use failure::Error;
|
||||
|
@ -58,7 +57,6 @@ use libimagrt::runtime::Runtime;
|
|||
use libimagerror::trace::MapErrTrace;
|
||||
use libimagerror::exit::ExitUnwrap;
|
||||
use libimagerror::io::ToExitCode;
|
||||
use libimagstore::storeid::IntoStoreId;
|
||||
|
||||
mod ui;
|
||||
|
||||
|
@ -88,13 +86,6 @@ fn main() {
|
|||
}
|
||||
|
||||
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 parse = |value: &str| -> (i64, i64, i64) {
|
||||
debug!("Parsing '{}' into degree, minute and second", value);
|
||||
|
@ -120,6 +111,8 @@ fn add(rt: &Runtime) {
|
|||
(*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 lati = parse(scmd.value_of("latitude").unwrap()); // unwrap safed by clap
|
||||
|
||||
|
@ -129,70 +122,78 @@ fn add(rt: &Runtime) {
|
|||
Coordinates::new(long, lati)
|
||||
};
|
||||
|
||||
rt.store()
|
||||
.get(sid)
|
||||
rt.ids::<::ui::PathProvider>()
|
||||
.map_err_trace_exit_unwrap(1)
|
||||
.map(|mut entry| {
|
||||
let _ = entry.set_coordinates(c).map_err_trace_exit_unwrap(1);
|
||||
})
|
||||
.unwrap_or_else(|| {
|
||||
error!("No such entry: {}", entry_name);
|
||||
exit(1)
|
||||
.into_iter()
|
||||
.for_each(|id| {
|
||||
rt.store()
|
||||
.get(id.clone())
|
||||
.map_err_trace_exit_unwrap(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) {
|
||||
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
|
||||
let sid = PathBuf::from(entry_name)
|
||||
.into_storeid()
|
||||
.map_err_trace_exit_unwrap(1);
|
||||
|
||||
let removed_value = rt
|
||||
.store()
|
||||
.get(sid)
|
||||
rt.ids::<::ui::PathProvider>()
|
||||
.map_err_trace_exit_unwrap(1)
|
||||
.unwrap_or_else(|| { // if we have Ok(None)
|
||||
error!("No such entry: {}", entry_name);
|
||||
exit(1)
|
||||
})
|
||||
.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: {}", entry_name);
|
||||
exit(1)
|
||||
})
|
||||
.map_err_trace_exit_unwrap(1); // The parsing of the deleted values failed
|
||||
.into_iter()
|
||||
.for_each(|id| {
|
||||
let removed_value = rt
|
||||
.store()
|
||||
.get(id.clone())
|
||||
.map_err_trace_exit_unwrap(1)
|
||||
.unwrap_or_else(|| { // if we have Ok(None)
|
||||
error!("No such entry: {}", id);
|
||||
exit(1)
|
||||
})
|
||||
.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") {
|
||||
let _ = writeln!(rt.stdout(), "{}", removed_value).to_exit_code().unwrap_or_exit();
|
||||
}
|
||||
if print_removed {
|
||||
let _ = writeln!(rt.stdout(), "{}", removed_value).to_exit_code().unwrap_or_exit();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn get(rt: &Runtime) {
|
||||
let scmd = rt.cli().subcommand_matches("get").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 value = rt
|
||||
.store()
|
||||
.get(sid)
|
||||
let mut stdout = rt.stdout();
|
||||
rt.ids::<::ui::PathProvider>()
|
||||
.map_err_trace_exit_unwrap(1)
|
||||
.unwrap_or_else(|| { // if we have Ok(None)
|
||||
error!("No such entry: {}", entry_name);
|
||||
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: {}", entry_name);
|
||||
exit(1)
|
||||
});
|
||||
.into_iter()
|
||||
.for_each(|id| {
|
||||
let value = rt
|
||||
.store()
|
||||
.get(id.clone())
|
||||
.map_err_trace_exit_unwrap(1)
|
||||
.unwrap_or_else(|| { // if we have Ok(None)
|
||||
error!("No such entry: {}", id);
|
||||
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();
|
||||
}
|
||||
|
||||
|
|
|
@ -17,7 +17,14 @@
|
|||
// 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> {
|
||||
app
|
||||
|
@ -42,7 +49,7 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
|
|||
.index(1)
|
||||
.takes_value(true)
|
||||
.required(true)
|
||||
.multiple(false)
|
||||
.multiple(true)
|
||||
.help("The entry to add the latitude/longitude to")
|
||||
.value_name("ENTRY"))
|
||||
)
|
||||
|
@ -60,7 +67,7 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
|
|||
.index(1)
|
||||
.takes_value(true)
|
||||
.required(true)
|
||||
.multiple(false)
|
||||
.multiple(true)
|
||||
.help("The entry to remove the latitude/longitude from")
|
||||
.value_name("ENTRY"))
|
||||
)
|
||||
|
@ -72,7 +79,7 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
|
|||
.index(1)
|
||||
.takes_value(true)
|
||||
.required(true)
|
||||
.multiple(false)
|
||||
.multiple(true)
|
||||
.help("The entry to get the latitude/longitude from")
|
||||
.value_name("ENTRY"))
|
||||
.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)"))
|
||||
)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue