bcs/
error.rs

1// Copyright (c) The Diem Core Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4use serde::{de, ser};
5use std::{fmt, io::ErrorKind};
6use thiserror::Error;
7
8pub type Result<T, E = Error> = std::result::Result<T, E>;
9
10#[derive(Clone, Debug, Error, Eq, PartialEq)]
11pub enum Error {
12    #[error("unexpected end of input")]
13    Eof,
14    #[error("I/O error: {0}")]
15    Io(String),
16    #[error("exceeded max sequence length: {0}")]
17    ExceededMaxLen(usize),
18    #[error("exceeded max container depth while entering: {0}")]
19    ExceededContainerDepthLimit(&'static str),
20    #[error("expected boolean")]
21    ExpectedBoolean,
22    #[error("expected map key")]
23    ExpectedMapKey,
24    #[error("expected map value")]
25    ExpectedMapValue,
26    #[error("keys of serialized maps must be unique and in increasing order")]
27    NonCanonicalMap,
28    #[error("expected option type")]
29    ExpectedOption,
30    #[error("{0}")]
31    Custom(String),
32    #[error("sequence missing length")]
33    MissingLen,
34    #[error("not supported: {0}")]
35    NotSupported(&'static str),
36    #[error("remaining input")]
37    RemainingInput,
38    #[error("malformed utf8")]
39    Utf8,
40    #[error("ULEB128 encoding was not minimal in size")]
41    NonCanonicalUleb128Encoding,
42    #[error("ULEB128-encoded integer did not fit in the target size")]
43    IntegerOverflowDuringUleb128Decoding,
44}
45
46impl From<std::io::Error> for Error {
47    fn from(err: std::io::Error) -> Self {
48        if err.kind() == ErrorKind::UnexpectedEof {
49            Error::Eof
50        } else {
51            Error::Io(err.to_string())
52        }
53    }
54}
55
56impl ser::Error for Error {
57    fn custom<T: fmt::Display>(msg: T) -> Self {
58        Error::Custom(msg.to_string())
59    }
60}
61
62impl de::Error for Error {
63    fn custom<T: fmt::Display>(msg: T) -> Self {
64        Error::Custom(msg.to_string())
65    }
66}