2017-09-14 17:43:51 +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-09-14 17:43:51 +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 toml::Value;
|
|
|
|
|
|
|
|
use libimagstore::storeid::IntoStoreId;
|
|
|
|
use libimagstore::store::FileLockEntry;
|
|
|
|
use libimagstore::store::Store;
|
|
|
|
|
2018-02-06 19:39:05 +00:00
|
|
|
use toml_query::insert::TomlValueInsertExt;
|
2017-09-14 17:43:51 +00:00
|
|
|
|
|
|
|
use module_path::ModuleEntryPath;
|
|
|
|
use error::Result;
|
2018-03-12 18:21:48 +00:00
|
|
|
use error::NoteError as NE;
|
2017-09-14 17:43:51 +00:00
|
|
|
use iter::*;
|
|
|
|
|
|
|
|
pub trait NoteStore<'a> {
|
|
|
|
fn new_note(&'a self, name: String, text: String) -> Result<FileLockEntry<'a>>;
|
|
|
|
fn delete_note(&'a self, name: String) -> Result<()>;
|
|
|
|
fn retrieve_note(&'a self, name: String) -> Result<FileLockEntry<'a>>;
|
|
|
|
fn get_note(&'a self, name: String) -> Result<Option<FileLockEntry<'a>>>;
|
|
|
|
fn all_notes(&'a self) -> Result<NoteIterator>;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a> NoteStore<'a> for Store {
|
|
|
|
|
|
|
|
fn new_note(&'a self, name: String, text: String) -> Result<FileLockEntry<'a>> {
|
|
|
|
use std::ops::DerefMut;
|
|
|
|
|
|
|
|
debug!("Creating new Note: '{}'", name);
|
|
|
|
let fle = {
|
2017-10-30 19:19:29 +00:00
|
|
|
let mut lockentry = ModuleEntryPath::new(name.clone())
|
2017-09-14 17:43:51 +00:00
|
|
|
.into_storeid()
|
2018-03-12 18:21:48 +00:00
|
|
|
.and_then(|id| self.create(id))?;
|
2017-09-14 17:43:51 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
let entry = lockentry.deref_mut();
|
2018-02-06 19:39:05 +00:00
|
|
|
entry.get_header_mut().insert("note.name", Value::String(name))?;
|
2017-09-14 17:43:51 +00:00
|
|
|
*entry.get_content_mut() = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
lockentry
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(fle)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn delete_note(&'a self, name: String) -> Result<()> {
|
|
|
|
ModuleEntryPath::new(name)
|
|
|
|
.into_storeid()
|
|
|
|
.and_then(|id| self.delete(id))
|
2018-03-12 18:21:48 +00:00
|
|
|
.map_err(NE::from)
|
2017-09-14 17:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn retrieve_note(&'a self, name: String) -> Result<FileLockEntry<'a>> {
|
|
|
|
ModuleEntryPath::new(name)
|
|
|
|
.into_storeid()
|
|
|
|
.and_then(|id| self.retrieve(id))
|
2018-03-12 18:21:48 +00:00
|
|
|
.map_err(NE::from)
|
2017-09-14 17:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_note(&'a self, name: String) -> Result<Option<FileLockEntry<'a>>> {
|
|
|
|
ModuleEntryPath::new(name)
|
|
|
|
.into_storeid()
|
|
|
|
.and_then(|id| self.get(id))
|
2018-03-12 18:21:48 +00:00
|
|
|
.map_err(NE::from)
|
2017-09-14 17:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn all_notes(&'a self) -> Result<NoteIterator> {
|
|
|
|
self.retrieve_for_module("notes")
|
|
|
|
.map(NoteIterator::new)
|
2018-03-12 18:21:48 +00:00
|
|
|
.map_err(NE::from)
|
2017-09-14 17:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|