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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use crate::{
    context::{
        BooleanContext, BytesContext, MapStructContext, NullContext, NumberContext,
        SequenceContext, StringContext,
    },
    Coalesce, StructuralEq,
};

/// This enum is the core output of the analysis, it describes the structure of a document.
///
/// Each variant also contains [context](crate::context) data that allows it to store information
/// about the values it has encountered.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum Schema {
    /// The Null variant is a special one that is only ever found when a document has a single
    /// null value at the root of the document.
    /// Null values in [Struct](Schema::Struct)s or [Sequence](Schema::Sequence)s are instead
    /// handled at the [Field] level, where it is more ergonomic.
    Null(NullContext),
    /// Represents a boolean value.
    Boolean(BooleanContext),
    /// Represents an integer value.
    Integer(NumberContext<i128>),
    /// Represents a floating point value.
    Float(NumberContext<f64>),
    /// Represents a textual value.
    String(StringContext),
    /// Represents a value of raw bytes.
    Bytes(BytesContext),
    /// Represents a sequence of values described by a [Field].
    /// It assumes all values share the same schema.
    Sequence {
        /// The field is the structure shared by all the elements of the sequence.
        field: Box<Field>,
        /// The context aggregates information about the sequence.
        /// It is passed the length of the sequence.
        context: SequenceContext,
    },
    /// Represents a [String]->[Field] mapping.
    ///
    /// Note: currently there is not a true map and only strings may be used as keys.
    Struct {
        /// Each [String] key gets assigned a [Field].
        /// Currently we are using a [BTreeMap], but that might change in the future.
        fields: BTreeMap<String, Field>,
        /// The context aggregates information about the struct.
        /// It is passed a vector of the key names.
        context: MapStructContext,
    },
    /// Simply a vector of [Schema]s, it should never contain an Union or multiple instances of the
    /// same variant inside.
    ///
    /// Note: content needs to be a struct variant to work with `#[serde(tag = "type")]`.
    Union {
        /// A list of the possible schemas that were found.
        variants: Vec<Schema>,
    },
    // Tuple(..),
    // Map(..),
}

/// A [Field] is a useful abstraction to record metadata that does not belong or would be unyieldy
/// to place into the [Schema] and to account for cases in which the existence of a [Field] might be
/// known, but nothing is known about its shape.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct Field {
    /// The status holds information on the the field, like whether it might be null or
    /// missing altogether. Duplicate fields are also recorded.
    #[serde(flatten)]
    pub status: FieldStatus,
    /// The inner Schema is optional because we might have no information on the shape of the field
    /// (like for an empty array).
    #[serde(flatten)]
    pub schema: Option<Schema>,
}

/// The FieldStatus keeps track of what kind of values a [Field] has been found to have.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
pub struct FieldStatus {
    /// The [Field] has been found to be [None] or of the unit type `()`.
    pub may_be_null: bool,
    /// The [Field] has been found to be a normal value, where normal means
    /// any valid value for the [Schema] associated with the [Field].
    pub may_be_normal: bool,
    /// The [Field] was found only on some [Struct](Schema::Struct)s or the
    /// [Sequence](Schema::Sequence) to which it belongs might be empty (if only empty sequences
    /// are found, then the [Schema] in the [Field] will also be [None]).
    pub may_be_missing: bool,
    /// Sometimes a field might appear more than once in the same [Struct](Schema::Struct).
    /// In that case all instances are considered, but this flag is also enabled.
    /// This is useful to spot suspicious data, but also to detect sequences in xml files.
    /// See [here](crate::helpers::xml) for more info.
    pub may_be_duplicate: bool,
}

//
// Schema implementations
//
impl StructuralEq for Schema {
    fn structural_eq(&self, other: &Self) -> bool {
        use Schema::*;
        match (self, other) {
            (Null(_), Null(_)) => true,
            (Boolean(_), Boolean(_)) => true,
            (Integer(_), Integer(_)) => true,
            (Float(_), Float(_)) => true,
            (String(_), String(_)) => true,
            (Bytes(_), Bytes(_)) => true,

            (Sequence { field: field_1, .. }, Sequence { field: field_2, .. }) => {
                field_1.structural_eq(field_2)
            }

            (
                Struct {
                    fields: fields_1, ..
                },
                Struct {
                    fields: fields_2, ..
                },
            ) => fields_1.structural_eq(fields_2),

            (Union { variants: s }, Union { variants: o }) => {
                let mut s = s.clone();
                let mut o = o.clone();
                s.sort_by(schema_cmp);
                o.sort_by(schema_cmp);
                s.structural_eq(&o)
            }

            // Listing these out makes sure it fails if new variants are added.
            (Null(_), _)
            | (Boolean(_), _)
            | (Integer(_), _)
            | (Float(_), _)
            | (String(_), _)
            | (Bytes(_), _)
            | (Sequence { .. }, _)
            | (Struct { .. }, _)
            | (Union { .. }, _) => false,
        }
    }
}
impl Coalesce for Schema {
    fn coalesce(&mut self, other: Self) {
        use Schema::*;
        match (self, other) {
            (Boolean(s), Boolean(o)) => s.coalesce(o),
            (Integer(s), Integer(o)) => s.coalesce(o),
            (Float(s), Float(o)) => s.coalesce(o),
            (String(s), String(o)) => s.coalesce(o),
            (Bytes(s), Bytes(o)) => s.coalesce(o),

            (
                Sequence {
                    field: self_boxed,
                    context: self_agg,
                },
                Sequence {
                    field: other_boxed,
                    context: other_agg,
                },
            ) => {
                self_agg.coalesce(other_agg);
                self_boxed.coalesce(*other_boxed);
            }

            (
                Struct {
                    fields: self_fields,
                    context: self_agg,
                },
                Struct {
                    fields: other_fields,
                    context: other_agg,
                },
            ) => {
                self_agg.coalesce(other_agg);
                for (name, other_schema) in other_fields {
                    self_fields
                        .entry(name)
                        .and_modify(|schema| schema.coalesce(other_schema.clone()))
                        .or_insert_with(|| other_schema);
                }
            }
            (
                Union {
                    variants: self_alternatives,
                },
                Union {
                    variants: other_alternatives,
                },
            ) => coalesce_unions(self_alternatives, other_alternatives),
            (
                Union {
                    variants: self_alternatives,
                },
                any_other,
            ) => coalesce_to_alternatives(self_alternatives, any_other),
            (
                any_self,
                Union {
                    variants: mut other_alternatives,
                },
            ) => {
                let self_original = std::mem::replace(any_self, Schema::Null(Default::default()));
                coalesce_to_alternatives(&mut other_alternatives, self_original);
                *any_self = Schema::Union {
                    variants: other_alternatives,
                };
            }

            (any_self, any_other) => {
                let self_original = std::mem::replace(any_self, Schema::Null(Default::default()));
                *any_self = Union {
                    variants: vec![self_original, any_other],
                };
            }
        };
        return;

        fn coalesce_unions(selfs: &mut Vec<Schema>, others: Vec<Schema>) {
            for o in others {
                coalesce_to_alternatives(selfs, o);
            }
        }

        /// This function attempts to match the incomming schema against all the
        /// alternatives already present, and if it fails it pushes it to the vector as a
        /// new alternative.
        fn coalesce_to_alternatives(alternatives: &mut Vec<Schema>, mut other: Schema) {
            use Schema::*;
            for s in alternatives.iter_mut() {
                match (s, other) {
                    // Nested unions should never happen.
                    // It is the job of the root impl of Coalesce for Schema to guarantee this.
                    (Union { .. }, _) | (_, Union { .. }) => {
                        unreachable!("nested union")
                    }

                    // If they are the same, go ahead and coalesce!
                    (Boolean(s), Boolean(o)) => {
                        s.coalesce(o);
                        return;
                    }
                    (Integer(s), Integer(o)) => {
                        s.coalesce(o);
                        return;
                    }
                    (Float(s), Float(o)) => {
                        s.coalesce(o);
                        return;
                    }
                    (String(s), String(o)) => {
                        s.coalesce(o);
                        return;
                    }
                    (Bytes(s), Bytes(o)) => {
                        s.coalesce(o);
                        return;
                    }

                    (
                        Sequence {
                            field: self_boxed,
                            context: self_agg,
                        },
                        Sequence {
                            field: other_boxed,
                            context: other_agg,
                        },
                    ) => {
                        self_agg.coalesce(other_agg);
                        self_boxed.coalesce(*other_boxed);
                        return;
                    }

                    (
                        Struct {
                            fields: self_fields,
                            context: self_agg,
                        },
                        Struct {
                            fields: other_fields,
                            context: other_agg,
                        },
                    ) => {
                        self_agg.coalesce(other_agg);
                        for (name, other_schema) in other_fields {
                            self_fields
                                .entry(name)
                                .and_modify(|schema| schema.coalesce(other_schema.clone()))
                                .or_insert_with(|| other_schema);
                        }
                        return;
                    }

                    // If they don't match just continue ahead to the next one.
                    (_, caught_other) => {
                        other = caught_other;
                    }
                }
            }

            // If we were unable to find a match, push the schema to the alternatives:
            alternatives.push(other);
        }
    }
}
impl PartialEq for Schema {
    fn eq(&self, other: &Self) -> bool {
        use Schema::*;
        match (self, other) {
            (Null(s), Null(o)) => s == o,
            (Boolean(s), Boolean(o)) => s == o,
            (Integer(s), Integer(o)) => s == o,
            (Float(s), Float(o)) => s == o,
            (String(s), String(o)) => s == o,
            (Bytes(s), Bytes(o)) => s == o,

            (
                Sequence {
                    field: field_1,
                    context: context_1,
                },
                Sequence {
                    field: field_2,
                    context: context_2,
                },
            ) => field_1 == field_2 && context_1 == context_2,

            (
                Struct {
                    fields: fields_1,
                    context: context_1,
                },
                Struct {
                    fields: fields_2,
                    context: context_2,
                },
            ) => fields_1 == fields_2 && context_1 == context_2,

            (Union { variants: s }, Union { variants: o }) => {
                let mut s = s.clone();
                let mut o = o.clone();
                s.sort_by(schema_cmp);
                o.sort_by(schema_cmp);
                s == o
            }

            // Listing these out makes sure it fails if new variants are added.
            (Null(_), _)
            | (Boolean(_), _)
            | (Integer(_), _)
            | (Float(_), _)
            | (String(_), _)
            | (Bytes(_), _)
            | (Sequence { .. }, _)
            | (Struct { .. }, _)
            | (Union { .. }, _) => false,
        }
    }
}

//
// Field implementations
//
impl Field {
    /// Returns a [Field] with the given [Schema] and default [FieldStatus].
    pub fn with_schema(schema: Schema) -> Self {
        Self {
            status: FieldStatus::default(),
            schema: Some(schema),
        }
    }
}
impl Coalesce for Field {
    fn coalesce(&mut self, other: Self)
    where
        Self: Sized,
    {
        self.status.coalesce(other.status);
        self.schema = match (self.schema.take(), other.schema) {
            (Some(mut s), Some(o)) => {
                s.coalesce(o);
                Some(s)
            }
            (Some(s), None) => Some(s),
            (None, Some(o)) => Some(o),
            (None, None) => None,
        }
    }
}
impl StructuralEq for Field {
    fn structural_eq(&self, other: &Self) -> bool {
        self.status == other.status && self.schema.structural_eq(&other.schema)
    }
}

//
// FieldStatus implementations
//
impl FieldStatus {
    /// If the value passed is true, then the status will allow duplicates.
    /// Otherwise no changes are made.
    pub fn allow_duplicates(&mut self, is_duplicate: bool) {
        self.may_be_duplicate |= is_duplicate;
    }
    /// `true` if the status allows for null or missing values.
    pub fn is_option(&self) -> bool {
        self.may_be_null || self.may_be_missing
    }
}
impl Coalesce for FieldStatus {
    fn coalesce(&mut self, other: Self)
    where
        Self: Sized,
    {
        self.may_be_null |= other.may_be_null;
        self.may_be_normal |= other.may_be_normal;
        self.may_be_missing |= other.may_be_missing;
        self.may_be_duplicate |= other.may_be_duplicate;
    }
}

//
// Helper functions
//

/// A helper function that returns the [Ordering](std::cmp::Ordering) of two [Schema]s
/// to help in comparing two [Schema::Union].
/// Since a [Schema::Union] should never hold two schemas of the same type, it is enough to
/// just compare the top level without recursion.
fn schema_cmp(first: &Schema, second: &Schema) -> std::cmp::Ordering {
    use std::cmp::Ordering::*;
    use Schema::*;
    match first {
        Null(_) => match second {
            Null(_) => Equal,
            _ => Less,
        },
        Boolean(_) => match second {
            Null(_) | Boolean(_) => Equal,
            _ => Less,
        },
        Integer(_) => match second {
            Null(_) | Boolean(_) => Greater,
            Integer(_) => Equal,
            _ => Less,
        },
        Float(_) => match second {
            Null(_) | Boolean(_) | Integer(_) => Greater,
            Float(_) => Equal,
            _ => Less,
        },
        String(_) => match second {
            Null(_) | Boolean(_) | Integer(_) | Float(_) => Greater,
            String(_) => Equal,
            _ => Less,
        },
        Bytes(_) => match second {
            Null(_) | Boolean(_) | Integer(_) | Float(_) | String(_) => Greater,
            Bytes(_) => Equal,
            _ => Less,
        },
        Sequence { .. } => match second {
            Null(_) | Boolean(_) | Integer(_) | Float(_) | String(_) | Bytes(_) => Greater,
            Sequence { .. } => Equal,
            _ => Less,
        },
        Struct { .. } => match second {
            Null(_)
            | Boolean(_)
            | Integer(_)
            | Float(_)
            | String(_)
            | Bytes(_)
            | Sequence { .. } => Greater,
            Struct { .. } => Equal,
            _ => Less,
        },
        Union { .. } => match second {
            Null(_)
            | Boolean(_)
            | Integer(_)
            | Float(_)
            | String(_)
            | Bytes(_)
            | Sequence { .. }
            | Struct { .. } => Greater,
            Union { .. } => Equal,
        },
    }
}