Merge branch 'remove-file-data-parser'
This commit is contained in:
commit
92fddae230
2 changed files with 16 additions and 36 deletions
|
@ -2,7 +2,7 @@ use std::error::Error;
|
||||||
use std::fmt::{Debug, Display, Formatter};
|
use std::fmt::{Debug, Display, Formatter};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use super::parser::{FileHeaderParser, FileDataParser, Parser, ParserError};
|
use super::parser::{FileHeaderParser, Parser, ParserError};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum FileHeaderSpec {
|
pub enum FileHeaderSpec {
|
||||||
|
@ -30,11 +30,6 @@ pub enum FileHeaderData {
|
||||||
Array { values: Box<Vec<FileHeaderData>> },
|
Array { values: Box<Vec<FileHeaderData>> },
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait FileData : Sized {
|
|
||||||
fn get_fulltext(&self) -> String;
|
|
||||||
fn get_abbrev(&self) -> String;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for FileHeaderSpec {
|
impl Display for FileHeaderSpec {
|
||||||
|
|
||||||
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
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 type FileID = String;
|
||||||
|
|
||||||
pub struct File<D: FileData> {
|
pub struct File {
|
||||||
header : FileHeaderData,
|
header : FileHeaderData,
|
||||||
data : D,
|
data : String,
|
||||||
id : 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>
|
fn new<HP>(prs: &Parser<HP>, path: &String) -> Result<File, ParserError>
|
||||||
where HP: FileHeaderParser<'a>,
|
where HP: FileHeaderParser<'a>
|
||||||
DP: FileDataParser<D>,
|
|
||||||
{
|
{
|
||||||
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)|
|
.and_then(|(h, d)|
|
||||||
Ok(File {
|
Ok(File {
|
||||||
header: h,
|
header: h,
|
||||||
data: d,
|
data: d,
|
||||||
id: File::<D>::get_id_from_path(path),
|
id: File::get_id_from_path(path),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::error::Error;
|
||||||
use std::fmt::{Debug, Display, Formatter};
|
use std::fmt::{Debug, Display, Formatter};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use super::file::{FileHeaderSpec, FileHeaderData, FileData};
|
use super::file::{FileHeaderSpec, FileHeaderData};
|
||||||
|
|
||||||
pub struct ParserError {
|
pub struct ParserError {
|
||||||
summary: String,
|
summary: String,
|
||||||
|
@ -80,34 +80,24 @@ pub trait FileHeaderParser<'a> : Sized {
|
||||||
fn write(&self, data: &FileHeaderData) -> Result<String, ParserError>;
|
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>);
|
type TextTpl = (Option<String>, Option<String>);
|
||||||
|
|
||||||
pub struct Parser<HP, DP>
|
pub struct Parser<HP>
|
||||||
{
|
{
|
||||||
headerp : HP,
|
headerp : HP,
|
||||||
datap : DP,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, HP, DP> Parser<HP, DP> where
|
impl<'a, HP> Parser<HP> where
|
||||||
HP: FileHeaderParser<'a>,
|
HP: FileHeaderParser<'a>,
|
||||||
{
|
{
|
||||||
|
|
||||||
fn new(headerp: HP, datap: DP) -> Parser<HP, DP> {
|
fn new(headerp: HP) -> Parser<HP> {
|
||||||
Parser {
|
Parser {
|
||||||
headerp: headerp,
|
headerp: headerp,
|
||||||
datap: datap,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read<FD>(&self, s: String) -> Result<(FileHeaderData, FD), ParserError>
|
pub fn read(&self, s: String) -> Result<(FileHeaderData, String), ParserError>
|
||||||
where FD: FileData + Sized,
|
|
||||||
DP: FileDataParser<FD>
|
|
||||||
{
|
{
|
||||||
let divided = self.divide_text(&s);
|
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 (header, data) = divided.ok().unwrap();
|
||||||
|
|
||||||
let h_parseres = try!(self.headerp.read(header));
|
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>
|
fn write(&self, tpl : (FileHeaderData, String)) -> Result<String, ParserError>
|
||||||
where FD: FileData + Sized,
|
|
||||||
DP: FileDataParser<FD>
|
|
||||||
{
|
{
|
||||||
let (header, data) = tpl;
|
let (header, data) = tpl;
|
||||||
let h_text = try!(self.headerp.write(&header));
|
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> {
|
fn divide_text(&self, text: &String) -> Result<TextTpl, ParserError> {
|
||||||
|
|
Loading…
Reference in a new issue