pochoir_lang/value/
error.rs

1use serde::{de, ser};
2use std::{fmt, string::ToString};
3use thiserror::Error;
4
5/// Shorthand for [`Result`] type.
6///
7/// [`Result`]: std::result::Result
8pub type Result<T> = std::result::Result<T, Error>;
9
10#[derive(Error, Debug)]
11pub enum Error {
12    #[error("error when deserializing: {0}")]
13    Deserialize(String),
14
15    #[error("error when serializing: {0}")]
16    Serialize(String),
17
18    #[error("method not allowed on a value of type `{found}`, expected one of type `{expected}`")]
19    BadValueType {
20        expected: &'static str,
21        found: &'static str,
22    },
23}
24
25impl de::Error for Error {
26    fn custom<T: fmt::Display>(msg: T) -> Self {
27        Self::Deserialize(msg.to_string())
28    }
29
30    fn invalid_type(unexp: de::Unexpected, exp: &dyn de::Expected) -> Self {
31        if let de::Unexpected::Unit = unexp {
32            Self::custom(format_args!("invalid type: null, expected {exp}"))
33        } else {
34            Self::custom(format_args!("invalid type: {unexp}, expected {exp}"))
35        }
36    }
37}
38
39impl ser::Error for Error {
40    fn custom<T: fmt::Display>(msg: T) -> Self {
41        Self::Serialize(msg.to_string())
42    }
43}