use melodium_core::*;
use melodium_macro::{check, mel_function, mel_treatment};
#[mel_treatment(
input chars Stream<char>
output text Stream<string>
)]
pub async fn to_string() {
while let Ok(chars) = chars.recv_many_as::<char>().await {
check!(
text.send_one_as(chars.into_iter().collect::<String>())
.await
);
}
}
#[mel_treatment(
input text Stream<string>
output chars Stream<char>
)]
pub async fn from_string() {
while let Ok(text) = text.recv_many_as::<string>().await {
let mut output = Vec::new();
for text in text {
output.extend(text.chars());
}
check!(chars.send_many_as(output).await);
}
}
#[mel_treatment(
input text Stream<char>
output encoded Stream<byte>
)]
pub async fn to_utf8() {
while let Ok(text) = text.recv_many_as::<char>().await {
let mut output = Vec::new();
for text in text {
output.extend(text.to_string().as_bytes());
}
check!(
encoded
.send_many(TransmissionValue::Byte(output.into()))
.await
);
}
}
#[mel_function]
pub fn to_utf8(char: char) -> Vec<byte> {
char.to_string().as_bytes().into()
}
#[mel_function]
pub fn from_utf8(encoded: Vec<byte>) -> Vec<char> {
String::from_utf8_lossy(&encoded).chars().collect()
}