add serialization prototyping
All checks were successful
Rust/sat-rs/pipeline/head This commit looks good

This commit is contained in:
Robin Müller 2024-04-26 10:01:29 +02:00
parent 312849bddb
commit b970154488
7 changed files with 292 additions and 0 deletions

1
serialization-prototyping/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

161
serialization-prototyping/Cargo.lock generated Normal file
View File

@ -0,0 +1,161 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "autocfg"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1fdabc7756949593fe60f30ec81974b613357de856987752631dea1e3394c80"
[[package]]
name = "byteorder"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "itoa"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
[[package]]
name = "msg-pack-test"
version = "0.1.0"
dependencies = [
"rmp-serde",
"rmpv",
"serde",
"serde_json",
]
[[package]]
name = "num-traits"
version = "0.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a"
dependencies = [
"autocfg",
]
[[package]]
name = "paste"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c"
[[package]]
name = "proc-macro2"
version = "1.0.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d1597b0c024618f09a9c3b8655b7e430397a36d23fdafec26d6965e9eec3eba"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rmp"
version = "0.8.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "228ed7c16fa39782c3b3468e974aec2795e9089153cd08ee2e9aefb3613334c4"
dependencies = [
"byteorder",
"num-traits",
"paste",
]
[[package]]
name = "rmp-serde"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "938a142ab806f18b88a97b0dea523d39e0fd730a064b035726adcfc58a8a5188"
dependencies = [
"byteorder",
"rmp",
"serde",
]
[[package]]
name = "rmpv"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e540282f11751956c82bc5529a7fb71b871b998fbf9cf06c2419b22e1b4350df"
dependencies = [
"num-traits",
"rmp",
"serde",
"serde_bytes",
]
[[package]]
name = "ryu"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1"
[[package]]
name = "serde"
version = "1.0.198"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9846a40c979031340571da2545a4e5b7c4163bdae79b301d5f86d03979451fcc"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_bytes"
version = "0.11.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b8497c313fd43ab992087548117643f6fcd935cbf36f176ffda0aacf9591734"
dependencies = [
"serde",
]
[[package]]
name = "serde_derive"
version = "1.0.198"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e88edab869b01783ba905e7d0153f9fc1a6505a96e4ad3018011eedb838566d9"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.116"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e17db7126d17feb94eb3fad46bf1a96b034e8aacbc2e775fe81505f8b0b2813"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "syn"
version = "2.0.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "909518bc7b1c9b779f1bbf07f2929d35af9f0f37e47c6e9ef7f9dddc1e1821f3"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"

View File

@ -0,0 +1,12 @@
[package]
name = "msg-pack-test"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rmp-serde = "1"
rmpv = { version = "1", features = ["with-serde"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1"

View File

@ -0,0 +1 @@
/venv

View File

@ -0,0 +1,22 @@
#!/usr/bin/env python3
from socket import AF_INET, SOCK_DGRAM, socket
import msgpack
nadine = {
"age": 24,
"name": "Nadine",
}
msg_pack_stuff = msgpack.packb(nadine)
assert msg_pack_stuff is not None
server_socket = socket(AF_INET, SOCK_DGRAM)
target_address = "localhost", 7301
bytes_sent = server_socket.sendto(msg_pack_stuff, target_address)
recv_back = server_socket.recv(4096)
print(f"recv back: {recv_back}")
unpacked = msgpack.unpackb(recv_back)
print(f"unpacked: {unpacked}")
# human_test = {:x for x in unpacked}
loaded_back = msgpack.loads(recv_back)
print(loaded_back)

View File

@ -0,0 +1 @@
msgpack==1.0.8

View File

@ -0,0 +1,94 @@
use rmp_serde::{Deserializer, Serializer};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, net::UdpSocket};
#[derive(Debug, Clone, Copy, PartialEq, Deserialize, Serialize)]
pub enum Color {
Red = 0,
Green = 1,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
struct Human {
age: u16,
name: String,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
struct HumanAdvanced {
id: u32,
age: u16,
name: String,
fav_color: Color,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
struct HumanGroup {
humans: Vec<HumanAdvanced>,
bank: HashMap<u32, usize>,
}
fn wild_testing() {
let mut buf = Vec::new();
let john = HumanAdvanced {
id: 0,
age: 42,
name: "John".into(),
fav_color: Color::Green,
};
john.serialize(&mut Serializer::new(&mut buf)).unwrap();
println!("{:?}", buf);
let new_val: HumanAdvanced = rmp_serde::from_slice(&buf).expect("deserialization failed");
let rmpv_val: rmpv::Value = rmp_serde::from_slice(&buf).expect("serialization into val failed");
println!("RMPV value: {:?}", rmpv_val);
let json_str = serde_json::to_string(&rmpv_val).expect("creating json failed");
assert_eq!(john, new_val);
println!("JSON str: {}", json_str);
let val_test: HumanAdvanced = serde_json::from_str(&json_str).expect("wild");
println!("val test: {:?}", val_test);
let nadine = HumanAdvanced {
id: 1,
age: 24,
name: "Nadine".into(),
fav_color: Color::Red,
};
let mut bank = HashMap::default();
bank.insert(john.id, 1000000);
bank.insert(nadine.id, 1);
let human_group = HumanGroup {
humans: vec![john, nadine.clone()],
bank,
};
let json_str = serde_json::to_string(&nadine).unwrap();
println!("Nadine as JSON: {}", json_str);
let nadine_is_back: HumanAdvanced = serde_json::from_str(&json_str).unwrap();
println!("nadine deserialized: {:?}", nadine_is_back);
let human_group_json = serde_json::to_string(&human_group).unwrap();
println!("human group: {}", human_group_json);
println!("human group json size: {}", human_group_json.len());
let human_group_rmp_vec = rmp_serde::to_vec(&human_group_json).unwrap();
println!("human group msg pack size: {:?}", human_group_rmp_vec.len());
}
fn main() {
let socket = UdpSocket::bind("127.0.0.1:7301").expect("binding UDP socket failed");
// Receives a single datagram message on the socket. If `buf` is too small to hold
// the message, it will be cut off.
let mut buf = [0; 4096];
let (received, src) = socket.recv_from(&mut buf).expect("receive call failed");
let human_from_python: rmpv::Value = rmp_serde::from_slice(&buf[..received]).expect("blablah");
let human_attempt_2: Human = rmp_serde::from_slice(&buf[..received]).expect("blhfwhfw");
println!("human from python: {}", human_from_python);
println!("human 2 from python: {:?}", human_attempt_2);
let send_back_human = rmp_serde::to_vec(&human_attempt_2).expect("k32k323k2");
socket.send_to(&send_back_human, src).expect("sending back failed");
}