2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// 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;
|
2016-08-09 11:52:41 +00:00
|
|
|
use libimagentryedit::edit::Edit;
|
|
|
|
use libimagentryedit::result::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;
|
2016-11-15 20:30:39 +00:00
|
|
|
use libimagstore::toml_ext::TomlValueExt;
|
2016-05-03 21:10:32 +00:00
|
|
|
use libimagentrytag::tag::{Tag, TagSlice};
|
2016-04-16 19:58:19 +00:00
|
|
|
use libimagentrytag::tagable::Tagable;
|
|
|
|
use libimagentrytag::result::Result as TagResult;
|
2016-02-13 17:29:34 +00:00
|
|
|
|
|
|
|
use module_path::ModuleEntryPath;
|
|
|
|
use result::Result;
|
|
|
|
use error::NoteError as NE;
|
|
|
|
use error::NoteErrorKind as NEK;
|
2016-08-25 17:56:47 +00:00
|
|
|
use error::MapErrInto;
|
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 = {
|
2016-08-25 17:56:47 +00:00
|
|
|
let mut lockentry = try!(ModuleEntryPath::new(name.clone())
|
|
|
|
.into_storeid()
|
|
|
|
.and_then(|id| store.create(id))
|
|
|
|
.map_err_into(NEK::StoreWriteError));
|
2016-02-13 17:29:34 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut entry = lockentry.deref_mut();
|
|
|
|
|
2016-03-21 17:53:53 +00:00
|
|
|
{
|
|
|
|
let mut header = entry.get_header_mut();
|
|
|
|
let setres = header.set("note", Value::Table(BTreeMap::new()));
|
|
|
|
if setres.is_err() {
|
|
|
|
let kind = NEK::StoreWriteError;
|
2016-04-17 19:08:37 +00:00
|
|
|
return Err(NE::new(kind, Some(Box::new(setres.unwrap_err()))));
|
2016-03-21 17:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let setres = header.set("note.name", Value::String(name));
|
|
|
|
if setres.is_err() {
|
|
|
|
let kind = NEK::StoreWriteError;
|
2016-04-17 19:08:37 +00:00
|
|
|
return Err(NE::new(kind, Some(Box::new(setres.unwrap_err()))));
|
2016-03-21 17:53:53 +00:00
|
|
|
}
|
2016-02-13 17:29:34 +00:00
|
|
|
}
|
2016-03-21 17:53:53 +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<()> {
|
2016-04-21 11:21:47 +00:00
|
|
|
let mut header = self.entry.get_header_mut();
|
2016-02-13 17:29:34 +00:00
|
|
|
header.set("note.name", Value::String(n))
|
|
|
|
.map_err(|e| NE::new(NEK::StoreWriteError, Some(Box::new(e))))
|
|
|
|
.map(|_| ())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_name(&self) -> Result<String> {
|
2016-04-21 11:21:47 +00:00
|
|
|
let header = self.entry.get_header();
|
2016-02-13 17:29:34 +00:00
|
|
|
match header.read("note.name") {
|
|
|
|
Ok(Some(Value::String(s))) => Ok(String::from(s)),
|
|
|
|
Ok(_) => {
|
|
|
|
let e = NE::new(NEK::HeaderTypeError, None);
|
|
|
|
Err(NE::new(NEK::StoreReadError, Some(Box::new(e))))
|
|
|
|
},
|
|
|
|
Err(e) => Err(NE::new(NEK::StoreReadError, Some(Box::new(e))))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_text(&mut self, n: String) {
|
2016-04-21 11:21:47 +00:00
|
|
|
*self.entry.get_content_mut() = n
|
2016-02-13 17:29:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_text(&self) -> &String {
|
2016-04-21 11:21:47 +00:00
|
|
|
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<()> {
|
2016-08-25 17:56:47 +00:00
|
|
|
ModuleEntryPath::new(name)
|
|
|
|
.into_storeid()
|
|
|
|
.and_then(|id| store.delete(id))
|
|
|
|
.map_err_into(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> {
|
2016-08-25 17:56:47 +00:00
|
|
|
ModuleEntryPath::new(name)
|
|
|
|
.into_storeid()
|
|
|
|
.and_then(|id| store.retrieve(id))
|
|
|
|
.map_err_into(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>> {
|
2016-08-25 17:56:47 +00:00
|
|
|
ModuleEntryPath::new(name)
|
|
|
|
.into_storeid()
|
|
|
|
.and_then(|id| store.get(id))
|
|
|
|
.map_err_into(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))
|
|
|
|
.map_err(|e| NE::new(NEK::StoreReadError, Some(Box::new(e))))
|
|
|
|
}
|
|
|
|
|
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-02-13 17:29:34 +00:00
|
|
|
impl<'a> Tagable for Note<'a> {
|
|
|
|
|
|
|
|
fn get_tags(&self) -> TagResult<Vec<Tag>> {
|
2016-04-21 11:21:47 +00:00
|
|
|
self.entry.get_tags()
|
2016-02-13 17:29:34 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 21:10:32 +00:00
|
|
|
fn set_tags(&mut self, ts: &[Tag]) -> TagResult<()> {
|
2016-04-21 11:21:47 +00:00
|
|
|
self.entry.set_tags(ts)
|
2016-02-13 17:29:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn add_tag(&mut self, t: Tag) -> TagResult<()> {
|
2016-04-21 11:21:47 +00:00
|
|
|
self.entry.add_tag(t)
|
2016-02-13 17:29:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn remove_tag(&mut self, t: Tag) -> TagResult<()> {
|
2016-04-21 11:21:47 +00:00
|
|
|
self.entry.remove_tag(t)
|
2016-02-13 17:29:34 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 21:10:32 +00:00
|
|
|
fn has_tag(&self, t: TagSlice) -> TagResult<bool> {
|
2016-04-21 11:21:47 +00:00
|
|
|
self.entry.has_tag(t)
|
2016-02-13 17:29:34 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 21:10:32 +00:00
|
|
|
fn has_tags(&self, ts: &[Tag]) -> TagResult<bool> {
|
2016-04-21 11:21:47 +00:00
|
|
|
self.entry.has_tags(ts)
|
2016-02-13 17:29:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-19 18:22:00 +00:00
|
|
|
trait FromStoreId {
|
2016-05-03 21:10:32 +00:00
|
|
|
fn from_storeid(&Store, StoreId) -> Result<Note>;
|
2016-03-19 18:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FromStoreId for Note<'a> {
|
|
|
|
|
2016-05-03 21:10:32 +00:00
|
|
|
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) {
|
|
|
|
Err(e) => Err(NE::new(NEK::StoreReadError, Some(Box::new(e)))),
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|