Skip to content

Commit

Permalink
feat(andax/fns/kokoro): new find_all() function
Browse files Browse the repository at this point in the history
  • Loading branch information
madonuko committed Dec 13, 2024
1 parent 3ab6232 commit 9096e07
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions andax/fns/kokoro.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::error::{AndaxError as AErr, AndaxRes};
use crate::run::rf;
use regex::Regex;
use rhai::{
plugin::{
Expand Down Expand Up @@ -29,16 +28,22 @@ pub mod ar {
serde_json::from_str(&a).ehdl(&ctx)
}
#[rhai_fn(return_raw, global)]
pub fn find(ctx: NativeCallContext, r: &str, text: &str, group: i64) -> Res<String> {
pub fn find(ctx: NativeCallContext, r: &str, text: &str, group: usize) -> Res<String> {
let captures = Regex::new(r).ehdl(&ctx)?.captures(text);
let cap = captures.ok_or_else(|| format!("Can't match regex: {r}\nText: {text}"))?;
Ok(cap
.get(rf(&ctx, group.try_into().map_err(color_eyre::Report::new))?)
.ok_or_else(|| format!("Can't get group: {r}\nText: {text}"))?
Ok((cap.get(group).ok_or_else(|| format!("Can't get group: {r}\nText: {text}"))?)
.as_str()
.into())
}
#[rhai_fn(return_raw, global)]
pub fn find_all(ctx: NativeCallContext, r: &str, text: &str) -> Res<Vec<Vec<Option<String>>>> {
Ok(Regex::new(r)
.ehdl(&ctx)?
.captures_iter(text)
.map(|cap| cap.iter().map(|m| m.map(|m| m.as_str().to_owned())).collect())
.collect())
}
#[rhai_fn(return_raw, global)]
pub fn sub(ctx: NativeCallContext, r: &str, rep: &str, text: &str) -> Res<String> {
Ok(Regex::new(r).ehdl(&ctx)?.replace_all(text, rep).into())
}
Expand Down

0 comments on commit 9096e07

Please sign in to comment.