oxblade 0.2.0

Laravel Blade templates for Rust: Blade's markup and directives with Rust expressions, rendering the same HTML as Laravel. Community package, not affiliated with Laravel.
Documentation
//! Source-aware diagnostics shared by compilation and rendering.

use crate::span::Span;
use std::fmt;

/// The operation that failed.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ErrorKind {
    /// An invalid Blade token.
    Lex,
    /// Invalid template structure.
    Parse,
    /// An invalid expression, statement, or pattern.
    Syntax,
    /// A source could not be loaded.
    Load,
    /// Compilation failed.
    Compile,
    /// A static check failed.
    Check,
    /// Rendering or expression evaluation failed.
    Render,
    /// A configured resource limit was exceeded.
    Limit,
    /// Rendering deliberately stopped through a debug directive.
    DebugAbort,
}

/// One step in the chain leading from a view to a nested render.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Frame {
    /// Description of the view, component or slot.
    pub name: String,
    /// Call site in the caller, when available.
    pub span: Option<Span>,
}

/// An engine diagnostic.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Error {
    /// Category of the failure.
    pub kind: ErrorKind,
    /// Diagnostic text.
    pub message: String,
    /// Source range, when known.
    pub span: Option<Span>,
    /// Optional explanation or suggested correction.
    pub help: Option<String>,
    /// Render call chain, most recent frame last.
    pub chain: Vec<Frame>,
}

impl Error {
    /// Creates a diagnostic with an optional source range.
    pub fn new(kind: ErrorKind, message: impl Into<String>, span: impl Into<Option<Span>>) -> Self {
        Self {
            kind,
            message: message.into(),
            span: span.into(),
            help: None,
            chain: Vec::new(),
        }
    }

    /// Attaches help text to this diagnostic.
    pub fn with_help(mut self, help: impl Into<String>) -> Self {
        self.help = Some(help.into());
        self
    }

    /// Adds the call site for a view, component or slot.
    pub fn with_frame(mut self, frame: Frame) -> Self {
        self.chain.push(frame);
        self
    }
}

impl fmt::Display for Error {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.message)
    }
}

impl std::error::Error for Error {}

/// The result of an engine operation.
pub type Result<T> = std::result::Result<T, Error>;