std_mel/text/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_many_as::<string>().await {
13 let output = text
14 .into_iter()
15 .map(|text| Value::Vec(text.chars().map(|c| Value::Char(c)).collect()))
16 .collect::<VecDeque<_>>();
17
18 check!(chars.send_many(TransmissionValue::Other(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
37 .recv_many()
38 .await
39 .map(|values| Into::<VecDeque<Value>>::into(values))
40 {
41 let output = chars
42 .into_iter()
43 .map(|text| {
44 let text = match text {
45 Value::Vec(text) => text,
46 Value::Packed(arr) => arr.into_values(),
47 _ => panic!("Vec<char> expected"),
48 };
49 text.into_iter()
50 .map(|c| match c {
51 Value::Char(c) => c,
52 _ => panic!("char expected"),
53 })
54 .collect::<String>()
55 })
56 .collect::<Vec<_>>();
57
58 check!(text.send_many_as(output).await);
59 }
60}
61
62#[mel_function]
64pub fn from_char(chars: Vec<char>) -> string {
65 chars.into_iter().collect()
66}
67
68#[mel_treatment(
72 input text Stream<string>
73 output encoded Stream<byte>
74)]
75pub async fn to_utf8() {
76 while let Ok(text) = text.recv_many_as::<string>().await {
77 let mut output = VecDeque::new();
78 for text in text {
79 output.extend(text.as_bytes());
80 }
81
82 check!(encoded.send_many(TransmissionValue::Byte(output)).await);
83 }
84}
85
86#[mel_function]
88pub fn to_utf8(text: string) -> Vec<byte> {
89 text.as_bytes().into()
90}
91
92#[mel_treatment(
96 input encoded Stream<byte>
97 output text Stream<string>
98)]
99pub async fn from_utf8() {
100 while let Ok(encoded) = encoded.recv_many_as::<byte>().await {
101 let output = String::from_utf8_lossy(&encoded).to_string();
102
103 check!(text.send_one_as(output).await);
104 }
105}
106
107#[mel_function]
111pub fn from_utf8(encoded: Vec<byte>) -> string {
112 String::from_utf8_lossy(&encoded).to_string()
113}