arr_rs/alphanumeric/operations/
validate.rs

1use crate::{
2    alphanumeric::prelude::*,
3    core::prelude::*,
4    errors::prelude::*,
5};
6
7/// `ArrayTrait` - Alphanumeric Array operations
8pub trait ArrayStringValidate<N: Alphanumeric> where Self: Sized + Clone {
9
10    /// Check if all characters in the string are alphabetic and there is at least one character
11    ///
12    /// # Examples
13    ///
14    /// ```
15    /// use arr_rs::prelude::*;
16    ///
17    /// let expected = Array::flat(vec![true, false, false]);
18    /// let arr = Array::flat(vec!["abcd".to_string(), "abc12".to_string(), "".to_string()]);
19    /// assert_eq!(expected, arr.is_alpha());
20    /// ```
21    ///
22    /// # Errors
23    ///
24    /// may returns `ArrayError`
25    fn is_alpha(&self) -> Result<Array<bool>, ArrayError>;
26
27    /// Check if all characters in the string are alphanumeric and there is at least one character
28    ///
29    /// # Examples
30    ///
31    /// ```
32    /// use arr_rs::prelude::*;
33    ///
34    /// let expected = Array::flat(vec![true, true, false]);
35    /// let arr = Array::flat(vec!["abcd".to_string(), "abc12".to_string(), "".to_string()]);
36    /// assert_eq!(expected, arr.is_alnum());
37    /// ```
38    ///
39    /// # Errors
40    ///
41    /// may returns `ArrayError`
42    fn is_alnum(&self) -> Result<Array<bool>, ArrayError>;
43
44    /// Check if all characters in the string are decimal and there is at least one character
45    ///
46    /// # Examples
47    ///
48    /// ```
49    /// use arr_rs::prelude::*;
50    ///
51    /// let expected = Array::flat(vec![true, false, false]);
52    /// let arr = Array::flat(vec!["12345".to_string(), "abc12".to_string(), "".to_string()]);
53    /// assert_eq!(expected, arr.is_decimal());
54    /// ```
55    ///
56    /// # Errors
57    ///
58    /// may returns `ArrayError`
59    fn is_decimal(&self) -> Result<Array<bool>, ArrayError>;
60
61    /// Check if all characters in the string are numeric and there is at least one character
62    ///
63    /// # Examples
64    ///
65    /// ```
66    /// use arr_rs::prelude::*;
67    ///
68    /// let expected = Array::flat(vec![true, true, false, false, false, false, false]);
69    /// let arr = Array::flat(vec!["2".to_string(), "12345".to_string(), "a".to_string(), "abc12".to_string(), "1/4".to_string(), "VIII".to_string(), "".to_string()]);
70    /// assert_eq!(expected, arr.is_numeric());
71    /// ```
72    ///
73    /// # Errors
74    ///
75    /// may returns `ArrayError`
76    fn is_numeric(&self) -> Result<Array<bool>, ArrayError>;
77
78    /// Check if all characters in the string are digits and there is at least one character
79    ///
80    /// # Examples
81    ///
82    /// ```
83    /// use arr_rs::prelude::*;
84    ///
85    /// let expected = Array::flat(vec![true, false, false, false, false]);
86    /// let arr = Array::flat(vec!["2".to_string(), "12345".to_string(), "a".to_string(), "abc12".to_string(), "".to_string()]);
87    /// assert_eq!(expected, arr.is_digit());
88    /// ```
89    ///
90    /// # Errors
91    ///
92    /// may returns `ArrayError`
93    fn is_digit(&self) -> Result<Array<bool>, ArrayError>;
94
95    /// Check if all characters in the string are whitespace and there is at least one character
96    ///
97    /// # Examples
98    ///
99    /// ```
100    /// use arr_rs::prelude::*;
101    ///
102    /// let expected = Array::flat(vec![false, false, false, false, true, true]);
103    /// let arr = Array::flat(vec!["2".to_string(), "12345".to_string(), "a".to_string(), "abc12".to_string(), " ".to_string(), "    ".to_string()]);
104    /// assert_eq!(expected, arr.is_space());
105    /// ```
106    ///
107    /// # Errors
108    ///
109    /// may returns `ArrayError`
110    fn is_space(&self) -> Result<Array<bool>, ArrayError>;
111
112    /// Check if all characters in the string are lowercase and there is at least one character
113    ///
114    /// # Examples
115    ///
116    /// ```
117    /// use arr_rs::prelude::*;
118    ///
119    /// let expected = Array::flat(vec![true, true, false, false, false]);
120    /// let arr = Array::flat(vec!["a".to_string(), "abc12".to_string(), "1234".to_string(), "aBc12".to_string(), "".to_string()]);
121    /// assert_eq!(expected, arr.is_lower());
122    /// ```
123    ///
124    /// # Errors
125    ///
126    /// may returns `ArrayError`
127    fn is_lower(&self) -> Result<Array<bool>, ArrayError>;
128
129    /// Check if all characters in the string are uppercase and there is at least one character
130    ///
131    /// # Examples
132    ///
133    /// ```
134    /// use arr_rs::prelude::*;
135    ///
136    /// let expected = Array::flat(vec![true, true, false, false, false]);
137    /// let arr = Array::flat(vec!["A".to_string(), "ABC12".to_string(), "1234".to_string(), "aBc12".to_string(), "".to_string()]);
138    /// assert_eq!(expected, arr.is_upper());
139    /// ```
140    ///
141    /// # Errors
142    ///
143    /// may returns `ArrayError`
144    fn is_upper(&self) -> Result<Array<bool>, ArrayError>;
145}
146
147impl <N: Alphanumeric> ArrayStringValidate<N> for Array<N> {
148
149    fn is_alpha(&self) -> Result<Array<bool>, ArrayError> {
150        let elements = self.clone().into_iter()
151            .map(|item| !item.to_string().is_empty() && item.to_string().chars().all(char::is_alphabetic))
152            .collect();
153        Array::new(elements, self.get_shape()?)
154    }
155
156    fn is_alnum(&self) -> Result<Array<bool>, ArrayError> {
157        let elements = self.clone().into_iter()
158            .map(|item| !item.to_string().is_empty() && item.to_string().chars().all(char::is_alphanumeric))
159            .collect();
160        Array::new(elements, self.get_shape()?)
161    }
162
163    fn is_decimal(&self) -> Result<Array<bool>, ArrayError> {
164        let elements = self.clone().into_iter()
165            .map(|item| !item.to_string().is_empty() && item.to_string().chars().all(|c| c.is_ascii_digit()))
166            .collect();
167        Array::new(elements, self.get_shape()?)
168    }
169
170    fn is_numeric(&self) -> Result<Array<bool>, ArrayError> {
171        let elements = self.clone().into_iter()
172            .map(|item| !item.to_string().is_empty() && item.to_string().chars().all(char::is_numeric))
173            .collect();
174        Array::new(elements, self.get_shape()?)
175    }
176
177    fn is_digit(&self) -> Result<Array<bool>, ArrayError> {
178        let elements = self.clone().into_iter()
179            .map(|item| item.to_string().len() == 1 && item.to_string().chars().all(|c| c.is_ascii_digit()))
180            .collect();
181        Array::new(elements, self.get_shape()?)
182    }
183
184    fn is_space(&self) -> Result<Array<bool>, ArrayError> {
185        let elements = self.clone().into_iter()
186            .map(|item| !item.to_string().is_empty() && item.to_string().chars().all(char::is_whitespace))
187            .collect();
188        Array::new(elements, self.get_shape()?)
189    }
190
191    fn is_lower(&self) -> Result<Array<bool>, ArrayError> {
192        let elements = self.clone().into_iter()
193            .map(|item| {
194                let filtered = item.to_string().chars().filter(|c| c.is_alphabetic()).collect::<String>();
195                !filtered.is_empty() && filtered.chars().all(char::is_lowercase)
196            })
197            .collect();
198        Array::new(elements, self.get_shape()?)
199    }
200
201    fn is_upper(&self) -> Result<Array<bool>, ArrayError> {
202        let elements = self.clone().into_iter()
203            .map(|item| {
204                let filtered = item.to_string().chars().filter(|c| c.is_alphabetic()).collect::<String>();
205                !filtered.is_empty() && filtered.chars().all(char::is_uppercase)
206            })
207            .collect();
208        Array::new(elements, self.get_shape()?)
209    }
210}
211
212impl <N: Alphanumeric> ArrayStringValidate<N> for Result<Array<N>, ArrayError> {
213
214    fn is_alpha(&self) -> Result<Array<bool>, ArrayError> {
215        self.clone()?.is_alpha()
216    }
217
218    fn is_alnum(&self) -> Result<Array<bool>, ArrayError> {
219        self.clone()?.is_alnum()
220    }
221
222    fn is_decimal(&self) -> Result<Array<bool>, ArrayError> {
223        self.clone()?.is_decimal()
224    }
225
226    fn is_numeric(&self) -> Result<Array<bool>, ArrayError> {
227        self.clone()?.is_numeric()
228    }
229
230    fn is_digit(&self) -> Result<Array<bool>, ArrayError> {
231        self.clone()?.is_digit()
232    }
233
234    fn is_space(&self) -> Result<Array<bool>, ArrayError> {
235        self.clone()?.is_space()
236    }
237
238    fn is_lower(&self) -> Result<Array<bool>, ArrayError> {
239        self.clone()?.is_lower()
240    }
241
242    fn is_upper(&self) -> Result<Array<bool>, ArrayError> {
243        self.clone()?.is_upper()
244    }
245}