Move date <-> string conversion utility to libimagutil

This commit is contained in:
Matthias Beyer 2018-01-31 22:04:54 +01:00
parent 20f9c99c83
commit 4d94791b1f
9 changed files with 45 additions and 17 deletions

View File

@ -372,7 +372,7 @@ fn show(rt: &Runtime) {
}
fn instance_lister_fn(i: &FileLockEntry) -> Vec<String> {
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<FileLockEntry<'a>
}
fn date_to_string_helper(d: chrono::NaiveDate) -> String {
libimaghabit::util::date_to_string(&d)
libimagutil::date::date_to_string(&d)
}

View File

@ -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" }

View File

@ -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 {

View File

@ -54,11 +54,13 @@ impl HabitInstance for Entry {
}
fn get_date(&self) -> Result<NaiveDate> {
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)))

View File

@ -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");

View File

@ -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> {
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).

View File

@ -32,4 +32,5 @@ lazy_static = "0.2"
log = "0.4.0"
regex = "0.2"
tempfile = "2.1"
chrono = "0.4"

View File

@ -0,0 +1,32 @@
//
// 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::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, ParseError> {
NaiveDate::parse_from_str(&s, NAIVE_DATE_STRING_FORMAT)
}

View File

@ -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;