runjucks_core/errors.rs
1//! User-facing render and parse failures as a simple string message.
2//!
3//! Functions in this crate that can fail return [`Result`]`<T, `[`RunjucksError`]`>`.
4
5use std::fmt;
6
7/// Error returned when lexing, parsing, or rendering cannot complete.
8///
9/// Carries a human-readable message suitable for logs or passing to JavaScript via the NAPI layer.
10///
11/// # Examples
12///
13/// ```
14/// use runjucks_core::RunjucksError;
15///
16/// let e = RunjucksError::new("unclosed tag");
17/// assert_eq!(e.to_string(), "unclosed tag");
18/// ```
19#[derive(Debug)]
20pub struct RunjucksError {
21 message: String,
22}
23
24impl RunjucksError {
25 /// Creates an error with the given message.
26 pub fn new(message: impl Into<String>) -> Self {
27 Self {
28 message: message.into(),
29 }
30 }
31}
32
33impl fmt::Display for RunjucksError {
34 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35 f.write_str(&self.message)
36 }
37}
38
39impl std::error::Error for RunjucksError {}
40
41/// Convenient alias for `std::result::Result` with [`RunjucksError`] as the error type.
42pub type Result<T> = std::result::Result<T, RunjucksError>;