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;
|
2018-10-30 17:40:50 +00:00
|
|
|
use failure::Fallible as Result;
|
|
|
|
use failure::ResultExt;
|
2017-06-17 22:50:18 +00:00
|
|
|
|
2018-10-30 17:40:50 +00:00
|
|
|
use libimagerror::errors::ErrorMsg as EM;
|
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)> {
|
2018-10-30 17:40:50 +00:00
|
|
|
|
2017-06-17 22:50:18 +00:00
|
|
|
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-04-24 19:10:43 +00:00
|
|
|
let mut iter = buf.split('\n').skip(1).peekable(); // the first line is "---"
|
2018-03-02 20:48:17 +00:00
|
|
|
|
|
|
|
while let Some(line) = iter.next() {
|
2018-03-10 13:53:15 +00:00
|
|
|
if line == "---" && !header_consumed {
|
2018-02-20 18:55:06 +00:00
|
|
|
header_consumed = true;
|
|
|
|
// do not further process the line
|
2018-04-24 19:07:10 +00:00
|
|
|
} else if !header_consumed {
|
2018-10-30 17:40:50 +00:00
|
|
|
let _ = writeln!(header, "{}", line).context(EM::FormatError)?;
|
2018-04-24 19:07:10 +00:00
|
|
|
} else if iter.peek().is_some() {
|
2018-10-30 17:40:50 +00:00
|
|
|
let _ = writeln!(content, "{}", line).context(EM::FormatError)?;
|
2018-02-20 18:55:06 +00:00
|
|
|
} else {
|
2018-10-30 17:40:50 +00:00
|
|
|
let _ = write!(content, "{}", line).context(EM::FormatError)?;
|
2018-02-20 18:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-17 22:50:18 +00:00
|
|
|
|
2018-10-30 17:40:50 +00:00
|
|
|
let h = ::toml::de::from_str(&header).context(EM::TomlDeserError)?;
|
|
|
|
Ok((h, content))
|
2017-06-17 22:50:18 +00:00
|
|
|
}
|
|
|
|
|
2018-03-10 13:53:15 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2018-11-01 20:35:40 +00:00
|
|
|
extern crate env_logger;
|
|
|
|
|
2018-03-10 13:53:15 +00:00
|
|
|
use super::entry_buffer_to_header_content;
|
|
|
|
|
2018-11-01 20:35:40 +00:00
|
|
|
fn setup_logging() {
|
|
|
|
let _ = env_logger::try_init();
|
|
|
|
}
|
|
|
|
|
2018-03-10 13:53:15 +00:00
|
|
|
fn mkfile(content: &str) -> String {
|
|
|
|
format!(r#"---
|
|
|
|
[imag]
|
|
|
|
version = '{version}'
|
|
|
|
---
|
|
|
|
{content}"#, version = env!("CARGO_PKG_VERSION"), content = content)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_entry_buffer_to_header_content_1() {
|
|
|
|
let content = "Hai";
|
|
|
|
let file = format!(r#"---
|
|
|
|
[imag]
|
|
|
|
version = '{version}'
|
|
|
|
---
|
|
|
|
{content}"#, version = env!("CARGO_PKG_VERSION"), content = content);
|
|
|
|
|
|
|
|
let res = entry_buffer_to_header_content(&file);
|
|
|
|
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let (_, res_content) = res.unwrap();
|
|
|
|
assert_eq!(res_content, content)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_entry_buffer_to_header_content_2() {
|
2018-11-01 20:35:40 +00:00
|
|
|
setup_logging();
|
2018-03-10 13:53:15 +00:00
|
|
|
let content = r#"Hai
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let file = mkfile(&content);
|
2018-11-01 20:35:40 +00:00
|
|
|
debug!("FILE: <<<{}>>>", file);
|
2018-03-10 13:53:15 +00:00
|
|
|
let res = entry_buffer_to_header_content(&file);
|
|
|
|
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let (_, res_content) = res.unwrap();
|
2018-11-01 20:35:40 +00:00
|
|
|
debug!("CONTENT: <<<{}>>>", res_content);
|
2018-03-10 13:53:15 +00:00
|
|
|
assert_eq!(res_content, content)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_entry_buffer_to_header_content_3() {
|
|
|
|
let content = r#"Hai
|
|
|
|
|
|
|
|
barbar
|
|
|
|
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let file = mkfile(&content);
|
|
|
|
let res = entry_buffer_to_header_content(&file);
|
|
|
|
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let (_, res_content) = res.unwrap();
|
|
|
|
assert_eq!(res_content, content)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_entry_buffer_to_header_content_4() {
|
|
|
|
let content = r#"Hai
|
|
|
|
|
|
|
|
---
|
|
|
|
barbar
|
|
|
|
---
|
|
|
|
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let file = mkfile(&content);
|
|
|
|
let res = entry_buffer_to_header_content(&file);
|
|
|
|
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let (_, res_content) = res.unwrap();
|
|
|
|
assert_eq!(res_content, content)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_entry_buffer_to_header_content_5() {
|
|
|
|
let content = r#"Hai
|
|
|
|
|
|
|
|
---
|
|
|
|
barbar
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let file = mkfile(&content);
|
|
|
|
let res = entry_buffer_to_header_content(&file);
|
|
|
|
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let (_, res_content) = res.unwrap();
|
|
|
|
assert_eq!(res_content, content)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|