Skip to main content

kitchen_tools/tool/
report.rs

1//! What a run produced.
2
3use std::path::PathBuf;
4
5use kitchen_contract::{Format, Type};
6use serde::{Deserialize, Serialize};
7
8use crate::tool::Operation;
9
10/// A tool's answer to `run`.
11#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
12pub struct RunReport {
13    pub outputs: Vec<RunOutput>,
14    /// What a person should look at. The tool's own checks (a recording with dead channels, a
15    /// segmentation with almost no cells); later also a model's. Advisory: it never changes a
16    /// result.
17    #[serde(default)]
18    pub review: Vec<ReviewFlag>,
19    /// A few numbers worth showing in the orchestrator's list of runs (units found, cells).
20    #[serde(default)]
21    pub summary: serde_json::Value,
22}
23
24impl RunReport {
25    /// Every output a port of `operation`, of that port's type and format, under `output_dir`; every output
26    /// port written unless the tool says why not in `review`.
27    pub fn check(&self, operation: &Operation, output_dir: &std::path::Path) -> Result<(), String> {
28        for out in &self.outputs {
29            let Some(port) = operation.outputs.iter().find(|p| p.name == out.name) else {
30                return Err(format!("{}: no output port {}", operation.name, out.name));
31            };
32            if port.ty != out.ty || port.format != out.format {
33                return Err(format!(
34                    "{}.{}: wrote {} in {}, declared {} in {}",
35                    operation.name, out.name, out.ty, out.format, port.ty, port.format
36                ));
37            }
38            if !out.path.starts_with(output_dir) {
39                return Err(format!(
40                    "{}.{}: {} is outside the run's folder",
41                    operation.name,
42                    out.name,
43                    out.path.display()
44                ));
45            }
46        }
47        Ok(())
48    }
49}
50
51/// One result a run wrote.
52#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
53pub struct RunOutput {
54    /// Its output port.
55    pub name: String,
56    #[serde(rename = "type")]
57    pub ty: Type,
58    pub format: Format,
59    pub path: PathBuf,
60}
61
62/// Something a person should look at.
63#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
64pub struct ReviewFlag {
65    /// Why, in words a person can act on.
66    pub reason: String,
67    /// The output it is about, when it is about one.
68    #[serde(default, skip_serializing_if = "Option::is_none")]
69    pub output: Option<String>,
70}