diff options
Diffstat (limited to 'src/pattern.rs')
-rw-r--r-- | src/pattern.rs | 66 |
1 files changed, 42 insertions, 24 deletions
diff --git a/src/pattern.rs b/src/pattern.rs index fc3c8a7..0fc6acd 100644 --- a/src/pattern.rs +++ b/src/pattern.rs @@ -7,7 +7,7 @@ use std::borrow::Borrow; use std::collections::BTreeMap; use serde::de::Deserialize; -use serde::de::DeserializeSeed; +use serde::de::DeserializeSeed as _; use serde::de::Deserializer; use serde::ser::Serialize; @@ -26,7 +26,7 @@ use crate::vm::MAX_CALLS; /// use datafu::Pattern; /// /// let pattern = Pattern::<()>::compile::<&str, &str>( -/// "->'hello'", +/// "->['value']'hello'", /// None, None /// ).expect("failed to compile pattern"); /// ``` @@ -35,7 +35,28 @@ pub struct Pattern<O: Serialize> { } impl<O: Serialize> Pattern<O> { - /// Compiles the input into a pattern. + /// Matches the pattern against an input. + pub fn deserialize<'de, Der, De>(&self, der: Der) -> Result<De, Der::Error> + where + Der: Deserializer<'de>, + De: Deserialize<'de>, + { + let mut err = Default::default(); + let interp = vm::Interpreter::new(&self.consts, &mut err); + let pack = vm::Packer::new(interp, MAX_CALLS).deserialize(der)?; + let de = De::deserialize(vm::Unpacker::new(pack.0, MAX_CALLS)); + todo!() + } +} + +pub struct PatternBuilder<'s, PKey=&'static str, OKey=&'static str, O=()> { + input: &'s str, + preds: Option<BTreeMap<PKey, Box<Predicate>>>, + objs: Option<BTreeMap<OKey, O>>, +} + +impl<'s> PatternBuilder<'s> { + /// Creates a PatternBuilder for a given pattern. /// /// # Examples /// @@ -69,28 +90,25 @@ impl<O: Serialize> Pattern<O> { /// Some(preds), None /// ).expect("failed to compile pattern"); /// ``` - pub fn compile<'s, PKey, OKey>( - input: &'s str, - preds: Option<BTreeMap<PKey, Box<Predicate>>>, - objs: Option<BTreeMap<OKey, O>> - ) -> Result<Self, PatternError<'s>> - where - PKey: Borrow<str> + Ord, - OKey: Borrow<str> + Ord, - { - Ok(Self { - consts: parse(input, preds, objs)? - }) + pub fn for_pattern(pattern: &'s str) -> Self { + Self { + input: pattern, + preds: None, + objs: None, + } } +} - /// Matches the pattern against an input. - pub fn deserialize<'de, Der, De>(&self, der: Der) -> Result<De, Der::Error> - where - Der: Deserializer<'de>, - De: Deserialize<'de>, - { - let pack = vm::Packer::new(&self.consts, MAX_CALLS).deserialize(der)?; - let de = De::deserialize(vm::Unpacker::new(pack, MAX_CALLS)); - todo!() +impl<'s, PKey, OKey, O> PatternBuilder<'s, PKey, OKey, O> +where + PKey: Borrow<str> + Ord, + OKey: Borrow<str> + Ord, + O: Serialize, +{ + /// Compiles the pattern. + pub fn compile(self) -> Result<Pattern<O>, PatternError<'s>> { + Ok(Pattern { + consts: parse(self.input, self.preds, self.objs)? + }) } } |