Skip to main content

fluentbase_codec/
error.rs

1extern crate alloc;
2
3use alloc::{fmt, string::String};
4use core::fmt::{Display, Formatter};
5
6#[derive(Debug)]
7pub enum CodecError {
8    Overflow,
9    Encoding(EncodingError),
10    Decoding(DecodingError),
11}
12
13impl Display for CodecError {
14    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
15        match self {
16            CodecError::Overflow => write!(f, "Overflow error"),
17            CodecError::Encoding(err) => write!(f, "Encoding error: {}", err),
18            CodecError::Decoding(err) => write!(f, "Decoding error: {}", err),
19        }
20    }
21}
22
23#[derive(Debug)]
24pub enum EncodingError {
25    BufferTooSmall {
26        required: usize,
27        available: usize,
28        details: String,
29    },
30    InvalidInputData(String),
31}
32
33impl Display for EncodingError {
34    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
35        match self {
36            EncodingError::BufferTooSmall {
37                required,
38                available,
39                details,
40            } => {
41                write!(f, "Not enough space in the buf: required {} bytes, but only {} bytes available. {}", required, available, details)
42            }
43            EncodingError::InvalidInputData(msg) => {
44                write!(f, "Invalid data provided for encoding: {}", msg)
45            }
46        }
47    }
48}
49
50#[derive(Debug)]
51pub enum DecodingError {
52    InvalidSelector {
53        expected: [u8; 4],
54        found: [u8; 4],
55    },
56    InvalidData(String),
57    BufferTooSmall {
58        expected: usize,
59        found: usize,
60        msg: String,
61    },
62    BufferOverflow {
63        msg: String,
64    },
65    UnexpectedEof,
66    Overflow,
67    ParseError(String),
68}
69
70impl Display for DecodingError {
71    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
72        match self {
73            DecodingError::InvalidSelector { expected, found } => {
74                write!(
75                    f,
76                    "Invalid selector: expected {:?}, found {:?}",
77                    expected, found
78                )
79            }
80            DecodingError::InvalidData(msg) => {
81                write!(f, "Invalid data encountered during decoding: {}", msg)
82            }
83            DecodingError::BufferTooSmall {
84                expected,
85                found,
86                msg,
87            } => {
88                write!(
89                    f,
90                    "Not enough data in the buf: expected at least {} bytes, found {}. {}",
91                    expected, found, msg
92                )
93            }
94            DecodingError::BufferOverflow { msg } => write!(f, "Buffer overflow: {}", msg),
95            DecodingError::UnexpectedEof => write!(f, "Unexpected end of buf"),
96            DecodingError::Overflow => write!(f, "Overflow error"),
97            DecodingError::ParseError(msg) => write!(f, "Parsing error: {}", msg),
98        }
99    }
100}