diff --git a/libimagentrycategory/Cargo.toml b/libimagentrycategory/Cargo.toml index a14799fe..d2439728 100644 --- a/libimagentrycategory/Cargo.toml +++ b/libimagentrycategory/Cargo.toml @@ -14,3 +14,13 @@ repository = "https://github.com/matthiasbeyer/imag" homepage = "http://imag-pim.org" [dependencies] +toml = "0.4" +toml-query = "0.2" +is-match = "0.1" + +[dependencies.libimagerror] +path = "../libimagerror" + +[dependencies.libimagstore] +path = "../libimagstore" + diff --git a/libimagentrycategory/src/category.rs b/libimagentrycategory/src/category.rs new file mode 100644 index 00000000..b47282a0 --- /dev/null +++ b/libimagentrycategory/src/category.rs @@ -0,0 +1,94 @@ +// +// 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 toml_query::insert::TomlValueInsertExt; +use toml_query::read::TomlValueReadExt; +use toml_query::error::ErrorKind as TQEK; +use toml::Value; + +use libimagstore::store::Entry; +use libimagerror::into::IntoError; + +use error::CategoryErrorKind as CEK; +use error::MapErrInto; +use result::Result; + +#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] +pub struct Category(String); + +impl From for Category { + + fn from(s: String) -> Category { + Category(s) + } + +} + +impl Into for Category { + fn into(self) -> String { + self.0 + } +} + +pub trait EntryCategory { + + fn set_category(&mut self, s: Category) -> Result<()>; + fn get_category(&self) -> Result>; + + fn has_category(&self) -> Result; + +} + +impl EntryCategory for Entry { + + fn set_category(&mut self, s: Category) -> Result<()> { + self.get_header_mut() + .insert(&String::from("category.value"), Value::String(s.into())) + .map_err_into(CEK::HeaderWriteError) + .map(|_| ()) + } + + fn get_category(&self) -> Result> { + match self.get_header().read(&String::from("category.value")) { + Err(res) => match res.kind() { + &TQEK::IdentifierNotFoundInDocument(_) => Ok(None), + _ => Err(res), + } + .map_err_into(CEK::HeaderReadError), + + Ok(&Value::String(ref s)) => Ok(Some(s.clone().into())), + Ok(_) => Err(CEK::TypeError.into_error()).map_err_into(CEK::HeaderReadError), + } + } + + fn has_category(&self) -> Result { + let res = self.get_header().read(&String::from("category.value")); + if res.is_err() { + let res = res.unwrap_err(); + match res.kind() { + &TQEK::IdentifierNotFoundInDocument(_) => Ok(false), + _ => Err(res), + } + .map_err_into(CEK::HeaderReadError) + } else { + Ok(true) + } + } + +} diff --git a/libimagentrycategory/src/error.rs b/libimagentrycategory/src/error.rs new file mode 100644 index 00000000..17bc4543 --- /dev/null +++ b/libimagentrycategory/src/error.rs @@ -0,0 +1,31 @@ +// +// 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 +// + +generate_error_module!( + generate_error_types!(CategoryError, CategoryErrorKind, + HeaderReadError => "Header read error", + HeaderWriteError => "Header write error", + TypeError => "Found wrong type in header" + ); +); + +pub use self::error::CategoryError; +pub use self::error::CategoryErrorKind; +pub use self::error::MapErrInto; + diff --git a/libimagentrycategory/src/lib.rs b/libimagentrycategory/src/lib.rs index cdfbe1aa..8a9c867a 100644 --- a/libimagentrycategory/src/lib.rs +++ b/libimagentrycategory/src/lib.rs @@ -1,6 +1,30 @@ -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - } -} +// +// 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 +// + +extern crate toml_query; +extern crate toml; + +#[macro_use] +extern crate libimagerror; +extern crate libimagstore; + +pub mod category; +pub mod error; +pub mod result; + diff --git a/libimagentrycategory/src/result.rs b/libimagentrycategory/src/result.rs new file mode 100644 index 00000000..517d0e96 --- /dev/null +++ b/libimagentrycategory/src/result.rs @@ -0,0 +1,26 @@ + +// +// 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 std::result::Result as RResult; + +use error::CategoryError; + +pub type Result = RResult; +