63 lines
1.4 KiB
Rust
63 lines
1.4 KiB
Rust
extern crate self as kaptan_log;
|
|
|
|
use kaptan_log_derive::KaptanError;
|
|
|
|
pub struct StaticError {
|
|
path: &'static str,
|
|
additional_information: &'static str,
|
|
}
|
|
|
|
impl StaticError {
|
|
pub const fn new(text: &'static str) -> Self {
|
|
Self {
|
|
path: text,
|
|
additional_information: "",
|
|
}
|
|
}
|
|
|
|
pub const fn path(&self) -> &'static str {
|
|
self.path
|
|
}
|
|
|
|
pub const fn additional_information(&self) -> &'static str {
|
|
self.additional_information
|
|
}
|
|
}
|
|
|
|
pub trait ErrorReport {
|
|
fn static_error(&self) -> &'static StaticError;
|
|
}
|
|
|
|
#[derive(Debug, KaptanError)]
|
|
enum Generic<'a, T>
|
|
where
|
|
T: Copy,
|
|
{
|
|
Unit,
|
|
Tuple(T, u8),
|
|
Named { label: &'a str },
|
|
}
|
|
|
|
fn main() {
|
|
fn assert_error<T: core::error::Error>() {}
|
|
assert_error::<Generic<'_, u8>>();
|
|
assert_eq!(
|
|
Generic::<u8>::Unit.static_error().path(),
|
|
concat!(module_path!(), "::Generic::Unit"),
|
|
);
|
|
assert_eq!(
|
|
Generic::Tuple(1_u8, 2).static_error().path(),
|
|
concat!(module_path!(), "::Generic::Tuple"),
|
|
);
|
|
let named: Generic<'_, u8> = Generic::Named { label: "test" };
|
|
assert_eq!(
|
|
named.to_string(),
|
|
concat!(module_path!(), "::Generic::Named"),
|
|
);
|
|
assert_eq!(
|
|
named.static_error().additional_information(),
|
|
"",
|
|
"derives emit identity only; callers own optional context",
|
|
);
|
|
}
|