dctap/
value_constraint.rs

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
use iri_s::IriS;
use itertools::Itertools;
use serde_derive::{Deserialize, Serialize};
use std::fmt::{Debug, Display};

#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
pub enum ValueConstraint {
    PickList(Vec<Value>),
    Pattern(String),
    IRIStem(IriS),
    LanguageTag(String),
    MinLength(usize),
    MaxLength(usize),
    MinExclusive(Number),
    MinInclusive(Number),
    MaxExclusive(Number),
    MaxInclusive(Number),
}

impl ValueConstraint {
    pub fn picklist(values: Vec<Value>) -> ValueConstraint {
        ValueConstraint::PickList(values)
    }

    pub fn pattern(str: &str) -> ValueConstraint {
        ValueConstraint::Pattern(str.to_string())
    }
}

impl Display for ValueConstraint {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ValueConstraint::PickList(vs) => {
                write!(f, "[{}]", vs.iter().format(" | "))?;
            }
            ValueConstraint::Pattern(s) => write!(f, "Pattern({s})")?,
            ValueConstraint::IRIStem(s) => write!(f, "IRIStem({s})")?,
            ValueConstraint::LanguageTag(s) => write!(f, "LanguageTag({s})")?,
            ValueConstraint::MinLength(n) => write!(f, "MinLength({n})")?,
            ValueConstraint::MaxLength(n) => write!(f, "MaxLength({n})")?,
            ValueConstraint::MinInclusive(n) => write!(f, "MinInclusive({n})")?,
            ValueConstraint::MaxExclusive(n) => write!(f, "MaxInclusive({n})")?,
            ValueConstraint::MinExclusive(n) => write!(f, "MinExclusive({n})")?,
            ValueConstraint::MaxInclusive(n) => write!(f, "MaxExclusive({n})")?,
        }
        Ok(())
    }
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]

pub enum Value {
    Iri(IriS),
    Str(String),
}

impl Value {
    pub fn new(str: &str) -> Value {
        Value::Str(str.to_string())
    }
}

impl Display for Value {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Value::Iri(iri) => write!(f, "Iri({iri})")?,
            Value::Str(s) => write!(f, "{s}")?,
        }
        Ok(())
    }
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Clone, Default)]
pub enum ValueConstraintType {
    #[default]
    PickList,
    Pattern,
    IRIStem,
    LanguageTag,
    MinLength,
    MaxLength,
    MinInclusive,
    MinExclusive,
    MaxInclusive,
    MaxExclusive,
    Unknown {
        value: String,
        line: u64,
    },
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]

pub enum Number {
    Int(i64),
    Double(f64),
}

impl Display for Number {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Number::Int(n) => write!(f, "{n}")?,
            Number::Double(n) => write!(f, "{n}")?,
        }
        Ok(())
    }
}