From 1166f313a3983da032b77b9f2131c7309f48048b Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 28 Oct 2015 17:27:03 +0100 Subject: [PATCH] Add divide_text() function --- src/module/mod.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/module/mod.rs b/src/module/mod.rs index fd304137..4bcfaf21 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -57,6 +57,8 @@ pub trait Module { pub mod file { + use regex::Regex; + pub struct ParserError { summary: String, parsertext: String, @@ -127,4 +129,26 @@ pub mod file { fn write(&self, hdr: &header::FileHeaderData, data: &FileData) -> Result; } + pub type HeaderDataTpl = (Option, Option); + + pub fn divide_text(text: String) -> Result { + let re = Regex::new(r"(?m)^\-\-\-$\n(.*)^\-\-\-$\n(.*)").unwrap(); + + let captures = re.captures(&text[..]).unwrap_or( + return Err(ParserError::new("Cannot run regex on text", + text.clone(), 0, + "Cannot run regex on text to divide it into header and content.")) + ); + + if captures.len() != 2 { + return Err(ParserError::new("Unexpected Regex output", + text.clone(), 0, + "The regex to divide text into header and content had an unexpected output.")) + } + + let header = captures.at(0).map(|s| String::from(s)); + let content = captures.at(1).map(|s| String::from(s)); + Ok((header, content)) + } + }