wdl_engine/
lib.rs

1//! Execution engine for Workflow Description Language (WDL) documents.
2
3use std::sync::LazyLock;
4
5use num_enum::IntoPrimitive;
6use sysinfo::CpuRefreshKind;
7use sysinfo::MemoryRefreshKind;
8use sysinfo::System;
9use wdl_analysis::Document;
10use wdl_analysis::diagnostics::unknown_type;
11use wdl_analysis::types::Type;
12use wdl_analysis::types::TypeNameResolver;
13use wdl_analysis::types::v1::AstTypeConverter;
14use wdl_ast::Diagnostic;
15use wdl_ast::Span;
16use wdl_ast::TreeNode;
17
18mod backend;
19mod cache;
20pub mod config;
21mod diagnostics;
22mod digest;
23mod eval;
24mod http;
25mod inputs;
26mod outputs;
27mod path;
28mod stdlib;
29mod tree;
30mod units;
31mod value;
32
33pub use eval::*;
34pub use inputs::*;
35pub use outputs::*;
36pub use path::*;
37use units::*;
38pub use value::*;
39
40use crate::cache::Hashable;
41
42/// One gibibyte (GiB) as a float.
43///
44/// This is defined as a constant as it's a commonly performed conversion.
45const ONE_GIBIBYTE: f64 = 1024.0 * 1024.0 * 1024.0;
46
47/// Resolves a type name from a document.
48///
49/// This function will import the type into the type cache if not already
50/// cached.
51fn resolve_type_name(document: &Document, name: &str, span: Span) -> Result<Type, Diagnostic> {
52    document
53        .struct_by_name(name)
54        .map(|s| s.ty().expect("struct should have type").clone())
55        .or_else(|| {
56            document
57                .enum_by_name(name)
58                .map(|e| e.ty().expect("enum should have type").clone())
59        })
60        .ok_or_else(|| unknown_type(name, span))
61}
62
63/// Converts a V1 AST type to an analysis type.
64fn convert_ast_type_v1<N: TreeNode>(
65    document: &Document,
66    ty: &wdl_ast::v1::Type<N>,
67) -> Result<Type, Diagnostic> {
68    /// Used to resolve a type name from a document.
69    struct Resolver<'a>(&'a Document);
70
71    impl TypeNameResolver for Resolver<'_> {
72        fn resolve(&mut self, name: &str, span: Span) -> Result<Type, Diagnostic> {
73            resolve_type_name(self.0, name, span)
74        }
75    }
76
77    AstTypeConverter::new(Resolver(document)).convert_type(ty)
78}
79
80/// Cached information about the host system.
81static SYSTEM: LazyLock<System> = LazyLock::new(|| {
82    let mut system = System::new();
83    system.refresh_cpu_list(CpuRefreshKind::nothing());
84    system.refresh_memory_specifics(MemoryRefreshKind::nothing().with_ram());
85    system
86});
87
88/// Represents either file or directory content.
89#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, IntoPrimitive)]
90#[repr(u8)]
91enum ContentKind {
92    /// The content is a single file.
93    File,
94    /// The content is a directory.
95    Directory,
96}
97
98impl Hashable for ContentKind {
99    fn hash(&self, hasher: &mut blake3::Hasher) {
100        hasher.update(&[(*self).into()]);
101    }
102}
103
104impl From<ContentKind> for crankshaft::engine::task::input::Type {
105    fn from(value: ContentKind) -> Self {
106        match value {
107            ContentKind::File => Self::File,
108            ContentKind::Directory => Self::Directory,
109        }
110    }
111}