feat(diagnostics): split error and shutdown messages
This commit is contained in:
1 parent
9646451645
commit
afafd126d7
25 files changed
+608
-225
No files matched your search
@@ -47,15 +47,15 @@ fn expand(input: DeriveInput) -> Result<proc_macro2::TokenStream, Error> {
|
||||
};
|
||||
quote! {
|
||||
#pattern => {
|
||||
static MESSAGE: ::kaptan_log::StaticErrorMessage =
|
||||
::kaptan_log::StaticErrorMessage::new(concat!(
|
||||
static ERROR: ::kaptan_log::StaticError =
|
||||
::kaptan_log::StaticError::new(concat!(
|
||||
module_path!(),
|
||||
"::",
|
||||
stringify!(#ident),
|
||||
"::",
|
||||
stringify!(#variant_ident),
|
||||
));
|
||||
&MESSAGE
|
||||
&ERROR
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -70,7 +70,7 @@ fn expand(input: DeriveInput) -> Result<proc_macro2::TokenStream, Error> {
|
||||
|
||||
Ok(quote! {
|
||||
impl #impl_generics ::kaptan_log::ErrorReport for #ident #type_generics #where_clause {
|
||||
fn error_message(&self) -> &'static ::kaptan_log::StaticErrorMessage {
|
||||
fn static_error(&self) -> &'static ::kaptan_log::StaticError {
|
||||
match self {
|
||||
#(#arms),*
|
||||
}
|
||||
@@ -83,7 +83,7 @@ fn expand(input: DeriveInput) -> Result<proc_macro2::TokenStream, Error> {
|
||||
formatter: &mut ::core::fmt::Formatter<'_>,
|
||||
) -> ::core::fmt::Result {
|
||||
formatter.write_str(
|
||||
<Self as ::kaptan_log::ErrorReport>::error_message(self).as_str(),
|
||||
<Self as ::kaptan_log::ErrorReport>::static_error(self).path(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,20 +2,30 @@ extern crate self as kaptan_log;
|
||||
|
||||
use kaptan_log_derive::KaptanError;
|
||||
|
||||
pub struct StaticErrorMessage(&'static str);
|
||||
pub struct StaticError {
|
||||
path: &'static str,
|
||||
additional_information: &'static str,
|
||||
}
|
||||
|
||||
impl StaticErrorMessage {
|
||||
impl StaticError {
|
||||
pub const fn new(text: &'static str) -> Self {
|
||||
Self(text)
|
||||
Self {
|
||||
path: text,
|
||||
additional_information: "",
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn as_str(&self) -> &'static str {
|
||||
self.0
|
||||
pub const fn path(&self) -> &'static str {
|
||||
self.path
|
||||
}
|
||||
|
||||
pub const fn additional_information(&self) -> &'static str {
|
||||
self.additional_information
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ErrorReport {
|
||||
fn error_message(&self) -> &'static StaticErrorMessage;
|
||||
fn static_error(&self) -> &'static StaticError;
|
||||
}
|
||||
|
||||
#[derive(Debug, KaptanError)]
|
||||
@@ -32,11 +42,11 @@ fn main() {
|
||||
fn assert_error<T: core::error::Error>() {}
|
||||
assert_error::<Generic<'_, u8>>();
|
||||
assert_eq!(
|
||||
Generic::<u8>::Unit.error_message().as_str(),
|
||||
Generic::<u8>::Unit.static_error().path(),
|
||||
concat!(module_path!(), "::Generic::Unit"),
|
||||
);
|
||||
assert_eq!(
|
||||
Generic::Tuple(1_u8, 2).error_message().as_str(),
|
||||
Generic::Tuple(1_u8, 2).static_error().path(),
|
||||
concat!(module_path!(), "::Generic::Tuple"),
|
||||
);
|
||||
let named: Generic<'_, u8> = Generic::Named { label: "test" };
|
||||
@@ -44,4 +54,9 @@ fn main() {
|
||||
named.to_string(),
|
||||
concat!(module_path!(), "::Generic::Named"),
|
||||
);
|
||||
assert_eq!(
|
||||
named.static_error().additional_information(),
|
||||
"",
|
||||
"derives emit identity only; callers own optional context",
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user