Add divide_text() function

This commit is contained in:
Matthias Beyer 2015-10-28 17:27:03 +01:00
parent bdb0792040
commit 1166f313a3

View file

@ -57,6 +57,8 @@ pub trait Module {
pub mod file { pub mod file {
use regex::Regex;
pub struct ParserError { pub struct ParserError {
summary: String, summary: String,
parsertext: String, parsertext: String,
@ -127,4 +129,26 @@ pub mod file {
fn write(&self, hdr: &header::FileHeaderData, data: &FileData) -> Result<String, ParserError>; fn write(&self, hdr: &header::FileHeaderData, data: &FileData) -> Result<String, ParserError>;
} }
pub type HeaderDataTpl = (Option<String>, Option<String>);
pub fn divide_text(text: String) -> Result<HeaderDataTpl, ParserError> {
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))
}
} }