From 4d94791b1f74e20380f77de70d1c7d1209c070c7 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 31 Jan 2018 22:04:54 +0100 Subject: Move date <-> string conversion utility to libimagutil --- bin/domain/imag-habit/src/main.rs | 6 +++--- lib/domain/libimaghabit/Cargo.toml | 1 + lib/domain/libimaghabit/src/habit.rs | 4 ++-- lib/domain/libimaghabit/src/instance.rs | 4 +++- lib/domain/libimaghabit/src/lib.rs | 1 + lib/domain/libimaghabit/src/util.rs | 11 ----------- lib/etc/libimagutil/Cargo.toml | 1 + lib/etc/libimagutil/src/date.rs | 32 ++++++++++++++++++++++++++++++++ lib/etc/libimagutil/src/lib.rs | 2 ++ 9 files changed, 45 insertions(+), 17 deletions(-) create mode 100644 lib/etc/libimagutil/src/date.rs diff --git a/bin/domain/imag-habit/src/main.rs b/bin/domain/imag-habit/src/main.rs index 02ee8866..ba39b2e9 100644 --- a/bin/domain/imag-habit/src/main.rs +++ b/bin/domain/imag-habit/src/main.rs @@ -372,7 +372,7 @@ fn show(rt: &Runtime) { } fn instance_lister_fn(i: &FileLockEntry) -> Vec { - use libimaghabit::util::date_to_string; + use libimagutil::date::date_to_string; use libimaghabit::instance::HabitInstance; let date = date_to_string(&i.get_date().map_err_trace_exit_unwrap(1)); @@ -456,7 +456,7 @@ fn done(rt: &Runtime) { .map_err_trace_exit_unwrap(1); info!("Done on {date}: {name}", - date = libimaghabit::util::date_to_string(&next), + date = libimagutil::date::date_to_string(&next), name = next_instance_name); } else { info!("Ignoring: {}, because there is no due date (the habit is finised)", @@ -483,6 +483,6 @@ fn get_from_store<'a>(store: &'a Store, id: StoreId) -> Option } fn date_to_string_helper(d: chrono::NaiveDate) -> String { - libimaghabit::util::date_to_string(&d) + libimagutil::date::date_to_string(&d) } diff --git a/lib/domain/libimaghabit/Cargo.toml b/lib/domain/libimaghabit/Cargo.toml index 48dcace3..62ca1770 100644 --- a/lib/domain/libimaghabit/Cargo.toml +++ b/lib/domain/libimaghabit/Cargo.toml @@ -26,3 +26,4 @@ libimagerror = { version = "0.6.0", path = "../../../lib/core/libimagerror" libimagentryedit = { version = "0.6.0", path = "../../../lib/entry/libimagentryedit" } libimagentrylink = { version = "0.6.0", path = "../../../lib/entry/libimagentrylink" } libimagentryutil = { version = "0.6.0", path = "../../../lib/entry/libimagentryutil" } +libimagutil = { version = "0.6.0", path = "../../../lib/etc/libimagutil" } diff --git a/lib/domain/libimaghabit/src/habit.rs b/lib/domain/libimaghabit/src/habit.rs index fd6b9ff8..4bb40aef 100644 --- a/lib/domain/libimaghabit/src/habit.rs +++ b/lib/domain/libimaghabit/src/habit.rs @@ -28,7 +28,6 @@ use error::HabitError as HE; use error::HabitErrorKind as HEK; use error::*; use iter::HabitInstanceStoreIdIterator; -use util::date_to_string; use util::IsHabitCheck; use util::get_string_header_from_entry; use instance::IsHabitInstance; @@ -42,6 +41,7 @@ use libimagstore::storeid::IntoStoreId; use libimagstore::storeid::StoreIdIterator; use libimagentryutil::isa::Is; use libimagentryutil::isa::IsKindHeaderPathProvider; +use libimagutil::date::date_to_string; /// A HabitTemplate is a "template" of a habit. A user may define a habit "Eat vegetable". /// If the user ate a vegetable, she should create a HabitInstance from the Habit with the @@ -250,7 +250,7 @@ pub mod builder { use error::HabitError as HE; use error::HabitErrorKind as HEK; use error::*; - use util::date_to_string; + use libimagutil::date::date_to_string; use habit::IsHabitTemplate; pub struct HabitBuilder { diff --git a/lib/domain/libimaghabit/src/instance.rs b/lib/domain/libimaghabit/src/instance.rs index 578cc91b..82387592 100644 --- a/lib/domain/libimaghabit/src/instance.rs +++ b/lib/domain/libimaghabit/src/instance.rs @@ -54,11 +54,13 @@ impl HabitInstance for Entry { } fn get_date(&self) -> Result { - use util::date_from_string; + use libimagutil::date::date_from_string as dts; + let date_from_string = |d| dts(d).map_err(From::from); get_string_header_from_entry(self, "habit.instance.date").and_then(date_from_string) } fn set_date(&mut self, n: &NaiveDate) -> Result<()> { + use libimagutil::date::date_to_string; // Using `set` here because when creating the entry, these headers should be made present. self.get_header_mut() .set("habit.instance.date", Value::String(date_to_string(n))) diff --git a/lib/domain/libimaghabit/src/lib.rs b/lib/domain/libimaghabit/src/lib.rs index 06545d6c..7bfb494e 100644 --- a/lib/domain/libimaghabit/src/lib.rs +++ b/lib/domain/libimaghabit/src/lib.rs @@ -29,6 +29,7 @@ extern crate libimagerror; extern crate libimagentryedit; extern crate libimagentrylink; #[macro_use] extern crate libimagentryutil; +extern crate libimagutil; module_entry_path_mod!("habit"); diff --git a/lib/domain/libimaghabit/src/util.rs b/lib/domain/libimaghabit/src/util.rs index 72caf25b..ea89eba1 100644 --- a/lib/domain/libimaghabit/src/util.rs +++ b/lib/domain/libimaghabit/src/util.rs @@ -19,7 +19,6 @@ use std::ops::BitXor; -use chrono::NaiveDate; use error::Result; use habit::HabitTemplate; @@ -28,16 +27,6 @@ use instance::HabitInstance; use libimagstore::storeid::StoreId; use libimagstore::store::Entry; -pub const NAIVE_DATE_STRING_FORMAT : &'static str = "%Y-%m-%d"; - -pub fn date_to_string(ndt: &NaiveDate) -> String { - ndt.format(NAIVE_DATE_STRING_FORMAT).to_string() -} - -pub fn date_from_string(s: String) -> Result { - NaiveDate::parse_from_str(&s, NAIVE_DATE_STRING_FORMAT).map_err(From::from) -} - /// Helper trait to check whether a object which can be a habit instance and a habit template is /// actually a valid object, whereas "valid" is defined that it is _either_ an instance or a /// template (think XOR). diff --git a/lib/etc/libimagutil/Cargo.toml b/lib/etc/libimagutil/Cargo.toml index a0d1a70d..dfd87d1a 100644 --- a/lib/etc/libimagutil/Cargo.toml +++ b/lib/etc/libimagutil/Cargo.toml @@ -32,4 +32,5 @@ lazy_static = "0.2" log = "0.4.0" regex = "0.2" tempfile = "2.1" +chrono = "0.4" diff --git a/lib/etc/libimagutil/src/date.rs b/lib/etc/libimagutil/src/date.rs new file mode 100644 index 00000000..dc9f9de0 --- /dev/null +++ b/lib/etc/libimagutil/src/date.rs @@ -0,0 +1,32 @@ +// +// imag - the personal information management suite for the commandline +// Copyright (C) 2015, 2016 Matthias Beyer 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::NaiveDate; +use chrono::format::ParseError; + +pub const NAIVE_DATE_STRING_FORMAT : &'static str = "%Y-%m-%d"; + +pub fn date_to_string(ndt: &NaiveDate) -> String { + ndt.format(NAIVE_DATE_STRING_FORMAT).to_string() +} + +pub fn date_from_string(s: String) -> Result { + NaiveDate::parse_from_str(&s, NAIVE_DATE_STRING_FORMAT) +} + diff --git a/lib/etc/libimagutil/src/lib.rs b/lib/etc/libimagutil/src/lib.rs index cdf1a015..e2ee8397 100644 --- a/lib/etc/libimagutil/src/lib.rs +++ b/lib/etc/libimagutil/src/lib.rs @@ -39,9 +39,11 @@ extern crate regex; extern crate url; extern crate boolinator; extern crate tempfile; +extern crate chrono; #[macro_use] mod log_result; pub mod cli_validators; +pub mod date; pub mod debug_result; pub mod edit; pub mod info_result; -- cgit v1.2.3