1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use std::{fmt::Display, marker::PhantomData};
use serde_json::Value;
use super::Type;
#[derive(Debug)]
pub struct ParseError<T> {
message: String,
phantom: PhantomData<T>,
}
impl<T: Type, E: Display> From<E> for ParseError<T> {
fn from(error: E) -> Self {
Self::custom(error)
}
}
impl<T: Type> ParseError<T> {
fn new(message: String) -> Self {
Self {
message,
phantom: PhantomData,
}
}
#[must_use]
pub fn expected_type(actual: Value) -> Self {
Self::new(format!(
r#"Expected input type "{}", found {}."#,
T::NAME,
actual
))
}
#[must_use]
pub fn expected_input() -> Self {
Self::new(format!(r#"Type "{}" expects an input value."#, T::NAME))
}
#[must_use]
pub fn not_support_parsing_from_parameter() -> Self {
Self::custom("not support parsing from parameter")
}
#[must_use]
pub fn not_support_parsing_from_multipart() -> Self {
Self::custom("not support parsing from multipart")
}
#[must_use]
pub fn custom(msg: impl Display) -> Self {
Self::new(format!(r#"failed to parse "{}": {}"#, T::NAME, msg))
}
pub fn propagate<U: Type>(self) -> ParseError<U> {
if T::NAME != U::NAME {
ParseError::new(format!(
r#"{} (occurred while parsing "{}")"#,
self.message,
U::NAME
))
} else {
ParseError::new(self.message)
}
}
pub fn into_message(self) -> String {
self.message
}
}
pub type ParseResult<T> = Result<T, ParseError<T>>;