use melodium_core::{executive::*, *};
use melodium_macro::{check, mel_data, mel_function, mel_treatment};
use std::collections::HashMap;
use std::sync::Arc;
pub mod block;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[mel_data(traits(Serialize Deserialize PartialEquality Equality))]
pub struct StringMap {
pub map: HashMap<String, String>,
}
impl StringMap {
pub fn new() -> Self {
Self {
map: HashMap::new(),
}
}
pub fn new_with(map: HashMap<String, String>) -> Self {
Self { map }
}
}
impl Display for StringMap {
fn display(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
write!(f, "{:#?}", self)
}
}
#[mel_function]
pub fn map(entries: Vec<StringMap>) -> StringMap {
let mut map = HashMap::new();
for submap in entries {
map.extend(submap.map);
}
StringMap { map }
}
#[mel_function]
pub fn entry(key: string, value: string) -> StringMap {
let mut map = HashMap::new();
map.insert(key, value);
StringMap { map }
}
#[mel_treatment(
input value Stream<string>
output map Stream<StringMap>
)]
pub async fn entry(key: string) {
while let Ok(value) = value.recv_one_as::<String>().await {
let mut new_map = HashMap::new();
new_map.insert(key.clone(), value);
let new_map = StringMap { map: new_map };
check!(map.send_one(Value::Data(Arc::new(new_map))).await)
}
}
#[mel_function]
pub fn get(map: StringMap, key: string) -> Option<string> {
map.map.get(&key).cloned()
}
#[mel_treatment(
input map Stream<StringMap>
output value Stream<Option<string>>
)]
pub async fn get(key: string) {
while let Ok(map) = map.recv_one_as::<Arc<StringMap>>().await {
check!(value.send_one_as(map.map.get(&key).cloned()).await)
}
}
#[mel_function]
pub fn insert(mut map: StringMap, key: string, value: string) -> StringMap {
map.map.insert(key, value);
map
}
#[mel_treatment(
input base Stream<StringMap>
input value Stream<string>
output map Stream<StringMap>
)]
pub async fn insert(key: string) {
while let (Ok(base), Ok(value)) = (
base.recv_one_as::<Arc<StringMap>>().await,
value.recv_one_as::<String>().await,
) {
let mut new_map = Arc::unwrap_or_clone(base);
new_map.map.insert(key.clone(), value);
check!(map.send_one(Value::Data(Arc::new(new_map))).await)
}
}