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

66 lines
1.9 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 toml::Value;
use libimagstore::store::Entry;
2017-08-26 15:53:08 +00:00
use toml_query::read::TomlValueReadTypeExt;
2017-08-26 15:53:08 +00:00
use toml_query::set::TomlValueSetExt;
2016-02-13 17:29:34 +00:00
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
pub trait Note {
fn set_name(&mut self, n: String) -> Result<()>;
fn get_name(&self) -> Result<String>;
fn set_text(&mut self, n: String);
fn get_text(&self) -> &String;
2016-02-13 17:29:34 +00:00
}
impl Note for Entry {
2016-02-13 17:29:34 +00:00
fn set_name(&mut self, n: String) -> Result<()> {
self.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(|_| ())
}
fn get_name(&self) -> Result<String> {
self.get_header()
.read_string("note.name")
.chain_err(|| NEK::StoreReadError)?
.ok_or(NE::from_kind(NEK::HeaderTypeError))
2016-02-13 17:29:34 +00:00
}
fn set_text(&mut self, n: String) {
*self.get_content_mut() = n
2016-03-26 18:37:54 +00:00
}
fn get_text(&self) -> &String {
self.get_content()
2016-03-19 18:22:00 +00:00
}
}