mincode/
error.rs

1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8use std::error;
9use std::fmt;
10use std::fmt::Display;
11use std::io;
12use std::str;
13use std::string;
14
15use serde::de;
16use serde::ser;
17
18#[derive(Debug)]
19pub struct Error {
20    msg: String,
21}
22
23pub type Result<T> = std::result::Result<T, Error>;
24
25impl Error {
26    pub fn new<T: Display>(msg: T) -> Self {
27        Error {
28            msg: msg.to_string(),
29        }
30    }
31}
32
33impl error::Error for Error {
34    fn description(&self) -> &str {
35        &self.msg
36    }
37}
38
39impl Display for Error {
40    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
41        f.write_str(&self.msg)
42    }
43}
44
45impl ser::Error for Error {
46    fn custom<T: Display>(msg: T) -> Self {
47        Error::new(msg)
48    }
49}
50
51impl de::Error for Error {
52    fn custom<T: Display>(msg: T) -> Self {
53        Error::new(msg)
54    }
55}
56
57impl From<io::Error> for Error {
58    fn from(err: io::Error) -> Self {
59        Error::new(err)
60    }
61}
62
63impl From<str::Utf8Error> for Error {
64    fn from(err: str::Utf8Error) -> Self {
65        Error::new(err)
66    }
67}
68
69impl From<string::FromUtf8Error> for Error {
70    fn from(err: string::FromUtf8Error) -> Self {
71        Error::new(err)
72    }
73}