use std::collections::HashMap;
use std::path::Path;
#[cfg(any(test, feature = "test-utils"))]
use std::path::PathBuf;
use std::sync::{atomic::AtomicBool, Arc};
pub trait RunContext: Send + Sync {
fn injected_variables(&self) -> HashMap<&'static str, String>;
fn working_dir(&self) -> &Path;
fn working_dir_str(&self) -> String {
self.working_dir().to_string_lossy().into_owned()
}
fn get(&self, key: &str) -> Option<String> {
self.injected_variables().remove(key)
}
fn run_id(&self) -> &str;
fn workflow_name(&self) -> &str;
fn parent_run_id(&self) -> Option<&str> {
None
}
fn shutdown(&self) -> Option<&Arc<AtomicBool>> {
None
}
}
#[cfg(any(test, feature = "test-utils"))]
pub struct NoopRunContext {
vars: HashMap<&'static str, String>,
working_dir: PathBuf,
run_id: String,
workflow_name: String,
}
#[cfg(any(test, feature = "test-utils"))]
impl Default for NoopRunContext {
fn default() -> Self {
Self {
vars: HashMap::new(),
working_dir: PathBuf::from("/tmp"),
run_id: "noop-run".to_string(),
workflow_name: "noop-wf".to_string(),
}
}
}
#[cfg(any(test, feature = "test-utils"))]
impl NoopRunContext {
pub fn with_vars(vars: HashMap<&'static str, String>) -> Self {
Self {
vars,
..Self::default()
}
}
pub fn with_working_dir(mut self, dir: impl Into<PathBuf>) -> Self {
self.working_dir = dir.into();
self
}
pub fn with_run_id(mut self, id: impl Into<String>) -> Self {
self.run_id = id.into();
self
}
pub fn with_workflow_name(mut self, name: impl Into<String>) -> Self {
self.workflow_name = name.into();
self
}
}
#[cfg(any(test, feature = "test-utils"))]
impl RunContext for NoopRunContext {
fn injected_variables(&self) -> HashMap<&'static str, String> {
self.vars.clone()
}
fn working_dir(&self) -> &Path {
if self.working_dir == PathBuf::default() {
Path::new("/tmp")
} else {
&self.working_dir
}
}
fn get(&self, key: &str) -> Option<String> {
self.vars.get(key).cloned()
}
fn run_id(&self) -> &str {
&self.run_id
}
fn workflow_name(&self) -> &str {
&self.workflow_name
}
fn parent_run_id(&self) -> Option<&str> {
None
}
fn shutdown(&self) -> Option<&Arc<AtomicBool>> {
None
}
}