modewise_pipeline/
operation.rs1use std::collections::BTreeMap;
2use std::sync::Arc;
3
4use serde::Serialize;
5use serde::de::DeserializeOwned;
6use serde_json::Value;
7
8use crate::{Artifact, ArtifactKind};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub struct Port {
14 pub name: &'static str,
15 pub kinds: &'static [ArtifactKind],
16}
17
18impl Port {
19 pub fn output_kind(&self) -> ArtifactKind {
21 self.kinds.first().copied().unwrap_or(ArtifactKind::Series)
22 }
23}
24
25#[derive(Debug, Clone, Copy)]
26pub struct OperationDescriptor {
27 pub name: &'static str,
28 pub version: &'static str,
30 pub inputs: &'static [Port],
31 pub outputs: &'static [Port],
32}
33
34pub type Inputs = BTreeMap<String, Arc<Artifact>>;
35pub type Outputs = BTreeMap<String, Artifact>;
36
37pub trait Operation: Send + Sync {
39 fn descriptor(&self) -> OperationDescriptor;
40
41 fn resolve(&self, params: Value) -> Result<Value, String>;
43
44 fn run(&self, params: &Value, inputs: &Inputs) -> Result<Outputs, String>;
46
47 fn output_kind(&self, port: &Port, _inputs: &BTreeMap<String, ArtifactKind>) -> ArtifactKind {
50 port.output_kind()
51 }
52
53 fn summary(&self) -> &'static str {
55 ""
56 }
57
58 fn params_schema(&self) -> Value {
61 Value::Object(Default::default())
62 }
63}
64
65pub fn schema_of<T: schemars::JsonSchema>() -> Value {
67 schemars::schema_for!(T).to_value()
68}
69
70pub fn resolve_params<T: DeserializeOwned + Serialize>(
72 params: Value,
73) -> Result<(T, Value), String> {
74 let typed: T = serde_json::from_value(params).map_err(|e| e.to_string())?;
75 let resolved = serde_json::to_value(&typed).map_err(|e| e.to_string())?;
76 Ok((typed, resolved))
77}