use super::*;
use crate::graph::plan::Budget;
use crate::pipeline::{RunGuard, RunOptions, begin_run};
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
#[derive(Clone, Default)]
pub struct Run {
pub now: Option<(i64, u32)>,
pub allow_shell: bool,
pub quantifier_bound: Option<usize>,
pub reproducible: bool,
pub ref_targets: HashMap<String, NodeId>,
pub collation: Option<String>,
pub bonds: Option<Rc<dyn crate::value::ambient::SentenceBonds>>,
pub budget: Option<Budget>,
pub plan: bool,
pub topic: Option<Value>,
pub environments: Vec<crate::pipeline::env::Environment>,
pub libraries: Vec<std::sync::Arc<dyn crate::pipeline::library::CommandLibrary>>,
}
impl Run {
pub fn ambient() -> Run {
Run {
now: crate::value::ambient::invocation_instant(),
bonds: crate::value::ambient::sentence_bonds(),
..Run::default()
}
}
pub fn options(&self) -> RunOptions {
RunOptions {
now: self.now,
allow_shell: self.allow_shell,
quantifier_bound: self.quantifier_bound,
reproducible: self.reproducible,
collation: self.collation.clone(),
bonds: self.bonds.clone(),
topic: self.topic.clone(),
environments: self.environments.clone(),
libraries: self.libraries.clone(),
}
}
}
#[derive(Default)]
pub(crate) struct GraphRun {
pub(crate) run: Run,
pub(crate) scopes: HashMap<(usize, String), Rc<ScopeMemo>>,
pub(crate) last_out: LastLinks,
pub(crate) last_in: LastLinks,
pub(crate) children: HashMap<NodeId, Rc<Vec<NodeId>>>,
}
thread_local! {
static RUNS: RefCell<Vec<GraphRun>> = const { RefCell::new(Vec::new()) };
}
pub(crate) struct GraphGuard {
_pipeline: RunGuard,
}
impl Drop for GraphGuard {
fn drop(&mut self) {
RUNS.with(|r| r.borrow_mut().pop());
}
}
pub(crate) fn enter(run: Option<Run>) -> GraphGuard {
let run = run.unwrap_or_else(inherited);
let pipeline = begin_run(Some(run.options()));
RUNS.with(|r| {
r.borrow_mut().push(GraphRun {
run,
..GraphRun::default()
})
});
GraphGuard {
_pipeline: pipeline,
}
}
pub fn inherited() -> Run {
RUNS.with(|r| r.borrow().last().map(|g| g.run.clone()))
.unwrap_or_else(Run::ambient)
}
pub(crate) fn with_graph_run<R>(f: impl FnOnce(&mut GraphRun) -> R) -> R {
RUNS.with(|r| {
let mut r = r.borrow_mut();
match r.last_mut() {
Some(top) => f(top),
None => f(&mut GraphRun::default()),
}
})
}
pub(crate) fn ref_target(id: &str) -> Option<NodeId> {
with_graph_run(|g| g.run.ref_targets.get(id).copied())
}