Skip to main content

std_mel/engine/
mod.rs

1use melodium_core::{
2    common::executive::{Output, ResultStatus},
3    *,
4};
5use melodium_macro::{mel_function, mel_model};
6use std::collections::HashMap;
7
8pub mod log;
9
10/// Provides interactions with Mélodium engine.
11///
12/// `ready` source is triggered at startup when engine is ready to process.
13#[mel_model(
14    source ready () () (trigger Block<void>)
15    continuous (continuous)
16)]
17#[derive(Debug)]
18pub struct Engine {
19    model: std::sync::Weak<EngineModel>,
20}
21
22impl Engine {
23    fn new(model: std::sync::Weak<EngineModel>) -> Self {
24        Self { model }
25    }
26
27    async fn continuous(&self) {
28        let model = self.model.upgrade().unwrap();
29
30        model
31            .new_ready(
32                None,
33                &HashMap::new(),
34                Some(Box::new(|mut outputs| {
35                    let trigger = outputs.get("trigger");
36
37                    vec![Box::new(Box::pin(Self::ready(trigger)))]
38                })),
39            )
40            .await;
41    }
42
43    async fn ready(trigger: Box<dyn Output>) -> ResultStatus {
44        let _ = trigger.send_one(().into()).await;
45        trigger.close().await;
46        ResultStatus::Ok
47    }
48
49    fn invoke_source(&self, _source: &str, _params: HashMap<String, Value>) {}
50}
51
52#[mel_function]
53pub fn version() -> string {
54    melodium_engine::VERSION.to_string()
55}
56
57#[mel_function]
58pub fn run_id() -> string {
59    melodium_engine::execution_run_id().to_string()
60}
61
62#[mel_function]
63pub fn group_id() -> string {
64    melodium_engine::execution_group_id().to_string()
65}