serde_querystring/de/
error.rs1use std::fmt;
2
3#[derive(Debug, Eq, PartialEq)]
4pub enum ErrorKind {
5 InvalidType,
6 InvalidLength,
7 InvalidEncoding,
8 InvalidNumber,
9 InvalidBoolean,
10 Other,
11}
12
13#[derive(Debug, Eq, PartialEq)]
14pub struct Error {
15 pub kind: ErrorKind,
16 pub message: String,
17
18 pub value: String,
20 pub index: Option<usize>,
22}
23
24impl Error {
25 pub(crate) fn new(kind: ErrorKind) -> Self {
26 Error {
27 kind,
28 message: String::new(),
29 value: String::new(),
30 index: None,
31 }
32 }
33
34 pub(crate) fn message(mut self, message: String) -> Self {
35 self.message = message;
36 self
37 }
38
39 pub(crate) fn value(mut self, slice: &[u8]) -> Self {
40 self.value = String::from_utf8_lossy(slice).to_string();
41 self
42 }
43
44 pub(crate) fn index(mut self, index: usize) -> Self {
45 self.index = Some(index);
46 self
47 }
48}
49
50impl _serde::de::Error for Error {
51 fn custom<T>(msg: T) -> Self
52 where
53 T: fmt::Display,
54 {
55 Error::new(ErrorKind::Other).message(msg.to_string())
56 }
57
58 fn invalid_type(unexp: _serde::de::Unexpected, exp: &dyn _serde::de::Expected) -> Self {
59 Error::new(ErrorKind::InvalidType)
60 .message(format_args!("invalid type: {}, expected {}", unexp, exp).to_string())
61 }
62}
63
64impl std::error::Error for Error {}
65
66impl fmt::Display for Error {
67 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result {
68 f.write_fmt(format_args!(
69 "Error {:?}: {} in `{}`",
70 self.kind, self.message, self.value
71 ))
72 }
73}