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
pub mod char;

use melodium_core::*;
use melodium_macro::{check, mel_function, mel_treatment};

/// Tells if strings exactly matches a pattern.
#[mel_treatment(
    input text Stream<string>
    output matches Stream<bool>
)]
pub async fn exact(pattern: string) {
    while let Ok(text) = text
        .recv_many()
        .await
        .map(|values: TransmissionValue| TryInto::<Vec<string>>::try_into(values).unwrap())
    {
        check!(
            matches
                .send_many(
                    text.into_iter()
                        .map(|txt| txt == pattern)
                        .collect::<VecDeque<_>>()
                        .into()
                )
                .await
        );
    }
}

/// Tells if string exactly matches a pattern.
#[mel_function]
pub fn exact(text: string, pattern: string) -> bool {
    text == pattern
}

/// Tells if strings starts with a pattern.
#[mel_treatment(
    input text Stream<string>
    output matches Stream<bool>
)]
pub async fn starts_with(pattern: string) {
    while let Ok(text) = text
        .recv_many()
        .await
        .map(|values: TransmissionValue| TryInto::<Vec<string>>::try_into(values).unwrap())
    {
        check!(
            matches
                .send_many(
                    text.into_iter()
                        .map(|txt| txt.starts_with(&pattern))
                        .collect::<VecDeque<_>>()
                        .into()
                )
                .await
        );
    }
}

/// Tells if string starts with a pattern.
#[mel_function]
pub fn starts_with(text: string, pattern: string) -> bool {
    text.starts_with(&pattern)
}

/// Tells if strings ends with a pattern.
#[mel_treatment(
    input text Stream<string>
    output matches Stream<bool>
)]
pub async fn ends_with(pattern: string) {
    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| txt.ends_with(&pattern))
                        .collect::<VecDeque<_>>()
                        .into()
                )
                .await
        );
    }
}

/// Tells if string ends with a pattern.
#[mel_function]
pub fn ends_with(text: string, pattern: string) -> bool {
    text.ends_with(&pattern)
}