use std::fmt;
use techy::core::node::{BuildId, NodeBuildError, NodeRef, NodeSlice, NodeTree};
use techy::transform::{RestageContext, RestageError};
use crate::lang::flm::Flm;
use crate::lang::BuiltLang;
use crate::lang::origin::FlmDiagnostic;
use crate::lang::rewrites::userdefs::template::{CompiledTemplate, PlaceholderTarget};
pub type ExpandCx<'t> = RestageContext<'t, Flm, (), ()>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Deserialize)]
#[derive(schemars::JsonSchema)]
#[serde(rename_all = "kebab-case")]
pub enum ExpandPolicy {
Always,
#[default]
PerPlan,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ExpandSet {
Always,
PerPlan,
Both,
}
impl ExpandSet {
pub fn includes(self, policy: ExpandPolicy) -> bool {
match self {
ExpandSet::Always => policy == ExpandPolicy::Always,
ExpandSet::PerPlan => policy == ExpandPolicy::PerPlan,
ExpandSet::Both => true,
}
}
}
#[derive(Debug)]
pub enum Expansion {
Default,
Emit(Vec<BuildId>),
}
#[derive(Debug)]
pub enum ExpandError {
Restage(Box<RestageError<ExpandError>>),
Build(NodeBuildError),
BadPlaceholder {
reference: String,
},
Hook {
detail: String,
},
}
impl ExpandError {
pub fn descent_refusal(&self) -> Option<&str> {
match self {
ExpandError::Restage(error) => match &**error {
RestageError::DescentLimitExceeded { detail, .. } => Some(detail),
RestageError::Visitor(inner) => inner.descent_refusal(),
_ => None,
},
_ => None,
}
}
pub fn bad_placeholder(&self) -> Option<&str> {
match self {
ExpandError::BadPlaceholder { reference } => Some(reference),
ExpandError::Restage(error) => match &**error {
RestageError::Visitor(inner) => inner.bad_placeholder(),
_ => None,
},
_ => None,
}
}
}
impl From<RestageError<ExpandError>> for ExpandError {
fn from(error: RestageError<ExpandError>) -> ExpandError {
ExpandError::Restage(Box::new(error))
}
}
impl From<NodeBuildError> for ExpandError {
fn from(error: NodeBuildError) -> ExpandError {
ExpandError::Build(error)
}
}
impl fmt::Display for ExpandError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ExpandError::Restage(error) => write!(f, "{error}"),
ExpandError::Build(error) => write!(f, "building the expanded nodes failed: {error}"),
ExpandError::BadPlaceholder { reference } => {
write!(f, "placeholder ‘#{reference}’ has no value")
}
ExpandError::Hook { detail } => {
write!(f, "the expansion code of a definition failed: {detail}")
}
}
}
}
impl std::error::Error for ExpandError {}
pub trait Expand: Send + Sync {
fn policy(&self) -> ExpandPolicy {
ExpandPolicy::PerPlan
}
fn expand(
&self,
node: NodeRef<'_, Flm>,
cx: &mut ExpandCx<'_>,
xp: &mut dyn Expander,
) -> Result<Expansion, ExpandError>;
}
pub trait Expander {
fn depth(&self) -> usize;
fn recursion_limit(&self) -> usize;
fn instantiate(
&mut self,
cx: &mut ExpandCx<'_>,
site: NodeRef<'_, Flm>,
template: &CompiledTemplate,
bindings: &dyn PlaceholderBindings,
) -> Result<Vec<BuildId>, ExpandError>;
fn recover(
&mut self,
cx: &mut ExpandCx<'_>,
site: NodeRef<'_, Flm>,
diagnostic: FlmDiagnostic,
) -> Result<Vec<BuildId>, ExpandError>;
fn diagnose(&mut self, diagnostic: FlmDiagnostic);
fn lang(&self) -> &BuiltLang;
}
#[derive(Debug, Clone, Copy)]
pub enum PlaceholderValue<'a> {
NoValue,
Provided(NodeSlice<'a, Flm>),
Default(&'a CompiledTemplate),
Tree(&'a NodeTree<Flm>),
Substitution,
Unresolvable,
}
pub trait PlaceholderBindings {
fn value<'a>(
&'a self,
target: &PlaceholderTarget,
substitution: Option<NodeSlice<'_, Flm>>,
) -> PlaceholderValue<'a>;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_per_plan_set_excludes_always_definitions() {
assert!(!ExpandSet::PerPlan.includes(ExpandPolicy::Always));
assert!(ExpandSet::PerPlan.includes(ExpandPolicy::PerPlan));
}
#[test]
fn the_both_set_includes_every_policy() {
assert!(ExpandSet::Both.includes(ExpandPolicy::Always));
assert!(ExpandSet::Both.includes(ExpandPolicy::PerPlan));
}
#[test]
fn the_default_policy_is_per_plan() {
assert_eq!(ExpandPolicy::default(), ExpandPolicy::PerPlan);
}
}