sayit 0.3.0

String replacements using regex
Documentation
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#[doc(hidden)] // pub for bench
pub mod literal;

pub use literal::Literal;

use std::{borrow::Cow, error::Error, fmt::Display};

use crate::{tag::Tag, Match};

/// Same as [`Literal`] with `"$0"` argument: returns entire match.
///
/// Does not act as template unlike [`Literal`]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
pub struct Original;

#[allow(clippy::new_without_default)]
impl Original {
    pub fn new() -> Self {
        Self {}
    }

    pub fn new_boxed() -> Box<Self> {
        Box::new(Self::new())
    }
}

#[cfg_attr(feature = "deserialize", typetag::deserialize)]
impl Tag for Original {
    fn generate<'a>(&self, m: &Match<'a>) -> Cow<'a, str> {
        m.get_match().into()
    }
}

/// Deletes match
///
/// This is a shortcut for [`Literal`] with `""`
#[derive(Clone, Debug)]
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
pub struct Delete;

impl Delete {
    #![allow(clippy::new_without_default)]
    pub fn new() -> Self {
        Self
    }

    pub fn new_boxed() -> Box<Self> {
        Box::new(Self::new())
    }
}

#[cfg_attr(feature = "deserialize", typetag::deserialize)]
impl Tag for Delete {
    fn generate<'a>(&self, _: &Match<'a>) -> Cow<'a, str> {
        Cow::Borrowed("")
    }
}

/// [`Any`] creation might fail
#[derive(Debug)]
pub enum AnyError {
    /// Must provide at least one element
    ZeroItems,
}

impl Display for AnyError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::ZeroItems => "expected at least one element to choose from",
            }
        )
    }
}

impl Error for AnyError {}

/// Selects any of nested items with equal probabilities
#[derive(Clone, Debug)]
pub struct Any(Vec<Box<dyn Tag>>);

impl Any {
    pub fn new(items: Vec<Box<dyn Tag>>) -> Result<Self, AnyError> {
        if items.is_empty() {
            return Err(AnyError::ZeroItems);
        }

        Ok(Self(items))
    }

    pub fn new_boxed(items: Vec<Box<dyn Tag>>) -> Result<Box<Self>, AnyError> {
        Ok(Box::new(Self::new(items)?))
    }
}

#[cfg_attr(feature = "deserialize", typetag::deserialize)]
impl Tag for Any {
    fn generate<'a>(&self, m: &Match<'a>) -> Cow<'a, str> {
        let i = fastrand::usize(..self.0.len());

        self.0[i].generate(m)
    }
}

/// [`Weights`] creation might fail
#[derive(Debug)]
pub enum WeightsError {
    /// Must provide at least one element
    ZeroItems,
    /// Sum of all weights must be positive
    NonPositiveTotalWeights,
}

impl Display for WeightsError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::ZeroItems => "expected at least one element to choose from",
                Self::NonPositiveTotalWeights => "weights must add up to a positive number",
            }
        )
    }
}

/// Selects any of nested items with relative probabilities
#[derive(Clone, Debug)]
pub struct Weights {
    choices: Vec<Box<dyn Tag>>,
    cum_total: u64,
    cum_weights: Vec<u64>,
}

impl Weights {
    pub fn new(items: Vec<(u64, Box<dyn Tag>)>) -> Result<Self, WeightsError> {
        let (weights, choices) = items.into_iter().unzip();

        let (cum_total, cum_weights) = Self::cum_weights(weights)?;

        Ok(Self {
            choices,
            cum_total,
            cum_weights,
        })
    }

    pub fn new_boxed(items: Vec<(u64, Box<dyn Tag>)>) -> Result<Box<Self>, WeightsError> {
        Ok(Box::new(Self::new(items)?))
    }

    fn cum_weights(mut weights: Vec<u64>) -> Result<(u64, Vec<u64>), WeightsError> {
        if weights.is_empty() {
            return Err(WeightsError::ZeroItems);
        }

        let mut total = weights[0];
        for w in &mut weights[1..] {
            *w += total;
            total += *w - total;
        }

        if weights[weights.len() - 1] == 0 {
            return Err(WeightsError::NonPositiveTotalWeights);
        }

        Ok((total, weights))
    }

    fn random_choice(&self) -> usize {
        let random_point = fastrand::u64(0..=self.cum_total);

        match self.cum_weights.binary_search(&random_point) {
            Ok(i) | Err(i) => i,
        }
    }
}

#[cfg_attr(feature = "deserialize", typetag::deserialize)]
impl Tag for Weights {
    fn generate<'a>(&self, m: &Match<'a>) -> Cow<'a, str> {
        self.choices[self.random_choice()].generate(m)
    }
}

/// Uppercases result of inner tag
#[derive(Clone, Debug)]
#[cfg_attr(
    feature = "deserialize",
    derive(serde::Deserialize),
    serde(transparent)
)]
pub struct Upper(Box<dyn Tag>);

impl Upper {
    pub fn new(inner: Box<dyn Tag>) -> Self {
        Self(inner)
    }

    pub fn new_boxed(inner: Box<dyn Tag>) -> Box<Self> {
        Box::new(Self::new(inner))
    }
}

#[cfg_attr(feature = "deserialize", typetag::deserialize)]
impl Tag for Upper {
    fn generate<'a>(&self, m: &Match<'a>) -> Cow<'a, str> {
        self.0.generate(m).to_uppercase().into()
    }
}

/// Lowercases result of inner tag
#[derive(Clone, Debug)]
#[cfg_attr(
    feature = "deserialize",
    derive(serde::Deserialize),
    serde(transparent)
)]
pub struct Lower(Box<dyn Tag>);

impl Lower {
    pub fn new(inner: Box<dyn Tag>) -> Self {
        Self(inner)
    }

    pub fn new_boxed(inner: Box<dyn Tag>) -> Box<Self> {
        Box::new(Self::new(inner))
    }
}

#[cfg_attr(feature = "deserialize", typetag::deserialize)]
impl Tag for Lower {
    fn generate<'a>(&self, m: &Match<'a>) -> Cow<'a, str> {
        self.0.generate(m).to_lowercase().into()
    }
}

/// Adds results of left and right tags together
#[derive(Clone, Debug)]
#[cfg_attr(
    feature = "deserialize",
    derive(serde::Deserialize),
    serde(transparent)
)]
pub struct Concat((Box<dyn Tag>, Box<dyn Tag>));

impl Concat {
    pub fn new(left: Box<dyn Tag>, right: Box<dyn Tag>) -> Self {
        Self((left, right))
    }

    pub fn new_boxed(left: Box<dyn Tag>, right: Box<dyn Tag>) -> Box<Self> {
        Box::new(Self::new(left, right))
    }
}

#[cfg_attr(feature = "deserialize", typetag::deserialize)]
impl Tag for Concat {
    fn generate<'a>(&self, m: &Match<'a>) -> Cow<'a, str> {
        self.0 .0.generate(m) + self.0 .1.generate(m)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Match;

    use std::borrow::Cow;

    use regex_automata::meta::Regex;

    fn make_match(pattern: &str) -> Match {
        let re = Regex::new(".+").unwrap();
        let mut caps = re.create_captures();

        re.captures(pattern, &mut caps);

        Match {
            captures: caps,
            input: pattern,
        }
    }

    fn apply<'a>(tag: &dyn Tag, self_matching_pattern: &'a str) -> Cow<'a, str> {
        tag.generate(&make_match(self_matching_pattern)).into()
    }

    #[test]
    fn original() {
        let tag = Original::new();

        assert_eq!(apply(&tag, "bar"), "bar");
        assert_eq!(apply(&tag, "foo"), "foo");
    }

    #[test]
    fn delete() {
        let tag = Delete::new();

        assert_eq!(apply(&tag, "bar"), "");
        assert_eq!(apply(&tag, "foo"), "");
    }

    #[test]
    fn literal() {
        let tag = Literal::new_boxed("bar");

        assert_eq!(apply(tag.as_ref(), "foo"), "bar");
        assert_eq!(apply(tag.as_ref(), "bar"), "bar");
    }

    #[test]
    fn literal_templates() {
        let tag = Literal::new_boxed("$0");

        assert_eq!(apply(tag.as_ref(), "foo"), "foo");
    }

    #[test]
    fn literal_mimics_case() {
        let tag = Literal::new_boxed("bar");

        assert_eq!(apply(tag.as_ref(), "FOO"), "BAR");
    }

    #[test]
    fn any() {
        let tag = Any::new(vec![Literal::new_boxed("bar"), Literal::new_boxed("baz")]).unwrap();

        let selected = apply(&tag, "bar").into_owned();

        assert!(["bar", "baz"].contains(&selected.as_str()));
    }

    #[test]
    fn weights_cum_weights_errors() {
        assert!(Weights::cum_weights(Vec::new()).is_err());
        assert!(Weights::cum_weights(vec![0, 0, 0, 0, 0]).is_err());
    }

    #[test]
    fn weights_cum_weights() {
        assert_eq!(
            Weights::cum_weights(vec![1, 2, 3, 4, 5]).unwrap(),
            (15, vec![1, 3, 6, 10, 15])
        );
        assert_eq!(
            Weights::cum_weights(vec![5, 4, 3, 2, 1]).unwrap(),
            (15, vec![5, 9, 12, 14, 15])
        );
    }

    #[test]
    fn weights() {
        let tag = Weights::new(vec![
            (1, Literal::new_boxed("bar")),
            (1, Literal::new_boxed("baz")),
            (0, Literal::new_boxed("spam")),
        ])
        .unwrap();

        let selected = apply(&tag, "bar").into_owned();

        assert!(vec!["bar", "baz"].contains(&selected.as_str()));
    }

    #[test]
    fn weights_single() {
        let tag = Weights::new(vec![(50, Literal::new_boxed("test"))]).unwrap();

        let selected = apply(&tag, "test").into_owned();

        assert_eq!(selected, "test");
    }

    #[test]
    fn upper() {
        // double wrapped for coverage
        let tag = Upper::new(Upper::new_boxed(Original::new_boxed()));

        assert_eq!(apply(&tag, "lowercase"), "LOWERCASE");
        assert_eq!(apply(&tag, "UPPERCASE"), "UPPERCASE");
        assert_eq!(apply(&tag, "MiXeDcAsE"), "MIXEDCASE");
        assert_eq!(apply(&tag, "юникод"), "ЮНИКОД");
    }

    #[test]
    fn lower() {
        // double wrapped for coverage
        let tag = Lower::new(Lower::new_boxed(Original::new_boxed()));

        assert_eq!(apply(&tag, "lowercase"), "lowercase");
        assert_eq!(apply(&tag, "UPPERCASE"), "uppercase");
        assert_eq!(apply(&tag, "MiXeDcAsE"), "mixedcase");
        assert_eq!(apply(&tag, "ЮНИКОД"), "юникод");
    }

    #[test]
    fn concat() {
        let tag = Concat::new(Original::new_boxed(), Original::new_boxed());

        assert_eq!(apply(&tag, "double"), "doubledouble");
    }

    #[test]
    fn concatenated_not_mimics() {
        let tag = Concat::new(Literal::new_boxed("b"), Literal::new_boxed("ar"));

        assert_eq!(apply(&tag, "FOO"), "BAR");
    }

    #[test]
    fn expansion() {
        let swap_words_tag = Literal::new_boxed("$2 $1");

        let two_words_regex = Regex::new(r"(\w+) (\w+)").unwrap();
        let mut caps = two_words_regex.create_captures();
        two_words_regex.captures("swap us", &mut caps);

        assert_eq!(
            swap_words_tag.generate(&Match {
                captures: caps,
                input: "swap us",
            }),
            "us swap"
        );

        // nonexistent goup results in empty string
        let delete_word_tag = Literal::new_boxed("$nonexistent $2");

        let mut caps = two_words_regex.create_captures();
        two_words_regex.captures("DELETE US", &mut caps);

        assert_eq!(
            delete_word_tag.generate(&Match {
                captures: caps,
                input: "DELETE US",
            },),
            " US"
        );
    }
}