use {
super::*,
std::{fmt::Display, io},
};
pub struct Reload<T> {
pub(super) text: Vec<String>,
pub(super) inner: T,
}
impl<T: Boilerplate> Display for Reload<&T> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
self.inner.boilerplate(&self.text, f)
}
}
#[derive(Debug)]
pub enum Error {
Incompatible { new: String, old: String },
Io {
path: &'static str,
source: io::Error,
},
Length { new: usize, old: usize },
ParseNew(boilerplate_parser::Error),
ParseOld(boilerplate_parser::Error),
Path,
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::Incompatible { new, old } => {
write!(f, "template blocks are not compatible: {new} != {old}")
}
Self::Io { path, .. } => write!(f, "I/O error loading template from: {path}"),
Self::Length { new, old } => write!(
f,
"new template has {new} blocks but old template has {old} blocks",
),
Self::ParseNew(err) => write!(f, "failed to parse new template: {err}"),
Self::ParseOld(err) => write!(f, "failed to parse old template: {err}"),
Self::Path => write!(f, "template has no path"),
}
}
}
impl core::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
if let Self::Io { source, .. } = self {
Some(source)
} else {
None
}
}
}