Skip to main content

sofie_engine/
error.rs

1use std::fmt;
2
3use crate::syntax::{LocatedError, Position};
4
5#[derive(Debug, PartialEq, Eq)]
6pub struct TemplateError {
7    pub template: String,
8    pub line: usize,
9    pub column: usize,
10    pub message: String,
11}
12
13impl TemplateError {
14    pub(crate) fn located(template: &str, error: LocatedError) -> Self {
15        Self {
16            template: template.to_owned(),
17            line: error.position.line,
18            column: error.position.column,
19            message: error.message,
20        }
21    }
22
23    pub(crate) fn unlocated(template: &str, message: impl Into<String>) -> Self {
24        Self::located(template, LocatedError::new(Position::START, message))
25    }
26}
27
28impl fmt::Display for TemplateError {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        write!(
31            f,
32            "{}:{}:{}: {}",
33            self.template, self.line, self.column, self.message
34        )
35    }
36}
37
38impl std::error::Error for TemplateError {}
39
40#[derive(Debug, PartialEq, Eq)]
41pub enum RenderError {
42    FragmentNotFound { id: String },
43    Template(TemplateError),
44}
45
46impl fmt::Display for RenderError {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        match self {
49            Self::FragmentNotFound { id } => write!(
50                f,
51                "no element or component with id `{id}` is rendered for this data"
52            ),
53            Self::Template(error) => write!(f, "{error}"),
54        }
55    }
56}
57
58impl std::error::Error for RenderError {
59    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
60        match self {
61            Self::FragmentNotFound { .. } => None,
62            Self::Template(error) => Some(error),
63        }
64    }
65}