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
use crate::schema::Schema;
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::str::FromStr;

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub enum Form {
    Empty,
    Ref(Ref),
    Type(Type),
    Enum(Enum),
    Elements(Elements),
    Properties(Properties),
    Values(Values),
    Discriminator(Discriminator),
}

impl Default for Form {
    fn default() -> Self {
        Form::Empty
    }
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub struct Ref {
    pub nullable: bool,
    pub definition: String,
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub struct Type {
    pub nullable: bool,
    pub type_value: TypeValue,
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub enum TypeValue {
    Boolean,
    Float32,
    Float64,
    Int8,
    Uint8,
    Int16,
    Uint16,
    Int32,
    Uint32,
    String,
    Timestamp,
}

impl FromStr for TypeValue {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "boolean" => Ok(Self::Boolean),
            "float32" => Ok(Self::Float32),
            "float64" => Ok(Self::Float64),
            "int8" => Ok(Self::Int8),
            "uint8" => Ok(Self::Uint8),
            "int16" => Ok(Self::Int16),
            "uint16" => Ok(Self::Uint16),
            "int32" => Ok(Self::Int32),
            "uint32" => Ok(Self::Uint32),
            "string" => Ok(Self::String),
            "timestamp" => Ok(Self::Timestamp),
            _ => Err(()),
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub struct Enum {
    pub nullable: bool,
    pub values: BTreeSet<String>,
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub struct Elements {
    pub nullable: bool,
    pub schema: Box<Schema>,
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub struct Properties {
    pub nullable: bool,
    pub required: BTreeMap<String, Schema>,
    pub optional: BTreeMap<String, Schema>,
    pub additional: bool,
    pub has_required: bool,
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub struct Values {
    pub nullable: bool,
    pub schema: Box<Schema>,
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub struct Discriminator {
    pub nullable: bool,
    pub discriminator: String,
    pub mapping: BTreeMap<String, Schema>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn type_value_from_str() {
        assert_eq!(Err(()), "Boolean".parse::<TypeValue>());
        assert_eq!(Ok(TypeValue::Boolean), "boolean".parse());
        assert_eq!(Ok(TypeValue::Float32), "float32".parse());
        assert_eq!(Ok(TypeValue::Float64), "float64".parse());
        assert_eq!(Ok(TypeValue::Int8), "int8".parse());
        assert_eq!(Ok(TypeValue::Uint8), "uint8".parse());
        assert_eq!(Ok(TypeValue::Int16), "int16".parse());
        assert_eq!(Ok(TypeValue::Uint16), "uint16".parse());
        assert_eq!(Ok(TypeValue::Int32), "int32".parse());
        assert_eq!(Ok(TypeValue::Uint32), "uint32".parse());
        assert_eq!(Ok(TypeValue::String), "string".parse());
        assert_eq!(Ok(TypeValue::Timestamp), "timestamp".parse());
    }
}