1use std::collections::HashMap;
2use std::fmt;
3use std::path::PathBuf;
4use std::str::FromStr;
5
6use serde::{Deserialize, Serialize};
7
8use bv_types::{Cardinality, TypeRef};
9
10use crate::error::{BvError, Result};
11
12#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
14#[serde(rename_all = "lowercase")]
15pub enum Tier {
16 Core,
18 #[default]
20 Community,
21 Experimental,
23}
24
25impl Tier {
26 pub fn as_str(&self) -> &'static str {
27 match self {
28 Tier::Core => "core",
29 Tier::Community => "community",
30 Tier::Experimental => "experimental",
31 }
32 }
33}
34
35impl fmt::Display for Tier {
36 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37 f.write_str(self.as_str())
38 }
39}
40
41#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
43pub struct CudaVersion {
44 pub major: u32,
45 pub minor: u32,
46}
47
48impl fmt::Display for CudaVersion {
49 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50 write!(f, "{}.{}", self.major, self.minor)
51 }
52}
53
54impl FromStr for CudaVersion {
55 type Err = String;
56
57 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
58 let (maj, min) = s
59 .split_once('.')
60 .ok_or_else(|| format!("expected 'major.minor', got '{s}'"))?;
61 Ok(CudaVersion {
62 major: maj
63 .parse()
64 .map_err(|_| format!("invalid major version '{maj}'"))?,
65 minor: min
66 .parse()
67 .map_err(|_| format!("invalid minor version '{min}'"))?,
68 })
69 }
70}
71
72impl TryFrom<String> for CudaVersion {
73 type Error = String;
74 fn try_from(s: String) -> std::result::Result<Self, Self::Error> {
75 s.parse()
76 }
77}
78
79impl From<CudaVersion> for String {
80 fn from(v: CudaVersion) -> String {
81 v.to_string()
82 }
83}
84
85impl Serialize for CudaVersion {
86 fn serialize<S: serde::Serializer>(&self, s: S) -> std::result::Result<S::Ok, S::Error> {
87 s.serialize_str(&self.to_string())
88 }
89}
90
91impl<'de> Deserialize<'de> for CudaVersion {
92 fn deserialize<D: serde::Deserializer<'de>>(d: D) -> std::result::Result<Self, D::Error> {
93 let s = String::deserialize(d)?;
94 s.parse().map_err(serde::de::Error::custom)
95 }
96}
97
98#[derive(Debug, Clone, Serialize, Deserialize)]
99pub struct GpuSpec {
100 pub required: bool,
101 pub min_vram_gb: Option<u32>,
102 pub cuda_version: Option<CudaVersion>,
103}
104
105#[derive(Debug, Clone, Serialize, Deserialize)]
106pub struct HardwareSpec {
107 pub gpu: Option<GpuSpec>,
108 pub cpu_cores: Option<u32>,
109 pub ram_gb: Option<f64>,
110 pub disk_gb: Option<f64>,
111}
112
113impl HardwareSpec {
114 pub fn check_against(
117 &self,
118 detected: &crate::hardware::DetectedHardware,
119 ) -> Vec<crate::hardware::HardwareMismatch> {
120 use crate::hardware::HardwareMismatch;
121 let mut out = Vec::new();
122
123 if let Some(gpu_req) = &self.gpu
124 && gpu_req.required
125 {
126 if detected.gpus.is_empty() {
127 out.push(HardwareMismatch::NoGpu);
128 } else {
129 if let Some(min_vram) = gpu_req.min_vram_gb {
130 let best_vram_mb = detected.gpus.iter().map(|g| g.vram_mb).max().unwrap_or(0);
131 let best_vram_gb = (best_vram_mb as f64 / 1024.0).floor() as u32;
132 if best_vram_gb < min_vram {
133 out.push(HardwareMismatch::InsufficientVram {
134 required_gb: min_vram,
135 available_gb: best_vram_gb,
136 });
137 }
138 }
139 if let Some(min_cuda) = &gpu_req.cuda_version {
140 let best_cuda = detected
141 .gpus
142 .iter()
143 .filter_map(|g| g.cuda_version.as_ref())
144 .max();
145 match best_cuda {
146 None => out.push(HardwareMismatch::NoCuda {
147 required: min_cuda.clone(),
148 }),
149 Some(avail) if avail < min_cuda => {
150 out.push(HardwareMismatch::CudaTooOld {
151 required: min_cuda.clone(),
152 available: avail.clone(),
153 });
154 }
155 _ => {}
156 }
157 }
158 }
159 }
160
161 if let Some(min_ram) = self.ram_gb {
162 let avail = detected.ram_gb();
163 if avail < min_ram {
164 out.push(HardwareMismatch::InsufficientRam {
165 required_gb: min_ram,
166 available_gb: avail,
167 });
168 }
169 }
170
171 if let Some(min_disk) = self.disk_gb {
172 let avail = detected.disk_free_gb();
173 if avail < min_disk {
174 out.push(HardwareMismatch::InsufficientDisk {
175 required_gb: min_disk,
176 available_gb: avail,
177 });
178 }
179 }
180
181 out
182 }
183}
184
185#[derive(Debug, Clone, Serialize, Deserialize)]
186pub struct ImageSpec {
187 pub backend: String,
189 pub reference: String,
191 pub digest: Option<String>,
193}
194
195#[derive(Debug, Clone, Serialize, Deserialize)]
196pub struct ReferenceDataSpec {
197 pub id: String,
198 pub version: String,
199 pub required: bool,
200 #[serde(default, skip_serializing_if = "Option::is_none")]
202 pub mount_path: Option<String>,
203 #[serde(default, skip_serializing_if = "Option::is_none")]
205 pub size_bytes: Option<u64>,
206}
207
208#[derive(Debug, Clone, Serialize, Deserialize)]
210pub struct IoSpec {
211 pub name: String,
212 #[serde(rename = "type")]
214 pub r#type: TypeRef,
215 #[serde(default)]
217 pub cardinality: Cardinality,
218 #[serde(default, skip_serializing_if = "Option::is_none")]
220 pub mount: Option<PathBuf>,
221 #[serde(default, skip_serializing_if = "Option::is_none")]
222 pub description: Option<String>,
223 #[serde(default, skip_serializing_if = "Option::is_none")]
224 pub default: Option<String>,
225}
226
227#[derive(Debug, Clone, Serialize, Deserialize)]
228pub struct EntrypointSpec {
229 pub command: String,
230 pub args_template: Option<String>,
231 #[serde(default)]
232 pub env: HashMap<String, String>,
233}
234
235#[derive(Debug, Clone, Serialize, Deserialize)]
240pub struct BinariesSpec {
241 pub exposed: Vec<String>,
242}
243
244#[derive(Debug, Clone, Default, Serialize, Deserialize)]
252pub struct SmokeSpec {
253 #[serde(default, skip_serializing_if = "std::collections::HashMap::is_empty")]
258 pub probes: std::collections::HashMap<String, String>,
259 #[serde(default, skip_serializing_if = "Vec::is_empty")]
263 pub skip: Vec<String>,
264}
265
266#[allow(dead_code)]
267fn default_timeout() -> u64 {
268 60
269}
270
271#[derive(Debug, Clone, Serialize, Deserialize)]
273pub struct SignatureSpec {
274 #[serde(default, skip_serializing_if = "Option::is_none")]
276 pub image: Option<String>,
277 #[serde(default, skip_serializing_if = "Option::is_none")]
279 pub manifest: Option<String>,
280}
281
282#[derive(Debug, Clone, Serialize, Deserialize)]
283pub struct ToolManifest {
284 pub id: String,
285 pub version: String,
286 pub description: Option<String>,
287 pub homepage: Option<String>,
288 pub license: Option<String>,
289 #[serde(default)]
291 pub tier: Tier,
292 #[serde(default, skip_serializing_if = "Vec::is_empty")]
294 pub maintainers: Vec<String>,
295 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
297 pub deprecated: bool,
298 pub image: ImageSpec,
299 pub hardware: HardwareSpec,
300 #[serde(default)]
301 pub reference_data: HashMap<String, ReferenceDataSpec>,
302 #[serde(default)]
304 pub inputs: Vec<IoSpec>,
305 #[serde(default)]
307 pub outputs: Vec<IoSpec>,
308 #[serde(default, skip_serializing_if = "Option::is_none")]
311 pub entrypoint: Option<EntrypointSpec>,
312 #[serde(default, skip_serializing_if = "HashMap::is_empty")]
317 pub subcommands: HashMap<String, Vec<String>>,
318 #[serde(default, skip_serializing_if = "Vec::is_empty")]
324 pub cache_paths: Vec<String>,
325 #[serde(default, skip_serializing_if = "Option::is_none")]
328 pub binaries: Option<BinariesSpec>,
329 #[serde(default, skip_serializing_if = "Option::is_none")]
331 pub smoke: Option<SmokeSpec>,
332 #[serde(default, skip_serializing_if = "Option::is_none")]
334 pub signatures: Option<SignatureSpec>,
335}
336
337impl ToolManifest {
338 pub fn has_typed_io(&self) -> bool {
339 !self.inputs.is_empty() || !self.outputs.is_empty()
340 }
341
342 pub fn effective_binaries(&self) -> Vec<&str> {
348 if let Some(b) = &self.binaries {
349 return b.exposed.iter().map(|s| s.as_str()).collect();
350 }
351 let Some(ep) = &self.entrypoint else {
352 return vec![];
353 };
354 let cmd = &ep.command;
355 let name = cmd
356 .rfind('/')
357 .map(|i| &cmd[i + 1..])
358 .unwrap_or(cmd.as_str());
359 vec![name]
360 }
361}
362
363#[derive(Debug, Clone, Serialize, Deserialize)]
365pub struct Manifest {
366 pub tool: ToolManifest,
367}
368
369#[derive(Debug)]
370pub struct ValidationError {
371 pub field: String,
372 pub message: String,
373}
374
375impl fmt::Display for ValidationError {
376 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
377 write!(f, "{}: {}", self.field, self.message)
378 }
379}
380
381impl Manifest {
382 pub fn from_toml_str(s: &str) -> Result<Self> {
383 let m: Manifest = toml::from_str(s).map_err(|e| BvError::ManifestParse(e.to_string()))?;
384 m.validate_types()?;
385 Ok(m)
386 }
387
388 pub fn to_toml_string(&self) -> Result<String> {
389 toml::to_string_pretty(self).map_err(|e| BvError::ManifestParse(e.to_string()))
390 }
391
392 fn validate_types(&self) -> Result<()> {
394 let t = &self.tool;
395 for (side, specs) in [("inputs", &t.inputs), ("outputs", &t.outputs)] {
396 for spec in specs {
397 let id = spec.r#type.base_id();
398 if bv_types::lookup(id).is_none() {
399 let suggestion = bv_types::suggest(id)
400 .map(|s| format!(", did you mean `{s}`?"))
401 .unwrap_or_default();
402 return Err(BvError::ManifestParse(format!(
403 "tool.{side}[{}]: unknown type `{id}`{suggestion}",
404 spec.name
405 )));
406 }
407 }
408 }
409 Ok(())
410 }
411
412 pub fn validate(&self) -> std::result::Result<(), Vec<ValidationError>> {
414 let mut errors = Vec::new();
415 let t = &self.tool;
416
417 if t.id.is_empty() {
418 errors.push(ValidationError {
419 field: "tool.id".into(),
420 message: "must not be empty".into(),
421 });
422 }
423 if t.version.is_empty() {
424 errors.push(ValidationError {
425 field: "tool.version".into(),
426 message: "must not be empty".into(),
427 });
428 }
429 if t.image.backend.is_empty() {
430 errors.push(ValidationError {
431 field: "tool.image.backend".into(),
432 message: "must not be empty".into(),
433 });
434 }
435 if t.image.reference.is_empty() {
436 errors.push(ValidationError {
437 field: "tool.image.reference".into(),
438 message: "must not be empty".into(),
439 });
440 }
441 match (&t.entrypoint, t.subcommands.is_empty()) {
442 (None, true) => errors.push(ValidationError {
443 field: "tool.entrypoint".into(),
444 message: "must declare either [tool.entrypoint] or [tool.subcommands]".into(),
445 }),
446 (Some(ep), _) if ep.command.is_empty() => errors.push(ValidationError {
447 field: "tool.entrypoint.command".into(),
448 message: "must not be empty".into(),
449 }),
450 _ => {}
451 }
452
453 for (name, cmd) in &t.subcommands {
454 if name.is_empty() {
455 errors.push(ValidationError {
456 field: "tool.subcommands".into(),
457 message: "subcommand name must not be empty".into(),
458 });
459 continue;
460 }
461 if name.starts_with('-') {
462 errors.push(ValidationError {
463 field: format!("tool.subcommands.{name}"),
464 message: "subcommand name must not start with '-'".into(),
465 });
466 }
467 if cmd.is_empty() {
468 errors.push(ValidationError {
469 field: format!("tool.subcommands.{name}"),
470 message: "command vector must not be empty".into(),
471 });
472 }
473 }
474
475 for spec in &t.inputs {
476 if let Some(mount) = &spec.mount
477 && !mount.is_absolute()
478 {
479 errors.push(ValidationError {
480 field: format!("tool.inputs[{}].mount", spec.name),
481 message: "must be an absolute path".into(),
482 });
483 }
484 }
485 for spec in &t.outputs {
486 if let Some(mount) = &spec.mount
487 && !mount.is_absolute()
488 {
489 errors.push(ValidationError {
490 field: format!("tool.outputs[{}].mount", spec.name),
491 message: "must be an absolute path".into(),
492 });
493 }
494 }
495
496 if let Some(binaries) = &t.binaries {
497 let mut seen = std::collections::HashSet::new();
498 for name in &binaries.exposed {
499 if !seen.insert(name.as_str()) {
500 errors.push(ValidationError {
501 field: "tool.binaries.exposed".into(),
502 message: format!("duplicate binary name '{name}'"),
503 });
504 }
505 }
506 if !binaries.exposed.is_empty()
507 && let Some(ep) = &t.entrypoint
508 {
509 let cmd = &ep.command;
510 let basename = cmd.rfind('/').map(|i| &cmd[i + 1..]).unwrap_or(cmd);
511 if !binaries.exposed.iter().any(|b| b == basename) {
512 errors.push(ValidationError {
513 field: "tool.binaries.exposed".into(),
514 message: format!(
515 "entrypoint command '{basename}' must be listed in exposed"
516 ),
517 });
518 }
519 }
520 }
521
522 if errors.is_empty() {
523 Ok(())
524 } else {
525 Err(errors)
526 }
527 }
528}
529
530#[cfg(test)]
531mod tests {
532 use super::*;
533
534 const SAMPLE: &str = r#"
535[tool]
536id = "bwa"
537version = "0.7.17"
538description = "BWA short-read aligner"
539homepage = "http://bio-bwa.sourceforge.net/"
540license = "GPL-3.0"
541
542[tool.image]
543backend = "docker"
544reference = "biocontainers/bwa:0.7.17--h5bf99c6_8"
545digest = "sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890ab"
546
547[tool.hardware]
548cpu_cores = 8
549ram_gb = 32.0
550disk_gb = 50.0
551
552[tool.hardware.gpu]
553required = false
554
555[[tool.inputs]]
556name = "reads_r1"
557type = "fastq"
558cardinality = "one"
559description = "Forward reads"
560
561[[tool.inputs]]
562name = "reads_r2"
563type = "fastq"
564cardinality = "optional"
565description = "Reverse reads (paired-end)"
566
567[[tool.outputs]]
568name = "alignment"
569type = "bam"
570description = "Aligned reads"
571
572[tool.entrypoint]
573command = "bwa"
574args_template = "mem -t {cpu_cores} {reference} {reads_r1} {reads_r2}"
575
576[tool.entrypoint.env]
577MALLOC_ARENA_MAX = "4"
578"#;
579
580 const SAMPLE_NO_IO: &str = r#"
581[tool]
582id = "mytool"
583version = "1.0.0"
584
585[tool.image]
586backend = "docker"
587reference = "example/mytool:1.0.0"
588
589[tool.hardware]
590
591[tool.entrypoint]
592command = "mytool"
593"#;
594
595 #[test]
596 fn round_trip() {
597 let manifest = Manifest::from_toml_str(SAMPLE).expect("parse failed");
598 assert_eq!(manifest.tool.id, "bwa");
599 assert_eq!(manifest.tool.version, "0.7.17");
600 assert_eq!(manifest.tool.image.backend, "docker");
601 assert_eq!(manifest.tool.inputs.len(), 2);
602 assert_eq!(manifest.tool.outputs.len(), 1);
603 assert_eq!(manifest.tool.inputs[0].cardinality, Cardinality::One);
604 assert_eq!(manifest.tool.inputs[1].cardinality, Cardinality::Optional);
605
606 let serialised = manifest.to_toml_string().expect("serialise failed");
607 let reparsed = Manifest::from_toml_str(&serialised).expect("reparse failed");
608 assert_eq!(reparsed.tool.id, manifest.tool.id);
609 assert_eq!(reparsed.tool.version, manifest.tool.version);
610 }
611
612 #[test]
613 fn no_io_parses_unchanged() {
614 let m = Manifest::from_toml_str(SAMPLE_NO_IO).expect("parse failed");
615 assert!(m.tool.inputs.is_empty());
616 assert!(m.tool.outputs.is_empty());
617 assert!(!m.tool.has_typed_io());
618 }
619
620 #[test]
621 fn typeref_params_parsed() {
622 let s = r#"
623[tool]
624id = "t"
625version = "1.0.0"
626
627[tool.image]
628backend = "docker"
629reference = "example/t:1.0.0"
630
631[tool.hardware]
632
633[[tool.inputs]]
634name = "seqs"
635type = "fasta[protein]"
636cardinality = "one"
637
638[tool.entrypoint]
639command = "t"
640"#;
641 let m = Manifest::from_toml_str(s).unwrap();
642 assert_eq!(m.tool.inputs[0].r#type.params, vec!["protein"]);
643 }
644
645 #[test]
646 fn unknown_type_error() {
647 let s = r#"
648[tool]
649id = "t"
650version = "1.0.0"
651
652[tool.image]
653backend = "docker"
654reference = "example/t:1.0.0"
655
656[tool.hardware]
657
658[[tool.inputs]]
659name = "seqs"
660type = "protien_fasta"
661cardinality = "one"
662
663[tool.entrypoint]
664command = "t"
665"#;
666 let err = Manifest::from_toml_str(s).unwrap_err();
667 let msg = err.to_string();
668 assert!(msg.contains("unknown type"), "got: {msg}");
669 }
670
671 #[test]
672 fn cuda_version_ordering() {
673 let v12_1: CudaVersion = "12.1".parse().unwrap();
674 let v12_4: CudaVersion = "12.4".parse().unwrap();
675 let v13_0: CudaVersion = "13.0".parse().unwrap();
676 assert!(v12_1 < v12_4);
677 assert!(v12_4 < v13_0);
678 assert_eq!(v12_1, "12.1".parse::<CudaVersion>().unwrap());
679 }
680
681 #[test]
682 fn subcommands_only_parses() {
683 let s = r#"
684[tool]
685id = "genie2"
686version = "1.0.0"
687
688[tool.image]
689backend = "docker"
690reference = "ghcr.io/example/genie2:1.0.0"
691
692[tool.hardware]
693
694[tool.subcommands]
695train = ["python", "genie/train.py"]
696sample_unconditional = ["python", "genie/sample_unconditional.py"]
697"#;
698 let m = Manifest::from_toml_str(s).unwrap();
699 assert!(m.tool.entrypoint.is_none());
700 assert_eq!(m.tool.subcommands.len(), 2);
701 assert_eq!(
702 m.tool.subcommands.get("train").unwrap(),
703 &vec!["python".to_string(), "genie/train.py".to_string()]
704 );
705 m.validate().expect("subcommand-only manifest is valid");
706 assert!(m.tool.effective_binaries().is_empty());
708 }
709
710 #[test]
711 fn validate_requires_entrypoint_or_subcommands() {
712 let s = r#"
713[tool]
714id = "broken"
715version = "1.0.0"
716
717[tool.image]
718backend = "docker"
719reference = "example/broken:1.0.0"
720
721[tool.hardware]
722"#;
723 let m = Manifest::from_toml_str(s).unwrap();
724 let errs = m.validate().unwrap_err();
725 assert!(
726 errs.iter().any(|e| e.field == "tool.entrypoint"),
727 "expected entrypoint-or-subcommands error, got: {errs:?}"
728 );
729 }
730
731 #[test]
732 fn validate_rejects_dash_prefixed_subcommand() {
733 let s = r#"
734[tool]
735id = "t"
736version = "1.0.0"
737
738[tool.image]
739backend = "docker"
740reference = "example/t:1.0.0"
741
742[tool.hardware]
743
744[tool.subcommands]
745"-bad" = ["python", "x.py"]
746"#;
747 let m = Manifest::from_toml_str(s).unwrap();
748 let errs = m.validate().unwrap_err();
749 assert!(errs.iter().any(|e| e.field.contains("-bad")));
750 }
751
752 #[test]
753 fn subcommands_round_trip() {
754 let s = r#"
755[tool]
756id = "t"
757version = "1.0.0"
758
759[tool.image]
760backend = "docker"
761reference = "example/t:1.0.0"
762
763[tool.hardware]
764
765[tool.subcommands]
766go = ["python", "main.py"]
767"#;
768 let m = Manifest::from_toml_str(s).unwrap();
769 let serialised = m.to_toml_string().unwrap();
770 let reparsed = Manifest::from_toml_str(&serialised).unwrap();
771 assert_eq!(reparsed.tool.subcommands.len(), 1);
772 }
773
774 #[test]
775 fn validate_catches_empty_id() {
776 let mut manifest = Manifest::from_toml_str(SAMPLE).unwrap();
777 manifest.tool.id = String::new();
778 let errs = manifest.validate().unwrap_err();
779 assert!(errs.iter().any(|e| e.field == "tool.id"));
780 }
781
782 #[test]
783 fn registry_manifests_parse() {
784 let registry = concat!(env!("CARGO_MANIFEST_DIR"), "/../../bv-registry/tools");
785 let Ok(read) = std::fs::read_dir(registry) else {
786 return;
787 };
788 for entry in read {
789 let tool_dir = entry.unwrap().path();
790 if !tool_dir.is_dir() {
791 continue;
792 }
793 for version_entry in std::fs::read_dir(&tool_dir).unwrap() {
794 let path = version_entry.unwrap().path();
795 if path.extension().is_some_and(|e| e == "toml") {
796 let s = std::fs::read_to_string(&path)
797 .unwrap_or_else(|_| panic!("failed to read {}", path.display()));
798 Manifest::from_toml_str(&s)
799 .unwrap_or_else(|e| panic!("{}: {e}", path.display()));
800 }
801 }
802 }
803 }
804}