Merge pull request #1040 from matthiasbeyer/libimagentrygps/init
libimagentrygps: init
This commit is contained in:
commit
1760c41975
8 changed files with 408 additions and 0 deletions
|
@ -29,6 +29,7 @@ members = [
|
||||||
"lib/entry/libimagentrydatetime",
|
"lib/entry/libimagentrydatetime",
|
||||||
"lib/entry/libimagentryedit",
|
"lib/entry/libimagentryedit",
|
||||||
"lib/entry/libimagentryfilter",
|
"lib/entry/libimagentryfilter",
|
||||||
|
"lib/entry/libimagentrygps",
|
||||||
"lib/entry/libimagentrylink",
|
"lib/entry/libimagentrylink",
|
||||||
"lib/entry/libimagentrylist",
|
"lib/entry/libimagentrylist",
|
||||||
"lib/entry/libimagentrymarkdown",
|
"lib/entry/libimagentrymarkdown",
|
||||||
|
|
|
@ -25,6 +25,8 @@ This section contains the changelog from the last release to the next release.
|
||||||
* The codebase was moved to a more tree-ish approach, where several
|
* The codebase was moved to a more tree-ish approach, where several
|
||||||
subdirectories were introduced for different types of crates
|
subdirectories were introduced for different types of crates
|
||||||
* The documentation got a major overhaul and was partly rewritten
|
* The documentation got a major overhaul and was partly rewritten
|
||||||
|
* New
|
||||||
|
* `libimagentrygps` was introduced
|
||||||
* Fixed bugs
|
* Fixed bugs
|
||||||
* The config loading in `libimagrt`
|
* The config loading in `libimagrt`
|
||||||
[was fixed](http://git.imag-pim.org/imag/commit/?id=9193d50f96bce099665d2eb716bcaa29a8d9b8ff).
|
[was fixed](http://git.imag-pim.org/imag/commit/?id=9193d50f96bce099665d2eb716bcaa29a8d9b8ff).
|
||||||
|
|
23
lib/entry/libimagentrygps/Cargo.toml
Normal file
23
lib/entry/libimagentrygps/Cargo.toml
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
[package]
|
||||||
|
name = "libimagentrygps"
|
||||||
|
version = "0.4.0"
|
||||||
|
authors = ["Matthias Beyer <mail@beyermatthias.de>"]
|
||||||
|
|
||||||
|
description = "Library for the imag core distribution"
|
||||||
|
|
||||||
|
keywords = ["imag", "PIM", "personal", "information", "management"]
|
||||||
|
readme = "../README.md"
|
||||||
|
license = "LGPL-2.1"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
toml = "^0.4"
|
||||||
|
toml-query = "0.3.0"
|
||||||
|
serde_derive = "1"
|
||||||
|
serde = "1"
|
||||||
|
|
||||||
|
libimagstore = { version = "0.4.0", path = "../../../lib/core/libimagstore" }
|
||||||
|
libimagerror = { version = "0.4.0", path = "../../../lib/core/libimagerror" }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
env_logger = "0.3"
|
||||||
|
|
123
lib/entry/libimagentrygps/src/entry.rs
Normal file
123
lib/entry/libimagentrygps/src/entry.rs
Normal file
|
@ -0,0 +1,123 @@
|
||||||
|
//
|
||||||
|
// imag - the personal information management suite for the commandline
|
||||||
|
// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
|
||||||
|
//
|
||||||
|
// This library is free software; you can redistribute it and/or
|
||||||
|
// modify it under the terms of the GNU Lesser General Public
|
||||||
|
// License as published by the Free Software Foundation; version
|
||||||
|
// 2.1 of the License.
|
||||||
|
//
|
||||||
|
// This library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
// Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public
|
||||||
|
// License along with this library; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
//
|
||||||
|
|
||||||
|
use result::Result;
|
||||||
|
use error::GPSErrorKind as GPSEK;
|
||||||
|
use error::MapErrInto;
|
||||||
|
use types::*;
|
||||||
|
|
||||||
|
use libimagstore::store::Entry;
|
||||||
|
|
||||||
|
use toml_query::read::TomlValueReadExt;
|
||||||
|
use toml_query::insert::TomlValueInsertExt;
|
||||||
|
|
||||||
|
pub trait GPSEntry {
|
||||||
|
|
||||||
|
fn set_coordinates(&mut self, c: Coordinates) -> Result<()>;
|
||||||
|
fn get_coordinates(&self) -> Result<Option<Coordinates>>;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GPSEntry for Entry {
|
||||||
|
|
||||||
|
fn set_coordinates(&mut self, c: Coordinates) -> Result<()> {
|
||||||
|
self.get_header_mut()
|
||||||
|
.insert("gps.coordinates", c.into())
|
||||||
|
.map(|_| ())
|
||||||
|
.map_err_into(GPSEK::HeaderWriteError)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_coordinates(&self) -> Result<Option<Coordinates>> {
|
||||||
|
match self.get_header().read("gps.coordinates").map_err_into(GPSEK::HeaderWriteError) {
|
||||||
|
Ok(Some(hdr)) => Coordinates::from_value(hdr).map(Some),
|
||||||
|
Ok(None) => Ok(None),
|
||||||
|
Err(e) => Err(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use libimagstore::store::Store;
|
||||||
|
|
||||||
|
use entry::*;
|
||||||
|
|
||||||
|
fn setup_logging() {
|
||||||
|
use env_logger;
|
||||||
|
let _ = env_logger::init().unwrap_or(());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_store() -> Store {
|
||||||
|
Store::new(PathBuf::from("/"), None).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_set_gps() {
|
||||||
|
setup_logging();
|
||||||
|
|
||||||
|
let store = get_store();
|
||||||
|
|
||||||
|
let mut entry = store.create(PathBuf::from("test_set_gps")).unwrap();
|
||||||
|
|
||||||
|
let coordinates = Coordinates {
|
||||||
|
latitude: GPSValue::new(0, 0, 0),
|
||||||
|
longitude: GPSValue::new(0, 0, 0),
|
||||||
|
};
|
||||||
|
|
||||||
|
let res = entry.set_coordinates(coordinates);
|
||||||
|
|
||||||
|
assert!(res.is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_setget_gps() {
|
||||||
|
setup_logging();
|
||||||
|
|
||||||
|
let store = get_store();
|
||||||
|
|
||||||
|
let mut entry = store.create(PathBuf::from("test_setget_gps")).unwrap();
|
||||||
|
|
||||||
|
let coordinates = Coordinates {
|
||||||
|
latitude: GPSValue::new(0, 0, 0),
|
||||||
|
longitude: GPSValue::new(0, 0, 0),
|
||||||
|
};
|
||||||
|
|
||||||
|
let res = entry.set_coordinates(coordinates);
|
||||||
|
assert!(res.is_ok());
|
||||||
|
|
||||||
|
let coordinates = entry.get_coordinates();
|
||||||
|
|
||||||
|
assert!(coordinates.is_ok());
|
||||||
|
let coordinates = coordinates.unwrap();
|
||||||
|
|
||||||
|
assert!(coordinates.is_some());
|
||||||
|
let coordinates = coordinates.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(0, coordinates.longitude.degree);
|
||||||
|
assert_eq!(0, coordinates.longitude.minutes);
|
||||||
|
assert_eq!(0, coordinates.longitude.seconds);
|
||||||
|
assert_eq!(0, coordinates.latitude.degree);
|
||||||
|
assert_eq!(0, coordinates.latitude.minutes);
|
||||||
|
assert_eq!(0, coordinates.latitude.seconds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
43
lib/entry/libimagentrygps/src/error.rs
Normal file
43
lib/entry/libimagentrygps/src/error.rs
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
//
|
||||||
|
// imag - the personal information management suite for the commandline
|
||||||
|
// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
|
||||||
|
//
|
||||||
|
// This library is free software; you can redistribute it and/or
|
||||||
|
// modify it under the terms of the GNU Lesser General Public
|
||||||
|
// License as published by the Free Software Foundation; version
|
||||||
|
// 2.1 of the License.
|
||||||
|
//
|
||||||
|
// This library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
// Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public
|
||||||
|
// License along with this library; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
//
|
||||||
|
|
||||||
|
generate_error_module!(
|
||||||
|
generate_error_types!(GPSError, GPSErrorKind,
|
||||||
|
StoreReadError => "Store read error",
|
||||||
|
StoreWriteError => "Store write error",
|
||||||
|
|
||||||
|
HeaderWriteError => "Couldn't write Header for annotation",
|
||||||
|
HeaderReadError => "Couldn't read Header of Entry",
|
||||||
|
HeaderTypeError => "Header field has unexpected type",
|
||||||
|
|
||||||
|
TypeError => "Type Error",
|
||||||
|
DegreeMissing => "'degree' value missing",
|
||||||
|
MinutesMissing => "'minutes' value missing",
|
||||||
|
SecondsMissing => "'seconds' value missing",
|
||||||
|
LongitudeMissing => "'longitude' value missing",
|
||||||
|
LatitudeMissing => "'latitude' value missing",
|
||||||
|
|
||||||
|
NumberConversionError => "Cannot convert number to fit into variable"
|
||||||
|
);
|
||||||
|
);
|
||||||
|
|
||||||
|
pub use self::error::GPSError;
|
||||||
|
pub use self::error::GPSErrorKind;
|
||||||
|
pub use self::error::MapErrInto;
|
||||||
|
|
34
lib/entry/libimagentrygps/src/lib.rs
Normal file
34
lib/entry/libimagentrygps/src/lib.rs
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
//
|
||||||
|
// imag - the personal information management suite for the commandline
|
||||||
|
// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
|
||||||
|
//
|
||||||
|
// This library is free software; you can redistribute it and/or
|
||||||
|
// modify it under the terms of the GNU Lesser General Public
|
||||||
|
// License as published by the Free Software Foundation; version
|
||||||
|
// 2.1 of the License.
|
||||||
|
//
|
||||||
|
// This library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
// Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public
|
||||||
|
// License along with this library; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
//
|
||||||
|
|
||||||
|
extern crate toml;
|
||||||
|
extern crate toml_query;
|
||||||
|
#[macro_use] extern crate serde_derive;
|
||||||
|
|
||||||
|
extern crate libimagstore;
|
||||||
|
#[macro_use] extern crate libimagerror;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
extern crate env_logger;
|
||||||
|
|
||||||
|
pub mod entry;
|
||||||
|
pub mod error;
|
||||||
|
pub mod result;
|
||||||
|
pub mod types;
|
||||||
|
|
26
lib/entry/libimagentrygps/src/result.rs
Normal file
26
lib/entry/libimagentrygps/src/result.rs
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
//
|
||||||
|
// imag - the personal information management suite for the commandline
|
||||||
|
// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
|
||||||
|
//
|
||||||
|
// This library is free software; you can redistribute it and/or
|
||||||
|
// modify it under the terms of the GNU Lesser General Public
|
||||||
|
// License as published by the Free Software Foundation; version
|
||||||
|
// 2.1 of the License.
|
||||||
|
//
|
||||||
|
// This library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
// Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public
|
||||||
|
// License along with this library; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
//
|
||||||
|
|
||||||
|
use std::result::Result as RResult;
|
||||||
|
|
||||||
|
use error::GPSError;
|
||||||
|
|
||||||
|
pub type Result<T> = RResult<T, GPSError>;
|
||||||
|
|
||||||
|
|
156
lib/entry/libimagentrygps/src/types.rs
Normal file
156
lib/entry/libimagentrygps/src/types.rs
Normal file
|
@ -0,0 +1,156 @@
|
||||||
|
//
|
||||||
|
// imag - the personal information management suite for the commandline
|
||||||
|
// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
|
||||||
|
//
|
||||||
|
// This library is free software; you can redistribute it and/or
|
||||||
|
// modify it under the terms of the GNU Lesser General Public
|
||||||
|
// License as published by the Free Software Foundation; version
|
||||||
|
// 2.1 of the License.
|
||||||
|
//
|
||||||
|
// This library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
// Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public
|
||||||
|
// License along with this library; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
//
|
||||||
|
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
|
use toml::Value;
|
||||||
|
|
||||||
|
use libimagerror::into::IntoError;
|
||||||
|
|
||||||
|
use error::GPSErrorKind as GPSEK;
|
||||||
|
use result::Result;
|
||||||
|
|
||||||
|
pub trait FromValue : Sized {
|
||||||
|
fn from_value(v: &Value) -> Result<Self>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
|
pub struct GPSValue {
|
||||||
|
pub degree: i8,
|
||||||
|
pub minutes: i8,
|
||||||
|
pub seconds: i8
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GPSValue {
|
||||||
|
|
||||||
|
pub fn new(d: i8, m: i8, s: i8) -> GPSValue {
|
||||||
|
GPSValue {
|
||||||
|
degree: d,
|
||||||
|
minutes: m,
|
||||||
|
seconds: s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl Into<Value> for GPSValue {
|
||||||
|
|
||||||
|
fn into(self) -> Value {
|
||||||
|
let mut map = BTreeMap::new();
|
||||||
|
let _ = map.insert("degree".to_owned(), Value::Integer(self.degree as i64));
|
||||||
|
let _ = map.insert("minutes".to_owned(), Value::Integer(self.minutes as i64));
|
||||||
|
let _ = map.insert("seconds".to_owned(), Value::Integer(self.seconds as i64));
|
||||||
|
Value::Table(map)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromValue for GPSValue {
|
||||||
|
fn from_value(v: &Value) -> Result<Self> {
|
||||||
|
match *v {
|
||||||
|
Value::Table(ref map) => {
|
||||||
|
Ok(GPSValue::new(
|
||||||
|
map.get("degree")
|
||||||
|
.ok_or_else(|| GPSEK::DegreeMissing.into_error())
|
||||||
|
.and_then(|v| match *v {
|
||||||
|
Value::Integer(i) => i64_to_i8(i),
|
||||||
|
_ => Err(GPSEK::HeaderTypeError.into_error()),
|
||||||
|
})?,
|
||||||
|
|
||||||
|
map
|
||||||
|
.get("minutes")
|
||||||
|
.ok_or_else(|| GPSEK::MinutesMissing.into_error())
|
||||||
|
.and_then(|v| match *v {
|
||||||
|
Value::Integer(i) => i64_to_i8(i),
|
||||||
|
_ => Err(GPSEK::HeaderTypeError.into_error()),
|
||||||
|
})?,
|
||||||
|
|
||||||
|
map
|
||||||
|
.get("seconds")
|
||||||
|
.ok_or_else(|| GPSEK::SecondsMissing.into_error())
|
||||||
|
.and_then(|v| match *v {
|
||||||
|
Value::Integer(i) => i64_to_i8(i),
|
||||||
|
_ => Err(GPSEK::HeaderTypeError.into_error()),
|
||||||
|
})?
|
||||||
|
))
|
||||||
|
}
|
||||||
|
_ => Err(GPSEK::TypeError.into_error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Data-transfer type for transfering longitude-latitude-pairs
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
|
pub struct Coordinates {
|
||||||
|
pub longitude: GPSValue,
|
||||||
|
pub latitude: GPSValue,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Coordinates {
|
||||||
|
pub fn new(long: GPSValue, lat: GPSValue) -> Coordinates {
|
||||||
|
Coordinates {
|
||||||
|
longitude: long,
|
||||||
|
latitude: lat,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Into<Value> for Coordinates {
|
||||||
|
|
||||||
|
fn into(self) -> Value {
|
||||||
|
let mut map = BTreeMap::new();
|
||||||
|
let _ = map.insert("longitude".to_owned(), self.longitude.into());
|
||||||
|
let _ = map.insert("latitude".to_owned(), self.latitude.into());
|
||||||
|
Value::Table(map)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromValue for Coordinates {
|
||||||
|
fn from_value(v: &Value) -> Result<Self> {
|
||||||
|
match *v {
|
||||||
|
Value::Table(ref map) => {
|
||||||
|
Ok(Coordinates::new(
|
||||||
|
match map.get("longitude") {
|
||||||
|
Some(v) => GPSValue::from_value(v),
|
||||||
|
None => Err(GPSEK::LongitudeMissing.into_error()),
|
||||||
|
}?,
|
||||||
|
|
||||||
|
match map.get("latitude") {
|
||||||
|
Some(v) => GPSValue::from_value(v),
|
||||||
|
None => Err(GPSEK::LongitudeMissing.into_error()),
|
||||||
|
}?
|
||||||
|
))
|
||||||
|
}
|
||||||
|
_ => Err(GPSEK::TypeError.into_error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Helper to convert a i64 to i8 or return an error if this doesn't work.
|
||||||
|
fn i64_to_i8(i: i64) -> Result<i8> {
|
||||||
|
if i > (<i8>::max_value() as i64) {
|
||||||
|
Err(GPSEK::NumberConversionError.into_error())
|
||||||
|
} else {
|
||||||
|
Ok(i as i8)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue