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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#[macro_use]
mod macros;
mod base64_type;
mod binary;
mod error;
mod external;
mod password;
pub mod multipart;
use std::fmt::{self, Display, Formatter};
pub use base64_type::Base64;
pub use binary::Binary;
pub use error::{ParseError, ParseResult};
pub use password::Password;
use serde_json::Value;
use crate::{
poem::web::Field as PoemField,
registry::{MetaSchemaRef, Registry},
};
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum TypeName {
Normal {
ty: &'static str,
format: Option<&'static str>,
},
Array(&'static TypeName),
}
impl Display for TypeName {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
TypeName::Normal { ty, format } => match format {
Some(format) => write!(f, "{}(${})", ty, format),
None => write!(f, "{}", ty),
},
TypeName::Array(ty) => {
write!(f, "[{}]", ty)
}
}
}
}
pub trait Type: Sized + Send + Sync {
const NAME: TypeName;
const IS_REQUIRED: bool = true;
type ValueType;
fn schema_ref() -> MetaSchemaRef;
#[allow(unused_variables)]
fn register(registry: &mut Registry) {}
fn as_value(&self) -> Option<&Self::ValueType>;
}
pub trait ParseFromJSON: Type {
fn parse_from_json(value: Value) -> ParseResult<Self>;
}
pub trait ParseFromParameter: Type {
fn parse_from_parameter(value: Option<&str>) -> ParseResult<Self>;
}
#[poem::async_trait]
pub trait ParseFromMultipartField: Type {
async fn parse_from_multipart(field: Option<PoemField>) -> ParseResult<Self>;
async fn parse_from_repeated_field(self, _field: PoemField) -> ParseResult<Self> {
Err(ParseError::<Self>::custom("repeated field"))
}
}
pub trait ToJSON: Type {
fn to_json(&self) -> Value;
}