From f24123f943abaebffd098a12069bcca62181f862 Mon Sep 17 00:00:00 2001 From: SoniEx2 Date: Sat, 6 Aug 2022 15:06:18 -0300 Subject: Fix predicates --- src/pattern.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'src/pattern.rs') diff --git a/src/pattern.rs b/src/pattern.rs index 3a8c91f..2e69714 100644 --- a/src/pattern.rs +++ b/src/pattern.rs @@ -1,6 +1,8 @@ // Copyright (C) 2021-2022 Soni L. // SPDX-License-Identifier: MIT OR Apache-2.0 +//! Datafu Patterns. + use std::borrow::Borrow; use std::collections::BTreeMap; @@ -15,12 +17,57 @@ use crate::parser::parse; use crate::vm::PatternConstants; //use crate::vm::MAX_CALLS; +/// A compiled Datafu pattern. +/// +/// # Examples +/// +/// ``` +/// use datafu::Pattern; +/// +/// let pattern = Pattern::<()>::compile::<&str, &str>( +/// "->'hello'", +/// None, None +/// ).expect("failed to compile pattern"); +/// ``` pub struct Pattern { consts: PatternConstants, } impl Pattern { /// Compiles the input into a pattern. + /// + /// # Examples + /// + /// ``` + /// use datafu::Pattern; + /// use serde::Deserialize; + /// use charx; + /// + /// let preds = vec![ + /// ("dict", datafu::pred(|v| { todo!() })), + /// ("str", datafu::pred(|v| { String::deserialize(v).is_ok() })), + /// ("commit", datafu::pred(|v| { + /// if let Ok(v) = String::deserialize(v) { + /// v.len() == 40 && v.trim_start_matches( + /// charx::is_ascii_hexdigit + /// ).is_empty() + /// } else { + /// false + /// } + /// })), + /// ("uri", datafu::pred(|v| { todo!() })), + /// ("bool", datafu::pred(|v| { todo!() })), + /// ].into_iter().collect(); + /// let pattern = Pattern::<()>::compile::<&str, &str>(" + /// ->'projects':$dict + /// ->commit[:?$str:?$commit]:?$dict + /// ->url[:?$str:?$uri]:?$dict + /// ->branch:?$dict + /// (->active'active'?:?$bool) + /// (->federate'federate'?:?$bool)?", + /// Some(preds), None + /// ).expect("failed to compile pattern"); + /// ``` pub fn compile<'s, PKey, OKey>( input: &'s str, preds: Option>>, -- cgit 1.4.1