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
use nadi_plugin::nadi_internal_plugin;
/// This plugin provides the functions for the regex operations in
/// string. Refer to the
/// [regex](https://docs.rs/regex/latest/regex/index.html) crate for
/// more details on the regex patterns.
#[nadi_internal_plugin]
mod regex {
use nadi_plugin::env_func;
use regex::Regex;
/// Filter from the string list with only the values matching pattern
///
/// ```task
/// env assert_eq(str_filter(["abc", "and", "xyz"], "^a"), ["abc", "and"])
/// ```
#[env_func]
fn str_filter(
/// attribute to check for pattern
#[relaxed]
attrs: Vec<String>,
/// Regex pattern to match
pattern: Regex,
) -> Vec<String> {
attrs.into_iter().filter(|a| pattern.is_match(a)).collect()
}
/// Check if the given pattern matches the value or not
///
/// You can also use match operator for this
///
/// ```task
/// env assert_eq(str_match("abc", "^a"), true)
/// env assert_eq(str_match("abc", "^a"), "abc" match "^a")
/// ```
#[env_func]
fn str_match(
/// string to check for pattern
attr: &str,
/// Regex pattern to match
pattern: Regex,
) -> bool {
pattern.is_match(attr)
}
/// Replace the occurances of the given match
///
/// ```task
/// env assert_eq(str_replace("abc", "^a", "2"), "2bc")
/// env assert_eq(str_replace("abc", "[abc]", "2"), "222")
/// ```
#[env_func]
fn str_replace(
/// original string
attr: &str,
/// Regex pattern to match
pattern: Regex,
/// replacement string
rep: &str,
) -> String {
pattern.replace_all(attr, rep).to_string()
}
/// Find the given pattern in the value
///
/// ```task
/// env assert_eq(str_find("abc", "^[ab]"), "a")
/// ```
#[env_func]
fn str_find(
/// string to check for pattern
attr: &str,
/// Regex pattern to match
pattern: Regex,
) -> Option<String> {
pattern.find(attr).map(|m| m.as_str().to_string())
}
/// Find all the matches of the given pattern in the value
///
/// ```task
/// env assert_eq(str_find_all("abc", "[ab]"), ["a", "b"])
/// ```
#[env_func]
fn str_find_all(
/// string to check for pattern
attr: &str,
/// Regex pattern to match
pattern: Regex,
) -> Vec<String> {
pattern
.captures_iter(attr)
.map(|c| c[0].to_string())
.collect()
}
/// Count the number of matches of given pattern in the string
///
/// ```task
/// env assert_eq(str_count("abc", "[ab]"), 2)
/// ```
#[env_func]
fn str_count(
/// string to check for pattern
attr: &str,
/// Regex pattern to match
pattern: Regex,
) -> usize {
pattern.captures_iter(attr).count()
}
/// Split the string with the given pattern
///
/// ```task
/// env assert_eq(str_split("abc", "^[ab]"), ["", "bc"])
/// env assert_eq(str_split("abc", "[ab]"), ["", "", "c"])
/// env assert_eq(str_split("abc", "[ab]", limit=2), ["", "bc"])
/// ```
#[env_func]
fn str_split(
/// String to split
attr: &str,
/// Regex pattern to split with
pattern: Regex,
/// Limit the substrings to this number
limit: Option<usize>,
) -> Vec<String> {
if let Some(l) = limit {
pattern.splitn(attr, l).map(String::from).collect()
} else {
pattern.split(attr).map(String::from).collect()
}
}
/// Join the list of strings with the given string
///
/// ```task
/// env assert_eq(str_join(["abc", "de"]), "abcde")
/// env assert_eq(str_join(["abc", "de"], " "), "abc de")
/// env assert_eq(str_join(["abc", "de"], ", "), "abc, de")
/// ```
#[env_func(with = "")]
fn str_join(
/// Strings to join
strings: Vec<String>,
/// Substring to join with
with: &str,
) -> String {
strings.join(with)
}
}