1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! 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>;