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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc = include_str!("../README.md")]
use melodium_core::*;
use melodium_macro::{check, mel_function, mel_package, mel_treatment};
use regex::Regex;
use std::collections::HashMap;
use std::sync::Arc;
use std_mel::data::*;
/// Matches stream of strings against a regex.
///
/// Every string coming through the `text` stream is matched against `regex`.
/// `matches` tells if matching were found or not.
/// `error` is emitted only if regex contains error.
///
/// The regex engine is Unicode-aware. Please refer to [Regex Syntax](https://docs.rs/regex/latest/regex/index.html#syntax)
/// in documentation for full syntax description.
#[mel_treatment(
input text Stream<string>
output matches Stream<bool>
output error Block<string>
)]
pub async fn matches(#[mel(content(regex))] regex: string) {
match Regex::new(®ex) {
Ok(regex) => {
error.close().await;
while let Ok(text) = text
.recv_many()
.await
.map(|values| TryInto::<Vec<string>>::try_into(values).unwrap())
{
check!(
matches
.send_many(
text.into_iter()
.map(|txt| regex.is_match(&txt))
.collect::<VecDeque<_>>()
.into()
)
.await
);
}
}
Err(err) => {
let _ = error.send_one(err.to_string().into()).await;
}
}
}
/// Matches a string against a regex.
///
/// `text` is matched against `regex`, returns wether the match were successful or not.
///
/// The regex engine is Unicode-aware. Please refer to [Regex Syntax](https://docs.rs/regex/latest/regex/index.html#syntax)
/// in documentation for full syntax description.
#[mel_function]
pub fn matches(text: string, #[mel(content(regex))] regex: string) -> bool {
match Regex::new(®ex) {
Ok(regex) => regex.is_match(&text),
Err(_) => false,
}
}
/// Find in stream of strings according to a regex.
///
/// Every string coming through the `text` stream is looked up with `regex`.
/// `found` contains the found strings (or _none_ if corresonding `text` input do not match).
/// `error` is emitted only if regex contains error.
///
/// The regex syntax is Unicode-aware. Please refer to [Regex Syntax](https://docs.rs/regex/latest/regex/index.html#syntax)
/// in documentation for full syntax description.
#[mel_treatment(
input text Stream<string>
output found Stream<Option<string>>
output error Block<string>
)]
pub async fn find(#[mel(content(regex))] regex: string) {
match Regex::new(®ex) {
Ok(regex) => {
error.close().await;
while let Ok(text) = text
.recv_many()
.await
.map(|values| TryInto::<Vec<string>>::try_into(values).unwrap())
{
let mut vec_found = VecDeque::with_capacity(text.len());
for text in text {
match regex.find(&text) {
Some(m) => {
vec_found.push_back(Some(m.as_str().to_string()).into());
}
None => {
vec_found.push_back(Value::Option(None));
}
}
}
check!(found.send_many(TransmissionValue::Other(vec_found)).await)
}
}
Err(err) => {
let _ = error.send_one(err.to_string().into()).await;
}
}
}
/// Find in string according to a regex.
///
/// The regex syntax is Unicode-aware. Please refer to [Regex Syntax](https://docs.rs/regex/latest/regex/index.html#syntax)
/// in documentation for full syntax description.
#[mel_function]
pub fn find(text: string, #[mel(content(regex))] regex: string) -> Option<string> {
match Regex::new(®ex) {
Ok(regex) => regex.find(&text).map(|m| m.as_str().to_string()),
Err(_) => None,
}
}
/// Captures groups of text according to a regex.
///
/// Every string coming through the `text` stream is passed through `regex`.
/// `captured` contains the **named** groups contents (or _none_ if group is not captured).
/// `error` is emitted only if regex contains error.
///
/// The regex syntax is Unicode-aware. Please refer to [Regex Syntax](https://docs.rs/regex/latest/regex/index.html#syntax)
/// in documentation for full syntax description.
#[mel_treatment(
input text Stream<string>
output captured Stream<Option<Map>>
output error Block<string>
)]
pub async fn capture(#[mel(content(regex))] regex: string) {
match Regex::new(®ex) {
Ok(regex) => {
error.close().await;
while let Ok(text) = text
.recv_many()
.await
.map(|values| TryInto::<Vec<string>>::try_into(values).unwrap())
{
let mut vec_captured = VecDeque::with_capacity(text.len());
for text in text {
match regex.captures(&text) {
Some(captures) => {
let mut map_captured = HashMap::new();
for name in regex.capture_names() {
if let Some(name) = name {
if let Some(cap) = captures.name(name) {
map_captured.insert(
name.to_string(),
Value::String(cap.as_str().to_string()),
);
}
}
}
vec_captured.push_back(Value::Option(Some(Box::new(Value::Data(
Arc::new(Map::new_with(map_captured)),
)))));
}
None => {
vec_captured.push_back(Value::Option(None));
}
}
}
check!(
captured
.send_many(TransmissionValue::Other(vec_captured))
.await
)
}
}
Err(err) => {
let _ = error.send_one(err.to_string().into()).await;
}
}
}
/// Captures groups of text according to a regex.
///
/// If match, return a `Map` containing the captured **named** groups.
///
/// The regex syntax is Unicode-aware. Please refer to [Regex Syntax](https://docs.rs/regex/latest/regex/index.html#syntax)
/// in documentation for full syntax description.
#[mel_function]
pub fn capture(text: string, #[mel(content(regex))] regex: string) -> Option<Map> {
match Regex::new(®ex) {
Ok(regex) => match regex.captures(&text) {
Some(captures) => {
let mut map_captured = HashMap::new();
for name in regex.capture_names() {
if let Some(name) = name {
if let Some(cap) = captures.name(name) {
map_captured
.insert(name.to_string(), Value::String(cap.as_str().to_string()));
}
}
}
Some(Map::new_with(map_captured))
}
None => None,
},
Err(_) => None,
}
}
/// Replace text according to a regex.
///
/// Every string coming through the `text` stream is passed through `regex`,
/// and `replacer` is applied.
/// `error` is emitted only if regex contains error.
///
/// The regex syntax is Unicode-aware. Please refer to [Regex Syntax](https://docs.rs/regex/latest/regex/index.html#syntax)
/// in documentation for full syntax description.
#[mel_treatment(
input text Stream<string>
output replaced Stream<string>
output error Block<string>
)]
pub async fn replace(#[mel(content(regex))] regex: string, replacer: string) {
match Regex::new(®ex) {
Ok(regex) => {
error.close().await;
while let Ok(text) = text
.recv_many()
.await
.map(|values| TryInto::<Vec<string>>::try_into(values).unwrap())
{
let mut vec_replaced = Vec::with_capacity(text.len());
for text in text {
vec_replaced.push(regex.replace(&text, &replacer).to_string());
}
check!(replaced.send_many(vec_replaced.into()).await);
}
}
Err(err) => {
let _ = error.send_one(err.to_string().into()).await;
}
}
}
/// Replace text according to a regex and replacer.
///
/// Return string with replaced content, or _none_ if an error in regex occured.
///
/// The regex syntax is Unicode-aware. Please refer to [Regex Syntax](https://docs.rs/regex/latest/regex/index.html#syntax)
/// in documentation for full syntax description.
#[mel_function]
pub fn replace(
text: string,
#[mel(content(regex))] regex: string,
replacer: string,
) -> Option<string> {
match Regex::new(®ex) {
Ok(regex) => Some(regex.replace(&text, &replacer).to_string()),
Err(_) => None,
}
}
mel_package!();