multiversx_sc_codec/
codec_err_handler.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use core::convert::Infallible;

use crate::{DecodeError, EncodeError};

pub trait EncodeErrorHandler: Copy {
    type HandledErr: 'static;

    fn handle_error(&self, err: EncodeError) -> Self::HandledErr;
}

pub trait DecodeErrorHandler: Copy {
    type HandledErr: 'static;

    fn handle_error(&self, err: DecodeError) -> Self::HandledErr;
}

impl EncodeErrorHandler for DefaultErrorHandler {
    type HandledErr = EncodeError;

    #[inline]
    fn handle_error(&self, err: EncodeError) -> Self::HandledErr {
        err
    }
}

/// The simplest error handler, it simply passes the error on.
#[derive(Clone, Copy)]
pub struct DefaultErrorHandler;

impl DecodeErrorHandler for DefaultErrorHandler {
    type HandledErr = DecodeError;

    #[inline]
    fn handle_error(&self, err: DecodeError) -> Self::HandledErr {
        err
    }
}

/// An error handler that panics immediately, instead of returning a `Result`.
#[derive(Clone, Copy)]
pub struct PanicErrorHandler;

impl EncodeErrorHandler for PanicErrorHandler {
    type HandledErr = Infallible;

    #[inline]
    fn handle_error(&self, err: EncodeError) -> Self::HandledErr {
        panic!("Encode error occured: {}", err.message_str())
    }
}

impl DecodeErrorHandler for PanicErrorHandler {
    type HandledErr = Infallible;

    #[inline]
    fn handle_error(&self, err: DecodeError) -> Self::HandledErr {
        panic!("Decode error occured: {}", err.message_str())
    }
}