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
use crate::{Bag, DateTime, List, Tuple, Value};
use ion_rs::{Int, IonReader, IonType, Reader, StreamItem};
use once_cell::sync::Lazy;
use regex::RegexSet;
use rust_decimal::prelude::ToPrimitive;
use std::num::NonZeroU8;
use std::str::FromStr;

const BAG_ANNOT: &str = "$bag";
const TIME_ANNOT: &str = "$time";
const DATE_ANNOT: &str = "$date";
const MISSING_ANNOT: &str = "$missing";

// TODO handle errors more gracefully than `expect`/`unwrap`
/// For a given `contents` as Ion text produces a PartiQL Value
pub fn parse_ion(contents: &str) -> Value {
    let mut reader = ion_rs::ReaderBuilder::new()
        .build(contents)
        .expect("reading contents");

    // expecting a single top-level value
    let item = reader.next().expect("test value");
    let val = match item {
        StreamItem::Value(typ) => parse_value(&mut reader, typ),
        StreamItem::Null(_) => Value::Null,
        StreamItem::Nothing => panic!("expecting a test value"),
    };

    assert_eq!(reader.next().expect("test end"), StreamItem::Nothing);

    val
}

fn parse_value(reader: &mut Reader, typ: IonType) -> Value {
    match typ {
        IonType::Null => parse_null(reader),
        IonType::Bool => Value::Boolean(reader.read_bool().unwrap()),
        IonType::Int => match reader.read_int().unwrap() {
            Int::I64(i) => Value::Integer(i),
            Int::BigInt(_) => todo!("bigint"),
        },
        IonType::Float => Value::Real(reader.read_f64().unwrap().into()),
        IonType::Decimal => {
            // TODO ion Decimal doesn't give a lot of functionality to get at the data currently
            // TODO    and it's not clear whether we'll continue with rust decimal or switch to big decimal
            let ion_dec = reader.read_decimal().unwrap();
            let ion_dec_str = format!("{ion_dec}").replace('d', "e");
            let dec = rust_decimal::Decimal::from_str(&ion_dec_str)
                .or_else(|_| rust_decimal::Decimal::from_scientific(&ion_dec_str));
            Value::Decimal(dec.unwrap())
        }
        IonType::Timestamp => {
            if has_annotation(reader, DATE_ANNOT) {
                parse_date(reader).into()
            } else {
                parse_datetime(reader).into()
            }
        }
        IonType::Symbol => Value::String(Box::new(
            reader
                .read_symbol()
                .unwrap()
                .text()
                .unwrap_or("")
                .to_string(),
        )),
        IonType::String => Value::String(Box::new(reader.read_string().unwrap())),
        IonType::Clob => Value::Blob(Box::new(reader.read_clob().unwrap())),
        IonType::Blob => Value::Blob(Box::new(reader.read_blob().unwrap())),
        IonType::List => {
            if has_annotation(reader, BAG_ANNOT) {
                Bag::from(parse_sequence(reader)).into()
            } else {
                List::from(parse_sequence(reader)).into()
            }
        }
        IonType::SExp => todo!("sexp"),
        IonType::Struct => {
            if has_annotation(reader, TIME_ANNOT) {
                parse_time(reader).into()
            } else {
                parse_tuple(reader).into()
            }
        }
    }
}

#[inline]
fn has_annotation(reader: &Reader, annot: &str) -> bool {
    reader.annotations().any(|a| a.unwrap().eq(&annot))
}

#[inline]
fn parse_null(reader: &Reader) -> Value {
    if has_annotation(reader, MISSING_ANNOT) {
        Value::Missing
    } else {
        Value::Null
    }
}

const RE_SET_TIME_PARTS: [&str; 5] = [
    "^hour$",
    "^minute$",
    "^second$",
    "^timezone_hour$",
    "^timezone_minute$",
];
const TIME_PARTS_HOUR: usize = 0;
const TIME_PARTS_MINUTE: usize = 1;
const TIME_PARTS_SECOND: usize = 2;
const TIME_PARTS_TZ_HOUR: usize = 3;
const TIME_PARTS_TZ_MINUTE: usize = 4;
static TIME_PARTS_PATTERN_SET: Lazy<RegexSet> =
    Lazy::new(|| RegexSet::new(RE_SET_TIME_PARTS).unwrap());

fn parse_time(reader: &mut Reader) -> DateTime {
    fn expect_u8(reader: &mut Reader, typ: Option<IonType>) -> u8 {
        match typ {
            Some(IonType::Int) => match reader.read_int().unwrap() {
                Int::I64(i) => i as u8, // TODO check range
                Int::BigInt(_) => todo!("bigint"),
            },
            _ => {
                todo!("error; not a u8")
            }
        }
    }
    fn maybe_i8(reader: &mut Reader, typ: Option<IonType>) -> Option<i8> {
        match typ {
            Some(IonType::Int) => match reader.read_int().unwrap() {
                Int::I64(i) => Some(i as i8), // TODO check range
                Int::BigInt(_) => todo!("bigint"),
            },
            _ => None,
        }
    }
    fn expect_f64(reader: &mut Reader, typ: Option<IonType>) -> f64 {
        match typ {
            Some(IonType::Decimal) => {
                // TODO ion Decimal doesn't give a lot of functionality to get at the data currently
                // TODO    and it's not clear whether we'll continue with rust decimal or switch to big decimal
                let ion_dec = reader.read_decimal().unwrap();
                let ion_dec_str = format!("{ion_dec}").replace('d', "e");
                let dec = rust_decimal::Decimal::from_str(&ion_dec_str)
                    .or_else(|_| rust_decimal::Decimal::from_scientific(&ion_dec_str));
                let dec = dec.unwrap();
                dec.to_f64().unwrap()
            }
            Some(IonType::Float) => reader.read_f64().unwrap(),
            _ => {
                todo!("error; not a f64: {:?}", typ)
            }
        }
    }

    #[derive(Default)]
    struct TimeParts {
        pub hour: Option<u8>,
        pub minute: Option<u8>,
        pub second: Option<f64>,
        pub tz_hour: Option<i8>,
        pub tz_minute: Option<i8>,
    }

    let mut time = TimeParts::default();
    let patterns: &RegexSet = &TIME_PARTS_PATTERN_SET;

    reader.step_in().expect("step into struct");
    #[allow(irrefutable_let_patterns)]
    while let item = reader.next().expect("struct value") {
        let (key, typ) = match item {
            StreamItem::Value(typ) => (reader.field_name().expect("field name"), Some(typ)),
            StreamItem::Null(_) => (reader.field_name().expect("field name"), None),
            StreamItem::Nothing => break,
        };
        let matches = patterns.matches(key.text().unwrap());
        match matches.into_iter().next() {
            Some(TIME_PARTS_HOUR) => time.hour = Some(expect_u8(reader, typ)),
            Some(TIME_PARTS_MINUTE) => time.minute = Some(expect_u8(reader, typ)),
            Some(TIME_PARTS_SECOND) => time.second = Some(expect_f64(reader, typ)),
            Some(TIME_PARTS_TZ_HOUR) => time.tz_hour = maybe_i8(reader, typ),
            Some(TIME_PARTS_TZ_MINUTE) => time.tz_minute = maybe_i8(reader, typ),
            _ => {
                todo!("error: unexpected time field name")
            }
        }
    }
    reader.step_out().expect("step out of struct");

    DateTime::from_hmfs_tz(
        time.hour.expect("hour"),
        time.minute.expect("minute"),
        time.second.expect("second"),
        time.tz_hour,
        time.tz_minute,
    )
}

fn parse_datetime(reader: &mut Reader) -> DateTime {
    let ts = reader.read_timestamp().unwrap();
    // TODO: fractional seconds Cf. https://github.com/amazon-ion/ion-rust/pull/482#issuecomment-1470615286
    DateTime::from_ymdhms(
        ts.year(),
        NonZeroU8::new(ts.month() as u8).unwrap(),
        ts.day() as u8,
        ts.hour() as u8,
        ts.minute() as u8,
        ts.second() as f64,
    )
}

fn parse_date(reader: &mut Reader) -> DateTime {
    let ts = reader.read_timestamp().unwrap();
    DateTime::from_ymd(
        ts.year(),
        NonZeroU8::new(ts.month() as u8).unwrap(),
        ts.day() as u8,
    )
}

fn parse_tuple(reader: &mut Reader) -> Tuple {
    let mut tuple = Tuple::new();
    reader.step_in().expect("step into struct");
    loop {
        let item = reader.next().expect("struct value");
        let (key, value) = match item {
            StreamItem::Value(typ) => (
                reader.field_name().expect("field name"),
                parse_value(reader, typ),
            ),
            StreamItem::Null(_) => (reader.field_name().expect("field name"), Value::Null),
            StreamItem::Nothing => break,
        };
        tuple.insert(key.text().unwrap(), value);
    }
    reader.step_out().expect("step out of struct");
    tuple
}

fn parse_sequence(reader: &mut Reader) -> Vec<Value> {
    reader.step_in().expect("step into sequence");
    let mut values = vec![];
    loop {
        let item = reader.next().expect("test value");
        let val = match item {
            StreamItem::Value(typ) => parse_value(reader, typ),
            StreamItem::Null(_) => Value::Null,
            StreamItem::Nothing => break,
        };
        values.push(val);
    }
    reader.step_out().expect("step out of sequence");
    values
}