feat(errors): add stable error propagation
This commit is contained in:
1 parent
97acb2a2d7
commit
ba9c97ed3b
34 files changed
+1007
No files matched your search
@@ -0,0 +1,43 @@
|
||||
extern crate self as kaptan_err_propagate;
|
||||
|
||||
use kaptan_err_propagate_macros::KaptanError;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct ErrorId(u16);
|
||||
|
||||
impl ErrorId {
|
||||
pub const fn new(id: u16) -> Self {
|
||||
Self(id)
|
||||
}
|
||||
|
||||
pub const fn get(self) -> u16 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ErrorReport {
|
||||
fn error_id(&self) -> ErrorId;
|
||||
}
|
||||
|
||||
#[derive(Debug, KaptanError)]
|
||||
enum Generic<'a, T>
|
||||
where
|
||||
T: Copy,
|
||||
{
|
||||
#[kaptan_error(id = 65201)]
|
||||
Unit,
|
||||
#[kaptan_error(id = 65202)]
|
||||
Tuple(T, u8),
|
||||
#[kaptan_error(id = 65203)]
|
||||
Named { label: &'a str },
|
||||
}
|
||||
|
||||
fn main() {
|
||||
fn assert_error<T: core::error::Error>() {}
|
||||
assert_error::<Generic<'_, u8>>();
|
||||
assert_eq!(Generic::<u8>::Unit.error_id().get(), 65201);
|
||||
assert_eq!(Generic::Tuple(1_u8, 2).error_id().get(), 65202);
|
||||
let named: Generic<'_, u8> = Generic::Named { label: "test" };
|
||||
assert_eq!(named.error_id().get(), 65203);
|
||||
assert_eq!(named.to_string(), "65203");
|
||||
}
|
||||
Reference in new issue
Block a user