1#![warn(missing_docs)]
2
3pub trait LocalizableError: std::error::Error {
11 fn localization_key(&self) -> &str;
16
17 fn localize(&self, locale: &str) -> String;
26}
27
28#[macro_export]
38macro_rules! localizable_error {
39 ($name:ident {
40 $($variant:ident $(($($field:ty),*))? => $key:expr),* $(,)?
41 }) => {
42 #[derive(Debug, Clone, PartialEq)]
43 pub enum $name {
44 $($variant $(($($field),*))?,
45 )*
46 }
47
48 impl std::fmt::Display for $name {
49 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50 write!(f, "{}", self.localize(""))
51 }
52 }
53
54 impl std::error::Error for $name {}
55
56 impl $crate::LocalizableError for $name {
57 fn localization_key(&self) -> &str {
58 match self {
59 $(
60 Self::$variant $(($($field),*))? => $key,
61 )*
62 }
63 }
64
65 fn localize(&self, _locale: &str) -> String {
66 match self {
67 $(
68 Self::$variant $(($($field),*))? => {
69 $(let ($($field),*) = ($($field),*);)?
70 itools_localization::t($key)
71 }
72 )*
73 }
74 }
75 }
76 };
77}