From 11c2fe12602947040f026f01bdc0c731ab561a0a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 28 Oct 2015 21:28:34 +0100 Subject: [PATCH] Implement Abstract Parser which uses Header- and Data-Parser internally --- src/module/mod.rs | 58 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/src/module/mod.rs b/src/module/mod.rs index 93056718..58fe32bc 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -110,7 +110,7 @@ pub mod file { pub trait FileHeaderParser : Sized { fn new(spec: &FileHeaderSpec) -> Self; - fn read(&self, string: String) -> Result; + fn read(&self, string: Option) -> Result; fn write(&self, data: &FileHeaderData) -> Result; } @@ -119,15 +119,59 @@ pub mod file { fn get_abbrev(&self) -> String; } - pub trait FileDataParser : Sized { - fn new() -> FileDataParser; - fn read(&self, string: String) -> Result; - fn write(&self, data: &FileData) -> Result; + pub trait FileDataParser : Sized { + fn new() -> Self; + fn read(&self, string: Option) -> Result; + fn write(&self, data: &FD) -> Result; } - pub type HeaderDataTpl = (Option, Option); + type TextTpl = (Option, Option); - pub fn divide_text(text: String) -> Result { + pub struct Parser + { + headerp : HP, + datap : DP, + } + + impl Parser where + HP: FileHeaderParser, + { + + fn new(headerp: HP, datap: DP) -> Parser { + Parser { + headerp: headerp, + datap: datap, + } + } + + fn read(&self, s: String) -> Result<(FileHeaderData, FD), ParserError> + where FD: FileData + Sized, + DP: FileDataParser + { + let divided = divide_text(&s); + + if divided.is_err() { + return Err(divided.err().unwrap()); + } + + let (header, data) = divided.ok().unwrap(); + + let h_parseres = self.headerp.read(header); + let d_parseres = self.datap.read(data); + + if h_parseres.is_err() { + return Err(h_parseres.err().unwrap()); + } + + if d_parseres.is_err() { + return Err(d_parseres.err().unwrap()); + } + + Ok((h_parseres.ok().unwrap(), d_parseres.ok().unwrap())) + } + } + + fn divide_text(text: &String) -> Result { let re = Regex::new(r"(?m)^\-\-\-$\n(.*)^\-\-\-$\n(.*)").unwrap(); let captures = re.captures(&text[..]).unwrap_or(