Add Task trait for getting UUID from task

This commit is contained in:
Matthias Beyer 2017-09-02 22:03:12 +02:00
parent 72ea21ee1f
commit 39dd9e8d7c
3 changed files with 52 additions and 1 deletions

View file

@ -23,7 +23,10 @@ generate_error_module!(
StoreError => "Store Error",
StoreIdError => "Store Id handling error",
ImportError => "Error importing",
UTF8Error => "Encountered non-UTF8 characters while reading input"
UTF8Error => "Encountered non-UTF8 characters while reading input",
HeaderFieldMissing => "Header field missing",
HeaderTypeError => "Header field type error",
UuidParserError => "Uuid parser error"
);
);

View file

@ -47,5 +47,6 @@ module_entry_path_mod!("todo");
pub mod error;
pub mod result;
pub mod task;
pub mod taskstore;

View file

@ -0,0 +1,47 @@
//
// 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 error::TodoErrorKind as TEK;
use error::MapErrInto;
use result::Result;
use libimagstore::store::Entry;
use libimagerror::into::IntoError;
use uuid::Uuid;
use toml::Value;
use toml_query::read::TomlValueReadExt;
pub trait Task {
fn get_uuid(&self) -> Result<Uuid>;
}
impl Task for Entry {
fn get_uuid(&self) -> Result<Uuid> {
match self.get_header().read("todo.uuid") {
Ok(Some(&Value::String(ref uuid))) => {
Uuid::parse_str(uuid).map_err_into(TEK::UuidParserError)
},
Ok(Some(_)) => Err(TEK::HeaderTypeError.into_error()),
Ok(None) => Err(TEK::HeaderFieldMissing.into_error()),
Err(e) => Err(e).map_err_into(TEK::StoreError),
}
}
}