1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#![forbid(unsafe_code)]
#![deny(
non_camel_case_types,
non_snake_case,
path_statements,
trivial_numeric_casts,
unstable_features,
unused_allocation,
unused_import_braces,
unused_imports,
unused_must_use,
unused_mut,
unused_qualifications,
while_true,
)]
extern crate clap;
#[macro_use] extern crate log;
#[macro_use] extern crate failure;
extern crate libimagentrygps;
extern crate libimagrt;
extern crate libimagutil;
extern crate libimagerror;
extern crate libimagstore;
use std::io::Write;
use std::str::FromStr;
use failure::Error;
use failure::Fallible as Result;
use failure::err_msg;
use clap::App;
use libimagstore::storeid::StoreId;
use libimagentrygps::types::*;
use libimagentrygps::entry::*;
use libimagrt::application::ImagApplication;
use libimagrt::runtime::Runtime;
mod ui;
pub enum ImagGps {}
impl ImagApplication for ImagGps {
fn run(rt: Runtime) -> Result<()> {
match rt.cli().subcommand_name().ok_or_else(|| err_msg("No subcommand called"))? {
"add" => add(&rt),
"remove" => remove(&rt),
"get" => get(&rt),
other => {
debug!("Unknown command");
if rt.handle_unknown_subcommand("imag-gps", other, rt.cli())
.map_err(Error::from)?
.success()
{
Ok(())
} else {
Err(format_err!("Subcommand failed"))
}
}
}
}
fn build_cli<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
ui::build_ui(app)
}
fn name() -> &'static str {
env!("CARGO_PKG_NAME")
}
fn description() -> &'static str {
"Add GPS coordinates to entries"
}
fn version() -> &'static str {
env!("CARGO_PKG_VERSION")
}
}
fn rt_get_ids(rt: &Runtime) -> Result<Vec<StoreId>> {
rt
.ids::<crate::ui::PathProvider>()?
.ok_or_else(|| err_msg("No ids supplied"))
}
fn add(rt: &Runtime) -> Result<()> {
let c = {
let parse = |value: &str| -> Result<(i64, i64, i64)> {
debug!("Parsing '{}' into degree, minute and second", value);
let ary = value.split('.')
.map(|v| {debug!("Parsing = {}", v); v})
.map(FromStr::from_str)
.map(|elem| elem.or_else(|_| Err(err_msg("Error while converting number"))))
.collect::<Result<Vec<i64>>>()?;
let degree = ary.get(0).ok_or_else(|| err_msg("Degree missing. This value is required."))?;
let minute = ary.get(1).ok_or_else(|| err_msg("Degree missing. This value is required."))?;
let second = ary.get(2).unwrap_or(&0);
Ok((*degree, *minute, *second))
};
let scmd = rt.cli().subcommand_matches("add").unwrap();
let long = parse(scmd.value_of("longitude").unwrap())?;
let lati = parse(scmd.value_of("latitude").unwrap())?;
let long = GPSValue::new(long.0, long.1, long.2);
let lati = GPSValue::new(lati.0, lati.1, lati.2);
Coordinates::new(long, lati)
};
rt_get_ids(&rt)?
.into_iter()
.map(|id| {
rt.store()
.get(id.clone())?
.ok_or_else(|| format_err!("No such entry: {}", id))?
.set_coordinates(c.clone())?;
rt.report_touched(&id)
})
.collect()
}
fn remove(rt: &Runtime) -> Result<()> {
let print_removed = rt
.cli()
.subcommand_matches("remove")
.unwrap()
.is_present("print-removed");
rt_get_ids(&rt)?
.into_iter()
.map(|id| {
let removed_value : Coordinates = rt
.store()
.get(id.clone())?
.ok_or_else(|| format_err!("No such entry: {}", id))?
.remove_coordinates()?
.ok_or_else(|| format_err!("Entry had no coordinates: {}", id))??;
if print_removed {
writeln!(rt.stdout(), "{}", removed_value)?;
}
rt.report_touched(&id)
})
.collect()
}
fn get(rt: &Runtime) -> Result<()> {
let mut stdout = rt.stdout();
rt_get_ids(&rt)?
.into_iter()
.map(|id| {
let value = rt
.store()
.get(id.clone())?
.ok_or_else(|| {
format_err!("No such entry: {}", id)
})?
.get_coordinates()?
.ok_or_else(|| {
format_err!("Entry has no coordinates: {}", id)
})?;
writeln!(stdout, "{}", value)?;
rt.report_touched(&id)
})
.collect()
}