use melodium_core::*;
use melodium_macro::{check, mel_function, mel_treatment};
#[mel_treatment(
input text Stream<string>
output chars Stream<Vec<char>>
)]
pub async fn to_char() {
while let Ok(text) = text.recv_string().await {
let output = text
.into_iter()
.map(|text| text.chars().collect())
.collect();
check!(chars.send_vec_char(output).await);
}
}
#[mel_function]
pub fn to_char(text: string) -> Vec<char> {
text.chars().collect()
}
#[mel_treatment(
input chars Stream<Vec<char>>
output text Stream<string>
)]
pub async fn from_char() {
while let Ok(chars) = chars.recv_vec_char().await {
let output = chars
.into_iter()
.map(|text| text.into_iter().collect())
.collect();
check!(text.send_string(output).await);
}
}
#[mel_function]
pub fn from_char(chars: Vec<char>) -> string {
chars.into_iter().collect()
}
#[mel_treatment(
input text Stream<string>
output encoded Stream<byte>
)]
pub async fn to_utf8() {
while let Ok(text) = text.recv_string().await {
let mut output = Vec::new();
for text in text {
output.extend(text.as_bytes());
}
check!(encoded.send_byte(output).await);
}
}
#[mel_function]
pub fn to_utf8(text: string) -> Vec<byte> {
text.as_bytes().into()
}
#[mel_treatment(
input encoded Stream<byte>
output text Stream<string>
)]
pub async fn from_utf8() {
while let Ok(encoded) = encoded.recv_byte().await {
let output = String::from_utf8_lossy(&encoded).to_string();
check!(text.send_one_string(output).await);
}
}
#[mel_function]
pub fn from_utf8(encoded: Vec<byte>) -> string {
String::from_utf8_lossy(&encoded).to_string()
}