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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use crate::isl::isl_range::{Range, RangeType};
use crate::result::{invalid_schema_error, IonSchemaError};
use ion_rs::types::timestamp::Precision;
use ion_rs::value::owned::{text_token, OwnedElement};
use ion_rs::value::{Element, SymbolToken};
use ion_rs::Timestamp;
use std::cmp::Ordering;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Annotation {
value: String,
is_required: bool, }
impl Annotation {
pub fn new(value: String, is_required: bool) -> Self {
Self { value, is_required }
}
pub fn value(&self) -> &String {
&self.value
}
pub fn is_required(&self) -> bool {
self.is_required
}
pub(crate) fn is_annotation_required(value: &OwnedElement, list_level_required: bool) -> bool {
if value.annotations().any(|a| a.text().unwrap() == "required") {
true
} else if list_level_required {
!value.annotations().any(|a| a.text().unwrap() == "optional")
} else {
false
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TimestampPrecision {
Year,
Month,
Day,
Minute,
Second,
Millisecond,
Microsecond,
Nanosecond,
OtherFractionalSeconds(i64),
}
impl TimestampPrecision {
pub fn from_timestamp(timestamp_value: &Timestamp) -> TimestampPrecision {
use TimestampPrecision::*;
let precision_value = timestamp_value.precision();
match precision_value {
Precision::Year => Year,
Precision::Month => Month,
Precision::Day => Day,
Precision::HourAndMinute => Minute,
Precision::Second => match timestamp_value.fractional_seconds_scale().unwrap_or(0) {
0 => Second,
3 => Millisecond,
6 => Microsecond,
9 => Nanosecond,
scale => OtherFractionalSeconds(scale),
},
}
}
}
impl TryFrom<&str> for TimestampPrecision {
type Error = IonSchemaError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Ok(match value {
"year" => TimestampPrecision::Year,
"month" => TimestampPrecision::Month,
"day" => TimestampPrecision::Day,
"minute" | "hour" => TimestampPrecision::Minute,
"second" => TimestampPrecision::Second,
"millisecond" => TimestampPrecision::Millisecond,
"microsecond" => TimestampPrecision::Microsecond,
"nanosecond" => TimestampPrecision::Nanosecond,
_ => {
return invalid_schema_error(format!(
"Invalid timestamp precision specified {}",
value
))
}
})
}
}
impl PartialOrd for TimestampPrecision {
fn partial_cmp(&self, other: &TimestampPrecision) -> Option<Ordering> {
use TimestampPrecision::*;
let self_value = match self {
Year => -4,
Month => -3,
Day => -2,
Minute => -1,
Second => 0,
Millisecond => 3,
Microsecond => 6,
Nanosecond => 9,
OtherFractionalSeconds(scale) => *scale,
};
let other_value = match other {
Year => -4,
Month => -3,
Day => -2,
Minute => -1,
Second => 0,
Millisecond => 3,
Microsecond => 6,
Nanosecond => 9,
OtherFractionalSeconds(scale) => *scale,
};
Some(self_value.cmp(&other_value))
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum ValidValue {
Range(Range),
Element(OwnedElement),
}
impl TryFrom<&OwnedElement> for ValidValue {
type Error = IonSchemaError;
fn try_from(value: &OwnedElement) -> Result<Self, Self::Error> {
if value.annotations().any(|a| a == &text_token("range")) {
Ok(ValidValue::Range(Range::from_ion_element(
value,
RangeType::NumberOrTimestamp,
)?))
} else if value.annotations().any(|a| a != &text_token("range")) {
invalid_schema_error(
"Annotations are not allowed for valid_values constraint except `range` annotation",
)
} else {
Ok(ValidValue::Element(value.to_owned()))
}
}
}