use crate::node;
use std::fmt;
use thiserror::Error;
#[derive(Debug, Error)]
#[error("too many connections ({0}), max is {max}", max = node::Conns::MAX)]
pub struct TooManyConns(pub usize);
#[derive(Debug, Error)]
#[error("edge references invalid input index {index} (node has {n_inputs} inputs)")]
pub struct InvalidInputIndex {
pub index: usize,
pub n_inputs: usize,
}
#[derive(Debug, Error)]
#[error("edge references invalid output index {index} (node has {n_outputs} outputs)")]
pub struct InvalidOutputIndex {
pub index: usize,
pub n_outputs: usize,
}
#[derive(Debug, Error)]
#[error("nested graph not found at path {0:?}")]
pub struct NestedGraphNotFound(pub Vec<node::Id>);
#[derive(Debug)]
pub struct NodeExprError {
pub path: Vec<node::Id>,
pub error: node::ExprError,
}
#[derive(Debug, Error)]
pub enum NodeFnError {
#[error(transparent)]
NestedGraphNotFound(#[from] NestedGraphNotFound),
#[error(transparent)]
Expr(#[from] NodeExprError),
}
#[derive(Debug)]
pub struct NodeFnErrors(pub Vec<NodeFnError>);
#[derive(Debug, Error)]
pub enum NodeConnsError {
#[error(transparent)]
TooManyConns(#[from] TooManyConns),
#[error(transparent)]
InvalidInputIndex(#[from] InvalidInputIndex),
#[error(transparent)]
InvalidOutputIndex(#[from] InvalidOutputIndex),
}
#[derive(Debug)]
pub struct MetaError {
pub path: Vec<node::Id>,
pub error: NodeConnsError,
}
#[derive(Debug)]
pub struct MetaErrors(pub Vec<MetaError>);
#[derive(Debug, Error)]
pub enum CodegenError {
#[error(transparent)]
TooManyInputs(#[from] TooManyConns),
#[error(transparent)]
InvalidInputIndex(#[from] InvalidInputIndex),
}
#[derive(Debug, Error)]
pub enum ModuleError {
#[error(transparent)]
NodeConns(#[from] NodeConnsError),
#[error(transparent)]
Codegen(#[from] CodegenError),
#[error(transparent)]
NestedGraphNotFound(#[from] NestedGraphNotFound),
#[error(transparent)]
MetaErrors(#[from] MetaErrors),
#[error(transparent)]
NodeFnErrors(#[from] NodeFnErrors),
}
impl fmt::Display for MetaError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"node-{}: {}",
super::codegen::path_string(&self.path),
self.error
)
}
}
impl fmt::Display for MetaErrors {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, err) in self.0.iter().enumerate() {
if i > 0 {
writeln!(f)?;
}
write!(f, "{err}")?;
}
Ok(())
}
}
impl fmt::Display for NodeExprError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"node-{}: {}",
super::codegen::path_string(&self.path),
self.error
)
}
}
impl fmt::Display for NodeFnErrors {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, err) in self.0.iter().enumerate() {
if i > 0 {
writeln!(f)?;
}
write!(f, "{err}")?;
}
Ok(())
}
}
impl std::error::Error for MetaError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.error)
}
}
impl std::error::Error for MetaErrors {}
impl std::error::Error for NodeExprError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.error)
}
}
impl std::error::Error for NodeFnErrors {}