tonlib_core/cell/
error.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use thiserror::Error;

#[derive(Error, Debug)]
pub enum TonCellError {
    #[error("Bag of cells deserialization error ({0})")]
    BagOfCellsDeserializationError(String),

    #[error("Bag of cells serialization error ({0})")]
    BagOfCellsSerializationError(String),

    #[error("Cell builder error ({0})")]
    CellBuilderError(String),

    #[error("Cell parser error ({0})")]
    CellParserError(String),

    #[error("Internal error ({0})")]
    InternalError(String),

    #[error("Invalid index (Index: {idx}, reference count: {ref_count})")]
    InvalidIndex { idx: usize, ref_count: usize },

    #[error("Invalid address type (Type: {0})")]
    InvalidAddressType(u8),

    #[error("Invalid cell type for exotic cell (Type: {0:?})")]
    InvalidExoticCellType(Option<u8>),

    #[error("Invalid exotic cell data (({0})")]
    InvalidExoticCellData(String),

    #[error("Invalid cell data ({0})")]
    InvalidCellData(String),

    #[error("Invalid input error ({0})")]
    InvalidInput(String),

    #[error(
        "Non-empty reader (Remaining bits: {remaining_bits}, Remaining refs: {remaining_refs})"
    )]
    NonEmptyReader {
        remaining_bits: usize,
        remaining_refs: usize,
    },
}

pub trait MapTonCellError<R, E>
where
    E: std::error::Error,
{
    fn map_boc_deserialization_error(self) -> Result<R, TonCellError>;

    fn map_boc_serialization_error(self) -> Result<R, TonCellError>;

    fn map_cell_builder_error(self) -> Result<R, TonCellError>;

    fn map_cell_parser_error(self) -> Result<R, TonCellError>;
}

impl<R, E> MapTonCellError<R, E> for Result<R, E>
where
    E: std::error::Error,
{
    fn map_boc_serialization_error(self) -> Result<R, TonCellError> {
        self.map_err(|e| TonCellError::boc_serialization_error(e))
    }

    fn map_boc_deserialization_error(self) -> Result<R, TonCellError> {
        self.map_err(|e| TonCellError::boc_deserialization_error(e))
    }

    fn map_cell_builder_error(self) -> Result<R, TonCellError> {
        self.map_err(|e| TonCellError::cell_builder_error(e))
    }

    fn map_cell_parser_error(self) -> Result<R, TonCellError> {
        self.map_err(|e| TonCellError::cell_parser_error(e))
    }
}

impl TonCellError {
    pub fn boc_serialization_error<T>(e: T) -> TonCellError
    where
        T: ToString,
    {
        TonCellError::BagOfCellsSerializationError(format!(
            "BoC serialization error: {}",
            e.to_string()
        ))
    }

    pub fn boc_deserialization_error<T>(e: T) -> TonCellError
    where
        T: ToString,
    {
        TonCellError::BagOfCellsDeserializationError(format!(
            "BoC deserialization error: {}",
            e.to_string()
        ))
    }

    pub fn cell_builder_error<T>(e: T) -> TonCellError
    where
        T: ToString,
    {
        TonCellError::CellBuilderError(format!("Cell builder error: {}", e.to_string()))
    }

    pub fn cell_parser_error<T>(e: T) -> TonCellError
    where
        T: ToString,
    {
        TonCellError::CellParserError(format!("Cell parser error: {}", e.to_string()))
    }
}