mm1_common/errors/
error_of.rs

1use std::sync::Arc;
2
3use mm1_proto::message;
4
5use crate::errors::error_kind::{ErrorKind, HasErrorKind};
6
7#[derive(Debug, thiserror::Error)]
8#[error("{}: {}", kind, message)]
9#[message(base_path = ::mm1_proto)]
10pub struct ErrorOf<Kind: ErrorKind> {
11    pub kind:    Kind,
12    pub message: Arc<str>,
13}
14
15impl<K0> ErrorOf<K0>
16where
17    K0: ErrorKind,
18{
19    pub fn map_kind<K1>(self, map: impl FnOnce(K0) -> K1) -> ErrorOf<K1>
20    where
21        K1: ErrorKind,
22    {
23        let ErrorOf { kind, message } = self;
24        let kind = map(kind);
25        ErrorOf { kind, message }
26    }
27}
28
29impl<K: ErrorKind> ErrorOf<K> {
30    pub fn new(kind: K, message: impl Into<Arc<str>>) -> Self {
31        let message = message.into();
32        Self { kind, message }
33    }
34}
35
36impl<Kind: ErrorKind> HasErrorKind<Kind> for ErrorOf<Kind> {
37    fn kind(&self) -> Kind {
38        self.kind
39    }
40}