use crate::common::diagnostic::Diagnostic;
use crate::syntax::lexer::Mode;
use indexmap::IndexSet;
use smol_str::SmolStr;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use temple_dsl::{ExprUnit, Functions, InputRead, Span, TemplateUnit, Value};
pub(crate) const HASH_LEN: usize = 32;
#[derive(Debug, Clone)]
pub struct Flow {
pub(crate) name: SmolStr,
pub(crate) edition: u32,
pub(crate) annotations: Vec<Annotation>,
pub(crate) entries: Vec<NodeId>,
pub(crate) events: IndexSet<SmolStr>,
pub(crate) params: Vec<Param>,
pub(crate) lets: Vec<Let>,
pub(crate) helpers: Vec<Helper>,
pub(crate) functions: Functions,
pub(crate) nodes: Vec<Node>,
pub(crate) node_ids: HashMap<SmolStr, NodeId>,
pub(crate) param_positions: HashMap<SmolStr, usize>,
pub(crate) asks: IndexSet<SmolStr>,
pub(crate) hash: [u8; HASH_LEN],
pub(crate) warnings: Vec<Diagnostic>,
pub(crate) source: Arc<str>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct NodeId(pub(crate) u32);
impl NodeId {
pub(crate) fn index(self) -> usize {
self.0 as usize
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Annotation {
pub name: String,
pub value: Value,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Param {
pub name: String,
pub default: Value,
pub annotations: Vec<Annotation>,
}
#[derive(Debug, Clone)]
pub(crate) struct Let {
pub name: SmolStr,
pub expression: ExprUnit,
}
#[derive(Debug, Clone)]
pub(crate) struct Helper {
pub name: SmolStr,
pub params: Vec<SmolStr>,
pub body: ExprUnit,
}
#[derive(Debug, Clone)]
pub(crate) struct Node {
pub name: SmolStr,
pub params: Vec<SmolStr>,
pub annotations: Vec<Annotation>,
pub lets: Vec<Let>,
pub branches: Vec<Branch>,
pub resumes_ask: bool,
pub is_entry: bool,
pub span: Span,
}
#[derive(Debug, Clone)]
pub(crate) struct Branch {
pub guard: Option<ExprUnit>,
pub outcome: Outcome,
pub needs_event: bool,
pub span: Span,
}
#[derive(Debug, Clone)]
pub(crate) enum Outcome {
Jump {
node: NodeId,
args: Vec<ExprUnit>,
},
Value {
template: TemplateUnit,
wake: Option<ExprUnit>,
},
Ask {
name: SmolStr,
payload: Option<ExprUnit>,
node: NodeId,
args: Vec<ExprUnit>,
},
Ignore,
}
impl Outcome {
fn units(&self) -> impl Iterator<Item = UnitRef<'_>> {
let (payload, template, args, wake) = match self {
Outcome::Jump { args, .. } => (None, None, args.as_slice(), None),
Outcome::Value { template, wake } => {
(None, Some(template), [].as_slice(), wake.as_ref())
}
Outcome::Ask { payload, args, .. } => (payload.as_ref(), None, args.as_slice(), None),
Outcome::Ignore => (None, None, [].as_slice(), None),
};
payload
.map(UnitRef::Expression)
.into_iter()
.chain(template.map(UnitRef::Template))
.chain(args.iter().map(UnitRef::Expression))
.chain(wake.map(UnitRef::Expression))
}
}
impl Node {
fn units(&self) -> impl Iterator<Item = UnitRef<'_>> {
let lets = self
.lets
.iter()
.map(|binding| UnitRef::Expression(&binding.expression));
let branches = self.branches.iter().flat_map(|branch| {
let guard = branch.guard.iter().map(UnitRef::Expression);
guard.chain(branch.outcome.units())
});
lets.chain(branches)
}
pub(crate) fn unit_spans(&self) -> impl Iterator<Item = (Span, Mode)> + '_ {
self.units().map(|unit| match unit {
UnitRef::Expression(unit) => (unit.span(), Mode::Expression),
UnitRef::Template(unit) => (unit.span(), Mode::Output),
})
}
pub(crate) fn reads(&self) -> Vec<InputRead> {
self.units().flat_map(|unit| unit.reads()).collect()
}
}
enum UnitRef<'a> {
Expression(&'a ExprUnit),
Template(&'a TemplateUnit),
}
impl UnitRef<'_> {
fn reads(&self) -> Vec<InputRead> {
match self {
UnitRef::Expression(unit) => unit.reads(),
UnitRef::Template(unit) => unit.reads(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct NodeInfo<'a> {
pub name: &'a str,
pub params: Vec<&'a str>,
pub annotations: &'a [Annotation],
pub is_entry: bool,
pub resumes_ask: bool,
pub jumps_to: Vec<&'a str>,
pub asks: Vec<AskInfo<'a>>,
pub branch_count: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AskInfo<'a> {
pub name: &'a str,
pub node: &'a str,
}
impl Flow {
pub fn name(&self) -> &str {
&self.name
}
pub fn edition(&self) -> u32 {
self.edition
}
pub fn hash(&self) -> String {
hex(&self.hash)
}
pub fn entries(&self) -> impl Iterator<Item = &str> {
self.entries.iter().map(|&id| self.node_name(id))
}
pub fn events(&self) -> impl Iterator<Item = &str> {
self.events.iter().map(SmolStr::as_str)
}
pub fn params(&self) -> &[Param] {
&self.params
}
pub fn asks(&self) -> impl Iterator<Item = &str> {
self.asks.iter().map(SmolStr::as_str)
}
pub fn annotations(&self) -> &[Annotation] {
&self.annotations
}
pub fn warnings(&self) -> &[Diagnostic] {
&self.warnings
}
pub fn nodes(&self) -> impl Iterator<Item = NodeInfo<'_>> {
self.nodes.iter().map(|node| self.info(node))
}
pub fn node(&self, name: &str) -> Option<NodeInfo<'_>> {
self.node_by_name(name)
.map(|id| self.info(&self.nodes[id.index()]))
}
fn info<'a>(&'a self, node: &'a Node) -> NodeInfo<'a> {
let mut jumps_to = Vec::new();
let mut seen = HashSet::new();
let mut asks = Vec::new();
for branch in &node.branches {
match &branch.outcome {
Outcome::Jump { node: target, .. } => {
if seen.insert(*target) {
jumps_to.push(self.node_name(*target));
}
}
Outcome::Ask {
name, node: target, ..
} => asks.push(AskInfo {
name: name.as_str(),
node: self.node_name(*target),
}),
_ => {}
}
}
NodeInfo {
name: &node.name,
params: node.params.iter().map(SmolStr::as_str).collect(),
annotations: &node.annotations,
is_entry: node.is_entry,
resumes_ask: node.resumes_ask,
jumps_to,
asks,
branch_count: node.branches.len(),
}
}
pub(crate) fn node_by_name(&self, name: &str) -> Option<NodeId> {
self.node_ids.get(name).copied()
}
pub(crate) fn node_name(&self, id: NodeId) -> &str {
&self.nodes[id.index()].name
}
pub fn source(&self) -> &str {
&self.source
}
}
pub(crate) fn hex(bytes: &[u8]) -> String {
const DIGITS: &[u8; 16] = b"0123456789abcdef";
let mut out = String::with_capacity(2 * bytes.len());
for &byte in bytes {
out.push(char::from(DIGITS[usize::from(byte >> 4)]));
out.push(char::from(DIGITS[usize::from(byte & 0xf)]));
}
out
}