From 857da088662853c0e5e738e9c7722346b5f34e5a Mon Sep 17 00:00:00 2001 From: tiye Date: Tue, 4 Jun 2024 04:31:41 +0800 Subject: [PATCH] trying DynEq; tag 0.0.8 --- .github/workflows/check.yaml | 2 +- Cargo.lock | 10 +++++----- Cargo.toml | 4 ++-- calcit.cirru | 2 +- compact.cirru | 2 +- src/lib.rs | 35 ++++++++++++++++++++++++++--------- 6 files changed, 36 insertions(+), 19 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 94c95f9..0ee3a52 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -16,7 +16,7 @@ jobs: - uses: calcit-lang/setup-cr@0.0.3 with: - version: "0.8.52" + version: "0.8.56" - uses: dtolnay/rust-toolchain@stable with: diff --git a/Cargo.lock b/Cargo.lock index 5c8de03..7fdc697 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,7 +32,7 @@ dependencies = [ [[package]] name = "calcit_regex" -version = "0.0.7" +version = "0.0.8" dependencies = [ "cirru_edn", "cirru_parser", @@ -42,9 +42,9 @@ dependencies = [ [[package]] name = "cirru_edn" -version = "0.6.8" +version = "0.6.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4aa8a3c939e1480883ea1371deab36c179e621779f9a7731554b9bec3840189" +checksum = "e3d12eb005cacee402564d56df5f2e2ee66b1c9440f3de3a972fd4aa0ff5a283" dependencies = [ "bincode", "cirru_parser", @@ -54,9 +54,9 @@ dependencies = [ [[package]] name = "cirru_parser" -version = "0.1.29" +version = "0.1.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c96247e15778d471f8c94b321fa11220dfb987546fcf8fb73b1a5c2b3a018a78" +checksum = "320a8dd7792dfd18c39e2b6d06e894028cf2ce0dfb2fcd4964fb730dc37ea150" dependencies = [ "bincode", ] diff --git a/Cargo.toml b/Cargo.toml index 637ac30..74d2534 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "calcit_regex" -version = "0.0.7" +version = "0.0.8" authors = ["jiyinyiyong "] edition = "2021" @@ -13,7 +13,7 @@ crate-type = ["dylib"] # Creates dynamic lib # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -cirru_edn = "0.6.8" +cirru_edn = "0.6.10" # cirru_edn = { path = "/Users/chenyong/repo/cirru/edn.rs" } cirru_parser = "0.1.29" regex = "1.10.4" diff --git a/calcit.cirru b/calcit.cirru index ee9b347..cf9e79a 100644 --- a/calcit.cirru +++ b/calcit.cirru @@ -1,6 +1,6 @@ {} (:package |regex) - :configs $ {} (:init-fn |regex.test/main!) (:port 6001) (:reload-fn |regex.test/reload!) (:version |0.0.7) + :configs $ {} (:init-fn |regex.test/main!) (:port 6001) (:reload-fn |regex.test/reload!) (:version |0.0.8) :modules $ [] :entries $ {} :files $ {} diff --git a/compact.cirru b/compact.cirru index 0d654ec..4c7b876 100644 --- a/compact.cirru +++ b/compact.cirru @@ -1,6 +1,6 @@ {} (:package |regex) - :configs $ {} (:init-fn |regex.test/main!) (:reload-fn |regex.test/reload!) (:version |0.0.7) + :configs $ {} (:init-fn |regex.test/main!) (:reload-fn |regex.test/reload!) (:version |0.0.8) :modules $ [] :entries $ {} :files $ {} diff --git a/src/lib.rs b/src/lib.rs index 77af81e..b3ca400 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,26 @@ use std::sync::{Arc, RwLock}; -use cirru_edn::{Edn, EdnAnyRef, EdnListView}; +use cirru_edn::{DynEq, Edn, EdnAnyRef, EdnListView}; use regex::Regex; +#[derive(Debug)] +struct RegexWrapper(pub Regex); + +impl DynEq for RegexWrapper { + fn as_any(&self) -> &dyn std::any::Any { + &self.0 + } + + fn do_eq(&self, rhs: &dyn DynEq) -> bool { + if let Some(_rhs_concrete) = rhs.as_any().downcast_ref::() { + // does not compare the regex pattern + false + } else { + false + } + } +} + #[no_mangle] pub fn abi_version() -> String { String::from("0.0.9") @@ -14,8 +32,7 @@ pub fn re_pattern(args: Vec) -> Result { match &args[0] { Edn::Str(s) => match Regex::new(s) { Ok(pattern) => { - let p = Arc::from(RwLock::new(pattern)); - + let p = Arc::from(RwLock::new(RegexWrapper(pattern))); Ok(Edn::AnyRef(EdnAnyRef(p))) } Err(e) => Err(format!("re-pattern failed, {}", e)), @@ -36,7 +53,7 @@ pub fn re_matches(args: Vec) -> Result { Err(e) => Err(format!("re-matches failed, {}", e)), }, (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => { - if let Some(pattern) = p.read().map_err(|e| e.to_string())?.downcast_ref::() { + if let Some(pattern) = p.read().map_err(|e| e.to_string())?.as_any().downcast_ref::() { Ok(Edn::Bool(pattern.is_match(s))) } else { Err(format!("re-matches expected a regex, got {:?}", p)) @@ -63,7 +80,7 @@ pub fn re_find_index(args: Vec) -> Result { } } (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => { - if let Some(pattern) = p.read().map_err(|e| e.to_string())?.downcast_ref::() { + if let Some(pattern) = p.read().map_err(|e| e.to_string())?.as_any().downcast_ref::() { match pattern.find(s) { Some(matched) => Ok(Edn::Number(matched.start() as f64)), None => Ok(Edn::Number(-1.0)), // TODO maybe nil @@ -97,7 +114,7 @@ pub fn re_find(args: Vec) -> Result { } } (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => { - if let Some(pattern) = p.read().map_err(|e| e.to_string())?.downcast_ref::() { + if let Some(pattern) = p.read().map_err(|e| e.to_string())?.as_any().downcast_ref::() { let mut matched = pattern.find_iter(s); match matched.next() { Some(v) => Ok(Edn::str(v.as_str().to_string())), @@ -129,7 +146,7 @@ pub fn re_find_all(args: Vec) -> Result { Err(e) => Err(format!("re-find-all failed, {}", e)), }, (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => { - if let Some(pattern) = p.read().map_err(|e| e.to_string())?.downcast_ref::() { + if let Some(pattern) = p.read().map_err(|e| e.to_string())?.as_any().downcast_ref::() { let mut ys: Vec = vec![]; for v in pattern.find_iter(s) { ys.push(Edn::Str(v.as_str().to_string().into())) @@ -161,7 +178,7 @@ pub fn re_split(args: Vec) -> Result { Err(e) => Err(format!("re-split failed, {}", e)), }, (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => { - if let Some(pattern) = p.read().map_err(|e| e.to_string())?.downcast_ref::() { + if let Some(pattern) = p.read().map_err(|e| e.to_string())?.as_any().downcast_ref::() { let mut ys: Vec = vec![]; for piece in pattern.split(s) { ys.push(Edn::str(piece)); @@ -187,7 +204,7 @@ pub fn re_replace_all(args: Vec) -> Result { Err(e) => Err(format!("re-replace-all failed, {}", e)), }, (Edn::Str(s), Edn::AnyRef(EdnAnyRef(p)), Edn::Str(next)) => { - if let Some(pattern) = p.read().map_err(|e| e.to_string())?.downcast_ref::() { + if let Some(pattern) = p.read().map_err(|e| e.to_string())?.as_any().downcast_ref::() { Ok(Edn::str(pattern.replace_all(s, &**next).into_owned())) } else { Err(format!("re-replace-all expected a regex, got {:?}", p))