2017-05-31 16:29:22 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2018-02-07 01:48:53 +00:00
|
|
|
// Copyright (C) 2015-2018 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2017-05-31 16:29:22 +00:00
|
|
|
//
|
|
|
|
// 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::path::PathBuf;
|
|
|
|
|
|
|
|
use toml_query::insert::TomlValueInsertExt;
|
2018-01-12 15:32:18 +00:00
|
|
|
use toml_query::read::TomlValueReadTypeExt;
|
2017-05-31 16:29:22 +00:00
|
|
|
use toml::Value;
|
|
|
|
|
|
|
|
use libimagstore::store::Store;
|
|
|
|
use libimagstore::store::FileLockEntry;
|
|
|
|
use libimagstore::storeid::StoreId;
|
|
|
|
use libimagstore::storeid::StoreIdIterator;
|
|
|
|
|
|
|
|
use category::Category;
|
|
|
|
use error::CategoryErrorKind as CEK;
|
2017-09-03 19:28:36 +00:00
|
|
|
use error::CategoryError as CE;
|
2017-09-03 13:55:20 +00:00
|
|
|
use error::ResultExt;
|
2017-09-03 19:39:32 +00:00
|
|
|
use error::Result;
|
2017-05-31 16:29:22 +00:00
|
|
|
|
|
|
|
pub const CATEGORY_REGISTER_NAME_FIELD_PATH : &'static str = "category.register.name";
|
|
|
|
|
|
|
|
/// Extension on the Store to make it a register for categories
|
|
|
|
///
|
|
|
|
/// The register writes files to the
|
|
|
|
pub trait CategoryRegister {
|
|
|
|
|
|
|
|
fn category_exists(&self, name: &str) -> Result<bool>;
|
|
|
|
|
|
|
|
fn create_category(&self, name: &str) -> Result<bool>;
|
|
|
|
|
|
|
|
fn delete_category(&self, name: &str) -> Result<()>;
|
|
|
|
|
|
|
|
fn all_category_names(&self) -> Result<CategoryNameIter>;
|
|
|
|
|
|
|
|
fn get_category_by_name(&self, name: &str) -> Result<Option<FileLockEntry>>;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CategoryRegister for Store {
|
|
|
|
|
|
|
|
/// Check whether a category exists
|
|
|
|
fn category_exists(&self, name: &str) -> Result<bool> {
|
2017-10-30 19:17:21 +00:00
|
|
|
let sid = mk_category_storeid(self.path().clone(), name)?;
|
2017-05-31 16:29:22 +00:00
|
|
|
represents_category(self, sid, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a category
|
|
|
|
///
|
|
|
|
/// Fails if the category already exists (returns false then)
|
|
|
|
fn create_category(&self, name: &str) -> Result<bool> {
|
|
|
|
use libimagstore::error::StoreErrorKind as SEK;
|
|
|
|
|
2017-10-30 19:17:21 +00:00
|
|
|
let sid = mk_category_storeid(self.path().clone(), name)?;
|
2017-05-31 16:29:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
match self.create(sid) {
|
|
|
|
Ok(mut entry) => {
|
|
|
|
let val = Value::String(String::from(name));
|
|
|
|
entry.get_header_mut()
|
|
|
|
.insert(CATEGORY_REGISTER_NAME_FIELD_PATH, val)
|
|
|
|
.map(|opt| if opt.is_none() {
|
|
|
|
debug!("Setting category header worked")
|
|
|
|
} else {
|
|
|
|
warn!("Setting category header replaced existing value: {:?}", opt);
|
|
|
|
})
|
|
|
|
.map(|_| true)
|
2017-09-03 13:55:20 +00:00
|
|
|
.chain_err(|| CEK::HeaderWriteError)
|
|
|
|
.chain_err(|| CEK::StoreWriteError)
|
2017-05-31 16:29:22 +00:00
|
|
|
}
|
2017-09-09 19:38:59 +00:00
|
|
|
Err(store_error) => if is_match!(store_error.kind(), &SEK::EntryAlreadyExists(_)) {
|
2017-05-31 16:29:22 +00:00
|
|
|
Ok(false)
|
|
|
|
} else {
|
2017-09-03 13:55:20 +00:00
|
|
|
Err(store_error).chain_err(|| CEK::StoreWriteError)
|
2017-05-31 16:29:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Delete a category
|
|
|
|
fn delete_category(&self, name: &str) -> Result<()> {
|
2017-10-30 19:17:21 +00:00
|
|
|
let sid = mk_category_storeid(self.path().clone(), name)?;
|
2017-05-31 16:29:22 +00:00
|
|
|
|
2017-09-03 13:55:20 +00:00
|
|
|
self.delete(sid).chain_err(|| CEK::StoreWriteError)
|
2017-05-31 16:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get all category names
|
|
|
|
fn all_category_names(&self) -> Result<CategoryNameIter> {
|
2018-04-22 11:35:35 +00:00
|
|
|
Ok(CategoryNameIter::new(self, self.entries()?.without_store()))
|
2017-05-31 16:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a category by its name
|
|
|
|
///
|
|
|
|
/// Returns the FileLockEntry which represents the category, so one can link to it and use it
|
|
|
|
/// like a normal file in the store (which is exactly what it is).
|
|
|
|
fn get_category_by_name(&self, name: &str) -> Result<Option<FileLockEntry>> {
|
2017-10-30 19:17:21 +00:00
|
|
|
let sid = mk_category_storeid(self.path().clone(), name)?;
|
2017-05-31 16:29:22 +00:00
|
|
|
|
|
|
|
self.get(sid)
|
2017-09-03 13:55:20 +00:00
|
|
|
.chain_err(|| CEK::StoreWriteError)
|
2017-05-31 16:29:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 19:48:55 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
extern crate env_logger;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
use libimagstore::store::Store;
|
|
|
|
|
|
|
|
pub fn get_store() -> Store {
|
|
|
|
use libimagstore::store::InMemoryFileAbstraction;
|
|
|
|
let backend = Box::new(InMemoryFileAbstraction::new());
|
2017-12-22 10:24:04 +00:00
|
|
|
Store::new_with_backend(PathBuf::from("/"), &None, backend).unwrap()
|
2017-06-07 19:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_non_existing_category_exists() {
|
|
|
|
let exists = get_store().category_exists("nonexistent");
|
|
|
|
|
|
|
|
assert!(exists.is_ok(), format!("Expected Ok(_), got: {:?}", exists));
|
|
|
|
let exists = exists.unwrap();
|
|
|
|
|
|
|
|
assert!(!exists);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_creating_category() {
|
|
|
|
let category_name = "examplecategory";
|
|
|
|
let store = get_store();
|
|
|
|
let res = store.create_category(category_name);
|
|
|
|
|
|
|
|
assert!(res.is_ok(), format!("Expected Ok(_), got: {:?}", res));
|
|
|
|
let res = res.unwrap();
|
|
|
|
assert!(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_creating_category_creates_store_entry() {
|
|
|
|
let category_name = "examplecategory";
|
|
|
|
let store = get_store();
|
|
|
|
|
|
|
|
let res = store.create_category(category_name);
|
|
|
|
|
|
|
|
assert!(res.is_ok(), format!("Expected Ok(_), got: {:?}", res));
|
|
|
|
let res = res.unwrap();
|
|
|
|
assert!(res);
|
|
|
|
|
|
|
|
let category = store.get(PathBuf::from(format!("category/{}", category_name)));
|
|
|
|
|
|
|
|
assert!(category.is_ok(), format!("Expected Ok(_), got: {:?}", category));
|
|
|
|
let category = category.unwrap();
|
|
|
|
|
|
|
|
assert!(category.is_some());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_creating_category_creates_store_entry_with_header_field_set() {
|
2017-12-07 21:07:01 +00:00
|
|
|
let _ = env_logger::try_init();
|
2017-06-07 19:48:55 +00:00
|
|
|
let category_name = "examplecategory";
|
|
|
|
let store = get_store();
|
|
|
|
let res = store.create_category(category_name);
|
|
|
|
|
|
|
|
assert!(res.is_ok(), format!("Expected Ok(_), got: {:?}", res));
|
|
|
|
let res = res.unwrap();
|
|
|
|
assert!(res);
|
|
|
|
|
|
|
|
let id = PathBuf::from(format!("category/{}", category_name));
|
|
|
|
println!("Trying: {:?}", id);
|
|
|
|
let category = store.get(id);
|
|
|
|
|
|
|
|
assert!(category.is_ok(), format!("Expected Ok(_), got: {:?}", category));
|
|
|
|
let category = category.unwrap();
|
|
|
|
|
|
|
|
assert!(category.is_some());
|
|
|
|
let category = category.unwrap();
|
|
|
|
|
2018-01-12 15:32:18 +00:00
|
|
|
let header_field = category.get_header().read_string(CATEGORY_REGISTER_NAME_FIELD_PATH);
|
2017-06-07 19:48:55 +00:00
|
|
|
assert!(header_field.is_ok(), format!("Expected Ok(_), got: {:?}", header_field));
|
|
|
|
let header_field = header_field.unwrap();
|
|
|
|
|
2017-07-09 19:51:26 +00:00
|
|
|
match header_field {
|
2018-01-12 15:32:18 +00:00
|
|
|
Some(ref s) => assert_eq!(category_name, s),
|
|
|
|
None => assert!(false, "Header field not present"),
|
2017-06-07 19:48:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 16:29:22 +00:00
|
|
|
#[inline]
|
|
|
|
fn mk_category_storeid(base: PathBuf, s: &str) -> Result<StoreId> {
|
|
|
|
use libimagstore::storeid::IntoStoreId;
|
|
|
|
::module_path::ModuleEntryPath::new(s)
|
|
|
|
.into_storeid()
|
|
|
|
.map(|id| id.with_base(base))
|
2017-09-03 13:55:20 +00:00
|
|
|
.chain_err(|| CEK::StoreIdHandlingError)
|
2017-05-31 16:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn represents_category(store: &Store, sid: StoreId, name: &str) -> Result<bool> {
|
|
|
|
sid.exists()
|
2017-09-03 13:55:20 +00:00
|
|
|
.chain_err(|| CEK::StoreIdHandlingError)
|
2017-05-31 16:29:22 +00:00
|
|
|
.and_then(|bl| {
|
|
|
|
if bl {
|
|
|
|
store.get(sid)
|
2017-09-03 13:55:20 +00:00
|
|
|
.chain_err(|| CEK::StoreReadError)
|
2017-05-31 16:29:22 +00:00
|
|
|
.and_then(|fle| {
|
|
|
|
if let Some(fle) = fle {
|
2018-01-04 22:09:30 +00:00
|
|
|
fle.get_header()
|
2018-01-12 15:32:18 +00:00
|
|
|
.read_string(&String::from(CATEGORY_REGISTER_NAME_FIELD_PATH))
|
2018-01-04 22:09:30 +00:00
|
|
|
.chain_err(|| CEK::HeaderReadError)?
|
|
|
|
.ok_or(CE::from_kind(CEK::TypeError))
|
2018-01-12 15:32:18 +00:00
|
|
|
.map(|s| s == name)
|
2017-05-31 16:29:22 +00:00
|
|
|
} else {
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Ok(bl) // false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Iterator for Category names
|
|
|
|
///
|
|
|
|
/// Iterates over Result<Category>
|
|
|
|
///
|
|
|
|
/// # Return values
|
|
|
|
///
|
|
|
|
/// In each iteration, a Option<Result<Category>> is returned. Error kinds are as follows:
|
|
|
|
///
|
|
|
|
/// * CategoryErrorKind::StoreReadError if a name could not be fetched from the store
|
|
|
|
/// * CategoryErrorKind::HeaderReadError if the header of the fetched item couldn't be read
|
|
|
|
/// * CategoryErrorKind::TypeError if the name could not be fetched because it is not a String
|
|
|
|
///
|
|
|
|
pub struct CategoryNameIter<'a>(&'a Store, StoreIdIterator);
|
|
|
|
|
|
|
|
impl<'a> CategoryNameIter<'a> {
|
|
|
|
|
|
|
|
fn new(store: &'a Store, sidit: StoreIdIterator) -> CategoryNameIter<'a> {
|
|
|
|
CategoryNameIter(store, sidit)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for CategoryNameIter<'a> {
|
|
|
|
type Item = Result<Category>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
// TODO: Optimize me with lazy_static
|
2018-01-21 20:47:56 +00:00
|
|
|
let query = CATEGORY_REGISTER_NAME_FIELD_PATH;
|
2017-05-31 16:29:22 +00:00
|
|
|
|
2018-04-22 11:35:35 +00:00
|
|
|
while let Some(sid) = self.1.next() {
|
|
|
|
if sid.is_in_collection(&["category"]) {
|
|
|
|
let func = |store: &Store| { // hack for returning Some(Result<_, _>)
|
|
|
|
store
|
|
|
|
.get(sid)?
|
|
|
|
.ok_or_else(|| CE::from_kind(CEK::StoreReadError))?
|
|
|
|
.get_header()
|
|
|
|
.read_string(query)
|
|
|
|
.chain_err(|| CEK::HeaderReadError)?
|
|
|
|
.map(Category::from)
|
|
|
|
.ok_or_else(|| CE::from_kind(CEK::StoreReadError))
|
|
|
|
};
|
|
|
|
|
|
|
|
return Some(func(&self.0))
|
|
|
|
} // else continue
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
2017-05-31 16:29:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|