imag/lib/domain/libimagnotes/src/note.rs

203 lines
5.5 KiB
Rust
Raw Normal View History

//
// 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
//
2016-02-13 17:29:34 +00:00
use std::collections::BTreeMap;
2016-05-25 14:28:39 +00:00
use std::ops::Deref;
2016-02-13 17:29:34 +00:00
use toml::Value;
2016-03-25 15:38:09 +00:00
use libimagrt::runtime::Runtime;
use libimagentryedit::edit::Edit;
use libimagentryedit::error::Result as EditResult;
2016-02-13 17:29:34 +00:00
use libimagstore::storeid::IntoStoreId;
2016-03-19 18:22:00 +00:00
use libimagstore::storeid::StoreId;
use libimagstore::storeid::StoreIdIterator;
2016-02-13 17:29:34 +00:00
use libimagstore::store::FileLockEntry;
use libimagstore::store::Store;
2017-08-26 15:53:08 +00:00
use toml_query::read::TomlValueReadExt;
use toml_query::set::TomlValueSetExt;
2016-02-13 17:29:34 +00:00
use module_path::ModuleEntryPath;
use error::Result;
2016-02-13 17:29:34 +00:00
use error::NoteErrorKind as NEK;
use error::NoteError as NE;
2017-09-03 13:34:58 +00:00
use error::ResultExt;
2016-02-13 17:29:34 +00:00
2016-04-18 13:58:35 +00:00
#[derive(Debug)]
2016-02-13 17:29:34 +00:00
pub struct Note<'a> {
entry: FileLockEntry<'a>,
}
impl<'a> Note<'a> {
pub fn new(store: &Store, name: String, text: String) -> Result<Note> {
use std::ops::DerefMut;
debug!("Creating new Note: '{}'", name);
let fle = {
let mut lockentry = try!(ModuleEntryPath::new(name.clone())
.into_storeid()
.and_then(|id| store.create(id))
2017-09-03 13:34:58 +00:00
.chain_err(|| NEK::StoreWriteError));
2016-02-13 17:29:34 +00:00
{
let entry = lockentry.deref_mut();
2016-02-13 17:29:34 +00:00
{
let header = entry.get_header_mut();
2017-09-03 13:34:58 +00:00
let _ = header
.set("note", Value::Table(BTreeMap::new()))
.chain_err(|| NEK::StoreWriteError);
let _ = header
.set("note.name", Value::String(name))
.chain_err(|| NEK::StoreWriteError);
2016-02-13 17:29:34 +00:00
}
*entry.get_content_mut() = text;
2016-02-13 17:29:34 +00:00
}
lockentry
};
Ok(Note { entry: fle })
}
pub fn set_name(&mut self, n: String) -> Result<()> {
self.entry
.get_header_mut()
.set("note.name", Value::String(n))
2017-09-03 13:34:58 +00:00
.chain_err(|| NEK::StoreWriteError)
2016-02-13 17:29:34 +00:00
.map(|_| ())
}
pub fn get_name(&self) -> Result<String> {
let header = self.entry.get_header();
2016-02-13 17:29:34 +00:00
match header.read("note.name") {
2017-08-26 15:53:08 +00:00
Ok(Some(&Value::String(ref s))) => Ok(s.clone()),
2017-09-03 13:34:58 +00:00
Ok(_) => {
Err(NE::from_kind(NEK::HeaderTypeError)).chain_err(|| NEK::StoreReadError)
2016-02-13 17:29:34 +00:00
},
2017-09-03 13:34:58 +00:00
Err(e) => Err(e).chain_err(|| NEK::StoreReadError)
2016-02-13 17:29:34 +00:00
}
}
pub fn set_text(&mut self, n: String) {
*self.entry.get_content_mut() = n
2016-02-13 17:29:34 +00:00
}
pub fn get_text(&self) -> &String {
self.entry.get_content()
2016-02-13 17:29:34 +00:00
}
2016-03-19 18:21:13 +00:00
pub fn delete(store: &Store, name: String) -> Result<()> {
ModuleEntryPath::new(name)
.into_storeid()
.and_then(|id| store.delete(id))
2017-09-03 13:34:58 +00:00
.chain_err(|| NEK::StoreWriteError)
2016-03-19 18:21:13 +00:00
}
2016-03-19 18:21:33 +00:00
pub fn retrieve(store: &Store, name: String) -> Result<Note> {
ModuleEntryPath::new(name)
.into_storeid()
.and_then(|id| store.retrieve(id))
2017-09-03 13:34:58 +00:00
.chain_err(|| NEK::StoreWriteError)
2016-03-19 18:21:33 +00:00
.map(|entry| Note { entry: entry })
}
2016-05-28 19:26:05 +00:00
pub fn get(store: &Store, name: String) -> Result<Option<Note>> {
ModuleEntryPath::new(name)
.into_storeid()
.and_then(|id| store.get(id))
2017-09-03 13:34:58 +00:00
.chain_err(|| NEK::StoreWriteError)
.map(|o| o.map(|entry| Note { entry: entry }))
2016-05-28 19:26:05 +00:00
}
2016-03-19 18:22:00 +00:00
pub fn all_notes(store: &Store) -> Result<NoteIterator> {
store.retrieve_for_module("notes")
.map(|iter| NoteIterator::new(store, iter))
2017-09-03 13:34:58 +00:00
.chain_err(|| NEK::StoreReadError)
2016-03-19 18:22:00 +00:00
}
2016-02-13 17:29:34 +00:00
}
2016-03-25 15:38:09 +00:00
impl<'a> Edit for Note<'a> {
fn edit_content(&mut self, rt: &Runtime) -> EditResult<()> {
self.entry.edit_content(rt)
}
}
2016-03-19 18:22:00 +00:00
trait FromStoreId {
fn from_storeid(&Store, StoreId) -> Result<Note>;
2016-03-19 18:22:00 +00:00
}
impl<'a> FromStoreId for Note<'a> {
fn from_storeid(store: &Store, id: StoreId) -> Result<Note> {
2016-03-19 18:22:00 +00:00
debug!("Loading note from storeid: '{:?}'", id);
match store.retrieve(id) {
2017-09-03 13:34:58 +00:00
Err(e) => Err(e).chain_err(|| NEK::StoreReadError),
2016-03-19 18:22:00 +00:00
Ok(entry) => Ok(Note { entry: entry }),
}
}
}
2016-03-26 18:37:54 +00:00
impl<'a> Deref for Note<'a> {
type Target = FileLockEntry<'a>;
fn deref(&self) -> &FileLockEntry<'a> {
&self.entry
}
}
2016-04-18 16:23:47 +00:00
#[derive(Debug)]
2016-03-19 18:22:00 +00:00
pub struct NoteIterator<'a> {
store: &'a Store,
iditer: StoreIdIterator,
}
impl<'a> NoteIterator<'a> {
pub fn new(store: &'a Store, iditer: StoreIdIterator) -> NoteIterator<'a> {
NoteIterator {
store: store,
iditer: iditer,
}
}
}
impl<'a> Iterator for NoteIterator<'a> {
type Item = Result<Note<'a>>;
fn next(&mut self) -> Option<Result<Note<'a>>> {
self.iditer
.next()
.map(|id| Note::from_storeid(self.store, id))
}
}