Implement FileHeaderData::matches_with(&Regex)

This commit is contained in:
Matthias Beyer 2015-12-02 11:30:50 +01:00
parent 8f8badebb3
commit 58bd36599f

View file

@ -5,6 +5,8 @@ use std::fmt;
use super::parser::FileHeaderParser; use super::parser::FileHeaderParser;
use storage::file_id::*; use storage::file_id::*;
use regex::Regex;
#[derive(Debug)] #[derive(Debug)]
pub enum FileHeaderSpec { pub enum FileHeaderSpec {
Null, Null,
@ -56,6 +58,28 @@ impl Display for FileHeaderSpec {
} }
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> { pub struct MatchError<'a> {
summary: String, summary: String,
expected: &'a FileHeaderSpec, expected: &'a FileHeaderSpec,