From ffaba9fe489690f9b8be16e58ae74a861e967373 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 27 Dec 2020 21:31:46 +0800 Subject: [PATCH] feat: add no_proto --- __serialization/no_proto/Cargo.lock | 14 ++++++++ __serialization/no_proto/Cargo.toml | 10 ++++++ __serialization/no_proto/README.md | 3 ++ __serialization/no_proto/src/main.rs | 54 ++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 __serialization/no_proto/Cargo.lock create mode 100644 __serialization/no_proto/Cargo.toml create mode 100644 __serialization/no_proto/README.md create mode 100644 __serialization/no_proto/src/main.rs diff --git a/__serialization/no_proto/Cargo.lock b/__serialization/no_proto/Cargo.lock new file mode 100644 index 0000000..e38c444 --- /dev/null +++ b/__serialization/no_proto/Cargo.lock @@ -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" diff --git a/__serialization/no_proto/Cargo.toml b/__serialization/no_proto/Cargo.toml new file mode 100644 index 0000000..36f1151 --- /dev/null +++ b/__serialization/no_proto/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "no_proto" +version = "0.1.0" +authors = ["Hatter Jiang "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +no_proto = "0.7.2" diff --git a/__serialization/no_proto/README.md b/__serialization/no_proto/README.md new file mode 100644 index 0000000..b4c4b42 --- /dev/null +++ b/__serialization/no_proto/README.md @@ -0,0 +1,3 @@ + +url +https://mp.weixin.qq.com/s/EWb4ep0RPopeiab8RiTxOQ diff --git a/__serialization/no_proto/src/main.rs b/__serialization/no_proto/src/main.rs new file mode 100644 index 0000000..023e8b8 --- /dev/null +++ b/__serialization/no_proto/src/main.rs @@ -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 = 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::(&["age"])?; + // returns default value from schema + assert_eq!(age, Some(0_u16)); + + // close again + let user_bytes: Vec = user_buffer.close(); + + println!("{:?}", user_bytes); + + Ok(()) +}