Add first implementation
This commit is contained in:
parent
3c72c9d19d
commit
a5ae53c33c
5 changed files with 215 additions and 7 deletions
|
@ -1,6 +1,30 @@
|
||||||
[package]
|
[package]
|
||||||
name = "libimagentrydate"
|
name = "libimagentrydate"
|
||||||
version = "0.1.0"
|
version = "0.3.0"
|
||||||
authors = ["Matthias Beyer <mail@beyermatthias.de>"]
|
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"
|
||||||
|
|
||||||
|
documentation = "https://matthiasbeyer.github.io/imag/imag_documentation/index.html"
|
||||||
|
repository = "https://github.com/matthiasbeyer/imag"
|
||||||
|
homepage = "http://imag-pim.org"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
chrono = "0.3"
|
||||||
|
toml-query = "0.1"
|
||||||
|
lazy_static = "0.2"
|
||||||
|
toml = "0.4"
|
||||||
|
|
||||||
|
[dependencies.libimagerror]
|
||||||
|
path = "../libimagerror"
|
||||||
|
|
||||||
|
[dependencies.libimagstore]
|
||||||
|
path = "../libimagstore"
|
||||||
|
|
||||||
|
[dependencies.libimagutil]
|
||||||
|
path = "../libimagutil"
|
||||||
|
|
||||||
|
|
100
libimagentrydate/src/entrydate.rs
Normal file
100
libimagentrydate/src/entrydate.rs
Normal file
|
@ -0,0 +1,100 @@
|
||||||
|
//
|
||||||
|
// 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 chrono::naive::datetime::NaiveDateTime;
|
||||||
|
use toml_query::delete::TomlValueDeleteExt;
|
||||||
|
use toml_query::insert::TomlValueInsertExt;
|
||||||
|
use toml_query::read::TomlValueReadExt;
|
||||||
|
use toml::Value;
|
||||||
|
|
||||||
|
use libimagstore::store::Entry;
|
||||||
|
use libimagerror::into::IntoError;
|
||||||
|
|
||||||
|
use error::DateErrorKind as DEK;
|
||||||
|
use error::*;
|
||||||
|
use result::Result;
|
||||||
|
|
||||||
|
pub trait EntryDate {
|
||||||
|
|
||||||
|
fn delete_date(&mut self) -> Result<()>;
|
||||||
|
fn read_date(&self) -> Result<NaiveDateTime>;
|
||||||
|
fn set_date(&mut self, d: NaiveDateTime) -> Result<Option<Result<NaiveDateTime>>>;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
static ref DATE_HEADER_LOCATION : String = String::from("date.value");
|
||||||
|
static ref DATE_FMT : &'static str = "%Y-%m-%d %H:%M:%S";
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EntryDate for Entry {
|
||||||
|
|
||||||
|
fn delete_date(&mut self) -> Result<()> {
|
||||||
|
self.get_header_mut()
|
||||||
|
.delete(&DATE_HEADER_LOCATION)
|
||||||
|
.map(|_| ())
|
||||||
|
.map_err_into(DEK::StoreWriteError)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_date(&self) -> Result<NaiveDateTime> {
|
||||||
|
self.get_header()
|
||||||
|
.read(&DATE_HEADER_LOCATION)
|
||||||
|
.map_err_into(DEK::StoreReadError)
|
||||||
|
.and_then(|v| {
|
||||||
|
match v {
|
||||||
|
&Value::String(ref s) => s.parse::<NaiveDateTime>()
|
||||||
|
.map_err_into(DEK::DateTimeParsingError),
|
||||||
|
_ => Err(DEK::DateHeaderFieldTypeError.into_error()),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set a Date for this entry
|
||||||
|
///
|
||||||
|
/// # Return value
|
||||||
|
///
|
||||||
|
/// This function returns funny things, I know. But I find it more attractive to be explicit
|
||||||
|
/// what failed when here, instead of beeing nice to the user here.
|
||||||
|
///
|
||||||
|
/// So here's a list how things are returned:
|
||||||
|
///
|
||||||
|
/// - Err(_) if the inserting failed
|
||||||
|
/// - Ok(None) if the inserting succeeded and _did not replace an existing value_.
|
||||||
|
/// - Ok(Some(Ok(_))) if the inserting succeeded, but replaced an existing value which then got
|
||||||
|
/// parsed into a NaiveDateTime object
|
||||||
|
/// - Ok(Some(Err(_))) if the inserting succeeded, but replaced an existing value which then
|
||||||
|
/// got parsed into a NaiveDateTime object, where the parsing failed for some reason.
|
||||||
|
///
|
||||||
|
fn set_date(&mut self, d: NaiveDateTime) -> Result<Option<Result<NaiveDateTime>>> {
|
||||||
|
let date = d.format(&DATE_FMT).to_string();
|
||||||
|
|
||||||
|
self.get_header_mut()
|
||||||
|
.insert(&DATE_HEADER_LOCATION, Value::String(date))
|
||||||
|
.map(|opt| opt.map(|stri| {
|
||||||
|
match stri {
|
||||||
|
Value::String(ref s) => s.parse::<NaiveDateTime>()
|
||||||
|
.map_err_into(DEK::DateTimeParsingError),
|
||||||
|
_ => Err(DEK::DateHeaderFieldTypeError.into_error()),
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
.map_err_into(DEK::StoreWriteError)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
33
libimagentrydate/src/error.rs
Normal file
33
libimagentrydate/src/error.rs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
//
|
||||||
|
// 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!(DateError, DateErrorKind,
|
||||||
|
StoreReadError => "Store read error",
|
||||||
|
StoreWriteError => "Store write error",
|
||||||
|
|
||||||
|
DateHeaderFieldTypeError => "Expected the header field in the entry to have type 'String', but have other type",
|
||||||
|
DateTimeParsingError => "Error parsing DateTime"
|
||||||
|
);
|
||||||
|
);
|
||||||
|
|
||||||
|
pub use self::error::DateError;
|
||||||
|
pub use self::error::DateErrorKind;
|
||||||
|
pub use self::error::MapErrInto;
|
||||||
|
|
|
@ -1,6 +1,32 @@
|
||||||
#[cfg(test)]
|
//
|
||||||
mod tests {
|
// imag - the personal information management suite for the commandline
|
||||||
#[test]
|
// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
|
||||||
fn it_works() {
|
//
|
||||||
}
|
// 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
|
||||||
|
//
|
||||||
|
|
||||||
|
#[macro_use] extern crate lazy_static;
|
||||||
|
extern crate chrono;
|
||||||
|
extern crate toml_query;
|
||||||
|
extern crate toml;
|
||||||
|
|
||||||
|
#[macro_use] extern crate libimagerror;
|
||||||
|
extern crate libimagstore;
|
||||||
|
extern crate libimagutil;
|
||||||
|
|
||||||
|
pub mod entrydate;
|
||||||
|
pub mod error;
|
||||||
|
pub mod result;
|
||||||
|
|
||||||
|
|
25
libimagentrydate/src/result.rs
Normal file
25
libimagentrydate/src/result.rs
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
//
|
||||||
|
// 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::DateError;
|
||||||
|
|
||||||
|
pub type Result<T> = RResult<T, DateError>;
|
||||||
|
|
Loading…
Reference in a new issue