2015-10-30 15:48:48 +00:00
|
|
|
use std::error::Error;
|
|
|
|
use std::fmt::{Debug, Display, Formatter};
|
|
|
|
use std::fmt;
|
|
|
|
|
2015-12-02 10:33:58 +00:00
|
|
|
use module::Module;
|
|
|
|
use super::parser::{FileHeaderParser, Parser, ParserError};
|
2015-11-23 17:42:55 +00:00
|
|
|
use storage::file_id::*;
|
2015-10-31 19:20:04 +00:00
|
|
|
|
2015-12-02 10:30:50 +00:00
|
|
|
use regex::Regex;
|
|
|
|
|
2015-10-30 15:47:04 +00:00
|
|
|
#[derive(Debug)]
|
2015-12-03 11:09:13 +00:00
|
|
|
#[derive(Clone)]
|
2015-10-30 13:31:45 +00:00
|
|
|
pub enum FileHeaderSpec {
|
|
|
|
Null,
|
|
|
|
Bool,
|
|
|
|
Integer,
|
|
|
|
UInteger,
|
|
|
|
Float,
|
|
|
|
Text,
|
2015-11-10 15:53:30 +00:00
|
|
|
Key { name: String, value_type: Box<FileHeaderSpec> },
|
2015-10-30 17:09:01 +00:00
|
|
|
Map { keys: Vec<FileHeaderSpec> },
|
2015-10-30 17:20:25 +00:00
|
|
|
Array { allowed_types: Vec<FileHeaderSpec> },
|
2015-10-30 13:31:45 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 15:47:04 +00:00
|
|
|
#[derive(Debug)]
|
2015-11-24 18:27:57 +00:00
|
|
|
#[derive(Clone)]
|
2015-10-30 13:31:45 +00:00
|
|
|
pub enum FileHeaderData {
|
|
|
|
Null,
|
|
|
|
Bool(bool),
|
|
|
|
Integer(i64),
|
|
|
|
UInteger(u64),
|
|
|
|
Float(f64),
|
|
|
|
Text(String),
|
2015-11-10 15:53:30 +00:00
|
|
|
Key { name: String, value: Box<FileHeaderData> },
|
2015-10-30 17:09:01 +00:00
|
|
|
Map { keys: Vec<FileHeaderData> },
|
2015-10-30 13:31:45 +00:00
|
|
|
Array { values: Box<Vec<FileHeaderData>> },
|
|
|
|
}
|
|
|
|
|
2015-10-30 15:50:02 +00:00
|
|
|
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)
|
|
|
|
}
|
2015-10-30 17:09:01 +00:00
|
|
|
&FileHeaderSpec::Map{keys: ref ks} => {
|
|
|
|
write!(fmt, "Map -> {:?}", ks)
|
|
|
|
}
|
2015-10-30 15:50:02 +00:00
|
|
|
&FileHeaderSpec::Array{allowed_types: ref at} => {
|
|
|
|
write!(fmt, "Array({:?})", at)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-12-02 10:30:50 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-30 16:35:18 +00:00
|
|
|
pub struct MatchError<'a> {
|
2015-10-30 15:48:48 +00:00
|
|
|
summary: String,
|
2015-10-30 16:35:18 +00:00
|
|
|
expected: &'a FileHeaderSpec,
|
|
|
|
found: &'a FileHeaderData
|
2015-10-30 15:48:48 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 16:35:18 +00:00
|
|
|
impl<'a> MatchError<'a> {
|
|
|
|
|
|
|
|
pub fn new(s: String,
|
|
|
|
ex: &'a FileHeaderSpec,
|
|
|
|
found: &'a FileHeaderData) -> MatchError<'a> {
|
|
|
|
MatchError {
|
|
|
|
summary: s,
|
|
|
|
expected: ex,
|
|
|
|
found: found,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-30 15:48:48 +00:00
|
|
|
pub fn format(&self) -> String {
|
2015-10-30 16:46:25 +00:00
|
|
|
format!("MatchError: {:?}\nExpected: {:?}\nFound: {:?}\n",
|
|
|
|
self.summary, self.expected, self.found)
|
2015-10-30 15:48:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-30 16:35:18 +00:00
|
|
|
impl<'a> Error for MatchError<'a> {
|
2015-10-30 15:48:48 +00:00
|
|
|
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
&self.summary[..]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cause(&self) -> Option<&Error> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-30 16:35:18 +00:00
|
|
|
impl<'a> Debug for MatchError<'a> {
|
2015-10-30 15:48:48 +00:00
|
|
|
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
|
|
|
write!(fmt, "{}", self.format());
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-30 16:35:18 +00:00
|
|
|
impl<'a> Display for MatchError<'a> {
|
2015-10-30 15:48:48 +00:00
|
|
|
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
|
|
|
write!(fmt, "{}", self.format());
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-30 16:35:18 +00:00
|
|
|
pub fn match_header_spec<'a>(spec: &'a FileHeaderSpec, data: &'a FileHeaderData)
|
|
|
|
-> Option<MatchError<'a>>
|
2015-10-30 15:50:43 +00:00
|
|
|
{
|
2015-11-27 18:31:46 +00:00
|
|
|
debug!("Start matching:\n'{:?}'\non\n{:?}", spec, data);
|
2015-10-30 16:35:18 +00:00
|
|
|
match (spec, data) {
|
2015-10-30 15:50:43 +00:00
|
|
|
(&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}
|
|
|
|
) => {
|
2015-11-27 18:31:46 +00:00
|
|
|
debug!("Matching Key: '{:?}' == '{:?}', Value: '{:?}' == '{:?}'",
|
|
|
|
kname, n,
|
|
|
|
vtype, val);
|
2015-10-30 15:50:43 +00:00
|
|
|
if kname != n {
|
2015-11-27 18:31:46 +00:00
|
|
|
debug!("Keys not matching");
|
2015-11-01 17:07:45 +00:00
|
|
|
unimplemented!();
|
2015-10-30 15:50:43 +00:00
|
|
|
}
|
|
|
|
return match_header_spec(&*vtype, &*val);
|
|
|
|
}
|
|
|
|
|
2015-10-30 17:09:01 +00:00
|
|
|
(
|
|
|
|
&FileHeaderSpec::Map{keys: ref sks},
|
|
|
|
&FileHeaderData::Map{keys: ref dks}
|
|
|
|
) => {
|
2015-11-27 18:31:46 +00:00
|
|
|
debug!("Matching Map: '{:?}' == '{:?}'", sks, dks);
|
|
|
|
|
2015-10-30 17:09:01 +00:00
|
|
|
for (s, d) in sks.iter().zip(dks.iter()) {
|
|
|
|
let res = match_header_spec(s, d);
|
|
|
|
if res.is_some() {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-30 15:50:43 +00:00
|
|
|
(
|
|
|
|
&FileHeaderSpec::Array{allowed_types: ref vtypes},
|
|
|
|
&FileHeaderData::Array{values: ref vs}
|
|
|
|
) => {
|
2015-11-27 18:31:46 +00:00
|
|
|
debug!("Matching Array: '{:?}' == '{:?}'", vtypes, vs);
|
2015-10-30 15:50:43 +00:00
|
|
|
for (t, v) in vtypes.iter().zip(vs.iter()) {
|
|
|
|
let res = match_header_spec(t, v);
|
|
|
|
if res.is_some() {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-30 16:35:18 +00:00
|
|
|
(k, v) => {
|
|
|
|
return Some(MatchError::new(String::from("Expected type does not match found type"),
|
|
|
|
k, v
|
|
|
|
))
|
|
|
|
}
|
2015-10-30 15:50:43 +00:00
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2015-11-23 18:25:27 +00:00
|
|
|
/*
|
|
|
|
* Internal abstract view on a file. Does not exist on the FS and is just kept
|
|
|
|
* internally until it is written to disk.
|
|
|
|
*/
|
2015-12-02 10:33:58 +00:00
|
|
|
pub struct File<'a> {
|
|
|
|
owning_module : &'a Module,
|
|
|
|
header : FileHeaderData,
|
|
|
|
data : String,
|
|
|
|
id : FileID,
|
2015-10-31 19:20:04 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 10:33:58 +00:00
|
|
|
impl<'a> File<'a> {
|
2015-11-23 18:16:11 +00:00
|
|
|
|
2015-12-02 10:33:58 +00:00
|
|
|
pub fn new(module: &'a Module) -> File<'a> {
|
2015-11-27 18:37:20 +00:00
|
|
|
let f = File {
|
2015-12-02 10:33:58 +00:00
|
|
|
owning_module: module,
|
2015-11-23 18:16:11 +00:00
|
|
|
header: FileHeaderData::Null,
|
2015-11-23 18:25:27 +00:00
|
|
|
data: String::from(""),
|
|
|
|
id: File::get_new_file_id(),
|
2015-11-27 18:37:20 +00:00
|
|
|
};
|
|
|
|
debug!("Create new File object: {:?}", f);
|
|
|
|
f
|
2015-11-23 18:16:11 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 10:33:58 +00:00
|
|
|
pub fn from_parser_result(module: &Module, id: FileID, header: FileHeaderData, data: String) -> File {
|
2015-11-27 18:37:20 +00:00
|
|
|
let f = File {
|
2015-12-02 10:33:58 +00:00
|
|
|
owning_module: module,
|
2015-11-24 15:34:03 +00:00
|
|
|
header: header,
|
|
|
|
data: data,
|
|
|
|
id: id,
|
2015-11-27 18:37:20 +00:00
|
|
|
};
|
|
|
|
debug!("Create new File object from parser result: {:?}", f);
|
|
|
|
f
|
2015-11-24 09:58:54 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 10:33:58 +00:00
|
|
|
pub fn new_with_header(module: &Module, h: FileHeaderData) -> File {
|
2015-11-27 18:37:20 +00:00
|
|
|
let f = File {
|
2015-12-02 10:33:58 +00:00
|
|
|
owning_module: module,
|
2015-11-23 18:27:54 +00:00
|
|
|
header: h,
|
|
|
|
data: String::from(""),
|
|
|
|
id: File::get_new_file_id(),
|
2015-11-27 18:37:20 +00:00
|
|
|
};
|
|
|
|
debug!("Create new File object with header: {:?}", f);
|
|
|
|
f
|
2015-11-23 18:27:54 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 10:33:58 +00:00
|
|
|
pub fn new_with_data(module: &Module, d: String) -> File {
|
2015-11-27 18:37:20 +00:00
|
|
|
let f = File {
|
2015-12-02 10:33:58 +00:00
|
|
|
owning_module: module,
|
2015-11-23 18:27:54 +00:00
|
|
|
header: FileHeaderData::Null,
|
|
|
|
data: d,
|
|
|
|
id: File::get_new_file_id(),
|
2015-11-27 18:37:20 +00:00
|
|
|
};
|
|
|
|
debug!("Create new File object with data: {:?}", f);
|
|
|
|
f
|
2015-11-23 18:27:54 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 10:33:58 +00:00
|
|
|
pub fn new_with_content(module: &Module, h: FileHeaderData, d: String) -> File {
|
2015-11-27 18:37:20 +00:00
|
|
|
let f = File {
|
2015-12-02 10:33:58 +00:00
|
|
|
owning_module: module,
|
2015-11-23 18:27:54 +00:00
|
|
|
header: h,
|
|
|
|
data: d,
|
|
|
|
id: File::get_new_file_id(),
|
2015-11-27 18:37:20 +00:00
|
|
|
};
|
|
|
|
debug!("Create new File object with content: {:?}", f);
|
|
|
|
f
|
2015-11-23 18:27:54 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 10:32:59 +00:00
|
|
|
pub fn header(&self) -> FileHeaderData {
|
|
|
|
self.header.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn data(&self) -> String {
|
|
|
|
self.data.clone()
|
|
|
|
}
|
|
|
|
|
2015-11-24 09:27:20 +00:00
|
|
|
pub fn contents(&self) -> (FileHeaderData, String) {
|
2015-12-02 10:34:23 +00:00
|
|
|
(self.header(), self.data())
|
2015-11-24 09:27:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn id(&self) -> FileID {
|
2015-11-24 18:28:26 +00:00
|
|
|
self.id.clone()
|
2015-11-24 09:27:20 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 10:33:58 +00:00
|
|
|
pub fn owner(&self) -> &Module {
|
|
|
|
self.owning_module
|
|
|
|
}
|
|
|
|
|
2015-11-30 18:50:24 +00:00
|
|
|
pub fn matches_with(&self, r: &Regex) -> bool {
|
|
|
|
r.is_match(&self.data[..]) || self.header.matches_with(r)
|
|
|
|
}
|
|
|
|
|
2015-11-23 18:25:27 +00:00
|
|
|
fn get_new_file_id() -> FileID {
|
|
|
|
use uuid::Uuid;
|
2015-12-02 12:42:42 +00:00
|
|
|
FileID::new(FileIDType::UUID, Uuid::new_v4().to_hyphenated_string())
|
2015-10-31 19:20:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-02 10:35:01 +00:00
|
|
|
impl<'a> Display for File<'a> {
|
2015-11-27 18:37:08 +00:00
|
|
|
|
2015-12-02 10:35:01 +00:00
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
|
|
|
write!(fmt,
|
|
|
|
"[File] Owner : '{:?}'
|
|
|
|
FileID: '{:?}'
|
|
|
|
Header: '{:?}'
|
|
|
|
Data : '{:?}'",
|
|
|
|
self.owning_module,
|
|
|
|
self.header,
|
|
|
|
self.data,
|
|
|
|
self.id);
|
|
|
|
Ok(())
|
2015-11-27 18:37:08 +00:00
|
|
|
}
|
2015-12-02 10:35:01 +00:00
|
|
|
|
2015-11-27 18:37:08 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 10:35:01 +00:00
|
|
|
impl<'a> Debug for File<'a> {
|
2015-12-02 10:34:51 +00:00
|
|
|
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
|
|
|
write!(fmt,
|
|
|
|
"[File] Owner : '{:?}'
|
|
|
|
FileID: '{:?}'
|
|
|
|
Header: '{:?}'
|
|
|
|
Data : '{:?}'",
|
|
|
|
self.owning_module,
|
|
|
|
self.header,
|
|
|
|
self.data,
|
|
|
|
self.id);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-12-03 11:19:08 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
// we use the JSON parser here, so we can generate FileHeaderData
|
|
|
|
use storage::json::parser::JsonHeaderParser;
|
|
|
|
use super::match_header_spec;
|
|
|
|
use storage::parser::{FileHeaderParser, ParserError};
|
|
|
|
use storage::file::FileHeaderData as FHD;
|
|
|
|
use storage::file::FileHeaderSpec as FHS;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_spec_matching() {
|
|
|
|
let text = String::from("{\"a\": 1, \"b\": -2}");
|
|
|
|
let spec = FHS::Map {
|
|
|
|
keys: vec![
|
|
|
|
FHS::Key {
|
|
|
|
name: String::from("a"),
|
|
|
|
value_type: Box::new(FHS::UInteger)
|
|
|
|
},
|
|
|
|
FHS::Key {
|
|
|
|
name: String::from("b"),
|
|
|
|
value_type: Box::new(FHS::Integer)
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
let parser = JsonHeaderParser::new(Some(spec.clone()));
|
|
|
|
let datares = parser.read(Some(text.clone()));
|
|
|
|
assert!(datares.is_ok(), "Text could not be parsed: '{}'", text);
|
|
|
|
let data = datares.unwrap();
|
|
|
|
|
|
|
|
let matchres = match_header_spec(&spec, &data);
|
|
|
|
assert!(matchres.is_none(), "Matching returns error: {:?}", matchres);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|