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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use std::collections::HashMap;

#[cfg(feature = "core")]
use serde::{Deserialize, Serialize};

#[cfg(feature = "proc")]
use procmeta::prelude::*;

#[cfg_attr(feature = "core", derive(Debug, Serialize, Deserialize, Clone))]
#[cfg_attr(feature = "proc", derive(GetTokenStream))]
pub enum Value {
    Null,

    Bool(bool),

    Number(Number),

    String(String),

    Array(Vec<Value>),

    Object(HashMap<String, Value>),
}

#[cfg_attr(feature = "core", derive(Debug, Serialize, Deserialize, Clone))]
#[cfg_attr(feature = "proc", derive(GetTokenStream))]
pub enum Number {
    PosInt(u64),
    NegInt(i64),
    Float(f64),
}

#[cfg_attr(feature = "core", derive(Debug, Serialize, Deserialize, Clone))]
#[cfg_attr(feature = "proc", derive(GetTokenStream))]
pub enum ConstraintType {
    // 无
    None,

    // 描述
    Description(String),

    // 校验器
    SelfValidator,

    // 正则表达式
    Regex(String),

    // 值
    Value(Value),

    // 数值范围
    Range(Number, Number),

    // 长度
    Length(usize, usize),

    // 其中一个
    Anyone(Vec<ConstraintType>),

    // 枚举: 模型信息
    Enumer(String),
}

#[cfg_attr(feature = "core", derive(Debug, Serialize, Deserialize, Clone))]
#[cfg_attr(feature = "proc", derive(GetTokenStream))]
pub struct LengthError {
    pub min: usize,
    pub max: usize,
    pub len: usize,
}

#[cfg_attr(feature = "core", derive(Debug, Serialize, Deserialize, Clone))]
#[cfg_attr(feature = "proc", derive(GetTokenStream))]
pub struct RangeError {
    pub min: String,
    pub max: String,
    pub value: String,
}

#[cfg_attr(feature = "core", derive(Debug, Serialize, Deserialize, Clone))]
#[cfg_attr(feature = "proc", derive(GetTokenStream))]
pub struct RegexError {
    pub regex: String,
    pub value: String,
}

#[cfg_attr(feature = "core", derive(Debug, Serialize, Deserialize, Clone))]
#[cfg_attr(feature = "proc", derive(GetTokenStream))]
pub struct EnumError;

#[cfg_attr(feature = "core", derive(Debug, Serialize, Deserialize, Clone))]
#[cfg_attr(feature = "proc", derive(GetTokenStream))]
pub enum ErrorType {
    Regex(RegexError),

    Length(LengthError),

    Range(RangeError),

    Model(ModelValidatorError),

    Enumer(EnumError),

    Value,

    Other,
}

#[cfg_attr(feature = "core", derive(Debug, Serialize, Deserialize, Clone))]
#[cfg_attr(feature = "proc", derive(GetTokenStream))]
pub struct ModelValidatorError {
    pub error_ty: Box<ErrorType>,
    pub error_message: Option<String>,
    pub field_id: Option<String>,
    pub field_name: Option<String>,
}

impl From<LengthError> for ErrorType {
    fn from(value: LengthError) -> Self {
        Self::Length(value)
    }
}

impl From<RangeError> for ErrorType {
    fn from(value: RangeError) -> Self {
        Self::Range(value)
    }
}

impl From<ModelValidatorError> for ErrorType {
    fn from(value: ModelValidatorError) -> Self {
        Self::Model(value)
    }
}

impl From<RegexError> for ErrorType {
    fn from(value: RegexError) -> Self {
        Self::Regex(value)
    }
}

impl From<EnumError> for ErrorType {
    fn from(value: EnumError) -> Self {
        Self::Enumer(value)
    }
}