use std::fmt;
use techy::core::node::{NodeBuildError, NodeTree};
use techy::error::{Diagnostic, DiagnosticInfo};
use techy::transform::RestageError;
use crate::model::names::ElementName;
use crate::lang::flm::Flm;
use crate::lang::guard::NestingTooDeep;
use crate::lang::origin::FlmDiagnostic;
#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
#[diagnostic(
id = "flm.blocks.block-in-inline",
message = "Block-level element ‘{element}’ may not be placed inside inline content"
)]
pub struct BlockInInline {
pub element: String,
}
#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
#[diagnostic(
id = "flm.blocks.parbreak-in-inline",
message = "Paragraph breaks may not be placed inside inline content"
)]
pub struct ParbreakInInline {
pub context: String,
}
#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
#[diagnostic(
id = "flm.blocks.decorator-without-home",
message = "Misconfigured block-structure hook ‘{hook}’: The hook failed to place a node for ‘{element}’"
)]
pub struct DecoratorWithoutHome {
pub element: String,
pub hook: String,
}
#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
#[diagnostic(
id = "flm.blocks.structure-failed",
message = "Block and paragraph structure processing failed: {detail}"
)]
pub struct StructureFailed {
pub detail: String,
}
#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
#[diagnostic(
id = "flm.blocks.worker-thread-unavailable",
message = "Failed to create a thread with a sufficiently large stack: {detail}"
)]
pub struct NoWorkerThread {
pub detail: String,
}
#[derive(Debug, Clone)]
pub enum StructureError {
NestingTooDeep {
detail: String,
},
WorkerThread {
detail: String,
},
Restage(Box<RestageError<StructureError>>),
Hook {
element: ElementName,
detail: String,
},
NoBody {
element: ElementName,
},
Cut {
element: ElementName,
detail: String,
},
}
impl StructureError {
pub fn hook(element: ElementName, detail: impl Into<String>) -> StructureError {
StructureError::Hook {
element,
detail: detail.into(),
}
}
pub fn cut(element: ElementName, detail: impl Into<String>) -> StructureError {
StructureError::Cut {
element,
detail: detail.into(),
}
}
pub fn diagnostic(&self, tree: &NodeTree<Flm>) -> FlmDiagnostic {
let span = tree.root().span().clone();
match self {
StructureError::NestingTooDeep { detail } => {
Diagnostic::error(NestingTooDeep::new(detail.clone()), span)
}
StructureError::WorkerThread { detail } => {
Diagnostic::error(NoWorkerThread::new(detail.clone()), span)
}
other => Diagnostic::error(StructureFailed::new(other.to_string()), span),
}
}
}
impl fmt::Display for StructureError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
StructureError::NestingTooDeep { detail } => {
write!(f, "Content too deeply nested: {detail}")
}
StructureError::WorkerThread { detail } => {
write!(f, "The operating system failed to create a thread with a sufficiently large stack: {detail}")
}
StructureError::Restage(error) => write!(f, "Tree restaging failed: {error}"),
StructureError::Hook { element, detail } => {
write!(
f,
"The block-structure hook of ‘{element}’ failed: {detail}"
)
}
StructureError::NoBody { element } => {
write!(
f,
"Misconfigured block-structure hook: ‘{element}’ has no body"
)
}
StructureError::Cut { element, detail } => {
write!(f, "Block and paragraph structure processing failed for ‘{element}’: {detail}")
}
}
}
}
impl std::error::Error for StructureError {}
impl From<RestageError<StructureError>> for StructureError {
fn from(error: RestageError<StructureError>) -> StructureError {
match error {
RestageError::Visitor(error) => error,
RestageError::DescentLimitExceeded { detail, .. } => {
StructureError::NestingTooDeep { detail }
}
other => StructureError::Restage(Box::new(other)),
}
}
}
impl From<NodeBuildError> for StructureError {
fn from(error: NodeBuildError) -> StructureError {
StructureError::Restage(Box::new(RestageError::Build(error)))
}
}