logic_mesh/base/engine/
messages.rs

1// Copyright (c) 2022-2023, Radu Racariu.
2
3use std::collections::BTreeMap;
4
5use anyhow::Result;
6use libhaystack::val::Value;
7use serde::{Deserialize, Serialize};
8use uuid::Uuid;
9
10use crate::base::program::data::{BlockData, LinkData};
11
12/// Block input properties
13#[derive(Default, Debug, Serialize, Deserialize, Clone)]
14pub struct BlockInputData {
15    pub kind: String,
16    pub val: Value,
17}
18
19/// Block output properties
20#[derive(Default, Debug, Serialize, Deserialize, Clone)]
21pub struct BlockOutputData {
22    pub kind: String,
23    pub val: Value,
24}
25
26/// Block definition
27#[derive(Debug, Default, Serialize, Deserialize, Clone)]
28pub struct BlockDefinition {
29    pub id: String,
30    pub name: String,
31    pub library: String,
32    pub inputs: BTreeMap<String, BlockInputData>,
33    pub outputs: BTreeMap<String, BlockOutputData>,
34}
35
36/// Defines the source of a change
37#[derive(Debug, Clone)]
38pub enum ChangeSource {
39    Input(String, Value),
40    Output(String, Value),
41}
42
43/// A notification message for a block change
44#[derive(Debug, Clone)]
45pub struct WatchMessage {
46    pub block_id: Uuid,
47    pub changes: BTreeMap<String, ChangeSource>,
48}
49
50/// Messages that engine accepts
51#[derive(Debug, Clone)]
52pub enum EngineMessage<WatchEventSender: Clone> {
53    AddBlockReq(Uuid, String, Option<String>, Option<String>),
54    AddBlockRes(Result<Uuid, String>),
55
56    RemoveBlockReq(Uuid, Uuid),
57    RemoveBlockRes(Result<Uuid, String>),
58
59    WatchBlockSubReq(Uuid, WatchEventSender),
60    WatchBlockSubRes(Result<Uuid, &'static str>),
61
62    WriteBlockOutputReq(Uuid, Uuid, String, Value),
63    WriteBlockOutputRes(Result<Value, String>),
64
65    WriteBlockInputReq(Uuid, Uuid, String, Value),
66    WriteBlockInputRes(Result<Option<Value>, String>),
67
68    WatchBlockUnsubReq(Uuid),
69    WatchBlockUnsubRes(Result<Uuid, &'static str>),
70
71    GetCurrentProgramReq(Uuid),
72    GetCurrentProgramRes(Result<(Vec<BlockData>, Vec<LinkData>), String>),
73
74    InspectBlockReq(Uuid, Uuid),
75    InspectBlockRes(Result<BlockDefinition, String>),
76
77    EvaluateBlockReq(Uuid, String, Vec<Value>, Option<String>),
78    EvaluateBlockRes(Result<Vec<Value>, String>),
79
80    ConnectBlocksReq(Uuid, LinkData),
81    ConnectBlocksRes(Result<LinkData, String>),
82
83    RemoveLinkReq(Uuid, Uuid),
84    RemoveLinkRes(Result<bool, String>),
85
86    Shutdown,
87    Pause,
88    Resume,
89    Reset,
90}