mod token;
mod walk;
pub use token::{Token, TokenInfo};
use self::walk::Run;
use crate::common::error::{InputError, RunError};
use crate::common::limits::Limits;
use crate::flow::Flow;
use indexmap::IndexMap;
use std::fmt;
use temple_dsl::Value;
#[derive(Debug, Clone)]
pub struct Input {
begin: Begin,
event: Option<Event>,
data: Value,
params: IndexMap<String, Value>,
result: Option<Value>,
failure: Option<Value>,
}
#[derive(Debug, Clone)]
enum Begin {
Entry(String),
Token(Token),
}
impl Input {
pub fn start(entry: impl Into<String>, data: impl Into<Value>) -> Input {
Input::new(Begin::Entry(entry.into()), data.into())
}
pub fn resume(token: Token, data: impl Into<Value>) -> Input {
Input::new(Begin::Token(token), data.into())
}
fn new(begin: Begin, data: Value) -> Input {
Input {
begin,
event: None,
data,
params: IndexMap::new(),
result: None,
failure: None,
}
}
pub fn event(mut self, name: impl Into<String>, payload: impl Into<Value>) -> Input {
self.event = Some(Event {
name: name.into(),
payload: payload.into(),
});
self
}
pub fn param(mut self, name: impl Into<String>, value: impl Into<Value>) -> Input {
self.params.insert(name.into(), value.into());
self
}
pub fn result(mut self, value: impl Into<Value>) -> Input {
self.result = Some(value.into());
self
}
pub fn failure(mut self, reason: impl Into<Value>) -> Input {
self.failure = Some(reason.into());
self
}
fn answers(&self) -> bool {
self.result.is_some() || self.failure.is_some()
}
}
#[derive(Debug, Clone)]
struct Event {
name: String,
payload: Value,
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum Decision {
Value {
value: Value,
node: String,
token: Token,
wake_at: Option<i64>,
},
Ask {
name: String,
payload: Value,
node: String,
token: Token,
},
Ignored,
Error {
error: RunError,
token: Option<Token>,
},
}
impl Decision {
pub fn token(&self) -> Option<&Token> {
match self {
Decision::Value { token, .. } | Decision::Ask { token, .. } => Some(token),
Decision::Error { token, .. } => token.as_ref(),
Decision::Ignored => None,
}
}
pub fn is_error(&self) -> bool {
matches!(self, Decision::Error { .. })
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Trace {
pub branches: Vec<BranchTaken>,
pub steps_used: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BranchTaken {
pub node: String,
pub branch: usize,
pub kind: BranchKind,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum BranchKind {
Jump,
Value,
Ask,
Ignore,
Error,
}
impl BranchKind {
pub fn as_str(self) -> &'static str {
match self {
BranchKind::Jump => "jump",
BranchKind::Value => "value",
BranchKind::Ask => "ask",
BranchKind::Ignore => "ignore",
BranchKind::Error => "error",
}
}
}
impl fmt::Display for BranchKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl Flow {
pub fn run(&self, input: Input, limits: &Limits) -> Result<Decision, InputError> {
Ok(Run::begin(self, input, limits, None)?.walk())
}
pub fn run_traced(
&self,
input: Input,
limits: &Limits,
) -> Result<(Decision, Trace), InputError> {
let mut trace = Trace::default();
let decision = Run::begin(self, input, limits, Some(&mut trace))?.walk();
Ok((decision, trace))
}
}