tonlib_core/cell/
error.rs

1use thiserror::Error;
2
3use crate::tlb_types::tlb::TLBPrefix;
4use crate::types::TonHashParseError;
5
6#[derive(Error, Debug)]
7pub enum TonCellError {
8    #[error("Bag of cells deserialization error ({0})")]
9    BagOfCellsDeserializationError(String),
10
11    #[error("Bag of cells serialization error ({0})")]
12    BagOfCellsSerializationError(String),
13
14    #[error("Cell builder error ({0})")]
15    CellBuilderError(String),
16
17    #[error("Cell parser error ({0})")]
18    CellParserError(String),
19
20    #[error("Internal error ({0})")]
21    InternalError(String),
22
23    #[error("Invalid index (Index: {idx}, reference count: {ref_count})")]
24    InvalidIndex { idx: usize, ref_count: usize },
25
26    #[error("Invalid address type (Type: {0})")]
27    InvalidAddressType(u8),
28
29    #[error("Invalid cell type for exotic cell (Type: {0:?})")]
30    InvalidExoticCellType(Option<u8>),
31
32    #[error("Invalid exotic cell data (({0})")]
33    InvalidExoticCellData(String),
34
35    #[error("Invalid cell data ({0})")]
36    InvalidCellData(String),
37
38    #[error("Invalid input error ({0})")]
39    InvalidInput(String),
40
41    #[error("Invalid TLB prefix: (expected: {expected_prefix}, actual {actual_prefix}, expected bit_len: {expected_bit_len}, remaining bit_len: {remaining_bit_len}")]
42    InvalidTLBPrefix {
43        expected_prefix: u64,
44        actual_prefix: u64,
45        expected_bit_len: usize,
46        remaining_bit_len: usize,
47    },
48
49    #[error(
50        "Non-empty reader (Remaining bits: {remaining_bits}, Remaining refs: {remaining_refs})"
51    )]
52    NonEmptyReader {
53        remaining_bits: usize,
54        remaining_refs: usize,
55    },
56    #[error("TonHash parse error ({0})")]
57    TonHashParseError(#[from] TonHashParseError),
58
59    #[error("{0}")]
60    IO(#[from] std::io::Error),
61}
62
63pub trait MapTonCellError<R, E>
64where
65    E: std::error::Error,
66{
67    fn map_boc_deserialization_error(self) -> Result<R, TonCellError>;
68
69    fn map_boc_serialization_error(self) -> Result<R, TonCellError>;
70
71    fn map_cell_builder_error(self) -> Result<R, TonCellError>;
72
73    fn map_cell_parser_error(self) -> Result<R, TonCellError>;
74}
75
76impl<R, E> MapTonCellError<R, E> for Result<R, E>
77where
78    E: std::error::Error,
79{
80    fn map_boc_serialization_error(self) -> Result<R, TonCellError> {
81        self.map_err(|e| TonCellError::boc_serialization_error(e))
82    }
83
84    fn map_boc_deserialization_error(self) -> Result<R, TonCellError> {
85        self.map_err(|e| TonCellError::boc_deserialization_error(e))
86    }
87
88    fn map_cell_builder_error(self) -> Result<R, TonCellError> {
89        self.map_err(|e| TonCellError::cell_builder_error(e))
90    }
91
92    fn map_cell_parser_error(self) -> Result<R, TonCellError> {
93        self.map_err(|e| TonCellError::cell_parser_error(e))
94    }
95}
96
97impl TonCellError {
98    pub fn boc_serialization_error<T>(e: T) -> TonCellError
99    where
100        T: ToString,
101    {
102        TonCellError::BagOfCellsSerializationError(format!(
103            "BoC serialization error: {}",
104            e.to_string()
105        ))
106    }
107
108    pub fn boc_deserialization_error<T>(e: T) -> TonCellError
109    where
110        T: ToString,
111    {
112        TonCellError::BagOfCellsDeserializationError(format!(
113            "BoC deserialization error: {}",
114            e.to_string()
115        ))
116    }
117
118    pub fn cell_builder_error<T>(e: T) -> TonCellError
119    where
120        T: ToString,
121    {
122        TonCellError::CellBuilderError(format!("Cell builder error: {}", e.to_string()))
123    }
124
125    pub fn cell_parser_error<T>(e: T) -> TonCellError
126    where
127        T: ToString,
128    {
129        TonCellError::CellParserError(format!("Cell parser error: {}", e.to_string()))
130    }
131
132    pub fn tlb_prefix_error(
133        expected_prefix: TLBPrefix,
134        actual_prefix: u64,
135        bit_len_remaining: usize,
136    ) -> TonCellError {
137        TonCellError::InvalidTLBPrefix {
138            expected_prefix: expected_prefix.value,
139            actual_prefix,
140            expected_bit_len: expected_prefix.bit_len,
141            remaining_bit_len: bit_len_remaining,
142        }
143    }
144}