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
use crate::{
    alphanumeric::prelude::*,
    core::prelude::*,
    errors::prelude::*,
};

/// ArrayTrait - Alphanumeric Array operations
pub trait ArrayStringValidate<N: Alphanumeric> where Self: Sized + Clone {

    /// Check if all characters in the string are alphabetic and there is at least one character
    ///
    /// # Examples
    ///
    /// ```
    /// use arr_rs::prelude::*;
    ///
    /// let expected = Array::flat(vec![true, false, false]);
    /// let arr = Array::flat(vec!["abcd".to_string(), "abc12".to_string(), "".to_string()]);
    /// assert_eq!(expected, arr.is_alpha());
    /// ```
    fn is_alpha(&self) -> Result<Array<bool>, ArrayError>;

    /// Check if all characters in the string are alphanumeric and there is at least one character
    ///
    /// # Examples
    ///
    /// ```
    /// use arr_rs::prelude::*;
    ///
    /// let expected = Array::flat(vec![true, true, false]);
    /// let arr = Array::flat(vec!["abcd".to_string(), "abc12".to_string(), "".to_string()]);
    /// assert_eq!(expected, arr.is_alnum());
    /// ```
    fn is_alnum(&self) -> Result<Array<bool>, ArrayError>;

    /// Check if all characters in the string are decimal and there is at least one character
    ///
    /// # Examples
    ///
    /// ```
    /// use arr_rs::prelude::*;
    ///
    /// let expected = Array::flat(vec![true, false, false]);
    /// let arr = Array::flat(vec!["12345".to_string(), "abc12".to_string(), "".to_string()]);
    /// assert_eq!(expected, arr.is_decimal());
    /// ```
    fn is_decimal(&self) -> Result<Array<bool>, ArrayError>;

    /// Check if all characters in the string are numeric and there is at least one character
    ///
    /// # Examples
    ///
    /// ```
    /// use arr_rs::prelude::*;
    ///
    /// let expected = Array::flat(vec![true, true, false, false, false, false, false]);
    /// 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()]);
    /// assert_eq!(expected, arr.is_numeric());
    /// ```
    fn is_numeric(&self) -> Result<Array<bool>, ArrayError>;

    /// Check if all characters in the string are digits and there is at least one character
    ///
    /// # Examples
    ///
    /// ```
    /// use arr_rs::prelude::*;
    ///
    /// let expected = Array::flat(vec![true, false, false, false, false]);
    /// let arr = Array::flat(vec!["2".to_string(), "12345".to_string(), "a".to_string(), "abc12".to_string(), "".to_string()]);
    /// assert_eq!(expected, arr.is_digit());
    /// ```
    fn is_digit(&self) -> Result<Array<bool>, ArrayError>;

    /// Check if all characters in the string are whitespace and there is at least one character
    ///
    /// # Examples
    ///
    /// ```
    /// use arr_rs::prelude::*;
    ///
    /// let expected = Array::flat(vec![false, false, false, false, true, true]);
    /// let arr = Array::flat(vec!["2".to_string(), "12345".to_string(), "a".to_string(), "abc12".to_string(), " ".to_string(), "    ".to_string()]);
    /// assert_eq!(expected, arr.is_space());
    /// ```
    fn is_space(&self) -> Result<Array<bool>, ArrayError>;

    /// Check if all characters in the string are lowercase and there is at least one character
    ///
    /// # Examples
    ///
    /// ```
    /// use arr_rs::prelude::*;
    ///
    /// let expected = Array::flat(vec![true, true, false, false, false]);
    /// let arr = Array::flat(vec!["a".to_string(), "abc12".to_string(), "1234".to_string(), "aBc12".to_string(), "".to_string()]);
    /// assert_eq!(expected, arr.is_lower());
    /// ```
    fn is_lower(&self) -> Result<Array<bool>, ArrayError>;

    /// Check if all characters in the string are uppercase and there is at least one character
    ///
    /// # Examples
    ///
    /// ```
    /// use arr_rs::prelude::*;
    ///
    /// let expected = Array::flat(vec![true, true, false, false, false]);
    /// let arr = Array::flat(vec!["A".to_string(), "ABC12".to_string(), "1234".to_string(), "aBc12".to_string(), "".to_string()]);
    /// assert_eq!(expected, arr.is_upper());
    /// ```
    fn is_upper(&self) -> Result<Array<bool>, ArrayError>;
}

impl <N: Alphanumeric> ArrayStringValidate<N> for Array<N> {

    fn is_alpha(&self) -> Result<Array<bool>, ArrayError> {
        let elements = self.clone().into_iter()
            .map(|item| !item.to_string().is_empty() && item.to_string().chars().all(|c| c.is_alphabetic()))
            .collect();
        Array::new(elements, self.get_shape()?)
    }

    fn is_alnum(&self) -> Result<Array<bool>, ArrayError> {
        let elements = self.clone().into_iter()
            .map(|item| !item.to_string().is_empty() && item.to_string().chars().all(|c| c.is_alphanumeric()))
            .collect();
        Array::new(elements, self.get_shape()?)
    }

    fn is_decimal(&self) -> Result<Array<bool>, ArrayError> {
        let elements = self.clone().into_iter()
            .map(|item| !item.to_string().is_empty() && item.to_string().chars().all(|c| c.is_ascii_digit()))
            .collect();
        Array::new(elements, self.get_shape()?)
    }

    fn is_numeric(&self) -> Result<Array<bool>, ArrayError> {
        let elements = self.clone().into_iter()
            .map(|item| !item.to_string().is_empty() && item.to_string().chars().all(|c| c.is_numeric()))
            .collect();
        Array::new(elements, self.get_shape()?)
    }

    fn is_digit(&self) -> Result<Array<bool>, ArrayError> {
        let elements = self.clone().into_iter()
            .map(|item| item.to_string().len() == 1 && item.to_string().chars().all(|c| c.is_ascii_digit()))
            .collect();
        Array::new(elements, self.get_shape()?)
    }

    fn is_space(&self) -> Result<Array<bool>, ArrayError> {
        let elements = self.clone().into_iter()
            .map(|item| !item.to_string().is_empty() && item.to_string().chars().all(|c| c.is_whitespace()))
            .collect();
        Array::new(elements, self.get_shape()?)
    }

    fn is_lower(&self) -> Result<Array<bool>, ArrayError> {
        let elements = self.clone().into_iter()
            .map(|item| {
                let filtered = item.to_string().chars().filter(|c| c.is_alphabetic()).collect::<String>();
                !filtered.is_empty() && filtered.chars().all(|c| c.is_lowercase())
            })
            .collect();
        Array::new(elements, self.get_shape()?)
    }

    fn is_upper(&self) -> Result<Array<bool>, ArrayError> {
        let elements = self.clone().into_iter()
            .map(|item| {
                let filtered = item.to_string().chars().filter(|c| c.is_alphabetic()).collect::<String>();
                !filtered.is_empty() && filtered.chars().all(|c| c.is_uppercase())
            })
            .collect();
        Array::new(elements, self.get_shape()?)
    }
}

impl <N: Alphanumeric> ArrayStringValidate<N> for Result<Array<N>, ArrayError> {

    fn is_alpha(&self) -> Result<Array<bool>, ArrayError> {
        self.clone()?.is_alpha()
    }

    fn is_alnum(&self) -> Result<Array<bool>, ArrayError> {
        self.clone()?.is_alnum()
    }

    fn is_decimal(&self) -> Result<Array<bool>, ArrayError> {
        self.clone()?.is_decimal()
    }

    fn is_numeric(&self) -> Result<Array<bool>, ArrayError> {
        self.clone()?.is_numeric()
    }

    fn is_digit(&self) -> Result<Array<bool>, ArrayError> {
        self.clone()?.is_digit()
    }

    fn is_space(&self) -> Result<Array<bool>, ArrayError> {
        self.clone()?.is_space()
    }

    fn is_lower(&self) -> Result<Array<bool>, ArrayError> {
        self.clone()?.is_lower()
    }

    fn is_upper(&self) -> Result<Array<bool>, ArrayError> {
        self.clone()?.is_upper()
    }
}