text_mel/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.recv_string().await {
13        let output = text
14            .into_iter()
15            .map(|text| text.chars().collect())
16            .collect();
17
18        check!(chars.send_vec_char(output).await);
19    }
20}
21
22/// Convert string into vector of chars.
23#[mel_function]
24pub fn to_char(text: string) -> Vec<char> {
25    text.chars().collect()
26}
27
28/// Convert stream of char vectors into stream of strings.
29///
30/// Each streamed char vector is turned into its string equivalent.
31#[mel_treatment(
32    input chars Stream<Vec<char>>
33    output text Stream<string>
34)]
35pub async fn from_char() {
36    while let Ok(chars) = chars.recv_vec_char().await {
37        let output = chars
38            .into_iter()
39            .map(|text| text.into_iter().collect())
40            .collect();
41
42        check!(text.send_string(output).await);
43    }
44}
45
46/// Convert vector of chars into string.
47#[mel_function]
48pub fn from_char(chars: Vec<char>) -> string {
49    chars.into_iter().collect()
50}
51
52/// Converts stream of strings into UTF-8 encoded stream of bytes.
53///
54///
55#[mel_treatment(
56    input text Stream<string>
57    output encoded Stream<byte>
58)]
59pub async fn to_utf8() {
60    while let Ok(text) = text.recv_string().await {
61        let mut output = Vec::new();
62        for text in text {
63            output.extend(text.as_bytes());
64        }
65
66        check!(encoded.send_byte(output).await);
67    }
68}
69
70/// Convert string into UTF-8 encoded vector of bytes.
71#[mel_function]
72pub fn to_utf8(text: string) -> Vec<byte> {
73    text.as_bytes().into()
74}
75
76/// Converts stream of bytes into stream of strings according to UTF-8 encoding.
77///
78/// If any sequence of bytes doesn't follow UTF-8 encoding, it is replaced by the `U+FFFD REPLACEMENT CHARACTER` (�).
79#[mel_treatment(
80    input encoded Stream<byte>
81    output text Stream<string>
82)]
83pub async fn from_utf8() {
84    while let Ok(encoded) = encoded.recv_byte().await {
85        let output = String::from_utf8_lossy(&encoded).to_string();
86
87        check!(text.send_one_string(output).await);
88    }
89}
90
91/// Converts vector of bytes into a string according to UTF-8 encoding.
92///
93/// If any sequence of bytes doesn't follow UTF-8 encoding, it is replaced by the `U+FFFD REPLACEMENT CHARACTER` (�).
94#[mel_function]
95pub fn from_utf8(encoded: Vec<byte>) -> string {
96    String::from_utf8_lossy(&encoded).to_string()
97}