elrond_codec/
codec_err.rs

1#[derive(Debug, PartialEq, Eq)]
2pub struct EncodeError(&'static str);
3
4impl From<&'static str> for EncodeError {
5    #[inline]
6    fn from(message_bytes: &'static str) -> Self {
7        EncodeError(message_bytes)
8    }
9}
10
11// TODO: convert to "from_bytes" deprecated method in next minor release.
12// Please avoid: it bloats the contract with an unnecessary utf8 validation.
13impl From<&'static [u8]> for EncodeError {
14    #[inline]
15    fn from(message_bytes: &'static [u8]) -> Self {
16        EncodeError(core::str::from_utf8(message_bytes).unwrap())
17    }
18}
19
20impl EncodeError {
21    #[inline]
22    pub fn message_bytes(&self) -> &'static [u8] {
23        self.0.as_bytes()
24    }
25
26    #[inline]
27    pub fn message_str(&self) -> &'static str {
28        self.0
29    }
30
31    pub const UNSUPPORTED_OPERATION: EncodeError = EncodeError("unsupported operation");
32}
33
34#[derive(Debug, PartialEq, Eq)]
35pub struct DecodeError(&'static str);
36
37impl From<&'static str> for DecodeError {
38    #[inline]
39    fn from(message_bytes: &'static str) -> Self {
40        DecodeError(message_bytes)
41    }
42}
43
44// TODO: convert to "from_bytes" deprecated method in next minor release.
45// Please avoid: it bloats the contract with an unnecessary utf8 validation.
46impl From<&'static [u8]> for DecodeError {
47    #[inline]
48    fn from(message_bytes: &'static [u8]) -> Self {
49        DecodeError(core::str::from_utf8(message_bytes).unwrap())
50    }
51}
52
53impl DecodeError {
54    #[inline]
55    pub fn message_bytes(&self) -> &'static [u8] {
56        self.0.as_bytes()
57    }
58
59    #[inline]
60    pub fn message_str(&self) -> &'static str {
61        self.0
62    }
63
64    pub const INPUT_TOO_SHORT: DecodeError = DecodeError("input too short");
65    pub const INPUT_TOO_LONG: DecodeError = DecodeError("input too long");
66    pub const INPUT_OUT_OF_RANGE: DecodeError = DecodeError("input out of range");
67    pub const INVALID_VALUE: DecodeError = DecodeError("invalid value");
68    pub const UNSUPPORTED_OPERATION: DecodeError = DecodeError("unsupported operation");
69    pub const ARRAY_DECODE_ERROR: DecodeError = DecodeError("array decode error");
70    pub const UTF8_DECODE_ERROR: DecodeError = DecodeError("utf-8 decode error");
71    pub const CAPACITY_EXCEEDED_ERROR: DecodeError = DecodeError("capacity exceeded");
72
73    pub const MULTI_TOO_FEW_ARGS: DecodeError = DecodeError("too few arguments");
74    pub const MULTI_TOO_MANY_ARGS: DecodeError = DecodeError("too many arguments");
75}
76
77#[cfg(test)]
78mod test {
79    use super::*;
80
81    #[test]
82    fn decode_error_from_bytes() {
83        let from_bytes = DecodeError::from(&b"error as bytes"[..]);
84        assert_eq!(from_bytes.message_bytes(), b"error as bytes");
85        assert_eq!(from_bytes.message_str(), "error as bytes");
86    }
87
88    #[test]
89    #[should_panic]
90    fn decode_error_from_bad_bytes() {
91        let _ = DecodeError::from(&[0, 159, 146, 150][..]);
92    }
93
94    #[test]
95    fn encode_error_from_bytes() {
96        let from_bytes = EncodeError::from(&b"error as bytes"[..]);
97        assert_eq!(from_bytes.message_bytes(), b"error as bytes");
98        assert_eq!(from_bytes.message_str(), "error as bytes");
99    }
100
101    #[test]
102    #[should_panic]
103    fn encode_error_from_bad_bytes() {
104        let _ = EncodeError::from(&[0, 159, 146, 150][..]);
105    }
106}