feat: add no_proto

This commit is contained in:
2020-12-27 21:31:46 +08:00
parent f0958ac291
commit ffaba9fe48
4 changed files with 81 additions and 0 deletions

14
__serialization/no_proto/Cargo.lock generated Normal file
View File

@@ -0,0 +1,14 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "no_proto"
version = "0.1.0"
dependencies = [
"no_proto 0.7.2",
]
[[package]]
name = "no_proto"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c3477477c67f7c4519591d94f8e7f3a6936a97773d0362b149f1f8e079c2140"

View File

@@ -0,0 +1,10 @@
[package]
name = "no_proto"
version = "0.1.0"
authors = ["Hatter Jiang <jht5945@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
no_proto = "0.7.2"

View File

@@ -0,0 +1,3 @@
url
https://mp.weixin.qq.com/s/EWb4ep0RPopeiab8RiTxOQ

View File

@@ -0,0 +1,54 @@
use no_proto::error::NP_Error;
use no_proto::NP_Factory;
fn main() -> Result<(), NP_Error> {
// 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"}],
["age", {"type": "u16", "default": 0}],
["tags", {"type": "list", "of": {
"type": "string"
}}]
]
}"#)?;
// create a new empty buffer
let mut user_buffer = user_factory.empty_buffer(None); // optional capacity, optional address size (u16 by default)
// set an internal value of the buffer, set the "name" column
user_buffer.set(&["name"], "Billy Joel")?;
// assign nested internal values, sets the first tag element
user_buffer.set(&["tags", "0"], "first tag")?;
// get an internal value of the buffer from the "name" column
let name = user_buffer.get::<&str>(&["name"])?;
assert_eq!(name, Some("Billy Joel"));
// close buffer and get internal bytes
let user_bytes: Vec<u8> = user_buffer.close();
// open the buffer again
let user_buffer = user_factory.open_buffer(user_bytes);
// get nested internal value, first tag from the tag list
let tag = user_buffer.get::<&str>(&["tags", "0"])?;
assert_eq!(tag, Some("first tag"));
// get nested internal value, the age field
let age = user_buffer.get::<u16>(&["age"])?;
// returns default value from schema
assert_eq!(age, Some(0_u16));
// close again
let user_bytes: Vec<u8> = user_buffer.close();
println!("{:?}", user_bytes);
Ok(())
}