mod builder;
pub(crate) mod node;
pub(crate) mod store;
pub(crate) mod value;
pub use builder::PlanBuilder;
pub use value::{Input, IntoInput, NodeId, OperationInputs, Value};
use crate::runner;
use crate::{
DryRunPolicy, DryRunReport, ExecuteError, PlanDescription, PlanExecutionDescription,
ProgressListener, SafeDryRun,
};
use node::ErasedNode;
use std::marker::PhantomData;
pub struct Plan<C, T, E> {
pub(crate) owner: value::PlanId,
pub(crate) name: String,
pub(crate) nodes: Vec<Box<dyn ErasedNode<C, E>>>,
pub(crate) output: Value<T>,
pub(crate) _marker: PhantomData<fn() -> (C, E)>,
}
impl<C, T, E> Plan<C, T, E>
where
C: Sync + 'static,
T: Clone + Send + Sync + 'static,
E: Send + 'static,
{
pub fn name(&self) -> &str {
&self.name
}
pub fn len(&self) -> usize {
self.nodes.len()
}
pub fn is_empty(&self) -> bool {
self.nodes.is_empty()
}
pub fn output(&self) -> Value<T> {
self.output
}
pub fn nodes(&self) -> impl Iterator<Item = PlanNode<'_>> {
self.nodes.iter().map(|node| PlanNode {
id: node.id(),
name: node.metadata().name(),
impact: node.metadata().impact(),
dependencies: node.dependencies(),
})
}
pub fn to_mermaid(&self) -> String {
let mut output = String::new();
output.push_str("flowchart TD\n");
output.push_str(&format!(
" %% plan: {}\n",
escape_mermaid_comment(&self.name)
));
for (index, node) in self.nodes.iter().enumerate() {
let metadata = node.metadata();
output.push_str(&format!(
" n{}[\"{}. {}\\n{}\"]\n",
node.id().index(),
index + 1,
escape_mermaid_label(metadata.name()),
metadata.impact()
));
}
for node in &self.nodes {
for dependency in node.dependencies() {
output.push_str(&format!(
" n{} --> n{}\n",
dependency.index(),
node.id().index()
));
}
}
output
}
pub fn describe(&self) -> PlanDescription {
self.describe_with_policy(&SafeDryRun)
}
pub fn describe_with_listener<L>(&self, listener: &mut L) -> PlanDescription
where
L: ProgressListener<E> + ?Sized,
{
self.describe_with_policy_and_listener(&SafeDryRun, listener)
}
pub fn describe_with_policy<P>(&self, policy: &P) -> PlanDescription
where
P: DryRunPolicy,
{
crate::describe::describe_plan(&self.name, &self.nodes, policy)
}
pub fn describe_with_policy_and_listener<P, L>(
&self,
policy: &P,
listener: &mut L,
) -> PlanDescription
where
P: DryRunPolicy,
L: ProgressListener<E> + ?Sized,
{
crate::describe::describe_plan_with_listener(&self.name, &self.nodes, policy, listener)
}
pub fn describe_execution(&self) -> PlanExecutionDescription {
crate::describe::describe_execution_plan(&self.name, &self.nodes)
}
pub fn describe_execution_with_listener<L>(&self, listener: &mut L) -> PlanExecutionDescription
where
L: ProgressListener<E> + ?Sized,
{
crate::describe::describe_execution_plan_with_listener(&self.name, &self.nodes, listener)
}
pub async fn execute(&self, context: &C) -> Result<T, ExecuteError<E>> {
runner::execute::execute(self, context).await
}
pub async fn execute_with_listener<L>(
&self,
context: &C,
listener: &mut L,
) -> Result<T, ExecuteError<E>>
where
L: ProgressListener<E> + ?Sized,
{
runner::execute::execute_with_listener(self, context, listener).await
}
pub async fn dry_run(&self, context: &C) -> DryRunReport<E> {
self.dry_run_with_policy(context, &SafeDryRun).await
}
pub async fn dry_run_with_listener<L>(&self, context: &C, listener: &mut L) -> DryRunReport<E>
where
L: ProgressListener<E> + ?Sized,
{
self.dry_run_with_policy_and_listener(context, &SafeDryRun, listener)
.await
}
pub async fn dry_run_with_policy<P>(&self, context: &C, policy: &P) -> DryRunReport<E>
where
P: DryRunPolicy,
{
runner::dry_run::dry_run(self, context, policy).await
}
pub async fn dry_run_with_policy_and_listener<P, L>(
&self,
context: &C,
policy: &P,
listener: &mut L,
) -> DryRunReport<E>
where
P: DryRunPolicy,
L: ProgressListener<E> + ?Sized,
{
runner::dry_run::dry_run_with_listener(self, context, policy, listener).await
}
}
fn escape_mermaid_label(value: &str) -> String {
value.replace('\\', "\\\\").replace('"', "\\\"")
}
fn escape_mermaid_comment(value: &str) -> String {
value.replace('\n', " ")
}
#[derive(Debug, Clone, Copy)]
pub struct PlanNode<'a> {
id: NodeId,
name: &'a str,
impact: crate::Impact,
dependencies: &'a [NodeId],
}
impl<'a> PlanNode<'a> {
pub fn id(&self) -> NodeId {
self.id
}
pub fn name(&self) -> &'a str {
self.name
}
pub fn impact(&self) -> crate::Impact {
self.impact
}
pub fn dependencies(&self) -> &'a [NodeId] {
self.dependencies
}
}