Skip to content

Commit

Permalink
trying DynEq; tag 0.0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Jun 3, 2024
1 parent 06abcdb commit 857da08
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

- uses: calcit-lang/[email protected]
with:
version: "0.8.52"
version: "0.8.56"

- uses: dtolnay/rust-toolchain@stable
with:
Expand Down
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "calcit_regex"
version = "0.0.7"
version = "0.0.8"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2021"

Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion calcit.cirru

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion compact.cirru
Original file line number Diff line number Diff line change
@@ -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 $ {}
Expand Down
35 changes: 26 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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::<Regex>() {
// does not compare the regex pattern
false
} else {
false
}
}
}

#[no_mangle]
pub fn abi_version() -> String {
String::from("0.0.9")
Expand All @@ -14,8 +32,7 @@ pub fn re_pattern(args: Vec<Edn>) -> Result<Edn, String> {
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)),
Expand All @@ -36,7 +53,7 @@ pub fn re_matches(args: Vec<Edn>) -> Result<Edn, String> {
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::<Regex>() {
if let Some(pattern) = p.read().map_err(|e| e.to_string())?.as_any().downcast_ref::<Regex>() {
Ok(Edn::Bool(pattern.is_match(s)))
} else {
Err(format!("re-matches expected a regex, got {:?}", p))
Expand All @@ -63,7 +80,7 @@ pub fn re_find_index(args: Vec<Edn>) -> Result<Edn, String> {
}
}
(Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => {
if let Some(pattern) = p.read().map_err(|e| e.to_string())?.downcast_ref::<Regex>() {
if let Some(pattern) = p.read().map_err(|e| e.to_string())?.as_any().downcast_ref::<Regex>() {
match pattern.find(s) {
Some(matched) => Ok(Edn::Number(matched.start() as f64)),
None => Ok(Edn::Number(-1.0)), // TODO maybe nil
Expand Down Expand Up @@ -97,7 +114,7 @@ pub fn re_find(args: Vec<Edn>) -> Result<Edn, String> {
}
}
(Edn::Str(s), Edn::AnyRef(EdnAnyRef(p))) => {
if let Some(pattern) = p.read().map_err(|e| e.to_string())?.downcast_ref::<Regex>() {
if let Some(pattern) = p.read().map_err(|e| e.to_string())?.as_any().downcast_ref::<Regex>() {
let mut matched = pattern.find_iter(s);
match matched.next() {
Some(v) => Ok(Edn::str(v.as_str().to_string())),
Expand Down Expand Up @@ -129,7 +146,7 @@ pub fn re_find_all(args: Vec<Edn>) -> Result<Edn, String> {
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::<Regex>() {
if let Some(pattern) = p.read().map_err(|e| e.to_string())?.as_any().downcast_ref::<Regex>() {
let mut ys: Vec<Edn> = vec![];
for v in pattern.find_iter(s) {
ys.push(Edn::Str(v.as_str().to_string().into()))
Expand Down Expand Up @@ -161,7 +178,7 @@ pub fn re_split(args: Vec<Edn>) -> Result<Edn, String> {
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::<Regex>() {
if let Some(pattern) = p.read().map_err(|e| e.to_string())?.as_any().downcast_ref::<Regex>() {
let mut ys: Vec<Edn> = vec![];
for piece in pattern.split(s) {
ys.push(Edn::str(piece));
Expand All @@ -187,7 +204,7 @@ pub fn re_replace_all(args: Vec<Edn>) -> Result<Edn, String> {
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::<Regex>() {
if let Some(pattern) = p.read().map_err(|e| e.to_string())?.as_any().downcast_ref::<Regex>() {
Ok(Edn::str(pattern.replace_all(s, &**next).into_owned()))
} else {
Err(format!("re-replace-all expected a regex, got {:?}", p))
Expand Down

0 comments on commit 857da08

Please sign in to comment.