libimagdiary: Move from error-chain to failure
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
parent
2a107477c0
commit
3951bc7e5d
8 changed files with 66 additions and 146 deletions
|
@ -25,7 +25,7 @@ log = "0.4.0"
|
|||
toml = "0.4"
|
||||
toml-query = "0.7"
|
||||
itertools = "0.7"
|
||||
error-chain = "0.12"
|
||||
failure = "0.1"
|
||||
filters = "0.3"
|
||||
|
||||
libimagstore = { version = "0.9.0", path = "../../../lib/core/libimagstore" }
|
||||
|
|
|
@ -29,13 +29,12 @@ use chrono::Datelike;
|
|||
use itertools::Itertools;
|
||||
use chrono::naive::NaiveDateTime;
|
||||
use chrono::Timelike;
|
||||
use failure::Fallible as Result;
|
||||
use failure::Error;
|
||||
|
||||
use entry::IsDiaryEntry;
|
||||
use diaryid::DiaryId;
|
||||
use diaryid::FromStoreId;
|
||||
use error::DiaryErrorKind as DEK;
|
||||
use error::ResultExt;
|
||||
use error::Result;
|
||||
use iter::DiaryEntryIterator;
|
||||
use iter::DiaryNameIterator;
|
||||
|
||||
|
@ -67,7 +66,7 @@ impl Diary for Store {
|
|||
let ndt = dt.naive_local();
|
||||
let id = DiaryId::new(String::from(diary_name), ndt.year(), ndt.month(), ndt.day(), 0, 0, 0);
|
||||
|
||||
let mut entry = self.retrieve(id).chain_err(|| DEK::StoreReadError)?;
|
||||
let mut entry = self.retrieve(id)?;
|
||||
let _ = entry.set_isflag::<IsDiaryEntry>()?;
|
||||
Ok(entry)
|
||||
}
|
||||
|
@ -87,7 +86,7 @@ impl Diary for Store {
|
|||
ndt.minute(),
|
||||
ndt.second());
|
||||
|
||||
let mut entry = self.retrieve(id).chain_err(|| DEK::StoreReadError)?;
|
||||
let mut entry = self.retrieve(id)?;
|
||||
let _ = entry.set_isflag::<IsDiaryEntry>()?;
|
||||
Ok(entry)
|
||||
}
|
||||
|
@ -97,15 +96,12 @@ impl Diary for Store {
|
|||
debug!("Building iterator for module 'diary' with diary name = '{}'", diary_name);
|
||||
Store::entries(self)
|
||||
.map(|iter| DiaryEntryIterator::new(String::from(diary_name), iter.without_store()))
|
||||
.chain_err(|| DEK::StoreReadError)
|
||||
}
|
||||
|
||||
/// get the id of the youngest entry
|
||||
///
|
||||
/// TODO: We collect internally here. We shouldn't do that. Solution unclear.
|
||||
fn get_youngest_entry_id(&self, diary_name: &str) -> Option<Result<DiaryId>> {
|
||||
use error::DiaryError as DE;
|
||||
|
||||
match Diary::entries(self, diary_name) {
|
||||
Err(e) => Some(Err(e)),
|
||||
Ok(entries) => {
|
||||
|
@ -114,7 +110,7 @@ impl Diary for Store {
|
|||
for entry in entries {
|
||||
let entry = match entry {
|
||||
Ok(e) => DiaryId::from_storeid(&e),
|
||||
Err(e) => return Some(Err(e).map_err(DE::from)),
|
||||
Err(e) => return Some(Err(e)),
|
||||
};
|
||||
|
||||
sorted_entries.push(entry);
|
||||
|
@ -156,7 +152,7 @@ impl Diary for Store {
|
|||
fn diary_names(&self) -> Result<DiaryNameIterator> {
|
||||
self.entries()
|
||||
.map(|it| DiaryNameIterator::new(it.without_store()))
|
||||
.map_err(::error::DiaryError::from)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -19,20 +19,20 @@
|
|||
|
||||
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;
|
||||
use libimagstore::store::Result as StoreResult;
|
||||
|
||||
use error::DiaryError as DE;
|
||||
use error::DiaryErrorKind as DEK;
|
||||
use error::ResultExt;
|
||||
|
||||
use module_path::ModuleEntryPath;
|
||||
|
||||
|
@ -149,7 +149,7 @@ impl DiaryId {
|
|||
|
||||
impl IntoStoreId for DiaryId {
|
||||
|
||||
fn into_storeid(self) -> StoreResult<StoreId> {
|
||||
fn into_storeid(self) -> Result<StoreId> {
|
||||
let s : String = self.into();
|
||||
ModuleEntryPath::new(s).into_storeid()
|
||||
}
|
||||
|
@ -167,7 +167,7 @@ impl Into<String> for DiaryId {
|
|||
|
||||
impl Display for DiaryId {
|
||||
|
||||
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
|
||||
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)
|
||||
}
|
||||
|
@ -185,32 +185,30 @@ impl Into<NaiveDateTime> for DiaryId {
|
|||
}
|
||||
|
||||
pub trait FromStoreId : Sized {
|
||||
|
||||
fn from_storeid(&StoreId) -> Result<Self, DE>;
|
||||
|
||||
fn from_storeid(&StoreId) -> Result<Self>;
|
||||
}
|
||||
|
||||
use std::path::Component;
|
||||
|
||||
fn component_to_str<'a>(com: Component<'a>) -> Result<&'a str, DE> {
|
||||
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(DE::from_kind(DEK::IdParseError))
|
||||
.ok_or_else(|| Error::from(err_msg("ID Parse error")))
|
||||
}
|
||||
|
||||
impl FromStoreId for DiaryId {
|
||||
|
||||
fn from_storeid(s: &StoreId) -> Result<DiaryId, DE> {
|
||||
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, DE> {
|
||||
fn next_component<'a>(components: &'a mut Rev<Components>) -> Result<&'a str> {
|
||||
components.next()
|
||||
.ok_or(DE::from_kind(DEK::IdParseError))
|
||||
.ok_or_else(|| Error::from(err_msg("ID parse error")))
|
||||
.and_then(component_to_str)
|
||||
}
|
||||
|
||||
|
@ -228,21 +226,33 @@ impl FromStoreId for DiaryId {
|
|||
|
||||
match (hour, minute, second) {
|
||||
(Some(h), Some(m), Some(s)) => Ok((h, m, s)),
|
||||
_ => return Err(DE::from_kind(DEK::IdParseError)),
|
||||
_ => return Err(Error::from(err_msg("ID Parse error"))),
|
||||
}
|
||||
})?;
|
||||
|
||||
let day: Result<u32,_> = next_component(&mut cmps)
|
||||
.and_then(|s| s.parse::<u32>()
|
||||
.chain_err(|| DEK::IdParseError));
|
||||
let day: Result<u32> = next_component(&mut cmps)
|
||||
.and_then(|s| {
|
||||
s.parse::<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>()
|
||||
.chain_err(|| DEK::IdParseError));
|
||||
let month: Result<u32> = next_component(&mut cmps)
|
||||
.and_then(|s| {
|
||||
s.parse::<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>()
|
||||
.chain_err(|| DEK::IdParseError));
|
||||
let year: Result<i32> = next_component(&mut cmps)
|
||||
.and_then(|s| {
|
||||
s.parse::<i32>()
|
||||
.map_err(Error::from)
|
||||
.context(err_msg("ID Parse error"))
|
||||
.map_err(Error::from)
|
||||
});
|
||||
|
||||
let name = next_component(&mut cmps).map(String::from);
|
||||
|
||||
|
|
|
@ -21,9 +21,10 @@ use libimagstore::store::Entry;
|
|||
use libimagentryutil::isa::Is;
|
||||
use libimagentryutil::isa::IsKindHeaderPathProvider;
|
||||
|
||||
use failure::Fallible as Result;
|
||||
|
||||
use diaryid::DiaryId;
|
||||
use diaryid::FromStoreId;
|
||||
use error::Result;
|
||||
|
||||
provide_kindflag_path!(pub IsDiaryEntry, "diary.is_diary_entry");
|
||||
|
||||
|
|
|
@ -1,92 +0,0 @@
|
|||
//
|
||||
// imag - the personal information management suite for the commandline
|
||||
// Copyright (C) 2015-2018 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
|
||||
//
|
||||
|
||||
error_chain! {
|
||||
types {
|
||||
DiaryError, DiaryErrorKind, ResultExt, Result;
|
||||
}
|
||||
|
||||
foreign_links {
|
||||
Io(::std::io::Error);
|
||||
}
|
||||
|
||||
links {
|
||||
StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind);
|
||||
EntryUtilError(::libimagentryutil::error::EntryUtilError, ::libimagentryutil::error::EntryUtilErrorKind);
|
||||
}
|
||||
|
||||
errors {
|
||||
StoreWriteError {
|
||||
description("Error writing store")
|
||||
display("Error writing store")
|
||||
}
|
||||
|
||||
StoreReadError {
|
||||
description("Error reading store")
|
||||
display("Error reading store")
|
||||
}
|
||||
|
||||
CannotFindDiary {
|
||||
description("Cannot find diary")
|
||||
display("Cannot find diary")
|
||||
}
|
||||
|
||||
CannotCreateNote {
|
||||
description("Cannot create Note object for diary entry")
|
||||
display("Cannot create Note object for diary entry")
|
||||
}
|
||||
|
||||
DiaryEditError {
|
||||
description("Cannot edit diary entry")
|
||||
display("Cannot edit diary entry")
|
||||
}
|
||||
|
||||
PathConversionError {
|
||||
description("Error while converting paths internally")
|
||||
display("Error while converting paths internally")
|
||||
}
|
||||
|
||||
EntryNotInDiary {
|
||||
description("Entry not in Diary")
|
||||
display("Entry not in Diary")
|
||||
}
|
||||
|
||||
IOError {
|
||||
description("IO Error")
|
||||
display("IO Error")
|
||||
}
|
||||
|
||||
ViewError {
|
||||
description("Error viewing diary entry")
|
||||
display("Error viewing diary entry")
|
||||
}
|
||||
|
||||
IdParseError {
|
||||
description("Error while parsing ID")
|
||||
display("Error while parsing ID")
|
||||
}
|
||||
|
||||
DiaryNameFindingError {
|
||||
description("Error while finding a diary name")
|
||||
display("Error while finding a diary name")
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -26,10 +26,9 @@ use libimagstore::storeid::StoreIdIterator;
|
|||
use libimagstore::storeid::StoreId;
|
||||
|
||||
use is_in_diary::IsInDiary;
|
||||
use error::DiaryErrorKind as DEK;
|
||||
use error::DiaryError as DE;
|
||||
use error::ResultExt;
|
||||
use error::Result;
|
||||
use failure::Fallible as Result;
|
||||
use failure::Error;
|
||||
use failure::err_msg;
|
||||
|
||||
/// A iterator for iterating over diary entries
|
||||
pub struct DiaryEntryIterator {
|
||||
|
@ -109,7 +108,7 @@ impl Iterator for DiaryEntryIterator {
|
|||
loop {
|
||||
match self.iter.next() {
|
||||
None => return None,
|
||||
Some(Err(e)) => return Some(Err(e).map_err(DE::from)),
|
||||
Some(Err(e)) => return Some(Err(e)),
|
||||
Some(Ok(s)) => {
|
||||
debug!("Next element: {:?}", s);
|
||||
if Filter::filter(self, &s) {
|
||||
|
@ -143,16 +142,15 @@ impl Iterator for DiaryNameIterator {
|
|||
fn next(&mut self) -> Option<Self::Item> {
|
||||
while let Some(next) = self.0.next() {
|
||||
match next {
|
||||
Err(e) => return Some(Err(e).map_err(DE::from)),
|
||||
Err(e) => return Some(Err(e)),
|
||||
Ok(next) => if next.is_in_collection(&["diary"]) {
|
||||
return Some(next
|
||||
.to_str()
|
||||
.chain_err(|| DEK::DiaryNameFindingError)
|
||||
.and_then(|s| {
|
||||
s.split("diary/")
|
||||
.nth(1)
|
||||
.and_then(|n| n.split("/").nth(0).map(String::from))
|
||||
.ok_or(DE::from_kind(DEK::DiaryNameFindingError))
|
||||
.ok_or_else(|| Error::from(err_msg("Error finding diary name")))
|
||||
}));
|
||||
},
|
||||
}
|
||||
|
|
|
@ -40,7 +40,7 @@ extern crate chrono;
|
|||
extern crate toml;
|
||||
extern crate toml_query;
|
||||
extern crate itertools;
|
||||
#[macro_use] extern crate error_chain;
|
||||
extern crate failure;
|
||||
extern crate filters;
|
||||
|
||||
#[macro_use] extern crate libimagstore;
|
||||
|
@ -53,7 +53,6 @@ extern crate libimagrt;
|
|||
module_entry_path_mod!("diary");
|
||||
|
||||
pub mod config;
|
||||
pub mod error;
|
||||
pub mod diaryid;
|
||||
pub mod diary;
|
||||
pub mod is_in_diary;
|
||||
|
|
|
@ -22,11 +22,13 @@
|
|||
use std::io::Write;
|
||||
use std::ops::Deref;
|
||||
|
||||
use failure::Fallible as Result;
|
||||
use failure::ResultExt;
|
||||
use failure::err_msg;
|
||||
use failure::Error;
|
||||
|
||||
use libimagstore::store::Entry;
|
||||
use libimagentryview::viewer::Viewer;
|
||||
use libimagentryview::error::ViewErrorKind as VEK;
|
||||
use libimagentryview::error::ResultExt;
|
||||
use libimagentryview::error::Result as ViewResult;
|
||||
use libimagentryview::builtin::plain::PlainViewer;
|
||||
use entry::DiaryEntry;
|
||||
|
||||
|
@ -51,7 +53,7 @@ impl DiaryViewer {
|
|||
|
||||
impl Viewer for DiaryViewer {
|
||||
|
||||
fn view_entry<W>(&self, e: &Entry, sink: &mut W) -> ViewResult<()>
|
||||
fn view_entry<W>(&self, e: &Entry, sink: &mut W) -> Result<()>
|
||||
where W: Write
|
||||
{
|
||||
self.0.view_entry(e, sink)
|
||||
|
@ -59,14 +61,20 @@ impl Viewer for DiaryViewer {
|
|||
|
||||
/// View all entries from the iterator, or stop immediately if an error occurs, returning that
|
||||
/// error.
|
||||
fn view_entries<I, E, W>(&self, entries: I, sink: &mut W) -> ViewResult<()>
|
||||
fn view_entries<I, E, W>(&self, entries: I, sink: &mut W) -> Result<()>
|
||||
where I: Iterator<Item = E>,
|
||||
E: Deref<Target = Entry>,
|
||||
W: Write
|
||||
{
|
||||
let mut entries = entries
|
||||
.map(|e| e.deref().diary_id().map(|id| (id, e)).chain_err(|| VEK::ViewError))
|
||||
.collect::<ViewResult<Vec<_>>>()?;
|
||||
.map(|e| {
|
||||
e.deref()
|
||||
.diary_id()
|
||||
.map(|id| (id, e))
|
||||
.context(err_msg("View error"))
|
||||
.map_err(Error::from)
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
|
||||
entries.sort_by_key(|&(ref id, _)| {
|
||||
[id.year() as u32, id.month(), id.day(), id.hour(), id.minute(), id.second()]
|
||||
|
|
Loading…
Reference in a new issue