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, Serialize, Deserialize)]
246pub struct TestSpec {
247 #[serde(default)]
249 pub inputs: std::collections::HashMap<String, String>,
250 #[serde(default)]
252 pub expected_outputs: Vec<String>,
253 #[serde(default)]
255 pub extra_args: Vec<String>,
256 #[serde(default = "default_timeout")]
258 pub timeout_seconds: u64,
259 #[serde(default)]
261 pub slow: bool,
262}
263
264fn default_timeout() -> u64 {
265 60
266}
267
268#[derive(Debug, Clone, Serialize, Deserialize)]
270pub struct SignatureSpec {
271 #[serde(default, skip_serializing_if = "Option::is_none")]
273 pub image: Option<String>,
274 #[serde(default, skip_serializing_if = "Option::is_none")]
276 pub manifest: Option<String>,
277}
278
279#[derive(Debug, Clone, Serialize, Deserialize)]
280pub struct ToolManifest {
281 pub id: String,
282 pub version: String,
283 pub description: Option<String>,
284 pub homepage: Option<String>,
285 pub license: Option<String>,
286 #[serde(default)]
288 pub tier: Tier,
289 #[serde(default, skip_serializing_if = "Vec::is_empty")]
291 pub maintainers: Vec<String>,
292 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
294 pub deprecated: bool,
295 pub image: ImageSpec,
296 pub hardware: HardwareSpec,
297 #[serde(default)]
298 pub reference_data: HashMap<String, ReferenceDataSpec>,
299 #[serde(default)]
301 pub inputs: Vec<IoSpec>,
302 #[serde(default)]
304 pub outputs: Vec<IoSpec>,
305 pub entrypoint: EntrypointSpec,
306 #[serde(default, skip_serializing_if = "Option::is_none")]
309 pub binaries: Option<BinariesSpec>,
310 #[serde(default, skip_serializing_if = "Option::is_none")]
312 pub test: Option<TestSpec>,
313 #[serde(default, skip_serializing_if = "Option::is_none")]
315 pub signatures: Option<SignatureSpec>,
316}
317
318impl ToolManifest {
319 pub fn has_typed_io(&self) -> bool {
320 !self.inputs.is_empty() || !self.outputs.is_empty()
321 }
322
323 pub fn effective_binaries(&self) -> Vec<&str> {
328 match &self.binaries {
329 Some(b) => b.exposed.iter().map(|s| s.as_str()).collect(),
330 None => {
331 let cmd = &self.entrypoint.command;
332 let name = cmd
333 .rfind('/')
334 .map(|i| &cmd[i + 1..])
335 .unwrap_or(cmd.as_str());
336 vec![name]
337 }
338 }
339 }
340}
341
342#[derive(Debug, Clone, Serialize, Deserialize)]
344pub struct Manifest {
345 pub tool: ToolManifest,
346}
347
348#[derive(Debug)]
349pub struct ValidationError {
350 pub field: String,
351 pub message: String,
352}
353
354impl fmt::Display for ValidationError {
355 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
356 write!(f, "{}: {}", self.field, self.message)
357 }
358}
359
360impl Manifest {
361 pub fn from_toml_str(s: &str) -> Result<Self> {
362 let m: Manifest = toml::from_str(s).map_err(|e| BvError::ManifestParse(e.to_string()))?;
363 m.validate_types()?;
364 Ok(m)
365 }
366
367 pub fn to_toml_string(&self) -> Result<String> {
368 toml::to_string_pretty(self).map_err(|e| BvError::ManifestParse(e.to_string()))
369 }
370
371 fn validate_types(&self) -> Result<()> {
373 let t = &self.tool;
374 for (side, specs) in [("inputs", &t.inputs), ("outputs", &t.outputs)] {
375 for spec in specs {
376 let id = spec.r#type.base_id();
377 if bv_types::lookup(id).is_none() {
378 let suggestion = bv_types::suggest(id)
379 .map(|s| format!(", did you mean `{s}`?"))
380 .unwrap_or_default();
381 return Err(BvError::ManifestParse(format!(
382 "tool.{side}[{}]: unknown type `{id}`{suggestion}",
383 spec.name
384 )));
385 }
386 }
387 }
388 Ok(())
389 }
390
391 pub fn validate(&self) -> std::result::Result<(), Vec<ValidationError>> {
393 let mut errors = Vec::new();
394 let t = &self.tool;
395
396 if t.id.is_empty() {
397 errors.push(ValidationError {
398 field: "tool.id".into(),
399 message: "must not be empty".into(),
400 });
401 }
402 if t.version.is_empty() {
403 errors.push(ValidationError {
404 field: "tool.version".into(),
405 message: "must not be empty".into(),
406 });
407 }
408 if t.image.backend.is_empty() {
409 errors.push(ValidationError {
410 field: "tool.image.backend".into(),
411 message: "must not be empty".into(),
412 });
413 }
414 if t.image.reference.is_empty() {
415 errors.push(ValidationError {
416 field: "tool.image.reference".into(),
417 message: "must not be empty".into(),
418 });
419 }
420 if t.entrypoint.command.is_empty() {
421 errors.push(ValidationError {
422 field: "tool.entrypoint.command".into(),
423 message: "must not be empty".into(),
424 });
425 }
426
427 for spec in &t.inputs {
428 if let Some(mount) = &spec.mount
429 && !mount.is_absolute()
430 {
431 errors.push(ValidationError {
432 field: format!("tool.inputs[{}].mount", spec.name),
433 message: "must be an absolute path".into(),
434 });
435 }
436 }
437 for spec in &t.outputs {
438 if let Some(mount) = &spec.mount
439 && !mount.is_absolute()
440 {
441 errors.push(ValidationError {
442 field: format!("tool.outputs[{}].mount", spec.name),
443 message: "must be an absolute path".into(),
444 });
445 }
446 }
447
448 if let Some(binaries) = &t.binaries {
449 let mut seen = std::collections::HashSet::new();
450 for name in &binaries.exposed {
451 if !seen.insert(name.as_str()) {
452 errors.push(ValidationError {
453 field: "tool.binaries.exposed".into(),
454 message: format!("duplicate binary name '{name}'"),
455 });
456 }
457 }
458 if !binaries.exposed.is_empty() {
459 let cmd = &t.entrypoint.command;
460 let basename = cmd.rfind('/').map(|i| &cmd[i + 1..]).unwrap_or(cmd);
461 if !binaries.exposed.iter().any(|b| b == basename) {
462 errors.push(ValidationError {
463 field: "tool.binaries.exposed".into(),
464 message: format!(
465 "entrypoint command '{basename}' must be listed in exposed"
466 ),
467 });
468 }
469 }
470 }
471
472 if errors.is_empty() {
473 Ok(())
474 } else {
475 Err(errors)
476 }
477 }
478}
479
480#[cfg(test)]
481mod tests {
482 use super::*;
483
484 const SAMPLE: &str = r#"
485[tool]
486id = "bwa"
487version = "0.7.17"
488description = "BWA short-read aligner"
489homepage = "http://bio-bwa.sourceforge.net/"
490license = "GPL-3.0"
491
492[tool.image]
493backend = "docker"
494reference = "biocontainers/bwa:0.7.17--h5bf99c6_8"
495digest = "sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890ab"
496
497[tool.hardware]
498cpu_cores = 8
499ram_gb = 32.0
500disk_gb = 50.0
501
502[tool.hardware.gpu]
503required = false
504
505[[tool.inputs]]
506name = "reads_r1"
507type = "fastq"
508cardinality = "one"
509description = "Forward reads"
510
511[[tool.inputs]]
512name = "reads_r2"
513type = "fastq"
514cardinality = "optional"
515description = "Reverse reads (paired-end)"
516
517[[tool.outputs]]
518name = "alignment"
519type = "bam"
520description = "Aligned reads"
521
522[tool.entrypoint]
523command = "bwa"
524args_template = "mem -t {cpu_cores} {reference} {reads_r1} {reads_r2}"
525
526[tool.entrypoint.env]
527MALLOC_ARENA_MAX = "4"
528"#;
529
530 const SAMPLE_NO_IO: &str = r#"
531[tool]
532id = "mytool"
533version = "1.0.0"
534
535[tool.image]
536backend = "docker"
537reference = "example/mytool:1.0.0"
538
539[tool.hardware]
540
541[tool.entrypoint]
542command = "mytool"
543"#;
544
545 #[test]
546 fn round_trip() {
547 let manifest = Manifest::from_toml_str(SAMPLE).expect("parse failed");
548 assert_eq!(manifest.tool.id, "bwa");
549 assert_eq!(manifest.tool.version, "0.7.17");
550 assert_eq!(manifest.tool.image.backend, "docker");
551 assert_eq!(manifest.tool.inputs.len(), 2);
552 assert_eq!(manifest.tool.outputs.len(), 1);
553 assert_eq!(manifest.tool.inputs[0].cardinality, Cardinality::One);
554 assert_eq!(manifest.tool.inputs[1].cardinality, Cardinality::Optional);
555
556 let serialised = manifest.to_toml_string().expect("serialise failed");
557 let reparsed = Manifest::from_toml_str(&serialised).expect("reparse failed");
558 assert_eq!(reparsed.tool.id, manifest.tool.id);
559 assert_eq!(reparsed.tool.version, manifest.tool.version);
560 }
561
562 #[test]
563 fn no_io_parses_unchanged() {
564 let m = Manifest::from_toml_str(SAMPLE_NO_IO).expect("parse failed");
565 assert!(m.tool.inputs.is_empty());
566 assert!(m.tool.outputs.is_empty());
567 assert!(!m.tool.has_typed_io());
568 }
569
570 #[test]
571 fn typeref_params_parsed() {
572 let s = r#"
573[tool]
574id = "t"
575version = "1.0.0"
576
577[tool.image]
578backend = "docker"
579reference = "example/t:1.0.0"
580
581[tool.hardware]
582
583[[tool.inputs]]
584name = "seqs"
585type = "fasta[protein]"
586cardinality = "one"
587
588[tool.entrypoint]
589command = "t"
590"#;
591 let m = Manifest::from_toml_str(s).unwrap();
592 assert_eq!(m.tool.inputs[0].r#type.params, vec!["protein"]);
593 }
594
595 #[test]
596 fn unknown_type_error() {
597 let s = r#"
598[tool]
599id = "t"
600version = "1.0.0"
601
602[tool.image]
603backend = "docker"
604reference = "example/t:1.0.0"
605
606[tool.hardware]
607
608[[tool.inputs]]
609name = "seqs"
610type = "protien_fasta"
611cardinality = "one"
612
613[tool.entrypoint]
614command = "t"
615"#;
616 let err = Manifest::from_toml_str(s).unwrap_err();
617 let msg = err.to_string();
618 assert!(msg.contains("unknown type"), "got: {msg}");
619 }
620
621 #[test]
622 fn cuda_version_ordering() {
623 let v12_1: CudaVersion = "12.1".parse().unwrap();
624 let v12_4: CudaVersion = "12.4".parse().unwrap();
625 let v13_0: CudaVersion = "13.0".parse().unwrap();
626 assert!(v12_1 < v12_4);
627 assert!(v12_4 < v13_0);
628 assert_eq!(v12_1, "12.1".parse::<CudaVersion>().unwrap());
629 }
630
631 #[test]
632 fn validate_catches_empty_id() {
633 let mut manifest = Manifest::from_toml_str(SAMPLE).unwrap();
634 manifest.tool.id = String::new();
635 let errs = manifest.validate().unwrap_err();
636 assert!(errs.iter().any(|e| e.field == "tool.id"));
637 }
638
639 #[test]
640 fn registry_manifests_parse() {
641 let registry = concat!(env!("CARGO_MANIFEST_DIR"), "/../../bv-registry/tools");
642 let Ok(read) = std::fs::read_dir(registry) else {
643 return;
644 };
645 for entry in read {
646 let tool_dir = entry.unwrap().path();
647 if !tool_dir.is_dir() {
648 continue;
649 }
650 for version_entry in std::fs::read_dir(&tool_dir).unwrap() {
651 let path = version_entry.unwrap().path();
652 if path.extension().is_some_and(|e| e == "toml") {
653 let s = std::fs::read_to_string(&path)
654 .unwrap_or_else(|_| panic!("failed to read {}", path.display()));
655 Manifest::from_toml_str(&s)
656 .unwrap_or_else(|e| panic!("{}: {e}", path.display()));
657 }
658 }
659 }
660 }
661}