Merge branch 'remove-file-data-parser'

This commit is contained in:
Matthias Beyer 2015-11-10 20:33:12 +01:00
commit 92fddae230
2 changed files with 16 additions and 36 deletions

View file

@ -2,7 +2,7 @@ use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use std::fmt;
use super::parser::{FileHeaderParser, FileDataParser, Parser, ParserError};
use super::parser::{FileHeaderParser, Parser, ParserError};
#[derive(Debug)]
pub enum FileHeaderSpec {
@ -30,11 +30,6 @@ pub enum FileHeaderData {
Array { values: Box<Vec<FileHeaderData>> },
}
pub trait FileData : Sized {
fn get_fulltext(&self) -> String;
fn get_abbrev(&self) -> String;
}
impl Display for FileHeaderSpec {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
@ -169,24 +164,23 @@ pub fn match_header_spec<'a>(spec: &'a FileHeaderSpec, data: &'a FileHeaderData)
pub type FileID = String;
pub struct File<D: FileData> {
pub struct File {
header : FileHeaderData,
data : D,
data : String,
id : String
}
impl<'a, D: FileData> File<D> {
impl<'a> File {
fn new<HP, DP>(prs: &Parser<HP, DP>, path: &String) -> Result<File<D>, ParserError>
where HP: FileHeaderParser<'a>,
DP: FileDataParser<D>,
fn new<HP>(prs: &Parser<HP>, path: &String) -> Result<File, ParserError>
where HP: FileHeaderParser<'a>
{
File::<D>::read_file(path).and_then(|p| prs.read(p))
File::read_file(path).and_then(|p| prs.read(p))
.and_then(|(h, d)|
Ok(File {
header: h,
data: d,
id: File::<D>::get_id_from_path(path),
id: File::get_id_from_path(path),
}))
}

View file

@ -3,7 +3,7 @@ use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use std::fmt;
use super::file::{FileHeaderSpec, FileHeaderData, FileData};
use super::file::{FileHeaderSpec, FileHeaderData};
pub struct ParserError {
summary: String,
@ -80,34 +80,24 @@ pub trait FileHeaderParser<'a> : Sized {
fn write(&self, data: &FileHeaderData) -> Result<String, ParserError>;
}
pub trait FileDataParser<FD: FileData + Sized> : Sized {
fn new() -> Self;
fn read(&self, string: Option<String>) -> Result<FD, ParserError>;
fn write(&self, data: &FD) -> Result<String, ParserError>;
}
type TextTpl = (Option<String>, Option<String>);
pub struct Parser<HP, DP>
pub struct Parser<HP>
{
headerp : HP,
datap : DP,
}
impl<'a, HP, DP> Parser<HP, DP> where
impl<'a, HP> Parser<HP> where
HP: FileHeaderParser<'a>,
{
fn new(headerp: HP, datap: DP) -> Parser<HP, DP> {
fn new(headerp: HP) -> Parser<HP> {
Parser {
headerp: headerp,
datap: datap,
}
}
pub fn read<FD>(&self, s: String) -> Result<(FileHeaderData, FD), ParserError>
where FD: FileData + Sized,
DP: FileDataParser<FD>
pub fn read(&self, s: String) -> Result<(FileHeaderData, String), ParserError>
{
let divided = self.divide_text(&s);
@ -118,20 +108,16 @@ impl<'a, HP, DP> Parser<HP, DP> where
let (header, data) = divided.ok().unwrap();
let h_parseres = try!(self.headerp.read(header));
let d_parseres = try!(self.datap.read(data));
Ok((h_parseres, d_parseres))
Ok((h_parseres, data.unwrap_or(String::new())))
}
fn write<FD>(&self, tpl : (FileHeaderData, FD)) -> Result<String, ParserError>
where FD: FileData + Sized,
DP: FileDataParser<FD>
fn write(&self, tpl : (FileHeaderData, String)) -> Result<String, ParserError>
{
let (header, data) = tpl;
let h_text = try!(self.headerp.write(&header));
let d_text = try!(self.datap.write(&data));
Ok(h_text + &d_text[..])
Ok(h_text + &data[..])
}
fn divide_text(&self, text: &String) -> Result<TextTpl, ParserError> {