std-mel 0.10.1

Mélodium standard library
Documentation
use melodium_core::{
    common::executive::{Output, ResultStatus},
    *,
};
use melodium_macro::{mel_function, mel_model};
use std::collections::HashMap;

pub mod log;

/// Provides interactions with Mélodium engine.
///
/// `ready` source is triggered at startup when engine is ready to process.
#[mel_model(
    source ready () () (trigger Block<void>)
    continuous (continuous)
)]
#[derive(Debug)]
pub struct Engine {
    model: std::sync::Weak<EngineModel>,
}

impl Engine {
    fn new(model: std::sync::Weak<EngineModel>) -> Self {
        Self { model }
    }

    async fn continuous(&self) {
        let model = self.model.upgrade().unwrap();

        model
            .new_ready(
                None,
                &HashMap::new(),
                Some(Box::new(|mut outputs| {
                    let trigger = outputs.get("trigger");

                    vec![Box::new(Box::pin(Self::ready(trigger)))]
                })),
            )
            .await;
    }

    async fn ready(trigger: Box<dyn Output>) -> ResultStatus {
        let _ = trigger.send_one(().into()).await;
        trigger.close().await;
        ResultStatus::Ok
    }

    fn invoke_source(&self, _source: &str, _params: HashMap<String, Value>) {}
}

/// Return the current Mélodium engine version string.
#[mel_function]
pub fn version() -> string {
    melodium_engine::VERSION.to_string()
}

/// Return the unique identifier of the current execution run.
#[mel_function]
pub fn run_id() -> string {
    melodium_engine::execution_run_id().to_string()
}

/// Return the group identifier of the current execution run, shared across all workers of the same job.
#[mel_function]
pub fn group_id() -> string {
    melodium_engine::execution_group_id().to_string()
}