Move FileHeader{Spec,Data} to own submodules

This commit is contained in:
Matthias Beyer 2015-12-06 13:09:51 +01:00
parent 3b89148cd0
commit 188aacad75
9 changed files with 215 additions and 196 deletions

View file

@ -1,6 +1,6 @@
use module::helpers::header as headerhelpers;
use storage::file::FileHeaderData as FHD;
use storage::file::FileHeaderSpec as FHS;
use storage::file::header::data::FileHeaderData as FHD;
use storage::file::header::spec::FileHeaderSpec as FHS;
pub fn get_spec() -> FHS {
FHS::Map {

View file

@ -3,7 +3,7 @@
*/
pub mod spec {
use storage::file::FileHeaderSpec as FHS;
use storage::file::header::spec::FileHeaderSpec as FHS;
use module::helpers::spec::{named_text, named_text_array};
pub fn url_key() -> FHS {
@ -18,7 +18,7 @@ pub mod spec {
pub mod data {
use std::ops::Deref;
use storage::file::FileHeaderData as FHD;
use storage::file::header::data::FileHeaderData as FHD;
pub fn build_tag_array(tags: &Vec<String>) -> FHD {
let texttags = tags.into_iter().map(|t| FHD::Text(t.clone())).collect();

View file

@ -2,7 +2,7 @@ pub mod header;
pub mod utils;
pub mod spec {
use storage::file::FileHeaderSpec as FHS;
use storage::file::header::spec::FileHeaderSpec as FHS;
pub fn named_text(name: &str) -> FHS {
FHS::Key { name: String::from(name), value_type: Box::new(FHS::Text) }

View file

@ -0,0 +1,37 @@
use regex::Regex;
#[derive(Debug)]
#[derive(Clone)]
pub enum FileHeaderData {
Null,
Bool(bool),
Integer(i64),
UInteger(u64),
Float(f64),
Text(String),
Key { name: String, value: Box<FileHeaderData> },
Map { keys: Vec<FileHeaderData> },
Array { values: Box<Vec<FileHeaderData>> },
}
impl FileHeaderData {
pub fn matches_with(&self, r: &Regex) -> bool {
match self {
&FileHeaderData::Text(ref t) => r.is_match(&t[..]),
&FileHeaderData::Key{name: ref n, value: ref val} => {
r.is_match(n) || val.matches_with(r)
},
&FileHeaderData::Map{keys: ref dks} => {
dks.iter().any(|x| x.matches_with(r))
},
&FileHeaderData::Array{values: ref vs} => {
vs.iter().any(|x| x.matches_with(r))
}
_ => false,
}
}
}

View file

@ -0,0 +1,126 @@
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use std::fmt;
pub mod spec;
pub mod data;
use self::data::*;
use self::spec::*;
pub struct MatchError<'a> {
summary: String,
expected: &'a FileHeaderSpec,
found: &'a FileHeaderData
}
impl<'a> MatchError<'a> {
pub fn new(s: String,
ex: &'a FileHeaderSpec,
found: &'a FileHeaderData) -> MatchError<'a> {
MatchError {
summary: s,
expected: ex,
found: found,
}
}
pub fn format(&self) -> String {
format!("MatchError: {:?}\nExpected: {:?}\nFound: {:?}\n",
self.summary, self.expected, self.found)
}
}
impl<'a> Error for MatchError<'a> {
fn description(&self) -> &str {
&self.summary[..]
}
fn cause(&self) -> Option<&Error> {
None
}
}
impl<'a> Debug for MatchError<'a> {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
write!(fmt, "{}", self.format());
Ok(())
}
}
impl<'a> Display for MatchError<'a> {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
write!(fmt, "{}", self.format());
Ok(())
}
}
pub fn match_header_spec<'a>(spec: &'a FileHeaderSpec, data: &'a FileHeaderData)
-> Option<MatchError<'a>>
{
debug!("Start matching:\n'{:?}'\non\n{:?}", spec, data);
match (spec, data) {
(&FileHeaderSpec::Null, &FileHeaderData::Null) => { }
(&FileHeaderSpec::Bool, &FileHeaderData::Bool(_)) => { }
(&FileHeaderSpec::Integer, &FileHeaderData::Integer(_)) => { }
(&FileHeaderSpec::UInteger, &FileHeaderData::UInteger(_)) => { }
(&FileHeaderSpec::Float, &FileHeaderData::Float(_)) => { }
(&FileHeaderSpec::Text, &FileHeaderData::Text(_)) => { }
(
&FileHeaderSpec::Key{name: ref kname, value_type: ref vtype},
&FileHeaderData::Key{name: ref n, value: ref val}
) => {
debug!("Matching Key: '{:?}' == '{:?}', Value: '{:?}' == '{:?}'",
kname, n,
vtype, val);
if kname != n {
debug!("Keys not matching");
unimplemented!();
}
return match_header_spec(&*vtype, &*val);
}
(
&FileHeaderSpec::Map{keys: ref sks},
&FileHeaderData::Map{keys: ref dks}
) => {
debug!("Matching Map: '{:?}' == '{:?}'", sks, dks);
for (s, d) in sks.iter().zip(dks.iter()) {
let res = match_header_spec(s, d);
if res.is_some() {
return res;
}
}
}
(
&FileHeaderSpec::Array{allowed_types: ref vtypes},
&FileHeaderData::Array{values: ref vs}
) => {
debug!("Matching Array: '{:?}' == '{:?}'", vtypes, vs);
for (t, v) in vtypes.iter().zip(vs.iter()) {
let res = match_header_spec(t, v);
if res.is_some() {
return res;
}
}
}
(k, v) => {
return Some(MatchError::new(String::from("Expected type does not match found type"),
k, v
))
}
}
None
}

View file

@ -0,0 +1,41 @@
use std::fmt::{Debug, Display, Formatter};
use std::fmt;
#[derive(Debug)]
#[derive(Clone)]
pub enum FileHeaderSpec {
Null,
Bool,
Integer,
UInteger,
Float,
Text,
Key { name: String, value_type: Box<FileHeaderSpec> },
Map { keys: Vec<FileHeaderSpec> },
Array { allowed_types: Vec<FileHeaderSpec> },
}
impl Display for FileHeaderSpec {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
match self {
&FileHeaderSpec::Null => write!(fmt, "NULL"),
&FileHeaderSpec::Bool => write!(fmt, "Bool"),
&FileHeaderSpec::Integer => write!(fmt, "Integer"),
&FileHeaderSpec::UInteger => write!(fmt, "UInteger"),
&FileHeaderSpec::Float => write!(fmt, "Float"),
&FileHeaderSpec::Text => write!(fmt, "Text"),
&FileHeaderSpec::Key{name: ref n, value_type: ref vt} => {
write!(fmt, "Key({:?}) -> {:?}", n, vt)
}
&FileHeaderSpec::Map{keys: ref ks} => {
write!(fmt, "Map -> {:?}", ks)
}
&FileHeaderSpec::Array{allowed_types: ref at} => {
write!(fmt, "Array({:?})", at)
}
}
}
}

View file

@ -5,200 +5,14 @@ use std::fmt;
use regex::Regex;
pub mod id;
pub mod header;
use module::Module;
use storage::file::id::*;
use super::parser::{FileHeaderParser, Parser, ParserError};
#[derive(Debug)]
#[derive(Clone)]
pub enum FileHeaderSpec {
Null,
Bool,
Integer,
UInteger,
Float,
Text,
Key { name: String, value_type: Box<FileHeaderSpec> },
Map { keys: Vec<FileHeaderSpec> },
Array { allowed_types: Vec<FileHeaderSpec> },
}
#[derive(Debug)]
#[derive(Clone)]
pub enum FileHeaderData {
Null,
Bool(bool),
Integer(i64),
UInteger(u64),
Float(f64),
Text(String),
Key { name: String, value: Box<FileHeaderData> },
Map { keys: Vec<FileHeaderData> },
Array { values: Box<Vec<FileHeaderData>> },
}
impl Display for FileHeaderSpec {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
match self {
&FileHeaderSpec::Null => write!(fmt, "NULL"),
&FileHeaderSpec::Bool => write!(fmt, "Bool"),
&FileHeaderSpec::Integer => write!(fmt, "Integer"),
&FileHeaderSpec::UInteger => write!(fmt, "UInteger"),
&FileHeaderSpec::Float => write!(fmt, "Float"),
&FileHeaderSpec::Text => write!(fmt, "Text"),
&FileHeaderSpec::Key{name: ref n, value_type: ref vt} => {
write!(fmt, "Key({:?}) -> {:?}", n, vt)
}
&FileHeaderSpec::Map{keys: ref ks} => {
write!(fmt, "Map -> {:?}", ks)
}
&FileHeaderSpec::Array{allowed_types: ref at} => {
write!(fmt, "Array({:?})", at)
}
}
}
}
impl FileHeaderData {
pub fn matches_with(&self, r: &Regex) -> bool {
match self {
&FileHeaderData::Text(ref t) => r.is_match(&t[..]),
&FileHeaderData::Key{name: ref n, value: ref val} => {
r.is_match(n) || val.matches_with(r)
},
&FileHeaderData::Map{keys: ref dks} => {
dks.iter().any(|x| x.matches_with(r))
},
&FileHeaderData::Array{values: ref vs} => {
vs.iter().any(|x| x.matches_with(r))
}
_ => false,
}
}
}
pub struct MatchError<'a> {
summary: String,
expected: &'a FileHeaderSpec,
found: &'a FileHeaderData
}
impl<'a> MatchError<'a> {
pub fn new(s: String,
ex: &'a FileHeaderSpec,
found: &'a FileHeaderData) -> MatchError<'a> {
MatchError {
summary: s,
expected: ex,
found: found,
}
}
pub fn format(&self) -> String {
format!("MatchError: {:?}\nExpected: {:?}\nFound: {:?}\n",
self.summary, self.expected, self.found)
}
}
impl<'a> Error for MatchError<'a> {
fn description(&self) -> &str {
&self.summary[..]
}
fn cause(&self) -> Option<&Error> {
None
}
}
impl<'a> Debug for MatchError<'a> {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
write!(fmt, "{}", self.format());
Ok(())
}
}
impl<'a> Display for MatchError<'a> {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
write!(fmt, "{}", self.format());
Ok(())
}
}
pub fn match_header_spec<'a>(spec: &'a FileHeaderSpec, data: &'a FileHeaderData)
-> Option<MatchError<'a>>
{
debug!("Start matching:\n'{:?}'\non\n{:?}", spec, data);
match (spec, data) {
(&FileHeaderSpec::Null, &FileHeaderData::Null) => { }
(&FileHeaderSpec::Bool, &FileHeaderData::Bool(_)) => { }
(&FileHeaderSpec::Integer, &FileHeaderData::Integer(_)) => { }
(&FileHeaderSpec::UInteger, &FileHeaderData::UInteger(_)) => { }
(&FileHeaderSpec::Float, &FileHeaderData::Float(_)) => { }
(&FileHeaderSpec::Text, &FileHeaderData::Text(_)) => { }
(
&FileHeaderSpec::Key{name: ref kname, value_type: ref vtype},
&FileHeaderData::Key{name: ref n, value: ref val}
) => {
debug!("Matching Key: '{:?}' == '{:?}', Value: '{:?}' == '{:?}'",
kname, n,
vtype, val);
if kname != n {
debug!("Keys not matching");
unimplemented!();
}
return match_header_spec(&*vtype, &*val);
}
(
&FileHeaderSpec::Map{keys: ref sks},
&FileHeaderData::Map{keys: ref dks}
) => {
debug!("Matching Map: '{:?}' == '{:?}'", sks, dks);
for (s, d) in sks.iter().zip(dks.iter()) {
let res = match_header_spec(s, d);
if res.is_some() {
return res;
}
}
}
(
&FileHeaderSpec::Array{allowed_types: ref vtypes},
&FileHeaderData::Array{values: ref vs}
) => {
debug!("Matching Array: '{:?}' == '{:?}'", vtypes, vs);
for (t, v) in vtypes.iter().zip(vs.iter()) {
let res = match_header_spec(t, v);
if res.is_some() {
return res;
}
}
}
(k, v) => {
return Some(MatchError::new(String::from("Expected type does not match found type"),
k, v
))
}
}
None
}
use self::header::spec::*;
use self::header::data::*;
/*
* Internal abstract view on a file. Does not exist on the FS and is just kept

View file

@ -8,7 +8,8 @@ use serde::ser::Serialize;
use serde::ser::Serializer as Ser;
use storage::parser::{FileHeaderParser, ParserError};
use storage::file::{FileHeaderSpec, FileHeaderData};
use storage::file::header::spec::FileHeaderSpec;
use storage::file::header::data::FileHeaderData;
pub struct JsonHeaderParser {
spec: Option<FileHeaderSpec>,

View file

@ -4,7 +4,7 @@ use std::fmt;
use regex::Regex;
use super::file::FileHeaderData;
use super::file::header::data::FileHeaderData;
pub struct ParserError {
summary: String,