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
mod binary;
mod error;
mod external;
mod password;
use std::fmt::{self, Display, Formatter};
pub use binary::Base64;
pub use error::{ParseError, ParseResult};
pub use password::Password;
use serde::Serialize;
use serde_json::Value;
use crate::registry::Registry;
#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
pub struct DataType {
#[serde(rename = "type")]
pub ty: &'static str,
#[serde(skip_serializing_if = "Option::is_none")]
pub format: Option<&'static str>,
#[serde(rename = "enum", skip_serializing_if = "<[_]>::is_empty")]
pub enum_items: &'static [&'static str],
}
impl Default for DataType {
fn default() -> Self {
Self::STRING
}
}
impl DataType {
pub const STRING: DataType = DataType::new("string");
pub const BINARY: DataType = DataType::new("binary");
#[must_use]
pub const fn new(ty: &'static str) -> DataType {
Self {
ty,
format: None,
enum_items: &[],
}
}
#[must_use]
pub const fn with_format(self, format: &'static str) -> Self {
Self {
format: Some(format),
..self
}
}
#[must_use]
pub const fn with_enum_items(self, items: &'static [&'static str]) -> Self {
Self {
enum_items: items,
..self
}
}
}
impl Display for DataType {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self.format {
Some(format) => write!(f, "{}({})", self.ty, format),
None => write!(f, "{}", self.ty),
}
}
}
pub trait Type: Sized + Send + Sync {
const DATA_TYPE: DataType;
const IS_REQUIRED: bool = true;
fn parse(value: Option<Value>) -> ParseResult<Self>;
fn parse_from_str(value: Option<&str>) -> ParseResult<Self>;
fn to_value(&self) -> Value;
#[allow(unused_variables)]
fn register(registry: &mut Registry) {}
}