1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
//! Optional module to perform matching and replacement of regular expressions on [`Value::String`].
//!
//! # Regex
//!
//! This modules uses the [`regex_lite`] crate and can be included using the `regex` feature.
use regex_lite::{Captures, Regex};
use crate::{StaticEnvironment, Value};
use super::{default_number, default_string, NativeError, NativeResult};
/// Extends a [`StaticEnvironment`] with `regex` functions.
pub fn extend_environment(env: &mut StaticEnvironment) {
    env.add_function("re_is_match", Some(2), 0, is_match);
    env.add_function("re_find", Some(2), 0, find);
    env.add_function("re_capture", Some(2), 0, capture);
    env.add_function("re_replace", Some(4), 2, replace);
}
/// Checks if a regex matches a [`Value::String`].
///
/// # Errors
///
/// Will return [`NativeError::WrongParameterCount`] if there is a mismatch in the supplied parameters.
/// Will return [`NativeError::WrongParameterType`] if the the supplied parameters have the wrong type.
/// Will return [`NativeError::CustomError`] if the regex produces an error.
pub fn is_match(params: &[Value]) -> NativeResult {
    match params {
        [Value::String(haystack), Value::String(needle)] => {
            let re = Regex::new(needle).map_err(|e| NativeError::from(e.to_string()))?;
            Ok(Value::Boolean(re.is_match(haystack)))
        }
        [_, _] => Err(NativeError::WrongParameterType),
        _ => Err(NativeError::WrongParameterCount(2)),
    }
}
/// Finds non overlapping matches for a given regex inside a [`Value::String`].
/// Returns a [`Value::Array`] containing all matches.
///
/// # Errors
///
/// Will return [`NativeError::WrongParameterCount`] if there is a mismatch in the supplied parameters.
/// Will return [`NativeError::WrongParameterType`] if the the supplied parameters have the wrong type.
/// Will return [`NativeError::CustomError`] if the regex produces an error.
pub fn find(params: &[Value]) -> NativeResult {
    match params {
        [Value::String(haystack), Value::String(re)] => {
            let re = Regex::new(re).map_err(|e| NativeError::from(e.to_string()))?;
            let groups: Vec<Value> = re
                .find_iter(haystack)
                .map(|m| Value::String(m.as_str().to_string()))
                .collect();
            Ok(Value::Array(groups))
        }
        [_, _] => Err(NativeError::WrongParameterType),
        _ => Err(NativeError::WrongParameterCount(2)),
    }
}
/// Extract a [`Value::Array`] from a [`Captures`] struct while preserving empty captures
/// as empty strings.
fn get_capture_groups(captures: Captures) -> Vec<Value> {
    captures
        .iter()
        .map(|c| c.map_or("", |m| m.as_str()))
        .map(|m| Value::String(m.to_string()))
        .collect()
}
/// Returns the matches of all regex capture groups inside a [`Value::Array`]. The first element is the full match.
/// Index 1 to N are the capture groups.
///
/// # Errors
///
/// Will return [`NativeError::WrongParameterCount`] if there is a mismatch in the supplied parameters.
/// Will return [`NativeError::WrongParameterType`] if the the supplied parameters have the wrong type.
/// Will return [`NativeError::CustomError`] if the regex produces an error.
pub fn capture(params: &[Value]) -> NativeResult {
    match params {
        [Value::String(haystack), Value::String(re)] => {
            let re = Regex::new(re).map_err(|e| NativeError::from(e.to_string()))?;
            let groups: Vec<Value> = re.captures(haystack).map_or_else(
                || vec![Value::String(String::new()); re.captures_len()],
                get_capture_groups,
            );
            Ok(Value::Array(groups))
        }
        [_, _] => Err(NativeError::WrongParameterType),
        _ => Err(NativeError::WrongParameterCount(2)),
    }
}
/// Replaces all regex matches inside a [`Value::String`] with a replacement [`Value::String`].
///
/// # Errors
///
/// Will return [`NativeError::WrongParameterCount`] if there is a mismatch in the supplied parameters.
/// Will return [`NativeError::WrongParameterType`] if the the supplied parameters have the wrong type.
/// Will return [`NativeError::CustomError`] if the regex produces an error.
pub fn replace(params: &[Value]) -> NativeResult {
    let rep = default_string(params, 2, "")?;
    let limit = default_number(params, 3, 0.0)? as usize;
    match params {
        [Value::String(haystack), Value::String(needle), ..] => {
            let re = Regex::new(needle).map_err(|e| NativeError::from(e.to_string()))?;
            Ok(Value::String(re.replacen(haystack, limit, rep).to_string()))
        }
        [_, _] => Err(NativeError::WrongParameterType),
        _ => Err(NativeError::WrongParameterCount(2)),
    }
}
#[cfg(test)]
mod test {
    use std::vec;
    use super::*;
    use crate::Value;
    #[test]
    fn re_is_match() {
        assert_eq!(
            Ok(Value::Boolean(true)),
            is_match(&vec![
                Value::String(String::from("Hello World")),
                Value::String(String::from(".*World"))
            ])
        );
        assert_eq!(
            Ok(Value::Boolean(true)),
            is_match(&vec![
                Value::String(String::from(
                    "I categorically deny having triskaidekaphobia."
                )),
                Value::String(String::from(r"\b\w{13}\b"))
            ])
        );
    }
    #[test]
    fn re_find() {
        assert_eq!(
            Ok(Value::Array(vec![
                Value::String(String::from("100")),
                Value::String(String::from("200")),
                Value::String(String::from("300"))
            ])),
            find(&vec![
                Value::String(String::from("10 20 30 100 200 300 1000 2000 3000")),
                Value::String(String::from(r"\b\d{3}\b"))
            ])
        );
    }
    #[test]
    fn re_capture() {
        assert_eq!(
            Ok(Value::Array(vec![
                Value::String(String::from("2023-09-30")),
                Value::String(String::from("2023")),
                Value::String(String::from("09")),
                Value::String(String::from("30"))
            ])),
            capture(&vec![
                Value::String(String::from("2023-09-30")),
                Value::String(String::from(r"(\d{4})-(\d{2})-(\d{2})"))
            ])
        );
    }
    #[test]
    fn re_replace() {
        assert_eq!(
            Ok(Value::String(String::from("9999-09-30"))),
            replace(&vec![
                Value::String(String::from("2023-09-30")),
                Value::String(String::from(r"\d{4}")),
                Value::String(String::from("9999"))
            ])
        );
        assert_eq!(
            Ok(Value::String(String::from("2023-9999-9999"))),
            replace(&vec![
                Value::String(String::from("2023-09-30")),
                Value::String(String::from(r"\b\d{2}\b")),
                Value::String(String::from("9999")),
            ])
        );
        assert_eq!(
            Ok(Value::String(String::from("2023-9999-30"))),
            replace(&vec![
                Value::String(String::from("2023-09-30")),
                Value::String(String::from(r"\b\d{2}\b")),
                Value::String(String::from("9999")),
                Value::Number(1.0)
            ])
        );
    }
}