From caee02c33300bbb5bedf560d37ff99fdd0276cfb Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 30 Oct 2015 16:50:43 +0100 Subject: [PATCH] Add algorithm to match spec on data --- src/storage/file.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/storage/file.rs b/src/storage/file.rs index bc1c57c9..302105c0 100644 --- a/src/storage/file.rs +++ b/src/storage/file.rs @@ -96,3 +96,42 @@ impl Display for MatchError { } +pub fn match_header_spec(spec: &FileHeaderSpec, data: &FileHeaderData) + -> Option +{ + let tpl = (spec, data); + match tpl { + (&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} + ) => { + if kname != n { + // error + } + return match_header_spec(&*vtype, &*val); + } + + ( + &FileHeaderSpec::Array{allowed_types: ref vtypes}, + &FileHeaderData::Array{values: ref vs} + ) => { + for (t, v) in vtypes.iter().zip(vs.iter()) { + let res = match_header_spec(t, v); + if res.is_some() { + return res; + } + } + } + + _ => { unreachable!() } + } + None +} +