Skip to main content

lmn_core/request_template/
error.rs

1use std::io;
2
3#[derive(Debug)]
4pub enum TemplateError {
5    Io(io::Error),
6    InvalidJson(serde_json::Error),
7    UnknownPlaceholder(String),
8    CircularReference(Vec<String>),
9    InvalidConstraint(String),
10    MissingDefinition(String),
11    MissingEnvVar(String),
12    InvalidEnvVarName(String),
13    Serialization(serde_json::Error),
14}
15
16impl std::fmt::Display for TemplateError {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        match self {
19            Self::Io(e) => write!(f, "failed to read template file: {e}"),
20            Self::InvalidJson(e) => write!(f, "template is not valid JSON: {e}"),
21            Self::UnknownPlaceholder(name) => {
22                write!(
23                    f,
24                    "placeholder '{{{{{name}}}}}' is not defined in the template"
25                )
26            }
27            Self::CircularReference(cycle) => {
28                write!(f, "circular reference: {}", cycle.join(" -> "))
29            }
30            Self::InvalidConstraint(msg) => write!(f, "invalid constraint: {msg}"),
31            Self::MissingDefinition(name) => {
32                write!(
33                    f,
34                    "placeholder '{name}' referenced in composition but not defined"
35                )
36            }
37            Self::MissingEnvVar(name) => {
38                write!(f, "ENV var {name} not set")
39            }
40            Self::InvalidEnvVarName(name) => {
41                write!(f, "ENV placeholder '{name}' has an empty variable name")
42            }
43            Self::Serialization(e) => write!(f, "failed to serialize rendered template: {e}"),
44        }
45    }
46}
47
48impl std::error::Error for TemplateError {}
49
50impl From<io::Error> for TemplateError {
51    fn from(e: io::Error) -> Self {
52        Self::Io(e)
53    }
54}
55
56impl From<serde_json::Error> for TemplateError {
57    fn from(e: serde_json::Error) -> Self {
58        Self::InvalidJson(e)
59    }
60}