2017-02-04 19:56:30 +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-02-04 19:56:30 +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
|
|
|
|
//
|
|
|
|
|
2018-02-20 18:55:06 +00:00
|
|
|
use std::fmt::Write;
|
|
|
|
|
2017-06-17 22:50:18 +00:00
|
|
|
use toml::Value;
|
|
|
|
|
|
|
|
use store::Result;
|
2017-08-26 15:48:11 +00:00
|
|
|
use store::Header;
|
2017-06-17 22:50:18 +00:00
|
|
|
|
2017-02-04 19:56:30 +00:00
|
|
|
#[cfg(feature = "early-panic")]
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! if_cfg_panic {
|
|
|
|
() => { panic!() };
|
|
|
|
($msg:expr) => { panic!($msg) };
|
2017-11-18 11:28:20 +00:00
|
|
|
($fmt:expr, $($arg:tt)+) => { panic!($fmt, $($arg),+) };
|
2017-02-04 19:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "early-panic"))]
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! if_cfg_panic {
|
|
|
|
() => { };
|
|
|
|
($msg:expr) => { };
|
|
|
|
($fmt:expr, $($arg:tt)+) => { };
|
|
|
|
}
|
|
|
|
|
2017-06-17 22:50:18 +00:00
|
|
|
pub fn entry_buffer_to_header_content(buf: &str) -> Result<(Value, String)> {
|
|
|
|
debug!("Building entry from string");
|
2018-02-20 18:55:06 +00:00
|
|
|
let mut header = String::new();
|
|
|
|
let mut content = String::new();
|
|
|
|
let mut header_consumed = false;
|
2017-06-17 22:50:18 +00:00
|
|
|
|
2018-03-02 20:48:17 +00:00
|
|
|
let mut iter = buf.lines().skip(1).peekable(); // the first line is "---"
|
|
|
|
|
|
|
|
while let Some(line) = iter.next() {
|
2018-02-20 18:55:06 +00:00
|
|
|
if line == "---" {
|
|
|
|
header_consumed = true;
|
|
|
|
// do not further process the line
|
|
|
|
} else {
|
|
|
|
if !header_consumed {
|
|
|
|
let _ = writeln!(header, "{}", line)?;
|
|
|
|
} else {
|
2018-03-02 20:48:17 +00:00
|
|
|
if iter.peek().is_some() {
|
|
|
|
let _ = writeln!(content, "{}", line)?;
|
|
|
|
} else {
|
|
|
|
let _ = write!(content, "{}", line)?;
|
|
|
|
}
|
2018-02-20 18:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-17 22:50:18 +00:00
|
|
|
|
2018-02-20 18:55:06 +00:00
|
|
|
Ok((Value::parse(&header)?, String::from(content)))
|
2017-06-17 22:50:18 +00:00
|
|
|
}
|
|
|
|
|