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
use std::fmt::Display;

use chrono::{DateTime, NaiveDate, Utc};

use crate::{
    date_time::{date_time_to_f64, date_time_to_i64, date_to_f64, date_to_i64},
    error::Error,
    file::SubFile,
    pqarray::read::SimpleIter,
};

use super::{
    BoundaryValues, GenericArrays, GenericFreeformSubblocks, GenericNumbers, GenericOptionalArrays,
    GenericScalars, NumberType,
};

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Boundary<T: NumberType> {
    Less(T),
    LessEqual(T),
}

impl<T: NumberType> Boundary<T> {
    pub fn from<U: NumberType + Into<T>>(other: Boundary<U>) -> Self {
        match other {
            Boundary::Less(value) => Self::Less(value.into()),
            Boundary::LessEqual(value) => Self::LessEqual(value.into()),
        }
    }

    pub fn from_value(value: T, is_inclusive: bool) -> Self {
        if is_inclusive {
            Self::LessEqual(value)
        } else {
            Self::Less(value)
        }
    }

    pub fn value(self) -> T {
        match self {
            Boundary::Less(value) | Boundary::LessEqual(value) => value,
        }
    }

    pub fn is_inclusive(self) -> bool {
        match self {
            Boundary::Less(_) => false,
            Boundary::LessEqual(_) => true,
        }
    }

    pub fn map<U: NumberType>(self, func: impl FnOnce(T) -> U) -> Boundary<U> {
        match self {
            Boundary::Less(t) => Boundary::Less(func(t)),
            Boundary::LessEqual(t) => Boundary::LessEqual(func(t)),
        }
    }
}

impl<T: NumberType + Display> Display for Boundary<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Less(value) => write!(f, "< {value}"),
            Self::LessEqual(value) => write!(f, "≤ {value}"),
        }
    }
}

/// Iterator for reading scalar data.
///
/// Casts to `f64` by default or you can access the variants directly.
#[derive(Debug)]
pub enum Scalars {
    F32(GenericScalars<f32>),
    F64(GenericScalars<f64>),
}

impl Iterator for Scalars {
    type Item = Result<f64, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            Self::F64(iter) => iter.next(),
            Self::F32(iter) => iter.next().map(|r| r.map(Into::into)),
        }
    }
}

/// Iterator for reading vertex data of various types.
///
/// Can be used as an iterator that casts to `[f64; 3]` or you can access the variants directly.
#[derive(Debug)]
pub enum Vertices {
    F32(GenericArrays<f32, 3>),
    F64(GenericArrays<f64, 3>),
}

impl Iterator for Vertices {
    type Item = Result<[f64; 3], Error>;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            Self::F64(iter) => iter.next(),
            Self::F32(iter) => array_item_cast(iter.next()),
        }
    }
}

fn array_item_cast<T, U: From<T>, const N: usize>(
    input: Option<Result<[T; N], Error>>,
) -> Option<Result<[U; N], Error>> {
    input.map(|r| r.map(|a| a.map(Into::into)))
}

/// Iterator for reading texture coordinate data of various types.
///
/// Can be used as an iterator that casts to `[f64; 2]` or you can access the variants directly.
#[derive(Debug)]
pub enum Texcoords {
    F32(GenericArrays<f32, 2>),
    F64(GenericArrays<f64, 2>),
}

impl Iterator for Texcoords {
    type Item = Result<[f64; 2], Error>;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            Self::F64(iter) => iter.next(),
            Self::F32(iter) => array_item_cast(iter.next()),
        }
    }
}

/// Iterator for reading number data of various types.
///
/// You can access the variants directly or use the `try_into_f64` and `try_into_i64` methods.
/// These methods can both fail so aren't automatic.
#[derive(Debug)]
pub enum Numbers {
    F32(GenericNumbers<f32>),
    F64(GenericNumbers<f64>),
    I64(GenericNumbers<i64>),
    Date(GenericNumbers<NaiveDate>),
    DateTime(GenericNumbers<DateTime<Utc>>),
}

impl Numbers {
    /// Turns this into an `f64` iterator, casting values.
    ///
    /// If the numbers use type `i64` this will fail with `Error::UnsafeCast`. Dates will become
    /// days since the '1970-01-01' epoch. Date-times will become seconds since the
    /// '1970-01-01T00:00:00Z' epoch with a small loss of precision.
    ///
    /// Currently can't fail but future number types might yield `Error::UnsafeCast`.
    pub fn try_into_f64(self) -> Result<NumbersF64, Error> {
        match &self {
            Numbers::I64(_) => Err(Error::UnsafeCast("64-bit integer", "64-bit float")),
            Numbers::F32(_) | Numbers::F64(_) | Numbers::Date(_) | Numbers::DateTime(_) => {
                Ok(NumbersF64(self))
            }
        }
    }

    /// Turns this into an `i64` iterator, casting values.
    ///
    /// Floating-point types will be rejected with `Error::UnsafeCast`. Dates will become
    /// days since the '1970-01-01' epoch. Date-times will become microseconds since the
    /// '1970-01-01T00:00:00Z' epoch.
    pub fn try_into_i64(self) -> Result<NumbersI64, Error> {
        match self {
            Numbers::F32(_) => Err(Error::UnsafeCast("32-bit float", "64-bit integer")),
            Numbers::F64(_) => Err(Error::UnsafeCast("64-bit float", "64-bit integer")),
            Numbers::I64(_) | Numbers::Date(_) | Numbers::DateTime(_) => Ok(NumbersI64(self)),
        }
    }
}

pub struct NumbersF64(Numbers);

impl Iterator for NumbersF64 {
    type Item = Result<Option<f64>, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        match &mut self.0 {
            Numbers::F32(i) => i.next().map(|r| r.map(|o| o.map(Into::into))),
            Numbers::F64(i) => i.next(),
            Numbers::Date(i) => i.next().map(|r| r.map(|o| o.map(date_to_f64))),
            Numbers::DateTime(i) => i.next().map(|r| r.map(|o| o.map(date_time_to_f64))),
            Numbers::I64(_) => None,
        }
    }
}

pub struct NumbersI64(Numbers);

impl Iterator for NumbersI64 {
    type Item = Result<Option<i64>, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        match &mut self.0 {
            Numbers::F32(_) | Numbers::F64(_) => None,
            Numbers::I64(i) => i.next(),
            Numbers::Date(i) => i.next().map(|r| r.map(|o| o.map(date_to_i64))),
            Numbers::DateTime(i) => i.next().map(|r| r.map(|o| o.map(date_time_to_i64))),
        }
    }
}

/// Iterator for reading vector data.
///
/// Casts to `Option<[f64; 3]>` by default or you can access the variants directly.
/// 2D vectors are cast to a 3D vector with zero in the Z component.
#[derive(Debug)]
pub enum Vectors {
    F32x2(GenericOptionalArrays<f32, 2>),
    F64x2(GenericOptionalArrays<f64, 2>),
    F32x3(GenericOptionalArrays<f32, 3>),
    F64x3(GenericOptionalArrays<f64, 3>),
}

impl Iterator for Vectors {
    type Item = Result<Option<[f64; 3]>, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            Self::F32x2(iter) => iter
                .next()
                .map(|r| r.map(|o| o.map(|[x, y]| [x.into(), y.into(), 0.0]))),
            Self::F64x2(iter) => iter.next().map(|r| r.map(|o| o.map(|[x, y]| [x, y, 0.0]))),
            Self::F32x3(iter) => {
                let input = iter.next();
                input.map(|r| r.map(|o| o.map(|a| a.map(Into::into))))
            }
            Self::F64x3(iter) => iter.next(),
        }
    }
}

/// Generic iterator for boundary data.
#[derive(Debug)]
pub struct GenericBoundaries<T: NumberType> {
    value: BoundaryValues<T>,
    inclusive: SimpleIter<bool, SubFile>,
}

impl<T: NumberType> GenericBoundaries<T> {
    pub fn new(value: SimpleIter<T, SubFile>, inclusive: SimpleIter<bool, SubFile>) -> Self {
        Self {
            value: BoundaryValues::new(value),
            inclusive,
        }
    }
}

impl<T: NumberType> Iterator for GenericBoundaries<T> {
    type Item = Result<Boundary<T>, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        match (self.value.next(), self.inclusive.next()) {
            (Some(Err(e)), _) | (_, Some(Err(e))) => Some(Err(e)),
            (None, _) | (_, None) => None,
            (Some(Ok(value)), Some(Ok(false))) => Some(Ok(Boundary::Less(value))),
            (Some(Ok(value)), Some(Ok(true))) => Some(Ok(Boundary::LessEqual(value))),
        }
    }
}

/// Iterator for reading color data.
///
/// Casting is the same as [`Numbers`](Numbers).
#[derive(Debug)]
pub enum Boundaries {
    F32(GenericBoundaries<f32>),
    F64(GenericBoundaries<f64>),
    I64(GenericBoundaries<i64>),
    Date(GenericBoundaries<NaiveDate>),
    DateTime(GenericBoundaries<DateTime<Utc>>),
}

impl Boundaries {
    /// Turns this into an `f64` boundary iterator, casting values.
    ///
    /// If the numbers use type `i64` this will fail with `Error::UnsafeCast`. Dates will become
    /// days since the '1970-01-01' epoch. Date-times will become seconds since the
    /// '1970-01-01T00:00:00Z' epoch with a small loss of precision.
    ///
    /// Currently can't fail but future number types might yield `Error::UnsafeCast`.
    pub fn try_into_f64(self) -> Result<BoundariesF64, Error> {
        match &self {
            Boundaries::I64(_) => Err(Error::UnsafeCast("64-bit integer", "64-bit float")),
            Boundaries::F32(_)
            | Boundaries::F64(_)
            | Boundaries::Date(_)
            | Boundaries::DateTime(_) => Ok(BoundariesF64(self)),
        }
    }

    /// Turns this into an `i64` boundary iterator, casting values.
    ///
    /// Floating-point types will be rejected with `Error::UnsafeCast`. Dates will become
    /// days since the '1970-01-01' epoch. Date-times will become microseconds since the
    /// '1970-01-01T00:00:00Z' epoch.
    pub fn try_into_i64(self) -> Result<BoundariesI64, Error> {
        match self {
            Boundaries::F32(_) => Err(Error::UnsafeCast("32-bit float", "64-bit integer")),
            Boundaries::F64(_) => Err(Error::UnsafeCast("64-bit float", "64-bit integer")),
            Boundaries::I64(_) | Boundaries::Date(_) | Boundaries::DateTime(_) => {
                Ok(BoundariesI64(self))
            }
        }
    }
}

pub struct BoundariesF64(Boundaries);

impl Iterator for BoundariesF64 {
    type Item = Result<Boundary<f64>, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        match &mut self.0 {
            Boundaries::F32(i) => i.next().map(|r| r.map(|o| o.map(Into::into))),
            Boundaries::F64(i) => i.next(),
            Boundaries::Date(i) => i.next().map(|r| r.map(|o| o.map(date_to_f64))),
            Boundaries::DateTime(i) => i.next().map(|r| r.map(|o| o.map(date_time_to_f64))),
            Boundaries::I64(_) => None,
        }
    }
}

pub struct BoundariesI64(Boundaries);

impl Iterator for BoundariesI64 {
    type Item = Result<Boundary<i64>, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        match &mut self.0 {
            Boundaries::F32(_) | Boundaries::F64(_) => None,
            Boundaries::I64(i) => i.next(),
            Boundaries::Date(i) => i.next().map(|r| r.map(|o| o.map(date_to_i64))),
            Boundaries::DateTime(i) => i.next().map(|r| r.map(|o| o.map(date_time_to_i64))),
        }
    }
}

/// Iterator for reading regular sub-block corner min/max data.
///
/// Casts to `[f64; 6]` by default or you can access the variants directly.
/// Each item is `[min_x, min_y, min_z, max_x, max_y, max_z]`.
#[derive(Debug)]
pub enum FreeformSubblocks {
    F32(GenericFreeformSubblocks<f32>),
    F64(GenericFreeformSubblocks<f64>),
}

impl Iterator for FreeformSubblocks {
    type Item = Result<([u32; 3], [f64; 6]), Error>;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            Self::F64(iter) => iter.next(),
            Self::F32(iter) => match iter.next() {
                Some(Ok((parent, corners))) => Some(Ok((parent, corners.map(Into::into)))),
                Some(Err(e)) => Some(Err(e)),
                None => None,
            },
        }
    }
}