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#[derive(thiserror::Error, Debug)]
11pub enum Error {
12 #[error("failed with reason: {0}")]
14 Custom(String),
15
16 #[error("parsing failed with error: '{0}' at position: {1}")]
18 Parse(String, usize),
19
20 #[error("unsupported type for serialization")]
22 Unsupported,
23
24 #[error(transparent)]
26 FromUtf8(#[from] string::FromUtf8Error),
27
28 #[error(transparent)]
30 Io(#[from] io::Error),
31
32 #[error(transparent)]
34 ParseInt(#[from] num::ParseIntError),
35
36 #[error(transparent)]
38 Utf8(#[from] str::Utf8Error),
39}
40
41impl Error {
42 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 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>;