Merge pull request #1307 from matthiasbeyer/libimagstore/entry-parse-fix
Rewrite entry parsing algorithm
This commit is contained in:
commit
1521005e79
5 changed files with 21 additions and 25 deletions
|
@ -58,6 +58,8 @@ This section contains the changelog from the last release to the next release.
|
||||||
default.
|
default.
|
||||||
* `libimagerror` printed errors with `write!()` rather than `writeln!()`
|
* `libimagerror` printed errors with `write!()` rather than `writeln!()`
|
||||||
when tracing.
|
when tracing.
|
||||||
|
* A parsing error in `libimagstore`, which caused the parsing of entries
|
||||||
|
with a line "---" in the content part to fail, was fixed.
|
||||||
|
|
||||||
|
|
||||||
## 0.6.1
|
## 0.6.1
|
||||||
|
|
|
@ -21,7 +21,6 @@ maintenance = { status = "actively-developed" }
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
glob = "0.2.11"
|
glob = "0.2.11"
|
||||||
lazy_static = "0.2"
|
|
||||||
log = "0.4.0"
|
log = "0.4.0"
|
||||||
regex = "0.2"
|
regex = "0.2"
|
||||||
semver = "0.8"
|
semver = "0.8"
|
||||||
|
|
|
@ -28,6 +28,7 @@ error_chain! {
|
||||||
|
|
||||||
foreign_links {
|
foreign_links {
|
||||||
Io(::std::io::Error);
|
Io(::std::io::Error);
|
||||||
|
Fmt(::std::fmt::Error);
|
||||||
TomlDeserError(::toml::de::Error);
|
TomlDeserError(::toml::de::Error);
|
||||||
GlobPatternError(::glob::PatternError);
|
GlobPatternError(::glob::PatternError);
|
||||||
TomlQueryError(::toml_query::error::Error);
|
TomlQueryError(::toml_query::error::Error);
|
||||||
|
|
|
@ -37,7 +37,6 @@
|
||||||
|
|
||||||
#[macro_use] extern crate log;
|
#[macro_use] extern crate log;
|
||||||
extern crate glob;
|
extern crate glob;
|
||||||
#[macro_use] extern crate lazy_static;
|
|
||||||
extern crate regex;
|
extern crate regex;
|
||||||
extern crate toml;
|
extern crate toml;
|
||||||
#[cfg(test)] extern crate tempdir;
|
#[cfg(test)] extern crate tempdir;
|
||||||
|
|
|
@ -17,13 +17,12 @@
|
||||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
//
|
//
|
||||||
|
|
||||||
use regex::Regex;
|
use std::fmt::Write;
|
||||||
|
|
||||||
use toml::Value;
|
use toml::Value;
|
||||||
|
|
||||||
use store::Result;
|
use store::Result;
|
||||||
use store::Header;
|
use store::Header;
|
||||||
use error::StoreErrorKind as SEK;
|
|
||||||
use error::StoreError as SE;
|
|
||||||
|
|
||||||
#[cfg(feature = "early-panic")]
|
#[cfg(feature = "early-panic")]
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
|
@ -43,27 +42,23 @@ macro_rules! if_cfg_panic {
|
||||||
|
|
||||||
pub fn entry_buffer_to_header_content(buf: &str) -> Result<(Value, String)> {
|
pub fn entry_buffer_to_header_content(buf: &str) -> Result<(Value, String)> {
|
||||||
debug!("Building entry from string");
|
debug!("Building entry from string");
|
||||||
lazy_static! {
|
let mut header = String::new();
|
||||||
static ref RE: Regex = Regex::new(r"(?smx)
|
let mut content = String::new();
|
||||||
^---$
|
let mut header_consumed = false;
|
||||||
(?P<header>.*) # Header
|
|
||||||
^---$\n
|
for line in buf.lines().skip(1) { // the first line is "---"
|
||||||
(?P<content>.*) # Content
|
if line == "---" {
|
||||||
").unwrap();
|
header_consumed = true;
|
||||||
|
// do not further process the line
|
||||||
|
} else {
|
||||||
|
if !header_consumed {
|
||||||
|
let _ = writeln!(header, "{}", line)?;
|
||||||
|
} else {
|
||||||
|
let _ = write!(content, "{}", line)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let matches = match RE.captures(buf) {
|
Ok((Value::parse(&header)?, String::from(content)))
|
||||||
None => return Err(SE::from_kind(SEK::MalformedEntry)),
|
|
||||||
Some(s) => s,
|
|
||||||
};
|
|
||||||
|
|
||||||
let header = match matches.name("header") {
|
|
||||||
None => return Err(SE::from_kind(SEK::MalformedEntry)),
|
|
||||||
Some(s) => s
|
|
||||||
};
|
|
||||||
|
|
||||||
let content = matches.name("content").map(|r| r.as_str()).unwrap_or("");
|
|
||||||
|
|
||||||
Ok((Value::parse(header.as_str())?, String::from(content)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue