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
//! Schemas are JSON used to declare & store the shape of buffer objects.
//! 
//! No Proto Schemas are JSON objects that describe how the data in a NP_ buffer is stored.
//! 
//! Every schema object has at least a "type" key that provides the kind of value stored at that part of the schema.  Additional keys are dependent on the type of schema.
//! 
//! Schemas are validated and sanity checked by the [NP_Factory](../struct.NP_Factory.html) struct upon creation.  You cannot pass an invalid schema into a factory constructor and build/parse buffers with it.
//! 
//! If you're familiar with typescript, schemas can be described by this recursive interface:
//! ```text
//! interface NP_Schema {
//!     type: string;
//!     
//!     // used by list types
//!     of?: NP_Schema
//!     
//!     // used by map types
//!     value?: NP_Schema
//! 
//!     // used by tuple types
//!     values?: NP_Schema[]
//! 
//!     // used by table types
//!     columns?: [string, NP_Schema][]
//! }
//! ```
//! 
//! Schemas do not have to contain collections, for example a perfectly valid schema for just a string would be:
//! ```text
//! {
//!     "type": "string"
//! }
//! ```
//! 
//! Nesting is easy to perform.  For example, this  is a list of tables.  Each table has two columns: id and title.  Both columns are a string type.
//! ```text
//! {
//!     "type": "list",
//!     "of": {
//!         "type": "table",
//!         "columns": [
//!             ["id",    {type: "string"}]
//!             ["title", {type: "string"}]
//!         ]
//!     }
//! }
//! ```
//! 
//! A list of strings is just as easy...
//! 
//! ```text
//! {
//!     "type": "list",
//!     "of": { type: "string" }
//! }
//! ```
//! 
//! Each type has trade offs associated with it.  The table and documentation below go into further detail.
//! 
//! Here is a table of supported types. 
//! 
//! | Type                                   | Rust Type / Struct                                                       | Bytes (Size)   | Limits / Notes                                                           |
//! |----------------------------------------|--------------------------------------------------------------------------|----------------|--------------------------------------------------------------------------|
//! | [`table`](#table)                      | [`NP_Table`](../collection/table/index.html)                             | 4 bytes - ~4GB | Linked list with indexed keys that map against up to 255 named columns.  |
//! | [`list`](#list)                        | [`NP_List`](../collection/list/index.html)                               | 8 bytes - ~4GB | Linked list with up to 65,535 items.                                     |
//! | [`map`](#map)                          | [`NP_Map`](../collection/map/index.html)                                 | 4 bytes - ~4GB | Linked list with `Vec<u8>` keys.                                         |
//! | [`tuple`](#tuple)                      | [`NP_Tuple`](../collection/tuple/index.html)                             | 4 bytes - ~4GB | Static sized collection of specific values.                              |
//! | [`any`](#any)                          | [`NP_Any`](https://doc.rust-lang.org/std/string/struct.String.html)      | 4 bytes - ~4GB | Generic type.                                                            |
//! | [`string`](#string)                    | [`String`](https://doc.rust-lang.org/std/string/struct.String.html)      | 4 bytes - ~4GB | Utf-8 formatted string.                                                  |
//! | [`bytes`](#bytes)                      | [`NP_Bytes`](https://doc.rust-lang.org/std/vec/struct.Vec.html)          | 4 bytes - ~4GB | Arbitrary bytes.                                                         |
//! | [`int8`](#int8-int16-int32-int64)      | [`i8`](https://doc.rust-lang.org/std/primitive.i8.html)                  | 1 byte         | -127 to 127                                                              |
//! | [`int16`](#int8-int16-int32-int64)     | [`i16`](https://doc.rust-lang.org/std/primitive.i16.html)                | 2 bytes        | -32,768 to 32,768                                                        |
//! | [`int32`](#int8-int16-int32-int64)     | [`i32`](https://doc.rust-lang.org/std/primitive.i32.html)                | 4 bytes        | -2,147,483,648 to 2,147,483,648                                          |
//! | [`int64`](#int8-int16-int32-int64)     | [`i64`](https://doc.rust-lang.org/std/primitive.i64.html)                | 8 bytes        | -9.22e18 to 9.22e18                                                      |
//! | [`uint8`](#uint8-uint16-uint32-uint64) | [`u8`](https://doc.rust-lang.org/std/primitive.u8.html)                  | 1 byte         | 0 - 255                                                                  |
//! | [`uint16`](#uint8-uint16-uint32-uint64)| [`u16`](https://doc.rust-lang.org/std/primitive.u16.html)                | 2 bytes        | 0 - 65,535                                                               |
//! | [`uint32`](#uint8-uint16-uint32-uint64)| [`u32`](https://doc.rust-lang.org/std/primitive.u32.html)                | 4 bytes        | 0 - 4,294,967,295                                                        |
//! | [`uint64`](#uint8-uint16-uint32-uint64)| [`u64`](https://doc.rust-lang.org/std/primitive.u64.html)                | 8 bytes        | 0 - 1.84e19                                                              |
//! | [`float`](#float-double)               | [`f32`](https://doc.rust-lang.org/std/primitive.f32.html)                | 4 bytes        | -3.4e38 to 3.4e38                                                        |
//! | [`double`](#float-double)              | [`f64`](https://doc.rust-lang.org/std/primitive.f64.html)                | 8 bytes        | -1.7e308 to 1.7e308                                                      |
//! | [`option`](#option)                    | [`NP_Option`](https://doc.rust-lang.org/std/string/.html)                | 1 byte         | Up to 255 strings in schema.                                             |
//! | [`bool`](#bool)                        | [`bool`](https://doc.rust-lang.org/std/primitive.bool.html)              | 1 byte         |                                                                          |
//! | [`dec64`](#dec64)                      | [`NP_Dec`](..pointer/struct.NP_Dec.html)                                 | 9 bytes        | Big Integer Decimal format.                                              |
//! | [`geo4`](#geo4-geo8-geo16)             | [`NP_Geo`](../pointer/struct.NP_Geo.html)                                | 4 bytes        | 1.5km resolution (city) geographic coordinate                            |
//! | [`geo8`](#geo4-geo8-geo16)             | [`NP_Geo`](../pointer/struct.NP_Geo.html)                                | 8 bytes        | 16mm resolution (marble) geographic coordinate                           |
//! | [`geo16`](#geo4-geo8-geo16)            | [`NP_Geo`](../pointer/struct.NP_Geo.html)                                | 16 bytes       | 3.5nm resolution (flea) geographic coordinate                            |
//! | [`tid`](#tid)                          | [`NP_TimeID`](../pointer/struct.NP_TimeID.html)                          | 16 bytes       | 8 byte u64 for time with 8 bytes of random numbers.                      |
//! | [`uuid`](#uuid)                        | [`NP_UUID`](../pointer/struct.NP_UUID.html)                              | 16 bytes       | v4 UUID, 2e37 possible UUID v4s                                          |
//! | [`date`](#date)                        | [`NP_Date`](https://doc.rust-lang.org/std/primitive.u64.html)            | 8 bytes        | Good to store unix epoch (in seconds) until the year 584,942,417,355     |
//!  
//! # table
//! 
//! # list
//! 
//! # map
//! 
//! # tuple
//! 
//! # any
//! 
//! # string
//! 
//! # bytes
//! 
//! # int8, int16, int32, int64
//! 
//! # uint8, uint16, uint32, uint64
//! 
//! # float, double
//! 
//! # option
//! 
//! # bool
//! 
//! # dec64
//! 
//! # geo4, ge8, geo16
//! 
//! # tid
//! 
//! # uuid
//! 
//! # date
//! 
//!  
use crate::pointer::NP_ValueInto;
use crate::pointer::any::NP_Any;
use crate::pointer::misc::NP_Date;
use crate::pointer::misc::NP_UUID;
use crate::pointer::misc::NP_TimeID;
use crate::pointer::misc::NP_Geo;
use crate::pointer::misc::NP_Dec;
use crate::collection::tuple::NP_Tuple;
use crate::pointer::bytes::NP_Bytes;
use crate::collection::{list::NP_List, table::NP_Table, map::NP_Map};
use crate::pointer::{misc::NP_Option, NP_Value};
use json::*;
use crate::error::NP_Error;


#[doc(hidden)]
pub enum NP_SchemaKinds {
    None,
    Scalar,
    Table { columns: Vec<Option<(u8, String, NP_Schema)>> },
    List { of: NP_Schema },
    Map { value: NP_Schema },
    Enum { choices: Vec<String> },
    Tuple { values: Vec<NP_Schema>}
}

/*
#[derive(Debug)]
pub enum NP_SchemaKinds {
    None,
    Utf8String,
    Bytes,
    Int8,
    Int16,
    Int32,
    Int64,
    Uint8,
    Uint16,
    Uint32,
    Uint64,
    Float,
    Double,
    Dec64,
    Boolean,
    Geo4,
    Geo8,
    Geo16,
    Uuid,
    Tid,
    Date,
    Table { columns: Vec<Option<(u8, String, NP_Schema)>> },
    List { of: NP_Schema },
    Map { value: NP_Schema },
    Enum { choices: Vec<String> },
    Tuple { values: Vec<NP_Schema>}
}
*/

#[doc(hidden)]
// These are just used for runtime type comparison, the type information is never stored in the buffer.
// When you cast a pointer to some type, this enum is used as comparing numbers is very efficient.
pub enum NP_TypeKeys {
    Any = 0,
    UTF8String = 1,
    Bytes = 2,
    Int8 = 3,
    Int16 = 4,
    Int32 = 5,
    Int64 = 6,
    Uint8 = 7,
    Uint16 = 8,
    Uint32 = 9,
    Uint64 = 10,
    Float = 11,
    Double = 12,
    Dec64 = 13,
    Boolean = 14,
    Geo = 15,
    Uuid = 16,
    Tid = 17,
    Date = 18,
    Enum = 19,
    Table = 20,
    Map = 21, 
    List = 22,
    Tuple = 23
}

#[doc(hidden)]
pub struct NP_Schema {
    pub kind: Box<NP_SchemaKinds>,
    pub type_data: (i64, String),
    pub type_state: i64
}

#[doc(hidden)]
pub struct NP_Types { }

impl<'a> NP_Types {
    pub fn do_check<T: NP_Value + Default + NP_ValueInto<'a>>(type_string: &str, json_schema: &JsonValue)-> std::result::Result<Option<NP_Schema>, NP_Error>{
        if T::is_type(type_string) {
            Ok(Some(NP_Schema { 
                kind: Box::new(NP_SchemaKinds::Scalar),
                type_data: T::type_idx(),
                type_state: T::schema_state(type_string, json_schema)?
            }))
        } else {
            Ok(None)
        }
    }

    pub fn get_type(type_string: &str, json_schema: &JsonValue)-> std::result::Result<NP_Schema, NP_Error> {

        let check = NP_Types::do_check::<NP_Any>(type_string, json_schema)?;
        match check { Some(x) => return Ok(x), None => {} };
    
        let check = NP_Types::do_check::<String>(type_string, json_schema)?;
        match check { Some(x) => return Ok(x), None => {} };

        let check = NP_Types::do_check::<NP_Bytes>(type_string, json_schema)?;
        match check { Some(x) => return Ok(x), None => {} };

        let check = NP_Types::do_check::<i8>(type_string, json_schema)?;
        match check { Some(x) => return Ok(x), None => {} };

        let check = NP_Types::do_check::<i16>(type_string, json_schema)?;
        match check { Some(x) => return Ok(x), None => {} };

        let check = NP_Types::do_check::<i32>(type_string, json_schema)?;
        match check { Some(x) => return Ok(x), None => {} };

        let check = NP_Types::do_check::<i64>(type_string, json_schema)?;
        match check { Some(x) => return Ok(x), None => {} };

        let check = NP_Types::do_check::<u8>(type_string, json_schema)?;
        match check { Some(x) => return Ok(x), None => {} };

        let check = NP_Types::do_check::<u16>(type_string, json_schema)?;
        match check { Some(x) => return Ok(x), None => {} };

        let check = NP_Types::do_check::<u32>(type_string, json_schema)?;
        match check { Some(x) => return Ok(x), None => {} };

        let check = NP_Types::do_check::<u64>(type_string, json_schema)?;
        match check { Some(x) => return Ok(x), None => {} };

        let check = NP_Types::do_check::<f32>(type_string, json_schema)?;
        match check { Some(x) => return Ok(x), None => {} };

        let check = NP_Types::do_check::<f64>(type_string, json_schema)?;
        match check { Some(x) => return Ok(x), None => {} };

        let check = NP_Types::do_check::<bool>(type_string, json_schema)?;
        match check { Some(x) => return Ok(x), None => {} };

        let check = NP_Types::do_check::<NP_Dec>(type_string, json_schema)?;
        match check { Some(x) => return Ok(x), None => {} };

        let check = NP_Types::do_check::<NP_Geo>(type_string, json_schema)?;
        match check { Some(x) => return Ok(x), None => {} };

        let check = NP_Types::do_check::<NP_TimeID>(type_string, json_schema)?;
        match check { Some(x) => return Ok(x), None => {} };
    
        let check = NP_Types::do_check::<NP_UUID>(type_string, json_schema)?;
        match check { Some(x) => return Ok(x), None => {} };

        let check = NP_Types::do_check::<NP_Date>(type_string, json_schema)?;
        match check { Some(x) => return Ok(x), None => {} };

        Err(NP_Error::new(format!("{} is not a valid type!", type_string).as_str()))
    }
}

#[doc(hidden)]
impl NP_Schema {

    pub fn blank() -> NP_Schema {

        NP_Schema {
            kind: Box::new(NP_SchemaKinds::None),
            type_data: (-1, "".to_owned()),
            type_state: 0
        }
    }

    pub fn from_json(json: JsonValue) -> std::result::Result<Self, NP_Error> {
        NP_Schema::validate_model(&json)
    }

    pub fn validate_model(json_schema: &JsonValue) -> std::result::Result<Self, NP_Error> {

        let type_string = json_schema["type"].as_str().unwrap_or("");

        if type_string.len() == 0 {
            return Err(NP_Error::new("Must declare a type for every schema!"));
        }


        // validate required properties are in place for each kind
        match type_string {
            "table" => {
                
                let mut columns: Vec<Option<(u8, String, NP_Schema)>> = vec![];

                for _x in 0..255 {
                    columns.push(None);
                }

                {
                    let borrowed_schema = json_schema;

                    if borrowed_schema["columns"].is_null() || borrowed_schema["columns"].is_array() == false {
                        return Err(NP_Error::new("Table kind requires 'columns' property as array!"));
                    }

                    let mut index = 0;
                    for column in borrowed_schema["columns"].members() {

                        let column_name = &column[0].to_string();

                        if column_name.len() == 0 {
                            return Err(NP_Error::new("Table kind requires all columns have a name!"));
                        }

                        let good_schema = NP_Schema::validate_model(&column[1])?;

                        let this_index = &column[1]["i"];

                        let use_index = if this_index.is_null() || this_index.is_number() == false {
                            index
                        } else {
                            this_index.as_usize().unwrap_or(index)
                        };

                        if use_index > 255 {
                            return Err(NP_Error::new("Table cannot have column index above 255!"));
                        }

                        match &columns[use_index] {
                            Some(_x) => {
                                return Err(NP_Error::new("Table column index numbering conflict!"));
                            },
                            None => {
                                columns[use_index] = Some((use_index as u8, column_name.to_string(), good_schema));
                            }
                        };

                        index += 1;
                    }
                }

                Ok(NP_Schema {
                    kind: Box::new(NP_SchemaKinds::Table { 
                        columns: columns 
                    }),
                    type_data: NP_Table::type_idx(),
                    type_state: 0
                })
            },
            "list" => {

                {
                    let borrowed_schema = json_schema;
                    if borrowed_schema["of"].is_null() || borrowed_schema["of"].is_object() == false {
                        return Err(NP_Error::new("List kind requires 'of' property as schema object!"));
                    }
                }

                Ok(NP_Schema {
                    kind: Box::new(NP_SchemaKinds::List { 
                        of: NP_Schema::validate_model(&json_schema["of"])? 
                    }),
                    type_data: NP_List::type_idx(),
                    type_state: 0
                })
            },
            "map" => {

                {
                    let borrowed_schema = json_schema;

                    if borrowed_schema["value"].is_null() || borrowed_schema["value"].is_object() == false {
                        return Err(NP_Error::new("Map kind requires 'value' property as schema object!"));
                    }
                }
                Ok(NP_Schema {
                    kind: Box::new(NP_SchemaKinds::Map { 
                        value: NP_Schema::validate_model(&json_schema["value"])?
                    }),
                    type_data: NP_Map::type_idx(),
                    type_state: 0
                })
            },
            "tuple" => {

                let mut schemas: Vec<NP_Schema> = vec![];

                {
                    let borrowed_schema = json_schema;

                    if borrowed_schema["values"].is_null() || borrowed_schema["values"].is_array() == false  {
                        return Err(NP_Error::new("Tuple type requires 'values' property as array of schema objects!"));
                    }

                    for schema in borrowed_schema["values"].members() {
                        let good_schema = NP_Schema::validate_model(schema)?;
                        schemas.push(good_schema);
                    }
                }
            
                Ok(NP_Schema {
                    kind: Box::new(NP_SchemaKinds::Tuple { 
                        values: schemas
                    }),
                    type_data: NP_Tuple::type_idx(),
                    type_state: 0
                })
            },
            "option" => { 

                let mut options: Vec<String> = vec![];

                {
                    let borrowed_schema = json_schema;

                    if borrowed_schema["options"].is_null() || borrowed_schema["options"].is_array() == false  {
                        return Err(NP_Error::new("Option kind requires 'options' property as array of choices!"));
                    }

                    for option in borrowed_schema["options"].members() {
                        options.push(option.to_string());
                    }
                }

                if options.len() > 255 {
                    return Err(NP_Error::new("Cannot have more than 255 choices for option type!"));
                }
                Ok(NP_Schema {
                    kind: Box::new(NP_SchemaKinds::Enum { 
                        choices: options
                    }),
                    type_data: NP_Option::type_idx(),
                    type_state: 0
                })
            },
            _ => {
                NP_Types::get_type(type_string, json_schema)
            }
        }
    }
}