bcs_link/
error.rs

1// Copyright (c) The Diem Core Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4use serde::{de, ser};
5use std::fmt;
6use thiserror::Error;
7
8pub type Result<T, E = Error> = std::result::Result<T, E>;
9
10#[derive(Clone, Debug, Error, 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        Error::Io(err.to_string())
49    }
50}
51
52impl ser::Error for Error {
53    fn custom<T: fmt::Display>(msg: T) -> Self {
54        Error::Custom(msg.to_string())
55    }
56}
57
58impl de::Error for Error {
59    fn custom<T: fmt::Display>(msg: T) -> Self {
60        Error::Custom(msg.to_string())
61    }
62}