scientific-workflow 0.17.0

Configuration-driven scientific tasks, typed state, and durable recordings
Documentation
//! Captured program context and named reproduction inputs.
use super::dependencies::Dependencies;
use crate::artifact::{ArtifactSet, RunArchive, StorageLocation};
use std::{
    io,
    path::{Path, PathBuf},
};
/// Immutable resolved values and task-local workspace for external Rust programs.
pub struct TaskContext {
    parameters: serde_json::Value,
    dependencies: Dependencies,
    output: PathBuf,
}
impl TaskContext {
    /// Reads and verifies Workflow's captured task snapshots from the environment.
    pub fn from_env() -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
        Ok(Self {
            parameters: super::project::parameters(None)?,
            dependencies: Dependencies::from_env()?,
            output: super::project::output_directory()?,
        })
    }
    /// Writable local task artifacts directory, never the authored input folder.
    pub fn output_directory(&self) -> &Path {
        &self.output
    }
    /// Resolved parameter section, decoded without another configuration compiler.
    pub fn parameters<T: serde::de::DeserializeOwned>(
        &self,
        section: &str,
    ) -> Result<T, serde_json::Error> {
        serde_json::from_value(
            self.parameters
                .get(section)
                .cloned()
                .unwrap_or(serde_json::Value::Null),
        )
    }
    /// Correlated prerequisite results from declared dependency phases.
    pub fn dependencies(&self) -> &Dependencies {
        &self.dependencies
    }
    /// A named captured external input, accessed through immutable artifact references.
    pub fn input(&self, name: &str) -> io::Result<ArtifactSet> {
        captured_input(name)
    }
}
pub(super) fn captured_input(name: &str) -> io::Result<ArtifactSet> {
    crate::artifact::validate_key(name)?;
    let path = std::env::var_os("WORKFLOW_RUN_HANDLE")
        .ok_or_else(|| io::Error::other("missing WORKFLOW_RUN_HANDLE"))?;
    let handle: serde_json::Value =
        serde_json::from_reader(std::fs::File::open(path)?).map_err(io::Error::other)?;
    let location: StorageLocation =
        serde_json::from_value(handle["storage"].clone()).map_err(io::Error::other)?;
    let run = handle["run_id"]
        .as_str()
        .ok_or_else(|| io::Error::other("invalid run handle"))?;
    ArtifactSet::new(
        RunArchive::open(&location, run)?,
        format!("reproduction/external-inputs/{name}"),
    )
}