1use std::path::PathBuf;
6
7use kitchen_contract::{ContentId, Format, RunKey, Type};
8use serde::{Deserialize, Serialize};
9
10pub const TOOL_PROTOCOL: u32 = 2;
14
15#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
17#[serde(tag = "request", rename_all = "snake_case")]
18pub enum ToolRequest {
19 Describe,
21 Validate { operation: String, recipe: serde_json::Value },
23 Run(RunRequest),
25}
26
27#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
29pub struct Validation {
30 pub problems: Vec<String>,
32}
33
34impl Validation {
35 pub fn ok(&self) -> bool {
36 self.problems.is_empty()
37 }
38}
39
40#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
43pub struct RunRequest {
44 pub operation: String,
45 pub recipe: serde_json::Value,
46 pub inputs: Vec<RunInput>,
47 pub output_dir: PathBuf,
48 #[serde(default)]
50 pub params: serde_json::Value,
51}
52
53impl RunRequest {
54 pub fn run_key(&self, tool: &ToolRef) -> RunKey {
58 let mut named: Vec<(&str, &str)> = self.inputs.iter().map(|i| (i.name.as_str(), i.id.as_str())).collect();
59 named.sort();
60 RunKey::from_fields(&serde_json::json!({
61 "key": "kitchen-run-v1",
62 "tool": tool.name,
63 "version": tool.version,
64 "image": tool.image,
65 "operation": self.operation,
66 "recipe": ContentId::of_json(&self.recipe).as_str(),
67 "inputs": named,
68 "params": self.params,
69 }))
70 }
71}
72
73#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
75pub struct RunInput {
76 pub name: String,
78 #[serde(rename = "type")]
79 pub ty: Type,
80 pub format: Format,
81 pub path: PathBuf,
82 pub id: ContentId,
83}
84
85#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
87pub struct ToolRef {
88 pub name: String,
89 pub version: String,
90 #[serde(default, skip_serializing_if = "Option::is_none")]
92 pub image: Option<String>,
93}
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98
99 #[test]
100 fn requests_are_tagged_json_lines() {
101 assert_eq!(serde_json::to_string(&ToolRequest::Describe).unwrap(), r#"{"request":"describe"}"#);
102 let run = ToolRequest::Run(RunRequest {
103 operation: "sort".into(),
104 recipe: serde_json::json!({"sorter": "ks4"}),
105 inputs: vec![],
106 output_dir: "/out".into(),
107 params: serde_json::Value::Null,
108 });
109 let line = serde_json::to_string(&run).unwrap();
110 assert!(line.starts_with(r#"{"request":"run","operation":"sort""#), "{line}");
111 assert_eq!(serde_json::from_str::<ToolRequest>(&line).unwrap(), run);
112 }
113
114 #[test]
115 fn the_same_run_has_the_same_key() {
116 let tool = ToolRef { name: "dspk".into(), version: "0.0.13".into(), image: None };
117 let input = |name: &str, c: char| RunInput {
118 name: name.into(),
119 ty: Type::new("signal"),
120 format: Format::parse("nwb-zarr").unwrap(),
121 path: "/data/x".into(),
122 id: ContentId::parse(&format!("sha256:{}", c.to_string().repeat(64))).unwrap(),
123 };
124 let reordered: serde_json::Value = serde_json::from_str(r#"{"threshold": 6, "sorter": "ks4"}"#).unwrap();
125 let run = |inputs: Vec<RunInput>, recipe: serde_json::Value| RunRequest {
126 operation: "sort".into(),
127 recipe,
128 inputs,
129 output_dir: "/out".into(),
130 params: serde_json::Value::Null,
131 };
132 let key = run(
133 vec![input("recording", 'a'), input("probe", 'b')],
134 serde_json::json!({"sorter": "ks4", "threshold": 6}),
135 )
136 .run_key(&tool);
137 assert_eq!(
138 key,
139 run(vec![input("probe", 'b'), input("recording", 'a')], reordered.clone()).run_key(&tool),
140 "order does not count"
141 );
142 assert_ne!(key, run(vec![input("recording", 'a'), input("probe", 'c')], reordered.clone()).run_key(&tool));
143 let newer = ToolRef { version: "0.0.14".into(), ..tool.clone() };
144 assert_ne!(key, run(vec![input("recording", 'a'), input("probe", 'b')], reordered).run_key(&newer));
145 }
146}