summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2018-03-02 22:13:04 +0100
committerMatthias Beyer <mail@beyermatthias.de>2018-03-02 22:13:04 +0100
commit8b866c51bf19a4ed4818d847a5ec3a4b90bb7eb8 (patch)
treee3f44936bd17624fc4c7907ed1caa39103340b33
parent9ec09659254c1eeb64596229bce79095a1325dac (diff)
Rewrite entry parsing algorithm
Rewrite without regex crate. The regex approach was broken. If the following _content_ was provided in the entry: foo --- bar The regex approach parsed the header until the "---" in the content. This is, of course, not the way to do that. Now, the parsing is implemented by hand. Should be faster as well, though I don't care about this. This fixes a severe bug. --- This patch was cherry-picked from 9fb5f453fee0d7cc2d4550980f52f833c38ada9c. The changelog was adapted.
-rw-r--r--doc/src/09020-changelog.md2
-rw-r--r--lib/core/libimagstore/Cargo.toml1
-rw-r--r--lib/core/libimagstore/src/error.rs1
-rw-r--r--lib/core/libimagstore/src/lib.rs1
-rw-r--r--lib/core/libimagstore/src/util.rs41
5 files changed, 21 insertions, 25 deletions
diff --git a/doc/src/09020-changelog.md b/doc/src/09020-changelog.md
index f14efc7..04bad33 100644
--- a/doc/src/09020-changelog.md
+++ b/doc/src/09020-changelog.md
@@ -27,6 +27,8 @@ This section contains the changelog from the last release to the next release.
Bugfix release for fixing:
* `imag-diary` did not recognize the "-d DIARY" setting.
+* 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
diff --git a/lib/core/libimagstore/Cargo.toml b/lib/core/libimagstore/Cargo.toml
index 985153f..9031988 100644
--- a/lib/core/libimagstore/Cargo.toml
+++ b/lib/core/libimagstore/Cargo.toml
@@ -21,7 +21,6 @@ maintenance = { status = "actively-developed" }
[dependencies]
glob = "0.2.11"
-lazy_static = "0.2"
log = "0.4.0"
regex = "0.2"
semver = "0.8"
diff --git a/lib/core/libimagstore/src/error.rs b/lib/core/libimagstore/src/error.rs
index e65741d..1714b45 100644
--- a/lib/core/libimagstore/src/error.rs
+++ b/lib/core/libimagstore/src/error.rs
@@ -28,6 +28,7 @@ error_chain! {
foreign_links {
Io(::std::io::Error);
+ Fmt(::std::fmt::Error);
TomlDeserError(::toml::de::Error);
GlobPatternError(::glob::PatternError);
TomlQueryError(::toml_query::error::Error);
diff --git a/lib/core/libimagstore/src/lib.rs b/lib/core/libimagstore/src/lib.rs
index 1662465..833dd4d 100644
--- a/lib/core/libimagstore/src/lib.rs
+++ b/lib/core/libimagstore/src/lib.rs
@@ -37,7 +37,6 @@
#[macro_use] extern crate log;
extern crate glob;
-#[macro_use] extern crate lazy_static;
extern crate regex;
extern crate toml;
#[cfg(test)] extern crate tempdir;
diff --git a/lib/core/libimagstore/src/util.rs b/lib/core/libimagstore/src/util.rs
index 6759bd6..160ecf6 100644
--- a/lib/core/libimagstore/src/util.rs
+++ b/lib/core/libimagstore/src/util.rs
@@ -17,13 +17,12 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
-use regex::Regex;
+use std::fmt::Write;
+
use toml::Value;
use store::Result;
use store::Header;
-use error::StoreErrorKind as SEK;
-use error::StoreError as SE;
#[cfg(feature = "early-panic")]
#[macro_export]
@@ -43,27 +42,23 @@ macro_rules! if_cfg_panic {
pub fn entry_buffer_to_header_content(buf: &str) -> Result<(Value, String)> {
debug!("Building entry from string");
- lazy_static! {
- static ref RE: Regex = Regex::new(r"(?smx)
- ^---$
- (?P<header>.*) # Header
- ^---$\n
- (?P<content>.*) # Content
- ").unwrap();
- }
-
- let matches = match RE.captures(buf) {
- None => return Err(SE::from_kind(SEK::MalformedEntry)),
- Some(s) => s,
- };
+ let mut header = String::new();
+ let mut content = String::new();
+ let mut header_consumed = false;
- 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("");
+ for line in buf.lines().skip(1) { // the first line is "---"
+ if line == "---" {
+ header_consumed = true;
+ // do not further process the line
+ } else {
+ if !header_consumed {
+ let _ = writeln!(header, "{}", line)?;
+ } else {
+ let _ = write!(content, "{}", line)?;
+ }
+ }
+ }
- Ok((Value::parse(header.as_str())?, String::from(content)))
+ Ok((Value::parse(&header)?, String::from(content)))
}