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

64 lines
1.8 KiB
Rust
Raw Normal View History

//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2019 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;
use libimagerror::errors::ErrorMsg as EM;
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 failure::Fallible as Result;
use failure::Error;
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))
.map_err(Error::from)
2016-02-13 17:29:34 +00:00
.map(|_| ())
}
fn get_name(&self) -> Result<String> {
self.get_header()
.read_string("note.name")?
.ok_or_else(|| Error::from(EM::EntryHeaderTypeError))
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
}
}