From 41254c58c9fface69f7db39b5236a7aed319047c Mon Sep 17 00:00:00 2001 From: SoniEx2 Date: Sat, 8 Apr 2023 22:08:23 -0300 Subject: Clean up --- Cargo.toml | 3 +-- src/lib.rs | 5 ++-- src/parser.rs | 17 ------------- src/pattern.rs | 72 +++++++++++++++++++++++++++++----------------------- tests/basic_match.rs | 72 +++++++++++++++++++++++++++++++++++++++------------- 5 files changed, 98 insertions(+), 71 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e16edb7..748ae41 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "datafu" -version = "0.1.0" +version = "0.1.0-alpha.1" authors = ["SoniEx2 "] license = "MIT OR Apache-2.0" description = "A Rust library for extracting data from config objects and other arbitrary object graphs." @@ -17,7 +17,6 @@ impl_trait = "0.1.7" indexmap = "1.9.1" regex = "1" serde = "1.0.140" -# serde_transmute = "0.1.4" smallvec = "1.10.0" [dev-dependencies] diff --git a/src/lib.rs b/src/lib.rs index 897618b..27d6c30 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -87,6 +87,7 @@ //! //! # Examples //! +// FIXME check if this is actually the case :v //! The Datafu pattern //! //! ```datafu @@ -111,7 +112,6 @@ pub mod errors; mod graph; -//pub mod type_tree; mod parser; mod pattern; mod vm; @@ -119,6 +119,7 @@ mod vm; pub use pattern::Pattern; pub use pattern::PatternBuilder; +// TODO remove/redesign /// A predicate. type Predicate = dyn (Fn( &mut dyn erased_serde::Deserializer<'_> @@ -139,7 +140,7 @@ type Predicate = dyn (Fn( /// /// But this does: /// -/// ```rust +/// ```rust,ignore /// use serde::Deserialize; /// /// let x = datafu::pred(|v| String::deserialize(v).is_ok()); diff --git a/src/parser.rs b/src/parser.rs index 744ab0f..e58107e 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -33,13 +33,6 @@ macro_rules! bry { }; } -// the following macros rely on unlabeled-break-through-labeled-block being an -// error. -// NOTE: always test changes to this module on nightly! -// still waiting for label-break-value stabilization... - -#[cfg(not(feature = "stable"))] -/// labeled block. on nightly: better compile errors. but also works on stable. macro_rules! lblock { // unfortunately ($l:lifetime : $b:block) => { $l: $b } didn't work. ($($t:tt)+) => { @@ -47,16 +40,6 @@ macro_rules! lblock { } } -#[cfg(feature = "stable")] -/// labeled block. on nightly: better compile errors. but also works on stable. -macro_rules! lblock { - ($l:lifetime : $b:block) => { - $l: loop { - break $b - } - } -} - /// Attempts to shift `s` forward by removing `prefix`. /// /// Returns whether `s` has had `prefix` removed. diff --git a/src/pattern.rs b/src/pattern.rs index 38cdcda..23d427e 100644 --- a/src/pattern.rs +++ b/src/pattern.rs @@ -24,12 +24,11 @@ use crate::vm::MAX_CALLS; /// # Examples /// /// ``` -/// use datafu::Pattern; +/// use datafu::PatternBuilder; /// -/// let pattern = Pattern::<()>::compile::<&str, &str>( +/// let pattern = PatternBuilder::for_pattern( /// "->['value']'hello'", -/// None, None -/// ).expect("failed to compile pattern"); +/// ).compile().expect("failed to compile pattern"); /// ``` pub struct Pattern { consts: PatternConstants, @@ -77,34 +76,43 @@ impl<'s> PatternBuilder<'s> { /// # 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"); + /// use datafu::PatternBuilder; + ///// 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 = PatternBuilder::for_pattern(" + ///// ->'projects':$dict + ///// ->commit[:?$str:?$commit]:?$dict + ///// ->url[:?$str:?$uri]:?$dict + ///// ->branch:?$dict + ///// (->active'active'?:?$bool) + ///// (->federate'federate'?:?$bool)?" + ///// ).compile().expect("failed to compile pattern"); + /// let pattern = PatternBuilder::for_pattern(" + /// :map + /// ->['projects'?]:map + /// ->[commit:?str]:?map + /// ->[url:?str]:?map + /// ->[branch:?str]:?map + /// (->['active'?]active:?bool)? + /// (->['federate'?]?federate:?bool)? + /// (->['pinned'?]?pinned:?bool)? + /// ").compile().expect("failed to compile pattern"); /// ``` pub fn for_pattern(pattern: &'s str) -> Self { Self { diff --git a/tests/basic_match.rs b/tests/basic_match.rs index b58457e..632a1fe 100644 --- a/tests/basic_match.rs +++ b/tests/basic_match.rs @@ -6,22 +6,22 @@ use serde::Deserialize; //use datafu::Predicate; -#[test] -fn test_basic() { - let mut der = JsonDer::from_str(r#"{"foo": 1, "bar": {"baz": 2}}"#); - let pat = datafu::PatternBuilder::for_pattern("->[x]:?map->[yk]y").compile().unwrap(); - #[derive(Deserialize)] - struct Values { - x: String, - yk: String, - y: usize, - } - let matches: Values = pat.deserialize(&mut der).unwrap().collect().unwrap(); - assert_eq!(matches.x, "bar"); - assert_eq!(matches.yk, "baz"); - assert_eq!(matches.y, 2); - assert!(der.end().is_ok()); -} +//#[test] +//fn test_basic() { +// let mut der = JsonDer::from_str(r#"{"foo": 1, "bar": {"baz": 2}}"#); +// let pat = datafu::PatternBuilder::for_pattern("->[x]:?map->[yk]y").compile().unwrap(); +// #[derive(Deserialize)] +// struct Values { +// x: String, +// yk: String, +// y: usize, +// } +// let matches: Values = pat.deserialize(&mut der).unwrap().collect().unwrap(); +// assert_eq!(matches.x, "bar"); +// assert_eq!(matches.yk, "baz"); +// assert_eq!(matches.y, 2); +// assert!(der.end().is_ok()); +//} #[test] fn test_real_use_case() { @@ -41,13 +41,14 @@ fn test_real_use_case() { {"base_url": "https://ganarchy.autistic.space", "repo_list_srcs": {"https://ganarchy.autistic.space/index.toml": {"active": false}}, "projects": {"385e734a52e13949a7a5c71827f6de920dbfea43": {"https://github.com/ganarchy/GAnarchy": {"HEAD": {"active": true}}, "https://soniex2.autistic.space/git-repos/ganarchy.git": {"HEAD": {"active": true, "pinned": true}}}, "a8fb5087f79eafe312db270082c052c427b208c2": {"https://soniex2.autistic.space/git-repos/mmorfc.git": {"HEAD": {"active": true, "pinned": true}}}, "2d0b363fe3179087de59d9ef4a2d14af21d89071": {"https://soniex2.autistic.space/git-repos/chewstuff.git": {"HEAD": {"active": true, "pinned": true}}}}} "#; let mut der = JsonDer::from_str(data); - #[derive(Deserialize)] + fn always_true() -> bool { true } + #[derive(Deserialize, PartialEq, Eq, Debug)] struct Values { commit: String, url: String, branch: String, active: bool, - #[serde(default)] + #[serde(default="always_true")] federate: bool, #[serde(default)] pinned: bool, @@ -55,6 +56,41 @@ fn test_real_use_case() { let graph = pat.deserialize(&mut der).unwrap(); let matches: Vec = graph.collect().unwrap(); assert!(der.end().is_ok()); + assert_eq!(matches.len(), 4); + assert_eq!(&*matches, &[ + Values { + commit: "385e734a52e13949a7a5c71827f6de920dbfea43".into(), + url: "https://github.com/ganarchy/GAnarchy".into(), + branch: "HEAD".into(), + active: true, + federate: true, + pinned: false, + }, + Values { + commit: "385e734a52e13949a7a5c71827f6de920dbfea43".into(), + url: "https://soniex2.autistic.space/git-repos/ganarchy.git".into(), + branch: "HEAD".into(), + active: true, + federate: true, + pinned: true, + }, + Values { + commit: "a8fb5087f79eafe312db270082c052c427b208c2".into(), + url: "https://soniex2.autistic.space/git-repos/mmorfc.git".into(), + branch: "HEAD".into(), + active: true, + federate: true, + pinned: true, + }, + Values { + commit: "2d0b363fe3179087de59d9ef4a2d14af21d89071".into(), + url: "https://soniex2.autistic.space/git-repos/chewstuff.git".into(), + branch: "HEAD".into(), + active: true, + federate: true, + pinned: true, + }, + ]); } //#[test] -- cgit 1.4.1