text_mel/convert/
string.rs1use melodium_core::*;
2use melodium_macro::{check, mel_function, mel_treatment};
3
4#[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#[mel_function]
24pub fn to_char(text: string) -> Vec<char> {
25 text.chars().collect()
26}
27
28#[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#[mel_function]
48pub fn from_char(chars: Vec<char>) -> string {
49 chars.into_iter().collect()
50}
51
52#[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#[mel_function]
72pub fn to_utf8(text: string) -> Vec<byte> {
73 text.as_bytes().into()
74}
75
76#[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#[mel_function]
95pub fn from_utf8(encoded: Vec<byte>) -> string {
96 String::from_utf8_lossy(&encoded).to_string()
97}