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
// #![deny(missing_docs, missing_debug_implementations, trivial_casts, trivial_numeric_casts, unused_results)]
#![allow(non_camel_case_types)]

//! ## High Performance Serialization Library
//! 
//! [Github](https://github.com/ClickSimply/NoProto)
//! [Crates.io](https://crates.io/crates/no_proto)
//! 
//! ### TODO: 
//! - [ ] Finish implementing Lists, Tuples & Maps
//! - [ ] Compaction
//! - [ ] Documentation
//! - [ ] Test
//! 
//! ### Features
//! - Nearly instant deserilization & serialization
//! - Schemas are dynamic/flexible at runtime
//! - Mutate/Update/Delete values in existing buffers
//! - Supports native data types
//! - Supports collection types (list, map, table & tuple)
//! - Supports deep nesting of collection types
//! 
//! NoProto allows you to store and mutate structured data with near zero overhead.  It's like JSON but faster, type safe and allows native types.  It's like Cap'N Proto/Flatbuffers except buffers and schemas are dynamic at runtime instead of requiring compilation.  
//! 
//! NoProto moves the cost of deserialization to the access methods instead of deserializing the entire object ahead of time. This makes it a perfect use case for things like database storage or file storage of structured data.
//! 
//! *Compared to FlatBuffers /Cap'N Proto*
//! - Schemas are dynamic at runtime, no compilation step
//! - Supports more types and better nested type support
//! - Mutate (add/delete/update) existing/imported buffers
//! 
//! *Compared to JSON*
//! - Has schemas / type safe
//! - Faster serialization & deserialization
//! - Supports raw bytes & other native types
//! 
//! *Compared to BSON*
//! - Faster serialization & deserialization
//! - Has schemas / type safe
//! - Supports much larger documents (4GB vs 16MB)
//! - Better collection support & more supported types
//! 
//! *Compared to Serde*
//! - Objects & schemas are dynamic at runtime
//! - Faster serialization & deserialization
//! 
//! #### Limitations
//! - Buffers cannot be larger than 2^32 bytes (~4GB).
//! - Tables & List collections cannot have more than 2^16 direct descendant child items (~16k).
//! - Enum/Option types are limited to 256 choices.
//! - Buffers are not validated or checked before deserializing.
//! 
//! # Quick Example
//! ```
//! use no_proto::error::NP_Error;
//! use no_proto::NP_Factory;
//! use no_proto::collection::table::NP_Table;
//! use no_proto::pointer::NP_Ptr;
//! 
//! // JSON is used to describe schema for the factory
//! // Each factory represents a single schema
//! // One factory can be used to serialize/deserialize any number of buffers
//! let user_factory = NP_Factory::new(r#"{
//!     "type": "table",
//!     "columns": [
//!         ["name",   {"type": "string"}],
//!         ["pass",   {"type": "string"}],
//!         ["age",    {"type": "uint16"}]
//!     ]
//! }"#)?;
//! 
//! // creating a new buffer from the `user_factory` schema
//! // user_buffer contains a deserialized Vec<u8> containing our data
//! let user_buffer: Vec<u8> = user_factory.new_buffer(None, |mut buffer| {
//!    
//!     // open the buffer to read or update values
//!     buffer.open(|root: NP_Ptr<NP_Table>| { // <- type cast the root
//!         
//!         // the root of our schema is a collection type (NP_Table), 
//!         // so we have to collapse the root pointer into the collection type.
//!         let mut table: NP_Table = root.into()?.unwrap();
//! 
//!         // Select a column and type cast it. Selected columns can be mutated or read from.
//!         let mut user_name = table.select::<String>("name")?;
//! 
//!         // set value of name column
//!         user_name.set("some name".to_owned())?;
//! 
//!         // select age column and set it's value
//!         let mut age = table.select::<u16>("age")?;
//!         age.set(75)?;
//!
//!         // done mutating/reading the buffer
//!         Ok(())
//!    })?;
//!    
//!    // close a buffer when we're done with it
//!    buffer.close()
//! })?;
//!  
//! // open the new buffer, `user_buffer`, we just created
//! // user_buffer_2 contains the deserialized Vec<u8>
//! let user_buffer_2: Vec<u8> = user_factory.load_buffer(user_buffer, |mut buffer| {
//! 
//!     // we can mutate and read the buffer here
//!     buffer.open(|root: NP_Ptr<NP_Table>| {
//!         
//!         // get the table root again
//!         let mut table = root.into()?.unwrap();
//! 
//!         // read the name column
//!         let mut user_name = table.select::<String>("name")?;
//!         assert_eq!(user_name.get()?, Some(String::from("some name")));
//! 
//!         // password value will be None since we haven't set it.
//!         let mut password = table.select::<String>("pass")?;
//!         assert_eq!(password.get()?, None);
//! 
//!         // read age value    
//!         let mut age = table.select::<u16>("age")?;
//!         assert_eq!(age.get()?, Some(75));    
//! 
//!         // done with the buffer
//!         Ok(())
//!    })?;
//!    
//!    // close a buffer when we're done with it
//!    buffer.close()
//! })?;
//! 
//! // we can now save user_buffer_2 to disk, 
//! // send it over the network, or whatever else is needed with the data
//! 
//! # Ok::<(), NP_Error>(()) 
//! ```
//! 
//! MIT License
//! 
//! Copyright (c) 2020 Scott Lott
//! 
//! Permission is hereby granted, free of charge, to any person obtaining a copy
//! of this software and associated documentation files (the "Software"), to deal
//! in the Software without restriction, including without limitation the rights
//! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//! copies of the Software, and to permit persons to whom the Software is
//! furnished to do so, subject to the following conditions:
//! 
//! The above copyright notice and this permission notice shall be included in all
//! copies or substantial portions of the Software.
//! 
//! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//! SOFTWARE.

pub mod pointer;
pub mod collection;
pub mod buffer;
pub mod schema;
pub mod error;
mod memory;

use crate::error::NP_Error;
use crate::schema::NP_Schema;
use buffer::NP_Buffer;

const PROTOCOL_VERSION: u8 = 0;


/// Factories allow you to serialize and deserialize buffers.
/// 
/// Each factory represents a schema, each factory can be reused for any number of buffers based on the factory's schema.
/// 
/// # Example
/// ```
/// 
/// 
/// ```
pub struct NP_Factory {
    schema: NP_Schema
}

impl NP_Factory {
    pub fn new(json_schema: &str) -> std::result::Result<NP_Factory, NP_Error> {

        match json::parse(json_schema) {
            Ok(x) => {
                Ok(NP_Factory {
                    schema:  NP_Schema::from_json(x)?
                })
            },
            Err(e) => {
                Err(NP_Error::new(format!("Error Parsing JSON Schema: {}", e.to_string()).as_str()))
            }
        }
    }

    pub fn new_buffer<F>(&self, capacity: Option<u32>, mut callback: F) -> std::result::Result<Vec<u8>, NP_Error>
        where F: FnMut(NP_Buffer) -> std::result::Result<Vec<u8>, NP_Error>
    {   
        callback(NP_Buffer::new(&self.schema, capacity))
    }

    pub fn load_buffer<F>(&self, buffer: Vec<u8>, mut callback: F) -> std::result::Result<Vec<u8>, NP_Error>
        where F: FnMut(NP_Buffer) -> std::result::Result<Vec<u8>, NP_Error>
    {   
        callback(NP_Buffer::load(&self.schema, buffer))
    }
}


#[cfg(test)]
mod tests {

    use crate::pointer::NP_Ptr;
    use crate::collection::table::NP_Table;
    use super::*;

    #[test]
    fn it_works() -> std::result::Result<(), NP_Error> {

        let factory: NP_Factory = NP_Factory::new(r#"{
            "type": "table",
            "columns": [
                ["userID",  {"type": "string"}],
                ["pass",    {"type": "string"}],
                ["age",     {"type": "uint16"}],
                ["address", {
                    "type": "table", 
                    "columns": [
                        ["street",  {"type": "string"}],
                        ["street2", {"type": "string"}],
                        ["city",    {"type": "string"}],
                        ["state",   {"type": "string"}],
                        ["zip",     {"type": "string"}],
                        ["country", {"type": "string"}]
                    ]
                }]
            ]
        }"#)?;

        let mut myvalue: Option<String> = None;

        let return_buffer = factory.new_buffer(None, |mut buffer| {

            // buffer.deep_set(".userID", "something".to_owned())?;

            // myvalue = buffer.deep_get::<String>(".userID")?;

            buffer.open(|root: NP_Ptr<NP_Table>| {
            
                let mut table = root.into()?.unwrap();

                let mut x = table.select::<String>("userID")?;
                x.set("username".to_owned())?;
        
                let mut x = table.select::<String>("pass")?;
                x.set("password123".to_owned())?;

                myvalue = x.get()?;

                let mut address = table.select::<NP_Table>("address")?.into()?.unwrap();
                address.select::<String>("street")?.set("13B Baker St".to_owned())?;
                address.select::<String>("city")?.set("London".to_owned())?;
                address.select::<String>("state")?.set("London".to_owned())?;
                address.select::<String>("country")?.set("UK".to_owned())?;

                let mut x = table.select::<u16>("age")?;
                x.set(1039)?;

                Ok(())
            })?;

            buffer.close()
        })?;

        let return_buffer_2 = factory.load_buffer(return_buffer, |mut buffer| {

            buffer.open(|root: NP_Ptr<NP_Table>| {
            
                let mut table = root.into()?.unwrap();

                let mut x = table.select::<String>("userID")?;
                println!("VALUE: {:?}", x.get()?);
        
                let mut x = table.select::<String>("pass")?;
                println!("VALUE 2: {:?}", x.get()?);

                println!("VALUE 3: {:?}", table.select::<u16>("age")?.get()?);

                let mut address = table.select::<NP_Table>("address")?.into()?.unwrap();
                let mut x = address.select::<String>("street")?;
                println!("VALUE 4: {:?}", x.get()?);

                Ok(())
            })?;

            buffer.close()
        })?;

        // println!("BYTES: {:?}", xx);

        println!("BYTES: {} {:?}", return_buffer_2.len(), return_buffer_2);

        assert_eq!(2 + 2, 4);

        Ok(())
    }
}