ion-schema 0.15.0

Implementation of Amazon Ion Schema
Documentation
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use crate::ion_extension::ElementExtensions;
use crate::isl::ranges::{NumberRange, TimestampRange};
use crate::isl::IslVersion;
use crate::isl_require;
use crate::result::{invalid_schema_error, IonSchemaError, IonSchemaResult};
use ion_rs::TimestampPrecision as Precision;
use ion_rs::{Element, IonResult, Value, ValueWriter, WriteAsIon};
use ion_rs::{IonType, Timestamp};
use num_traits::abs;
use std::cmp::Ordering;
use std::fmt;
use std::fmt::{Display, Formatter};

/// Represents an annotation for `annotations` constraint.
/// ```ion
/// Grammar: <ANNOTATION> ::= <SYMBOL>
///                | required::<SYMBOL>
///                | optional::<SYMBOL>
/// ```
/// `annotations`: `<https://amazon-ion.github.io/ion-schema/docs/isl-1-0/spec#annotations>`
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Annotation {
    isl_version: IslVersion,
    value: String,
    is_required: bool, // Specifies whether an annotation's occurrence is required or optional
}

impl Annotation {
    pub fn new(value: String, is_required: bool, isl_version: IslVersion) -> Self {
        Self {
            value,
            is_required,
            isl_version,
        }
    }

    pub fn value(&self) -> &String {
        &self.value
    }

    pub fn is_required(&self) -> bool {
        self.is_required
    }

    // Returns a bool value that represents if an annotation is required or not
    pub(crate) fn is_annotation_required(value: &Element, list_level_required: bool) -> bool {
        if value.annotations().contains("required") {
            true
        } else if list_level_required {
            // if the value is annotated with `optional` then it overrides the list-level `required` behavior
            !value.annotations().contains("optional")
        } else {
            // for any value the default annotation is `optional`
            false
        }
    }
}

impl WriteAsIon for Annotation {
    fn write_as_ion<V: ValueWriter>(&self, writer: V) -> IonResult<()> {
        if self.isl_version == IslVersion::V1_0 {
            if self.is_required {
                writer
                    .with_annotations(["required"])?
                    .write_symbol(self.value.as_str())
            } else {
                writer
                    .with_annotations(["optional"])?
                    .write_symbol(self.value.as_str())
            }
        } else {
            writer.write_symbol(self.value.as_str())
        }
    }
}

#[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,
            // `unwrap_or(0)` is a default to set second as timestamp precision.
            // currently `fractional_seconds_scale` doesn't return 0 for Precision::Second,
            // once that is fixed in ion-rust we can remove unwrap_or from here
            Precision::Second => match timestamp_value.fractional_seconds_scale().unwrap_or(0) {
                0 => Second,
                3 => Millisecond,
                6 => Microsecond,
                9 => Nanosecond,
                scale => OtherFractionalSeconds(scale),
            },
        }
    }

    fn string_value(&self) -> String {
        match self {
            TimestampPrecision::Year => "year".to_string(),
            TimestampPrecision::Month => "month".to_string(),
            TimestampPrecision::Day => "day".to_string(),
            TimestampPrecision::Minute => "minute".to_string(),
            TimestampPrecision::Second => "second".to_string(),
            TimestampPrecision::Millisecond => "millisecond".to_string(),
            TimestampPrecision::Microsecond => "microsecond".to_string(),
            TimestampPrecision::Nanosecond => "nanosecond".to_string(),
            TimestampPrecision::OtherFractionalSeconds(i) => format!("fractional second (10e{i})"),
        }
    }

    pub(crate) fn int_value(&self) -> i64 {
        use TimestampPrecision::*;
        match self {
            Year => -4,
            Month => -3,
            Day => -2,
            Minute => -1,
            Second => 0,
            Millisecond => 3,
            Microsecond => 6,
            Nanosecond => 9,
            OtherFractionalSeconds(scale) => *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" => 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> {
        let self_value = self.int_value();
        let other_value = other.int_value();

        Some(self_value.cmp(&other_value))
    }
}

impl WriteAsIon for TimestampPrecision {
    fn write_as_ion<V: ValueWriter>(&self, writer: V) -> IonResult<()> {
        writer.write_symbol(self.string_value().as_str())
    }
}

impl From<TimestampPrecision> for Element {
    fn from(value: TimestampPrecision) -> Self {
        Element::symbol(value.string_value())
    }
}

impl Display for TimestampPrecision {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
        f.write_str(&self.string_value())
    }
}

/// Represents a valid value to be ued within `valid_values` constraint
/// ValidValue could either be a range or Element
/// ```ion
/// Grammar: <VALID_VALUE> ::= <VALUE>
///                | <RANGE<TIMESTAMP>>
///                | <RANGE<NUMBER>>
/// ```
/// `valid_values`: `<https://amazon-ion.github.io/ion-schema/docs/isl-1-0/spec#valid_values>`
#[derive(Debug, Clone, PartialEq)]
pub enum ValidValue {
    NumberRange(NumberRange),
    TimestampRange(TimestampRange),
    Element(Value),
}

impl ValidValue {
    pub fn from_ion_element(element: &Element, isl_version: IslVersion) -> IonSchemaResult<Self> {
        let annotation = element.annotations();
        if element.annotations().contains("range") {
            isl_require!(annotation.len() == 1 => "Unexpected annotation(s) on valid values argument: {element}")?;
            // Does it contain any timestamps
            let has_timestamp = element.as_sequence().map_or(false, |s| {
                s.elements().any(|it| it.ion_type() == IonType::Timestamp)
            });
            let range = if has_timestamp {
                ValidValue::TimestampRange(TimestampRange::from_ion_element(element, |e| {
                    e.as_timestamp()
                        .filter(|t| isl_version != IslVersion::V1_0 || t.offset().is_some())
                })?)
            } else {
                ValidValue::NumberRange(NumberRange::from_ion_element(
                    element,
                    Element::any_number_as_decimal,
                )?)
            };
            Ok(range)
        } else {
            isl_require!(annotation.is_empty() => "Unexpected annotation(s) on valid values argument: {element}")?;
            Ok(ValidValue::Element(element.value().to_owned()))
        }
    }
}

impl Display for ValidValue {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
        match self {
            ValidValue::Element(element) => write!(f, "{element}"),
            ValidValue::NumberRange(r) => write!(f, "{r}"),
            ValidValue::TimestampRange(r) => write!(f, "{r}"),
        }
    }
}

impl WriteAsIon for ValidValue {
    fn write_as_ion<V: ValueWriter>(&self, writer: V) -> IonResult<()> {
        match self {
            ValidValue::Element(value) => writer.write(value),
            ValidValue::NumberRange(r) => writer.write(r),
            ValidValue::TimestampRange(r) => writer.write(r),
        }
    }
}

impl From<NumberRange> for ValidValue {
    fn from(number_range: NumberRange) -> Self {
        ValidValue::NumberRange(number_range)
    }
}

impl From<TimestampRange> for ValidValue {
    fn from(timestamp_range: TimestampRange) -> Self {
        ValidValue::TimestampRange(timestamp_range)
    }
}

impl<T: Into<Value>> From<T> for ValidValue {
    fn from(value: T) -> Self {
        ValidValue::Element(value.into())
    }
}

/// Represent a timestamp offset
/// Known timestamp offset value is stored in minutes as i32 value
/// For example, "+07::00" wil be stored as `TimestampOffset::Known(420)`
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TimestampOffset {
    Known(i32), // represents known timestamp offset in minutes
    Unknown,    // represents unknown timestamp offset "-00:00"
}

impl TryFrom<&str> for TimestampOffset {
    type Error = IonSchemaError;

    fn try_from(string_value: &str) -> Result<Self, Self::Error> {
        // unknown offset will be stored as None
        if string_value == "-00:00" {
            Ok(TimestampOffset::Unknown)
        } else {
            if string_value.len() != 6 || string_value.chars().nth(3).unwrap() != ':' {
                return invalid_schema_error(
                    "`timestamp_offset` values must be of the form \"[+|-]hh:mm\"",
                );
            }
            // convert string offset value into an i32 value of offset in minutes
            let h = &string_value[1..3];
            let m = &string_value[4..6];
            let sign = match &string_value[..1] {
                "-" => -1,
                "+" => 1,
                _ => {
                    return invalid_schema_error(format!(
                        "unrecognized `timestamp_offset` sign '{}'",
                        &string_value[..1]
                    ))
                }
            };
            match (h.parse::<i32>(), m.parse::<i32>()) {
                (Ok(hours), Ok(minutes))
                    if (0..24).contains(&hours) && (0..60).contains(&minutes) =>
                {
                    Ok(TimestampOffset::Known(sign * (hours * 60 + minutes)))
                }
                _ => invalid_schema_error(format!("invalid timestamp offset {string_value}")),
            }
        }
    }
}

impl From<Option<i32>> for TimestampOffset {
    fn from(value: Option<i32>) -> Self {
        use TimestampOffset::*;
        match value {
            None => Unknown,
            Some(offset_in_minutes) => Known(offset_in_minutes),
        }
    }
}

impl Display for TimestampOffset {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        use TimestampOffset::*;
        match &self {
            Unknown => write!(f, "-00:00"),
            Known(offset) => {
                let sign = if offset < &0 { "-" } else { "+" };
                let hours = abs(*offset) / 60;
                let minutes = abs(*offset) - hours * 60;
                write!(f, "{sign}{hours:02}:{minutes:02}")
            }
        }
    }
}

impl WriteAsIon for TimestampOffset {
    fn write_as_ion<V: ValueWriter>(&self, writer: V) -> IonResult<()> {
        match &self {
            TimestampOffset::Known(offset) => {
                let sign = if offset < &0 { "-" } else { "+" };
                let hours = abs(*offset) / 60;
                let minutes = abs(*offset) - hours * 60;
                writer.write_string(format!("{sign}{hours:02}:{minutes:02}"))
            }
            TimestampOffset::Unknown => writer.write_string("-00:00"),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Ieee754InterchangeFormat {
    Binary16,
    Binary32,
    Binary64,
}

impl TryFrom<&str> for Ieee754InterchangeFormat {
    type Error = IonSchemaError;
    fn try_from(string_value: &str) -> Result<Self, Self::Error> {
        use Ieee754InterchangeFormat::*;
        match string_value {
            "binary16" => Ok(Binary16),
            "binary32" => Ok(Binary32),
            "binary64" => Ok(Binary64),
            _ => invalid_schema_error(format!(
                "unrecognized `ieee754_float` value {}",
                &string_value
            )),
        }
    }
}

impl Display for Ieee754InterchangeFormat {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Ieee754InterchangeFormat::Binary16 => "binary16",
                Ieee754InterchangeFormat::Binary32 => "binary32",
                Ieee754InterchangeFormat::Binary64 => "binary64",
            }
        )
    }
}

impl WriteAsIon for Ieee754InterchangeFormat {
    fn write_as_ion<V: ValueWriter>(&self, writer: V) -> IonResult<()> {
        writer.write_symbol(match self {
            Ieee754InterchangeFormat::Binary16 => "binary16",
            Ieee754InterchangeFormat::Binary32 => "binary32",
            Ieee754InterchangeFormat::Binary64 => "binary64",
        })
    }
}