serde_url_params/
error.rs

1//! When serializing to URL parameters fails.
2
3use serde::ser;
4use std::fmt;
5
6#[derive(Debug)]
7/// Represents all possible errors that can occur when serializing into URL
8/// parameters.
9pub enum Error {
10    /// External error caused by e.g. utf8 string conversion or io.
11    Extern(Box<dyn std::error::Error + Send + Sync>),
12    /// Error when tried to serialize an unsupported type.
13    Unsupported(String),
14    /// Custom error caused by any error while serializing a type.
15    Custom(String),
16}
17
18/// Alias for `Result` with error type `serde_url_params::Error`.
19pub type Result<T> = std::result::Result<T, Error>;
20
21impl Error {
22    /// Creates a new error when a type is not supported for serializing into
23    /// URL parameters.
24    pub fn unsupported<T: fmt::Display>(msg: T) -> Self {
25        Error::Unsupported(format!("{}", msg))
26    }
27}
28
29impl fmt::Display for Error {
30    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31        match *self {
32            Error::Extern(ref err) => fmt::Display::fmt(err, f),
33            Error::Unsupported(ref msg) | Error::Custom(ref msg) => fmt::Display::fmt(msg, f),
34        }
35    }
36}
37
38impl std::error::Error for Error {}
39
40impl ser::Error for Error {
41    fn custom<T: fmt::Display>(msg: T) -> Error {
42        Error::Custom(msg.to_string())
43    }
44}
45
46impl From<std::io::Error> for Error {
47    fn from(err: std::io::Error) -> Self {
48        Error::Extern(Box::new(err))
49    }
50}
51
52impl From<std::string::FromUtf8Error> for Error {
53    fn from(err: std::string::FromUtf8Error) -> Self {
54        Error::Extern(Box::new(err))
55    }
56}