serde_qs/
error.rs

1use serde::de;
2
3use std::fmt::Display;
4use std::io;
5use std::num;
6use std::str;
7use std::string;
8
9/// Error type for `serde_qs`.
10#[derive(thiserror::Error, Debug)]
11pub enum Error {
12    /// Custom string-based error
13    #[error("failed with reason: {0}")]
14    Custom(String),
15
16    /// Parse error at a specified position in the query string
17    #[error("parsing failed with error: '{0}' at position: {1}")]
18    Parse(String, usize),
19
20    /// Unsupported type that `serde_qs` can't serialize into a query string
21    #[error("unsupported type for serialization")]
22    Unsupported,
23
24    /// Error proessing UTF-8 for a `String`
25    #[error(transparent)]
26    FromUtf8(#[from] string::FromUtf8Error),
27
28    /// I/O error
29    #[error(transparent)]
30    Io(#[from] io::Error),
31
32    /// Error parsing a number
33    #[error(transparent)]
34    ParseInt(#[from] num::ParseIntError),
35
36    /// Error processing UTF-8 for a `str`
37    #[error(transparent)]
38    Utf8(#[from] str::Utf8Error),
39}
40
41impl Error {
42    /// Generate error to show top-level type cannot be deserialized.
43    pub fn top_level(object: &'static str) -> Self {
44        Error::Custom(format!(
45            "cannot deserialize {} at the top level.\
46             Try deserializing into a struct.",
47            object
48        ))
49    }
50
51    /// Generate a parsing error message with position.
52    pub fn parse_err<T>(msg: T, position: usize) -> Self
53    where
54        T: Display,
55    {
56        Error::Parse(msg.to_string(), position)
57    }
58}
59
60impl de::Error for Error {
61    fn custom<T>(msg: T) -> Self
62    where
63        T: Display,
64    {
65        Error::Custom(msg.to_string())
66    }
67}
68
69pub type Result<T, E = Error> = core::result::Result<T, E>;