Skip to main content

std_mel/text/convert/
string.rs

1use melodium_core::*;
2use melodium_macro::{check, mel_function, mel_treatment};
3
4/// Convert stream of string into chars.
5///
6/// Each string is turned into equivalent vector of chars.
7#[mel_treatment(
8    input text Stream<string>
9    output chars Stream<Vec<char>>
10)]
11pub async fn to_char() {
12    while let Ok(text) = text
13        .recv_many()
14        .await
15        .map(|values| TryInto::<Vec<string>>::try_into(values).unwrap())
16    {
17        let output = text
18            .into_iter()
19            .map(|text| Value::Vec(text.chars().map(|c| Value::Char(c)).collect()))
20            .collect::<VecDeque<_>>();
21
22        check!(chars.send_many(TransmissionValue::Other(output)).await);
23    }
24}
25
26/// Convert string into vector of chars.
27#[mel_function]
28pub fn to_char(text: string) -> Vec<char> {
29    text.chars().collect()
30}
31
32/// Convert stream of char vectors into stream of strings.
33///
34/// Each streamed char vector is turned into its string equivalent.
35#[mel_treatment(
36    input chars Stream<Vec<char>>
37    output text Stream<string>
38)]
39pub async fn from_char() {
40    while let Ok(chars) = chars
41        .recv_many()
42        .await
43        .map(|values| Into::<VecDeque<Value>>::into(values))
44    {
45        let output = chars
46            .into_iter()
47            .map(|text| match text {
48                Value::Vec(text) => text
49                    .into_iter()
50                    .map(|c| match c {
51                        Value::Char(c) => c,
52                        _ => panic!("char expected"),
53                    })
54                    .collect::<String>(),
55                _ => panic!("Vec<char> expected"),
56            })
57            .collect::<VecDeque<_>>();
58
59        check!(text.send_many(output.into()).await);
60    }
61}
62
63/// Convert vector of chars into string.
64#[mel_function]
65pub fn from_char(chars: Vec<char>) -> string {
66    chars.into_iter().collect()
67}
68
69/// Converts stream of strings into UTF-8 encoded stream of bytes.
70///
71///
72#[mel_treatment(
73    input text Stream<string>
74    output encoded Stream<byte>
75)]
76pub async fn to_utf8() {
77    while let Ok(text) = text
78        .recv_many()
79        .await
80        .map(|values| TryInto::<Vec<string>>::try_into(values).unwrap())
81    {
82        let mut output = VecDeque::new();
83        for text in text {
84            output.extend(text.as_bytes());
85        }
86
87        check!(encoded.send_many(TransmissionValue::Byte(output)).await);
88    }
89}
90
91/// Convert string into UTF-8 encoded vector of bytes.
92#[mel_function]
93pub fn to_utf8(text: string) -> Vec<byte> {
94    text.as_bytes().into()
95}
96
97/// Converts stream of bytes into stream of strings according to UTF-8 encoding.
98///
99/// If any sequence of bytes doesn't follow UTF-8 encoding, it is replaced by the `U+FFFD REPLACEMENT CHARACTER` (�).
100#[mel_treatment(
101    input encoded Stream<byte>
102    output text Stream<string>
103)]
104pub async fn from_utf8() {
105    while let Ok(encoded) = encoded
106        .recv_many()
107        .await
108        .map(|values| TryInto::<Vec<byte>>::try_into(values).unwrap())
109    {
110        let output = String::from_utf8_lossy(&encoded).to_string();
111
112        check!(text.send_one(output.into()).await);
113    }
114}
115
116/// Converts vector of bytes into a string according to UTF-8 encoding.
117///
118/// If any sequence of bytes doesn't follow UTF-8 encoding, it is replaced by the `U+FFFD REPLACEMENT CHARACTER` (�).
119#[mel_function]
120pub fn from_utf8(encoded: Vec<byte>) -> string {
121    String::from_utf8_lossy(&encoded).to_string()
122}