summaryrefslogtreecommitdiff
path: root/lib/domain/libimagdiary/src/diaryid.rs
blob: c28aebdde8995c900306ec248ce0aa8a02bcf753 (plain)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2019 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::convert::Into;
use std::fmt::{Display, Formatter, Error as FmtError};
use std::result::Result as RResult;

use chrono::naive::NaiveDateTime;
use chrono::naive::NaiveTime;
use chrono::naive::NaiveDate;
use chrono::Datelike;
use chrono::Timelike;
use failure::Fallible as Result;
use failure::ResultExt;
use failure::Error;
use failure::err_msg;

use libimagstore::storeid::StoreId;
use libimagstore::storeid::IntoStoreId;

#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq)]
pub struct DiaryId {
    name: String,
    year: i32,
    month: u32,
    day: u32,
    hour: u32,
    minute: u32,
    second: u32,
}

impl DiaryId {

    pub fn new(name: String, y: i32, m: u32, d: u32, h: u32, min: u32, sec: u32) -> DiaryId {
        DiaryId {
            name: name,
            year: y,
            month: m,
            day: d,
            hour: h,
            minute: min,
            second: sec,
        }
    }

    pub fn from_datetime<DT: Datelike + Timelike>(diary_name: String, dt: DT) -> DiaryId {
        DiaryId::new(diary_name,
                     dt.year(),
                     dt.month(),
                     dt.day(),
                     dt.hour(),
                     dt.minute(),
                     dt.second())
    }

    pub fn diary_name(&self) -> &String {
        &self.name
    }

    pub fn year(&self) -> i32 {
        self.year
    }

    pub fn month(&self) -> u32 {
        self.month
    }

    pub fn day(&self) -> u32 {
        self.day
    }

    pub fn hour(&self) -> u32 {
        self.hour
    }

    pub fn minute(&self) -> u32 {
        self.minute
    }

    pub fn second(&self) -> u32 {
        self.second
    }

    pub fn with_diary_name(mut self, name: String) -> DiaryId {
        self.name = name;
        self
    }

    pub fn with_year(mut self, year: i32) -> DiaryId {
        self.year = year;
        self
    }

    pub fn with_month(mut self, month: u32) -> DiaryId {
        self.month = month;
        self
    }

    pub fn with_day(mut self, day: u32) -> DiaryId {
        self.day = day;
        self
    }

    pub fn with_hour(mut self, hour: u32) -> DiaryId {
        self.hour = hour;
        self
    }

    pub fn with_minute(mut self, minute: u32) -> DiaryId {
        self.minute = minute;
        self
    }

    pub fn with_second(mut self, sec: u32) -> DiaryId {
        self.second = sec;
        self
    }

    pub fn now(name: String) -> DiaryId {
        use chrono::offset::Local;

        let now = Local::now();
        let now_date = now.date().naive_local();
        let now_time = now.time();
        let dt = NaiveDateTime::new(now_date, now_time);

        DiaryId::new(name, dt.year(), dt.month(), dt.day(), dt.hour(), dt.minute(), dt.second())
    }

}

impl IntoStoreId for DiaryId {

    fn into_storeid(self) -> Result<StoreId> {
        use std::path::PathBuf;
        let s : String = self.into();
        crate::module_path::new_id(PathBuf::from(s))
    }

}

impl Into<String> for DiaryId {

    fn into(self) -> String {
        format!("{}/{:0>4}/{:0>2}/{:0>2}/{:0>2}:{:0>2}:{:0>2}",
                self.name, self.year, self.month, self.day, self.hour, self.minute, self.second)
    }

}

impl Display for DiaryId {

    fn fmt(&self, fmt: &mut Formatter) -> RResult<(), FmtError> {
        write!(fmt, "{}/{:0>4}/{:0>2}/{:0>2}/{:0>2}:{:0>2}:{:0>2}",
                self.name, self.year, self.month, self.day, self.hour, self.minute, self.second)
    }

}

impl Into<NaiveDateTime> for DiaryId {

    fn into(self) -> NaiveDateTime {
        let d = NaiveDate::from_ymd(self.year, self.month, self.day);
        let t = NaiveTime::from_hms(self.hour, self.minute, self.second);
        NaiveDateTime::new(d, t)
    }

}

pub trait FromStoreId : Sized {
    fn from_storeid(_: &StoreId) -> Result<Self>;
}

use std::path::Component;

fn component_to_str<'a>(com: Component<'a>) -> Result<&'a str> {
    match com {
        Component::Normal(s) => Some(s),
        _ => None,
    }.and_then(|s| s.to_str())
    .ok_or_else(|| Error::from(err_msg("ID Parse error")))
}

impl FromStoreId for DiaryId {

    fn from_storeid(s: &StoreId) -> Result<DiaryId> {
        use std::str::FromStr;

        use std::path::Components;
        use std::iter::Rev;

        fn next_component<'a>(components: &'a mut Rev<Components>) -> Result<&'a str> {
            components.next()
                .ok_or_else(|| Error::from(err_msg("ID parse error")))
                .and_then(component_to_str)
        }

        let mut cmps   = s.components().rev();
        trace!("Found components: {:?}", cmps);

        let (hour, minute, second) = next_component(&mut cmps).and_then(|time| {
            let mut time = time.split(":");
            let hour     = time.next().and_then(|s| FromStr::from_str(s).ok());
            let minute   = time.next().and_then(|s| FromStr::from_str(s).ok());
            let second   = time.next().and_then(|s| FromStr::from_str(s).ok());
            trace!("Found time   = {:?}", time);

            debug!("Hour   = {:?}", hour);
            debug!("Minute = {:?}", minute);
            debug!("Second = {:?}", second);

            match (hour, minute, second) {
                (Some(h), Some(m), Some(s)) => Ok((h, m, s)),
                _ => return Err(Error::from(err_msg("ID Parse error"))),
            }
        })?;

        let day: Result<u32> = next_component(&mut cmps)
            .and_then(|s| {
                s.parse::<u32>()
                    .context("Failed to parse day from u32")
                    .map_err(Error::from)
                    .context(err_msg("ID parse error"))
                    .map_err(Error::from)
            });

        let month: Result<u32> = next_component(&mut cmps)
            .and_then(|s| {
                s.parse::<u32>()
                    .context("Failed to parse month from u32")
                    .map_err(Error::from)
                    .context(err_msg("ID Parse error"))
                    .map_err(Error::from)
            });

        let year: Result<i32> = next_component(&mut cmps)
            .and_then(|s| {
                s.parse::<i32>()
                    .context("Failed to parse year from i32")
                    .map_err(Error::from)
                    .context(err_msg("ID Parse error"))
                    .map_err(Error::from)
            });

        let name = next_component(&mut cmps).map(String::from);

        debug!("Day   = {:?}", day);
        debug!("Month = {:?}", month);
        debug!("Year  = {:?}", year);
        debug!("Name  = {:?}", name);

        let day    = day?;
        let month  = month?;
        let year   = year?;
        let name   = name?;

        Ok(DiaryId::new(name, year, month, day, hour, minute, second))
    }

}