use std::path::PathBuf;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::params::ParamSpec;
use crate::types::Type;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct PortSpec {
pub name: String,
#[serde(rename = "type")]
pub ty: Type,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub optional: bool,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub many: bool,
}
impl PortSpec {
pub fn new(name: impl Into<String>, ty: impl AsRef<str>) -> Self {
Self { name: name.into(), ty: Type::new(ty), optional: false, many: false }
}
pub fn optional(mut self) -> Self {
self.optional = true;
self
}
pub fn many(mut self) -> Self {
self.many = true;
self
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Margin {
pub before: [usize; 3],
pub after: [usize; 3],
}
impl Margin {
pub const NONE: Margin = Margin { before: [0; 3], after: [0; 3] };
pub fn around(n: usize, axes: usize) -> Self {
let mut m = Margin::NONE;
for a in (3 - axes.min(3))..3 {
m.before[a] = n;
m.after[a] = n;
}
m
}
pub fn time(before: usize, after: usize) -> Self {
Margin { before: [0, 0, before], after: [0, 0, after] }
}
pub fn then(self, next: Margin) -> Margin {
let mut m = self;
for a in 0..3 {
m.before[a] += next.before[a];
m.after[a] += next.after[a];
}
m
}
pub fn max(self, other: Margin) -> Margin {
let mut m = self;
for a in 0..3 {
m.before[a] = m.before[a].max(other.before[a]);
m.after[a] = m.after[a].max(other.after[a]);
}
m
}
pub fn is_none(&self) -> bool {
*self == Margin::NONE
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum Language {
Python,
Rust,
}
impl Language {
pub fn of(path: &std::path::Path) -> Option<Language> {
match path.extension()?.to_str()? {
"py" => Some(Language::Python),
"rs" => Some(Language::Rust),
_ => None,
}
}
pub fn comment(self) -> &'static str {
match self {
Language::Python => "#",
Language::Rust => "//",
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "from", rename_all = "snake_case")]
pub enum OpSource {
Builtin,
Script {
path: PathBuf,
language: Language,
hash: String,
},
Tool {
tool: String,
operation: String,
},
Recipe {
hash: String,
},
}
fn one() -> u32 {
1
}
fn yes() -> bool {
true
}
fn is_one(v: &u32) -> bool {
*v == 1
}
fn is_true(v: &bool) -> bool {
*v
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct OpSpec {
pub id: String,
#[serde(default = "one", skip_serializing_if = "is_one")]
pub version: u32,
pub title: String,
#[serde(default)]
pub family: String,
#[serde(default)]
pub summary: String,
pub inputs: Vec<PortSpec>,
pub outputs: Vec<PortSpec>,
#[serde(default)]
pub params: Vec<ParamSpec>,
#[serde(default, alias = "context")]
pub margin: Margin,
pub source: OpSource,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub finish: Option<String>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub effects: bool,
#[serde(default = "yes", skip_serializing_if = "is_true")]
pub available: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub unavailable: Option<String>,
}
impl OpSpec {
pub fn new(id: impl Into<String>, title: impl Into<String>) -> Self {
Self {
id: id.into(),
version: 1,
title: title.into(),
family: String::new(),
summary: String::new(),
inputs: Vec::new(),
outputs: Vec::new(),
params: Vec::new(),
margin: Margin::NONE,
source: OpSource::Builtin,
finish: None,
effects: false,
available: true,
unavailable: None,
}
}
pub fn family(mut self, family: impl Into<String>) -> Self {
self.family = family.into();
self
}
pub fn summary(mut self, summary: impl Into<String>) -> Self {
self.summary = summary.into();
self
}
pub fn input(mut self, port: PortSpec) -> Self {
self.inputs.push(port);
self
}
pub fn output(mut self, port: PortSpec) -> Self {
self.outputs.push(port);
self
}
pub fn param(mut self, param: ParamSpec) -> Self {
self.params.push(param);
self
}
pub fn margin(mut self, margin: Margin) -> Self {
self.margin = margin;
self
}
pub fn context(self, margin: Margin) -> Self {
self.margin(margin)
}
pub fn version(mut self, version: u32) -> Self {
self.version = version;
self
}
pub fn finish(mut self, finish: impl Into<String>) -> Self {
self.finish = Some(finish.into());
self
}
pub fn effects(mut self) -> Self {
self.effects = true;
self
}
pub fn unavailable(mut self, why: impl Into<String>) -> Self {
self.available = false;
self.unavailable = Some(why.into());
self
}
pub fn version_key(&self) -> String {
match &self.source {
OpSource::Script { hash, .. } | OpSource::Recipe { hash } => hash.clone(),
OpSource::Builtin | OpSource::Tool { .. } => format!("v{}", self.version),
}
}
pub fn input_port(&self, name: &str) -> Option<&PortSpec> {
self.inputs.iter().find(|p| p.name == name)
}
pub fn output_port(&self, name: &str) -> Option<&PortSpec> {
self.outputs.iter().find(|p| p.name == name)
}
pub fn check(&self) -> Result<(), String> {
let id = &self.id;
if id.trim().is_empty() {
return Err("an operation needs an id".into());
}
if self.outputs.is_empty() {
return Err(format!("{id}: no outputs"));
}
if self.version == 0 {
return Err(format!("{id}: versions start at 1"));
}
if !self.available && self.unavailable.is_none() {
return Err(format!("{id}: unavailable without a reason"));
}
for (what, names) in [
("input", self.inputs.iter().map(|p| &p.name).collect::<Vec<_>>()),
("output", self.outputs.iter().map(|p| &p.name).collect()),
("parameter", self.params.iter().map(|p| &p.name).collect()),
] {
let mut seen = std::collections::BTreeSet::new();
for n in names {
if n.is_empty() || !n.chars().all(|c| c.is_alphanumeric() || c == '_') {
return Err(format!("{id}: {what} name {n:?} (letters, digits and _ only)"));
}
if !seen.insert(n) {
return Err(format!("{id}: {what} {n} twice"));
}
}
}
for p in &self.params {
p.check(&p.default).map_err(|e| format!("{id}: default of {e}"))?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::params::ParamSpec;
fn blur() -> OpSpec {
OpSpec::new("gaussian_blur", "Gaussian blur")
.family("Filter")
.input(PortSpec::new("image", "image"))
.output(PortSpec::new("image", "image"))
.param(ParamSpec::number("sigma", 2.0).range(0.2, 20.0))
.margin(Margin::around(6, 2))
}
#[test]
fn a_well_formed_spec_checks() {
blur().check().unwrap();
let twice = blur().input(PortSpec::new("image", "mask"));
assert!(twice.check().unwrap_err().contains("input image twice"));
let no_out = OpSpec::new("x", "X").input(PortSpec::new("a", "image"));
assert!(no_out.check().unwrap_err().contains("no outputs"));
let bad_default = blur().param(ParamSpec::number("gain", 50.0).range(0.0, 10.0));
assert!(bad_default.check().unwrap_err().contains("default"));
let bad_name = blur().param(ParamSpec::number("two words", 1.0));
assert!(bad_name.check().is_err());
assert!(blur().version(0).check().unwrap_err().contains("start at 1"));
}
#[test]
fn versions_effects_and_availability() {
let b = blur();
assert_eq!(b.version, 1);
assert_eq!(b.version_key(), "v1");
assert_eq!(blur().version(3).version_key(), "v3");
let mut script = blur();
script.source = OpSource::Script { path: "a.py".into(), language: Language::Python, hash: "sha256:ab".into() };
assert_eq!(script.version_key(), "sha256:ab", "a script is versioned by its hash");
let upload = OpSpec::new("dandi_upload", "Upload to DANDI")
.input(PortSpec::new("x", "file"))
.output(PortSpec::new("receipt", "table"))
.effects();
let json = serde_json::to_string(&upload).unwrap();
assert!(json.contains(r#""effects":true"#) && !json.contains("version"), "{json}");
let sam = blur().unavailable("weights not downloaded");
assert!(!sam.available);
sam.check().unwrap();
let mut silent = blur();
silent.available = false;
assert!(silent.check().is_err());
let old: OpSpec = serde_json::from_str(r#"{"id":"x","title":"X","inputs":[],"outputs":[{"name":"o","type":"image"}],"context":{"before":[0,0,3],"after":[0,0,3]},"source":{"from":"builtin"}}"#).unwrap();
assert_eq!(old.margin, Margin::time(3, 3));
}
#[test]
fn margins_add_along_a_pipeline_and_take_the_larger_at_a_join() {
let blur = Margin::around(12, 2);
assert_eq!(blur.before, [0, 12, 12]);
let along = blur.then(Margin::around(3, 2));
assert_eq!(along.after, [0, 15, 15]);
let filter = Margin::time(300, 0);
assert_eq!(filter.max(Margin::time(10, 40)), Margin::time(300, 40));
assert!(Margin::NONE.is_none());
}
#[test]
fn specs_read_and_write_json() {
let spec = blur();
let json = serde_json::to_string(&spec).unwrap();
assert!(json.contains(r#""from":"builtin""#));
let back: OpSpec = serde_json::from_str(&json).unwrap();
assert_eq!(back, spec);
assert_eq!(Language::of(std::path::Path::new("a/b.py")), Some(Language::Python));
}
}