shelly-liveview 0.6.0

Core runtime primitives for Shelly LiveView.
Documentation
use serde_json::Value;
use std::collections::BTreeMap;

/// Structured event payload emitted by adapter-managed runtime tasks.
#[derive(Debug, Clone, PartialEq)]
pub struct RuntimeEvent {
    pub event: String,
    pub target: Option<String>,
    pub value: Value,
    pub metadata: BTreeMap<String, Value>,
}

impl RuntimeEvent {
    pub fn new(event: impl Into<String>, value: impl Into<Value>) -> Self {
        Self {
            event: event.into(),
            target: None,
            value: value.into(),
            metadata: BTreeMap::new(),
        }
    }

    pub fn with_target(
        target: impl Into<String>,
        event: impl Into<String>,
        value: impl Into<Value>,
    ) -> Self {
        Self {
            event: event.into(),
            target: Some(target.into()),
            value: value.into(),
            metadata: BTreeMap::new(),
        }
    }
}

/// Internal runtime commands collected from `Context` and executed by the adapter.
#[derive(Debug, Clone, PartialEq)]
pub enum RuntimeCommand {
    ScheduleOnce {
        id: String,
        delay_ms: u64,
        dispatch: RuntimeEvent,
    },
    ScheduleInterval {
        id: String,
        every_ms: u64,
        dispatch: RuntimeEvent,
    },
    Cancel {
        id: String,
    },
}