chore: reorg
This commit is contained in:
21
__serialization/bytes/Cargo.lock
generated
Normal file
21
__serialization/bytes/Cargo.lock
generated
Normal file
@@ -0,0 +1,21 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "bytes"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bytes 0.5.6",
|
||||
"hex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bytes"
|
||||
version = "0.5.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38"
|
||||
|
||||
[[package]]
|
||||
name = "hex"
|
||||
version = "0.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "644f9158b2f133fd50f5fb3242878846d9eb792e445c893805ff0e3824006e35"
|
||||
11
__serialization/bytes/Cargo.toml
Normal file
11
__serialization/bytes/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "bytes"
|
||||
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]
|
||||
bytes = "0.5.6"
|
||||
hex = "0.4.2"
|
||||
24
__serialization/bytes/src/main.rs
Normal file
24
__serialization/bytes/src/main.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use bytes::{Bytes, BytesMut, Buf, BufMut};
|
||||
|
||||
fn main() {
|
||||
println!("Run #test_bytes()");
|
||||
test_bytes();
|
||||
println!("Run #test_bytes_mut()");
|
||||
test_bytes_mut();
|
||||
}
|
||||
|
||||
fn test_bytes() {
|
||||
let mut buf = Bytes::from(&b"1122334455"[..]);
|
||||
let v1 = buf.get_u16();
|
||||
let v2 = buf.get_u16();
|
||||
println!("v1: {}, v2: {}", v1, v2);
|
||||
}
|
||||
|
||||
fn test_bytes_mut() {
|
||||
let mut buf = BytesMut::with_capacity(2);
|
||||
println!("[{}], len: {}", hex::encode(buf.bytes()), buf.len());
|
||||
buf.put_u64(112233445566778899);
|
||||
println!("[{}], len: {}", hex::encode(buf.bytes()), buf.len());
|
||||
buf.put_u16(1);
|
||||
println!("[{}], len: {}", hex::encode(buf.bytes()), buf.len());
|
||||
}
|
||||
87
__serialization/csv/Cargo.lock
generated
Normal file
87
__serialization/csv/Cargo.lock
generated
Normal file
@@ -0,0 +1,87 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "bstr"
|
||||
version = "0.2.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2889e6d50f394968c8bf4240dc3f2a7eb4680844d27308f798229ac9d4725f41"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de"
|
||||
|
||||
[[package]]
|
||||
name = "csv"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"csv 1.1.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "csv"
|
||||
version = "1.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "00affe7f6ab566df61b4be3ce8cf16bc2576bca0963ceb0955e45d514bf9a279"
|
||||
dependencies = [
|
||||
"bstr",
|
||||
"csv-core",
|
||||
"itoa",
|
||||
"ryu",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "csv-core"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "0.4.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8b7a7c0c47db5545ed3fef7468ee7bb5b74691498139e4b3f6a20685dc6dd8e"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae1ded71d66a4a97f5e961fd0cb25a5f366a42a41570d16a763a69c092c26ae4"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ed3d612bc64430efeb3f7ee6ef26d590dce0c43249217bddc62112540c7941e1"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.106"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "36df6ac6412072f67cf767ebbde4133a5b2e88e76dc6187fa7104cd16f783399"
|
||||
11
__serialization/csv/Cargo.toml
Normal file
11
__serialization/csv/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "csv"
|
||||
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]
|
||||
csv = "1.1.3"
|
||||
|
||||
2
__serialization/csv/samle.csv
Normal file
2
__serialization/csv/samle.csv
Normal file
@@ -0,0 +1,2 @@
|
||||
a,b
|
||||
1,2
|
||||
|
26
__serialization/csv/src/main.rs
Normal file
26
__serialization/csv/src/main.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
fn get_csv_u8_slice() -> Vec<u8> {
|
||||
r#"a,b,c
|
||||
111,222,333
|
||||
china,usa,england"#.as_bytes().to_vec()
|
||||
}
|
||||
|
||||
// fn example() -> Result<(), Box<dyn Error>> {
|
||||
// let mut rdr = csv::Reader::from_reader(io::stdin());
|
||||
// for result in rdr.records() {
|
||||
// // Result<StringRecord, Error>
|
||||
// let record = result?;
|
||||
// println!("{:?}", record);
|
||||
// }
|
||||
// Ok(())
|
||||
// }
|
||||
|
||||
// https://crates.io/crates/csv
|
||||
fn main() {
|
||||
let csv_u8_slice: &[u8] = &get_csv_u8_slice()[..];
|
||||
let mut rdr = csv::Reader::from_reader(csv_u8_slice);
|
||||
for result in rdr.records() {
|
||||
if let Ok(r) = result {
|
||||
println!("{:?}", r);
|
||||
}
|
||||
}
|
||||
}
|
||||
72
__serialization/encoding/Cargo.lock
generated
Normal file
72
__serialization/encoding/Cargo.lock
generated
Normal file
@@ -0,0 +1,72 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "encoding"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"encoding 0.2.33",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "encoding"
|
||||
version = "0.2.33"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6b0d943856b990d12d3b55b359144ff341533e516d94098b1d3fc1ac666d36ec"
|
||||
dependencies = [
|
||||
"encoding-index-japanese",
|
||||
"encoding-index-korean",
|
||||
"encoding-index-simpchinese",
|
||||
"encoding-index-singlebyte",
|
||||
"encoding-index-tradchinese",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "encoding-index-japanese"
|
||||
version = "1.20141219.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "04e8b2ff42e9a05335dbf8b5c6f7567e5591d0d916ccef4e0b1710d32a0d0c91"
|
||||
dependencies = [
|
||||
"encoding_index_tests",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "encoding-index-korean"
|
||||
version = "1.20141219.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4dc33fb8e6bcba213fe2f14275f0963fd16f0a02c878e3095ecfdf5bee529d81"
|
||||
dependencies = [
|
||||
"encoding_index_tests",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "encoding-index-simpchinese"
|
||||
version = "1.20141219.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d87a7194909b9118fc707194baa434a4e3b0fb6a5a757c73c3adb07aa25031f7"
|
||||
dependencies = [
|
||||
"encoding_index_tests",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "encoding-index-singlebyte"
|
||||
version = "1.20141219.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3351d5acffb224af9ca265f435b859c7c01537c0849754d3db3fdf2bfe2ae84a"
|
||||
dependencies = [
|
||||
"encoding_index_tests",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "encoding-index-tradchinese"
|
||||
version = "1.20141219.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fd0e20d5688ce3cab59eb3ef3a2083a5c77bf496cb798dc6fcdb75f323890c18"
|
||||
dependencies = [
|
||||
"encoding_index_tests",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "encoding_index_tests"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569"
|
||||
11
__serialization/encoding/Cargo.toml
Normal file
11
__serialization/encoding/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "encoding"
|
||||
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]
|
||||
encoding = "0.2.33"
|
||||
|
||||
30
__serialization/encoding/src/main.rs
Normal file
30
__serialization/encoding/src/main.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
use encoding::{
|
||||
all::GBK,
|
||||
Encoding,
|
||||
EncoderTrap,
|
||||
DecoderTrap,
|
||||
};
|
||||
|
||||
fn gbk_to_utf8_bytes(b: &[u8]) -> Option<Vec<u8>> {
|
||||
match GBK.decode(b, DecoderTrap::Strict) {
|
||||
Err(_) => None,
|
||||
Ok(s) => Some(s.as_bytes().to_vec()),
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let h_china = GBK.encode("Hello 中国", EncoderTrap::Strict)?;
|
||||
|
||||
let d_china = GBK.decode(&h_china, DecoderTrap::Strict)?;
|
||||
|
||||
println!("Encoding name: {}, whatwg name: {}", GBK.name(), GBK.whatwg_name().unwrap_or("None"));
|
||||
println!("{:?}", h_china);
|
||||
println!("{:?}", d_china);
|
||||
|
||||
let utf8_vec = gbk_to_utf8_bytes(&h_china[..]);
|
||||
if let Some(u8_vec) = utf8_vec {
|
||||
println!("{:?}", String::from_utf8(u8_vec)?);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
30
__serialization/flatbuffers/Cargo.lock
generated
Normal file
30
__serialization/flatbuffers/Cargo.lock
generated
Normal file
@@ -0,0 +1,30 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "flatbuffers"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"flatbuffers 0.6.1",
|
||||
"hex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "flatbuffers"
|
||||
version = "0.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a788f068dd10687940565bf4b5480ee943176cbd114b12e811074bcf7c04e4b9"
|
||||
dependencies = [
|
||||
"smallvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hex"
|
||||
version = "0.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "644f9158b2f133fd50f5fb3242878846d9eb792e445c893805ff0e3824006e35"
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c7cb5678e1615754284ec264d9bb5b4c27d2018577fd90ac0ceb578591ed5ee4"
|
||||
11
__serialization/flatbuffers/Cargo.toml
Normal file
11
__serialization/flatbuffers/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "flatbuffers"
|
||||
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]
|
||||
flatbuffers = "0.6.1"
|
||||
hex = "0.4.2"
|
||||
41
__serialization/flatbuffers/README.md
Normal file
41
__serialization/flatbuffers/README.md
Normal file
@@ -0,0 +1,41 @@
|
||||
|
||||
|
||||
# Install `flatc`
|
||||
|
||||
## Install `cmake`
|
||||
|
||||
https://cmake.org/install/
|
||||
|
||||
## Get `flatbuffers`
|
||||
|
||||
```shell
|
||||
% git clone https://github.com/google/flatbuffers.git
|
||||
```
|
||||
|
||||
## Make
|
||||
|
||||
```shell
|
||||
% cmake -G "Unix Makefiles"
|
||||
% make
|
||||
% make install
|
||||
```
|
||||
|
||||
```shell
|
||||
% flatc --version
|
||||
```
|
||||
|
||||
# Compile `fbs`
|
||||
|
||||
```shell
|
||||
% flatc --rust file.fbs
|
||||
```
|
||||
|
||||
|
||||
----
|
||||
|
||||
FlatBuffers Binary Format
|
||||
|
||||
* https://qiita.com/termoshtt/items/c337cd67e678bac2c795#flatbuffers-binary-format
|
||||
* https://github.com/dvidelabs/flatcc/blob/master/doc/binary-format.md
|
||||
* https://halfrost.com/flatbuffers_encode/
|
||||
|
||||
34
__serialization/flatbuffers/monster.fbs
Normal file
34
__serialization/flatbuffers/monster.fbs
Normal file
@@ -0,0 +1,34 @@
|
||||
// Example IDL file for our monster's schema.
|
||||
// https://google.github.io/flatbuffers/flatbuffers_guide_tutorial.html
|
||||
|
||||
namespace MyGame.Sample;
|
||||
|
||||
enum Color:byte { Red = 0, Green, Blue = 2 }
|
||||
|
||||
union Equipment { Weapon } // Optionally add more tables.
|
||||
|
||||
struct Vec3 {
|
||||
x:float;
|
||||
y:float;
|
||||
z:float;
|
||||
}
|
||||
|
||||
table Monster {
|
||||
pos:Vec3; // Struct.
|
||||
mana:short = 150;
|
||||
hp:short = 100;
|
||||
name:string;
|
||||
friendly:bool = false (deprecated);
|
||||
inventory:[ubyte]; // Vector of scalars.
|
||||
color:Color = Blue; // Enum.
|
||||
weapons:[Weapon]; // Vector of tables.
|
||||
equipped:Equipment; // Union.
|
||||
path:[Vec3]; // Vector of structs.
|
||||
}
|
||||
|
||||
table Weapon {
|
||||
name:string;
|
||||
damage:short;
|
||||
}
|
||||
|
||||
root_type Monster;
|
||||
57
__serialization/flatbuffers/src/main.rs
Normal file
57
__serialization/flatbuffers/src/main.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
#[allow(dead_code, unused_imports)]
|
||||
mod monster_generated;
|
||||
|
||||
use flatbuffers::FlatBufferBuilder;
|
||||
use monster_generated::my_game::sample::Monster;
|
||||
use monster_generated::my_game::sample::MonsterArgs;
|
||||
|
||||
// https://github.com/frol/flatc-rust/blob/master/examples/tutorial/src/main.rs
|
||||
fn main() {
|
||||
let mut fbb = FlatBufferBuilder::new();
|
||||
|
||||
let name = fbb.create_string("Sample Name!");
|
||||
|
||||
let monster = Monster::create(&mut fbb, &MonsterArgs {
|
||||
mana: 150,
|
||||
hp: 80,
|
||||
name: Some(name),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
fbb.finish(monster, None);
|
||||
|
||||
let buff = fbb.finished_data();
|
||||
|
||||
println!("FlatBuffers: {}", hex::encode(&buff));
|
||||
|
||||
let monster_2 = flatbuffers::get_root::<Monster>(&buff);
|
||||
println!("Monster hp: {}", monster_2.hp());
|
||||
println!("Monster name: {:?}", monster_2.name());
|
||||
|
||||
let offset_to_body = u32::from_le_bytes(get_4bytes(&buff));
|
||||
println!("Offset to body : {:>4}, hex: {}", offset_to_body, hex::encode(&get_4bytes(&buff)));
|
||||
let xx = &buff[4..offset_to_body as usize];
|
||||
println!("{}", hex::encode(xx));
|
||||
let offset_to_vtable = u32::from_le_bytes(get_4bytes(&buff[offset_to_body as usize..]));
|
||||
println!("Offset to vtable: {:>4}, hex: {}", offset_to_vtable, hex::encode(get_4bytes(&buff[offset_to_body as usize..])));
|
||||
|
||||
let a1 = u16::from_le_bytes(get_2bytes(&buff[offset_to_body as usize..]));
|
||||
let a2 = u16::from_le_bytes(get_2bytes(&buff[(offset_to_body + 2) as usize..]));
|
||||
println!("a1: {}, a2: {}", a1, a2);
|
||||
}
|
||||
|
||||
fn get_2bytes(buff: &[u8]) -> [u8; 2] {
|
||||
let mut ret = [0_u8; 2];
|
||||
ret[0] = buff[0];
|
||||
ret[1] = buff[1];
|
||||
ret
|
||||
}
|
||||
|
||||
fn get_4bytes(buff: &[u8]) -> [u8; 4] {
|
||||
let mut ret = [0_u8; 4];
|
||||
ret[0] = buff[0];
|
||||
ret[1] = buff[1];
|
||||
ret[2] = buff[2];
|
||||
ret[3] = buff[3];
|
||||
ret
|
||||
}
|
||||
522
__serialization/flatbuffers/src/monster_generated.rs
Normal file
522
__serialization/flatbuffers/src/monster_generated.rs
Normal file
@@ -0,0 +1,522 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
|
||||
use std::mem;
|
||||
use std::cmp::Ordering;
|
||||
|
||||
extern crate flatbuffers;
|
||||
use self::flatbuffers::EndianScalar;
|
||||
|
||||
#[allow(unused_imports, dead_code)]
|
||||
pub mod my_game {
|
||||
|
||||
use std::mem;
|
||||
use std::cmp::Ordering;
|
||||
|
||||
extern crate flatbuffers;
|
||||
use self::flatbuffers::EndianScalar;
|
||||
#[allow(unused_imports, dead_code)]
|
||||
pub mod sample {
|
||||
|
||||
use std::mem;
|
||||
use std::cmp::Ordering;
|
||||
|
||||
extern crate flatbuffers;
|
||||
use self::flatbuffers::EndianScalar;
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
#[repr(i8)]
|
||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
||||
pub enum Color {
|
||||
Red = 0,
|
||||
Green = 1,
|
||||
Blue = 2,
|
||||
|
||||
}
|
||||
|
||||
pub const ENUM_MIN_COLOR: i8 = 0;
|
||||
pub const ENUM_MAX_COLOR: i8 = 2;
|
||||
|
||||
impl<'a> flatbuffers::Follow<'a> for Color {
|
||||
type Inner = Self;
|
||||
#[inline]
|
||||
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
flatbuffers::read_scalar_at::<Self>(buf, loc)
|
||||
}
|
||||
}
|
||||
|
||||
impl flatbuffers::EndianScalar for Color {
|
||||
#[inline]
|
||||
fn to_little_endian(self) -> Self {
|
||||
let n = i8::to_le(self as i8);
|
||||
let p = &n as *const i8 as *const Color;
|
||||
unsafe { *p }
|
||||
}
|
||||
#[inline]
|
||||
fn from_little_endian(self) -> Self {
|
||||
let n = i8::from_le(self as i8);
|
||||
let p = &n as *const i8 as *const Color;
|
||||
unsafe { *p }
|
||||
}
|
||||
}
|
||||
|
||||
impl flatbuffers::Push for Color {
|
||||
type Output = Color;
|
||||
#[inline]
|
||||
fn push(&self, dst: &mut [u8], _rest: &[u8]) {
|
||||
flatbuffers::emplace_scalar::<Color>(dst, *self);
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
pub const ENUM_VALUES_COLOR:[Color; 3] = [
|
||||
Color::Red,
|
||||
Color::Green,
|
||||
Color::Blue
|
||||
];
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
pub const ENUM_NAMES_COLOR:[&'static str; 3] = [
|
||||
"Red",
|
||||
"Green",
|
||||
"Blue"
|
||||
];
|
||||
|
||||
pub fn enum_name_color(e: Color) -> &'static str {
|
||||
let index = e as i8;
|
||||
ENUM_NAMES_COLOR[index as usize]
|
||||
}
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
#[repr(u8)]
|
||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
||||
pub enum Equipment {
|
||||
NONE = 0,
|
||||
Weapon = 1,
|
||||
|
||||
}
|
||||
|
||||
pub const ENUM_MIN_EQUIPMENT: u8 = 0;
|
||||
pub const ENUM_MAX_EQUIPMENT: u8 = 1;
|
||||
|
||||
impl<'a> flatbuffers::Follow<'a> for Equipment {
|
||||
type Inner = Self;
|
||||
#[inline]
|
||||
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
flatbuffers::read_scalar_at::<Self>(buf, loc)
|
||||
}
|
||||
}
|
||||
|
||||
impl flatbuffers::EndianScalar for Equipment {
|
||||
#[inline]
|
||||
fn to_little_endian(self) -> Self {
|
||||
let n = u8::to_le(self as u8);
|
||||
let p = &n as *const u8 as *const Equipment;
|
||||
unsafe { *p }
|
||||
}
|
||||
#[inline]
|
||||
fn from_little_endian(self) -> Self {
|
||||
let n = u8::from_le(self as u8);
|
||||
let p = &n as *const u8 as *const Equipment;
|
||||
unsafe { *p }
|
||||
}
|
||||
}
|
||||
|
||||
impl flatbuffers::Push for Equipment {
|
||||
type Output = Equipment;
|
||||
#[inline]
|
||||
fn push(&self, dst: &mut [u8], _rest: &[u8]) {
|
||||
flatbuffers::emplace_scalar::<Equipment>(dst, *self);
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
pub const ENUM_VALUES_EQUIPMENT:[Equipment; 2] = [
|
||||
Equipment::NONE,
|
||||
Equipment::Weapon
|
||||
];
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
pub const ENUM_NAMES_EQUIPMENT:[&'static str; 2] = [
|
||||
"NONE",
|
||||
"Weapon"
|
||||
];
|
||||
|
||||
pub fn enum_name_equipment(e: Equipment) -> &'static str {
|
||||
let index = e as u8;
|
||||
ENUM_NAMES_EQUIPMENT[index as usize]
|
||||
}
|
||||
|
||||
pub struct EquipmentUnionTableOffset {}
|
||||
// struct Vec3, aligned to 4
|
||||
#[repr(C, align(4))]
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct Vec3 {
|
||||
x_: f32,
|
||||
y_: f32,
|
||||
z_: f32,
|
||||
} // pub struct Vec3
|
||||
impl flatbuffers::SafeSliceAccess for Vec3 {}
|
||||
impl<'a> flatbuffers::Follow<'a> for Vec3 {
|
||||
type Inner = &'a Vec3;
|
||||
#[inline]
|
||||
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
<&'a Vec3>::follow(buf, loc)
|
||||
}
|
||||
}
|
||||
impl<'a> flatbuffers::Follow<'a> for &'a Vec3 {
|
||||
type Inner = &'a Vec3;
|
||||
#[inline]
|
||||
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
flatbuffers::follow_cast_ref::<Vec3>(buf, loc)
|
||||
}
|
||||
}
|
||||
impl<'b> flatbuffers::Push for Vec3 {
|
||||
type Output = Vec3;
|
||||
#[inline]
|
||||
fn push(&self, dst: &mut [u8], _rest: &[u8]) {
|
||||
let src = unsafe {
|
||||
::std::slice::from_raw_parts(self as *const Vec3 as *const u8, Self::size())
|
||||
};
|
||||
dst.copy_from_slice(src);
|
||||
}
|
||||
}
|
||||
impl<'b> flatbuffers::Push for &'b Vec3 {
|
||||
type Output = Vec3;
|
||||
|
||||
#[inline]
|
||||
fn push(&self, dst: &mut [u8], _rest: &[u8]) {
|
||||
let src = unsafe {
|
||||
::std::slice::from_raw_parts(*self as *const Vec3 as *const u8, Self::size())
|
||||
};
|
||||
dst.copy_from_slice(src);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl Vec3 {
|
||||
pub fn new<'a>(_x: f32, _y: f32, _z: f32) -> Self {
|
||||
Vec3 {
|
||||
x_: _x.to_little_endian(),
|
||||
y_: _y.to_little_endian(),
|
||||
z_: _z.to_little_endian(),
|
||||
|
||||
}
|
||||
}
|
||||
pub fn x<'a>(&'a self) -> f32 {
|
||||
self.x_.from_little_endian()
|
||||
}
|
||||
pub fn y<'a>(&'a self) -> f32 {
|
||||
self.y_.from_little_endian()
|
||||
}
|
||||
pub fn z<'a>(&'a self) -> f32 {
|
||||
self.z_.from_little_endian()
|
||||
}
|
||||
}
|
||||
|
||||
pub enum MonsterOffset {}
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
|
||||
pub struct Monster<'a> {
|
||||
pub _tab: flatbuffers::Table<'a>,
|
||||
}
|
||||
|
||||
impl<'a> flatbuffers::Follow<'a> for Monster<'a> {
|
||||
type Inner = Monster<'a>;
|
||||
#[inline]
|
||||
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
Self {
|
||||
_tab: flatbuffers::Table { buf: buf, loc: loc },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Monster<'a> {
|
||||
#[inline]
|
||||
pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
|
||||
Monster {
|
||||
_tab: table,
|
||||
}
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>(
|
||||
_fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>,
|
||||
args: &'args MonsterArgs<'args>) -> flatbuffers::WIPOffset<Monster<'bldr>> {
|
||||
let mut builder = MonsterBuilder::new(_fbb);
|
||||
if let Some(x) = args.path { builder.add_path(x); }
|
||||
if let Some(x) = args.equipped { builder.add_equipped(x); }
|
||||
if let Some(x) = args.weapons { builder.add_weapons(x); }
|
||||
if let Some(x) = args.inventory { builder.add_inventory(x); }
|
||||
if let Some(x) = args.name { builder.add_name(x); }
|
||||
if let Some(x) = args.pos { builder.add_pos(x); }
|
||||
builder.add_hp(args.hp);
|
||||
builder.add_mana(args.mana);
|
||||
builder.add_equipped_type(args.equipped_type);
|
||||
builder.add_color(args.color);
|
||||
builder.finish()
|
||||
}
|
||||
|
||||
pub const VT_POS: flatbuffers::VOffsetT = 4;
|
||||
pub const VT_MANA: flatbuffers::VOffsetT = 6;
|
||||
pub const VT_HP: flatbuffers::VOffsetT = 8;
|
||||
pub const VT_NAME: flatbuffers::VOffsetT = 10;
|
||||
pub const VT_INVENTORY: flatbuffers::VOffsetT = 14;
|
||||
pub const VT_COLOR: flatbuffers::VOffsetT = 16;
|
||||
pub const VT_WEAPONS: flatbuffers::VOffsetT = 18;
|
||||
pub const VT_EQUIPPED_TYPE: flatbuffers::VOffsetT = 20;
|
||||
pub const VT_EQUIPPED: flatbuffers::VOffsetT = 22;
|
||||
pub const VT_PATH: flatbuffers::VOffsetT = 24;
|
||||
|
||||
#[inline]
|
||||
pub fn pos(&self) -> Option<&'a Vec3> {
|
||||
self._tab.get::<Vec3>(Monster::VT_POS, None)
|
||||
}
|
||||
#[inline]
|
||||
pub fn mana(&self) -> i16 {
|
||||
self._tab.get::<i16>(Monster::VT_MANA, Some(150)).unwrap()
|
||||
}
|
||||
#[inline]
|
||||
pub fn hp(&self) -> i16 {
|
||||
self._tab.get::<i16>(Monster::VT_HP, Some(100)).unwrap()
|
||||
}
|
||||
#[inline]
|
||||
pub fn name(&self) -> Option<&'a str> {
|
||||
self._tab.get::<flatbuffers::ForwardsUOffset<&str>>(Monster::VT_NAME, None)
|
||||
}
|
||||
#[inline]
|
||||
pub fn inventory(&self) -> Option<&'a [u8]> {
|
||||
self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<'a, u8>>>(Monster::VT_INVENTORY, None).map(|v| v.safe_slice())
|
||||
}
|
||||
#[inline]
|
||||
pub fn color(&self) -> Color {
|
||||
self._tab.get::<Color>(Monster::VT_COLOR, Some(Color::Blue)).unwrap()
|
||||
}
|
||||
#[inline]
|
||||
pub fn weapons(&self) -> Option<flatbuffers::Vector<'a, flatbuffers::ForwardsUOffset<Weapon<'a>>>> {
|
||||
self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<flatbuffers::ForwardsUOffset<Weapon<'a>>>>>(Monster::VT_WEAPONS, None)
|
||||
}
|
||||
#[inline]
|
||||
pub fn equipped_type(&self) -> Equipment {
|
||||
self._tab.get::<Equipment>(Monster::VT_EQUIPPED_TYPE, Some(Equipment::NONE)).unwrap()
|
||||
}
|
||||
#[inline]
|
||||
pub fn equipped(&self) -> Option<flatbuffers::Table<'a>> {
|
||||
self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Table<'a>>>(Monster::VT_EQUIPPED, None)
|
||||
}
|
||||
#[inline]
|
||||
pub fn path(&self) -> Option<&'a [Vec3]> {
|
||||
self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<Vec3>>>(Monster::VT_PATH, None).map(|v| v.safe_slice() )
|
||||
}
|
||||
#[inline]
|
||||
#[allow(non_snake_case)]
|
||||
pub fn equipped_as_weapon(&self) -> Option<Weapon<'a>> {
|
||||
if self.equipped_type() == Equipment::Weapon {
|
||||
self.equipped().map(|u| Weapon::init_from_table(u))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub struct MonsterArgs<'a> {
|
||||
pub pos: Option<&'a Vec3>,
|
||||
pub mana: i16,
|
||||
pub hp: i16,
|
||||
pub name: Option<flatbuffers::WIPOffset<&'a str>>,
|
||||
pub inventory: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a , u8>>>,
|
||||
pub color: Color,
|
||||
pub weapons: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a , flatbuffers::ForwardsUOffset<Weapon<'a >>>>>,
|
||||
pub equipped_type: Equipment,
|
||||
pub equipped: Option<flatbuffers::WIPOffset<flatbuffers::UnionWIPOffset>>,
|
||||
pub path: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a , Vec3>>>,
|
||||
}
|
||||
impl<'a> Default for MonsterArgs<'a> {
|
||||
#[inline]
|
||||
fn default() -> Self {
|
||||
MonsterArgs {
|
||||
pos: None,
|
||||
mana: 150,
|
||||
hp: 100,
|
||||
name: None,
|
||||
inventory: None,
|
||||
color: Color::Blue,
|
||||
weapons: None,
|
||||
equipped_type: Equipment::NONE,
|
||||
equipped: None,
|
||||
path: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
pub struct MonsterBuilder<'a: 'b, 'b> {
|
||||
fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>,
|
||||
start_: flatbuffers::WIPOffset<flatbuffers::TableUnfinishedWIPOffset>,
|
||||
}
|
||||
impl<'a: 'b, 'b> MonsterBuilder<'a, 'b> {
|
||||
#[inline]
|
||||
pub fn add_pos(&mut self, pos: &'b Vec3) {
|
||||
self.fbb_.push_slot_always::<&Vec3>(Monster::VT_POS, pos);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_mana(&mut self, mana: i16) {
|
||||
self.fbb_.push_slot::<i16>(Monster::VT_MANA, mana, 150);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_hp(&mut self, hp: i16) {
|
||||
self.fbb_.push_slot::<i16>(Monster::VT_HP, hp, 100);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_name(&mut self, name: flatbuffers::WIPOffset<&'b str>) {
|
||||
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(Monster::VT_NAME, name);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_inventory(&mut self, inventory: flatbuffers::WIPOffset<flatbuffers::Vector<'b , u8>>) {
|
||||
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(Monster::VT_INVENTORY, inventory);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_color(&mut self, color: Color) {
|
||||
self.fbb_.push_slot::<Color>(Monster::VT_COLOR, color, Color::Blue);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_weapons(&mut self, weapons: flatbuffers::WIPOffset<flatbuffers::Vector<'b , flatbuffers::ForwardsUOffset<Weapon<'b >>>>) {
|
||||
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(Monster::VT_WEAPONS, weapons);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_equipped_type(&mut self, equipped_type: Equipment) {
|
||||
self.fbb_.push_slot::<Equipment>(Monster::VT_EQUIPPED_TYPE, equipped_type, Equipment::NONE);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_equipped(&mut self, equipped: flatbuffers::WIPOffset<flatbuffers::UnionWIPOffset>) {
|
||||
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(Monster::VT_EQUIPPED, equipped);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_path(&mut self, path: flatbuffers::WIPOffset<flatbuffers::Vector<'b , Vec3>>) {
|
||||
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(Monster::VT_PATH, path);
|
||||
}
|
||||
#[inline]
|
||||
pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> MonsterBuilder<'a, 'b> {
|
||||
let start = _fbb.start_table();
|
||||
MonsterBuilder {
|
||||
fbb_: _fbb,
|
||||
start_: start,
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
pub fn finish(self) -> flatbuffers::WIPOffset<Monster<'a>> {
|
||||
let o = self.fbb_.end_table(self.start_);
|
||||
flatbuffers::WIPOffset::new(o.value())
|
||||
}
|
||||
}
|
||||
|
||||
pub enum WeaponOffset {}
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
|
||||
pub struct Weapon<'a> {
|
||||
pub _tab: flatbuffers::Table<'a>,
|
||||
}
|
||||
|
||||
impl<'a> flatbuffers::Follow<'a> for Weapon<'a> {
|
||||
type Inner = Weapon<'a>;
|
||||
#[inline]
|
||||
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
Self {
|
||||
_tab: flatbuffers::Table { buf: buf, loc: loc },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Weapon<'a> {
|
||||
#[inline]
|
||||
pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
|
||||
Weapon {
|
||||
_tab: table,
|
||||
}
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>(
|
||||
_fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>,
|
||||
args: &'args WeaponArgs<'args>) -> flatbuffers::WIPOffset<Weapon<'bldr>> {
|
||||
let mut builder = WeaponBuilder::new(_fbb);
|
||||
if let Some(x) = args.name { builder.add_name(x); }
|
||||
builder.add_damage(args.damage);
|
||||
builder.finish()
|
||||
}
|
||||
|
||||
pub const VT_NAME: flatbuffers::VOffsetT = 4;
|
||||
pub const VT_DAMAGE: flatbuffers::VOffsetT = 6;
|
||||
|
||||
#[inline]
|
||||
pub fn name(&self) -> Option<&'a str> {
|
||||
self._tab.get::<flatbuffers::ForwardsUOffset<&str>>(Weapon::VT_NAME, None)
|
||||
}
|
||||
#[inline]
|
||||
pub fn damage(&self) -> i16 {
|
||||
self._tab.get::<i16>(Weapon::VT_DAMAGE, Some(0)).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WeaponArgs<'a> {
|
||||
pub name: Option<flatbuffers::WIPOffset<&'a str>>,
|
||||
pub damage: i16,
|
||||
}
|
||||
impl<'a> Default for WeaponArgs<'a> {
|
||||
#[inline]
|
||||
fn default() -> Self {
|
||||
WeaponArgs {
|
||||
name: None,
|
||||
damage: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
pub struct WeaponBuilder<'a: 'b, 'b> {
|
||||
fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>,
|
||||
start_: flatbuffers::WIPOffset<flatbuffers::TableUnfinishedWIPOffset>,
|
||||
}
|
||||
impl<'a: 'b, 'b> WeaponBuilder<'a, 'b> {
|
||||
#[inline]
|
||||
pub fn add_name(&mut self, name: flatbuffers::WIPOffset<&'b str>) {
|
||||
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(Weapon::VT_NAME, name);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_damage(&mut self, damage: i16) {
|
||||
self.fbb_.push_slot::<i16>(Weapon::VT_DAMAGE, damage, 0);
|
||||
}
|
||||
#[inline]
|
||||
pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> WeaponBuilder<'a, 'b> {
|
||||
let start = _fbb.start_table();
|
||||
WeaponBuilder {
|
||||
fbb_: _fbb,
|
||||
start_: start,
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
pub fn finish(self) -> flatbuffers::WIPOffset<Weapon<'a>> {
|
||||
let o = self.fbb_.end_table(self.start_);
|
||||
flatbuffers::WIPOffset::new(o.value())
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_root_as_monster<'a>(buf: &'a [u8]) -> Monster<'a> {
|
||||
flatbuffers::get_root::<Monster<'a>>(buf)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_size_prefixed_root_as_monster<'a>(buf: &'a [u8]) -> Monster<'a> {
|
||||
flatbuffers::get_size_prefixed_root::<Monster<'a>>(buf)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn finish_monster_buffer<'a, 'b>(
|
||||
fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>,
|
||||
root: flatbuffers::WIPOffset<Monster<'a>>) {
|
||||
fbb.finish(root, None);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn finish_size_prefixed_monster_buffer<'a, 'b>(fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>, root: flatbuffers::WIPOffset<Monster<'a>>) {
|
||||
fbb.finish_size_prefixed(root, None);
|
||||
}
|
||||
} // pub mod Sample
|
||||
} // pub mod MyGame
|
||||
|
||||
171
__serialization/serde_json/Cargo.lock
generated
Normal file
171
__serialization/serde_json/Cargo.lock
generated
Normal file
@@ -0,0 +1,171 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"num-integer 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "0.4.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.66"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.42"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.1.56"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.104"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde-json-test"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.104"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syn 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"ryu 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.1.42"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[metadata]
|
||||
"checksum autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
|
||||
"checksum chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "31850b4a4d6bae316f7a09e691c944c28299298837edc0a03f755618c23cbc01"
|
||||
"checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f"
|
||||
"checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558"
|
||||
"checksum num-integer 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "3f6ea62e9d81a77cd3ee9a2a5b9b609447857f3d358704331e4ef39eb247fcba"
|
||||
"checksum num-traits 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096"
|
||||
"checksum proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3acb317c6ff86a4e579dfa00fc5e6cca91ecbb4e7eb2df0468805b674eb88548"
|
||||
"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe"
|
||||
"checksum redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)" = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84"
|
||||
"checksum ryu 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bfa8506c1de11c9c4e4c38863ccbe02a305c8188e85a05a784c9e11e1c3910c8"
|
||||
"checksum serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "414115f25f818d7dfccec8ee535d76949ae78584fc4f79a6f45a904bf8ab4449"
|
||||
"checksum serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "128f9e303a5a29922045a830221b8f78ec74a5f544944f3d5984f8ec3895ef64"
|
||||
"checksum serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)" = "48c575e0cc52bdd09b47f330f646cf59afc586e9c4e3ccd6fc1f625b8ea1dad7"
|
||||
"checksum syn 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)" = "1e4ff033220a41d1a57d8125eab57bf5263783dfdcc18688b1dacc6ce9651ef8"
|
||||
"checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f"
|
||||
"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
|
||||
"checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"
|
||||
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
12
__serialization/serde_json/Cargo.toml
Normal file
12
__serialization/serde_json/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "serde-json-test"
|
||||
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]
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
chrono = "0.4.10"
|
||||
80
__serialization/serde_json/src/main.rs
Normal file
80
__serialization/serde_json/src/main.rs
Normal file
@@ -0,0 +1,80 @@
|
||||
#[macro_use]
|
||||
extern crate serde_json;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Result, Value};
|
||||
use chrono::{DateTime, Utc, TimeZone};
|
||||
|
||||
mod my_date_format {
|
||||
use chrono::{DateTime, Utc, TimeZone};
|
||||
use serde::{self, Deserialize, Serializer, Deserializer};
|
||||
|
||||
const FORMAT: &'static str = "%Y-%m-%d %H:%M:%S";
|
||||
|
||||
pub fn serialize<S>(date: &DateTime<Utc>, serializer: S ) -> Result<S::Ok, S::Error> where S: Serializer {
|
||||
let s = format!("{}", date.format(FORMAT));
|
||||
serializer.serialize_str(&s)
|
||||
}
|
||||
|
||||
pub fn deserialize<'de, D>(deserializer: D ) -> Result<DateTime<Utc>, D::Error> where D: Deserializer<'de> {
|
||||
let s = String::deserialize(deserializer)?;
|
||||
Utc.datetime_from_str(&s, FORMAT).map_err(serde::de::Error::custom)
|
||||
}
|
||||
}
|
||||
|
||||
// https://serde.rs/container-attrs.html
|
||||
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
|
||||
enum TestEnum {
|
||||
|
||||
#[serde(rename = "A")]
|
||||
TypeA,
|
||||
|
||||
#[serde(rename = "B")]
|
||||
TypeB,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
struct TestStruct {
|
||||
ty: TestEnum,
|
||||
#[serde(with = "my_date_format")]
|
||||
timestamp: DateTime<Utc>,
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let serialized = serde_json::to_string(&TestStruct{
|
||||
ty: TestEnum::TypeA,
|
||||
timestamp: Utc.datetime_from_str("2020-01-28 17:04:00", "%Y-%m-%d %H:%M:%S").map_err(serde::de::Error::custom)?,
|
||||
})?;
|
||||
|
||||
println!("{}", serialized);
|
||||
|
||||
let data = r#"
|
||||
{
|
||||
"name": "John Doe",
|
||||
"age": 43,
|
||||
"phones": [
|
||||
"+44 1234567",
|
||||
"+44 2345678"
|
||||
]
|
||||
}"#;
|
||||
let v: Value = serde_json::from_str(data)?;
|
||||
println!("{}", v);
|
||||
|
||||
let mut john = json!({
|
||||
"name": "John Doe",
|
||||
"age": 43,
|
||||
"phones": [
|
||||
"+44 1234567",
|
||||
"+44 2345678"
|
||||
]
|
||||
});
|
||||
println!("{}", &john);
|
||||
|
||||
if let Option::Some(ref mut m) = john.as_object_mut() {
|
||||
m.insert("test".into(), Value::Bool(true));
|
||||
m.insert("test2".into(), Value::String("hello world".into()));
|
||||
}
|
||||
println!("{}", john);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user