use std::{error::Error, fmt};
#[derive(Debug)]
pub struct VariableError {
pub content: String,
pub kind: VariableErrorKind,
}
#[derive(Debug)]
pub enum VariableErrorKind {
InvalidAddress,
InvalidDivert { address: String },
InvalidNumericValue { err: Box<dyn Error + 'static> },
}
impl Error for VariableError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&self.kind)
}
}
impl Error for VariableErrorKind {}
impl fmt::Display for VariableError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} in variable string '{}'", self.kind, self.content)
}
}
impl fmt::Display for VariableErrorKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use VariableErrorKind::*;
match &self {
InvalidAddress => write!(f, "invalid address"),
InvalidDivert { address } => write!(f, "invalid divert address '{}'", address),
InvalidNumericValue { .. } => write!(f, "could not parse number"),
}
}
}