summary refs log tree commit diff stats
path: root/src/pattern.rs
diff options
context:
space:
mode:
authorSoniEx2 <endermoneymod@gmail.com>2022-08-06 15:06:18 -0300
committerSoniEx2 <endermoneymod@gmail.com>2022-08-06 15:06:18 -0300
commitf24123f943abaebffd098a12069bcca62181f862 (patch)
treedd36cc3cafc0ec71fc388b610ec127bb53b657bf /src/pattern.rs
parenta8778ff35bde88bb63d9fec769edf66e68d7969e (diff)
Fix predicates
Diffstat (limited to 'src/pattern.rs')
-rw-r--r--src/pattern.rs47
1 files changed, 47 insertions, 0 deletions
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<O: Serialize> {
     consts: PatternConstants<O>,
 }
 
 impl<O: Serialize> Pattern<O> {
     /// 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<BTreeMap<PKey, Box<Predicate>>>,