use std::borrow::Cow;
use crate::{compose_message, ErrorLocation, SanthError};
pub trait SanthErrorContract: std::error::Error {
fn error_code(&self) -> &'static str;
fn fix_hint(&self) -> Cow<'_, str>;
fn title(&self) -> Cow<'_, str> {
Cow::Owned(self.to_string())
}
fn context(&self) -> Vec<(Cow<'static, str>, String)> {
Vec::new()
}
fn location(&self) -> Option<&ErrorLocation> {
None
}
fn actionable_message(&self) -> String {
let title = self.title();
let fix = self.fix_hint();
let context = self.context();
compose_message(
&title,
&fix,
&context,
self.location(),
std::error::Error::source(self),
)
}
}
impl SanthErrorContract for SanthError {
fn error_code(&self) -> &'static str {
self.code
}
fn fix_hint(&self) -> Cow<'_, str> {
Cow::Borrowed(&self.fix)
}
fn title(&self) -> Cow<'_, str> {
Cow::Borrowed(&self.title)
}
fn context(&self) -> Vec<(Cow<'static, str>, String)> {
self.context.clone()
}
fn location(&self) -> Option<&ErrorLocation> {
self.location.as_ref()
}
}