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
//! A `bound` is anything that restricts the instance value, please see `validation keywords` in the spec
use regex::Regex;
use serde_derive::Serialize;

use crate::dsl::schema::Annotations;
use crate::dsl::schema::Schema;

pub mod deserialization;
mod enums;

#[derive(Clone, Debug)]
pub enum StringObjectBounds {
    List(Vec<EnumerationValue>),
    Pattern(Regex),
    Length(StringLength),
}

#[derive(Clone, Debug)]
pub struct IntegerValueConditionObjectBounds {
    pub minimum: Option<IntegerBound>,
    pub maximum: Option<IntegerBound>,
    pub multiple_of: Option<i64>,
}

#[derive(Clone, Debug)]
pub enum IntegerObjectBounds {
    Conditions(IntegerValueConditionObjectBounds),
    List(Vec<EnumerationValue>),
}

#[derive(Clone, Debug)]
pub struct ArrayObjectBounds {
    pub minimum_number_of_items: Option<i64>,
    pub maximum_number_of_items: Option<i64>,
    pub items: Option<ArrayItemObjectBounds>,
    pub unique_items: Option<ArrayUniqueItemBound>,
}

#[derive(Clone, Debug)]
pub enum ArrayUniqueItemBound {
    All,
    Specific(Vec<String>),
}

#[derive(Clone, Debug)]
pub enum ArrayItemObjectBounds {
    AllItems(Box<Schema>),
    AnyItems(Vec<Schema>),
}

// TODO: impl serialize separately, to not have serialization code in the `dsl` module
#[derive(Clone, Debug, Serialize)]
pub enum IntegerBound {
    Inclusive(i64),
    Exclusive(i64),
}

impl IntegerBound {
    pub fn inner(&self) -> &i64 {
        match self {
            IntegerBound::Exclusive(value) => value,
            IntegerBound::Inclusive(value) => value,
        }
    }
}

impl IntegerObjectBounds {
    pub fn with_defaults(self, defaults: IntegerObjectBounds) -> IntegerObjectBounds {
        if let IntegerObjectBounds::Conditions(old) = self.clone() {
            if let IntegerObjectBounds::Conditions(default) = defaults {
                let new_conditions = IntegerValueConditionObjectBounds {
                    minimum: old.clone().minimum.or(default.minimum),
                    maximum: old.clone().maximum.or(default.maximum),
                    multiple_of: old.clone().multiple_of.or(default.multiple_of),
                };
                IntegerObjectBounds::Conditions(new_conditions)
            } else {
                self
            }
        } else {
            self
        }
    }
}

#[derive(Clone, Debug)]
pub struct StringLength {
    pub minimum: Option<i64>,
    pub maximum: Option<i64>,
}

#[derive(Clone, Debug)]
pub struct DefaultValue(serde_yaml::Value);

impl DefaultValue {
    pub fn value(&self) -> &serde_yaml::Value {
        &self.0
    }
}

#[derive(Clone, Debug)]
pub struct EnumerationValue {
    pub annotations: Annotations,
    pub value: serde_yaml::Value,
}

impl From<&str> for EnumerationValue {
    fn from(value: &str) -> Self {
        let value = value.to_string();
        let annotations = Annotations {
            title: Some(value.clone()),
            help: None,
            warning: None,
            description: None,
            widget: None,
        };
        EnumerationValue {
            value: value.into(),
            annotations,
        }
    }
}