diff --git a/serialization-prototyping/Cargo.lock b/serialization-prototyping/Cargo.lock index e11f5da..aad036d 100644 --- a/serialization-prototyping/Cargo.lock +++ b/serialization-prototyping/Cargo.lock @@ -266,6 +266,12 @@ dependencies = [ "allocator-api2", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.3.9" @@ -657,9 +663,11 @@ dependencies = [ "fern", "humantime", "log", + "num_enum", "satrs", "serde", "serde_json", + "strum", "thiserror", ] @@ -788,6 +796,28 @@ dependencies = [ "loom", ] +[[package]] +name = "strum" +version = "0.26.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d8cec3501a5194c432b2b7976db6b7d10ec95c253208b45f83f7136aa985e29" +dependencies = [ + "strum_macros", +] + +[[package]] +name = "strum_macros" +version = "0.26.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6cf59daf282c0a494ba14fd21610a0325f9f90ec9d1231dea26bcb1d696c946" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "rustversion", + "syn 2.0.60", +] + [[package]] name = "syn" version = "1.0.109" diff --git a/serialization-prototyping/python-test/main.py b/serialization-prototyping/python-test/main.py index 395d5aa..c77ae79 100755 --- a/serialization-prototyping/python-test/main.py +++ b/serialization-prototyping/python-test/main.py @@ -1,5 +1,7 @@ #!/usr/bin/env python3 +import enum from socket import AF_INET, SOCK_DGRAM, socket +from pydantic import BaseModel import msgpack @@ -9,10 +11,26 @@ } +class Devices(str, enum.Enum): + MGT = "Mgt" + MGM = "Mgm" + + +class SwitchState(str, enum.Enum): + OFF = "Off" + ON = "On" + UNKNOWN = "Unknown" + FAULTY = "Faulty" + + +class SwitchMap(BaseModel): + valid: bool + switch_map: dict[Devices, SwitchState] + + def msg_pack_unloading(recv_back: bytes): 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) @@ -25,7 +43,8 @@ def main(): _ = server_socket.sendto(msg_pack_stuff, target_address) recv_back = server_socket.recv(4096) print(f"recv back: {recv_back}") - pass + switch_map = SwitchMap.model_validate_json(recv_back) + print(f"switch map: {switch_map}") if __name__ == "__main__": diff --git a/serialization-prototyping/python-test/requirements.txt b/serialization-prototyping/python-test/requirements.txt index 8230895..7762595 100644 --- a/serialization-prototyping/python-test/requirements.txt +++ b/serialization-prototyping/python-test/requirements.txt @@ -1 +1,2 @@ msgpack==1.0.8 +pydantic==2.7 diff --git a/serialization-prototyping/src/archive.rs b/serialization-prototyping/src/archive.rs new file mode 100644 index 0000000..be4c3f5 --- /dev/null +++ b/serialization-prototyping/src/archive.rs @@ -0,0 +1,88 @@ +#[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, + bank: HashMap, +} + + +#[allow(dead_code)] +fn random_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_named(&human_group_json).unwrap(); + println!("human group msg pack size: {:?}", human_group_rmp_vec.len()); +} + +#[allow(dead_code)] +fn send_back_weird_stuff(buf: &[u8], received: usize, socket: &UdpSocket, src: SocketAddr) { + 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_named(&human_attempt_2).expect("k32k323k2"); + socket + .send_to(&send_back_human, src) + .expect("sending back failed"); +} diff --git a/serialization-prototyping/src/main.rs b/serialization-prototyping/src/main.rs index 89c22a0..ee01a03 100644 --- a/serialization-prototyping/src/main.rs +++ b/serialization-prototyping/src/main.rs @@ -1,97 +1,16 @@ +#![allow(unused_imports)] use rmp_serde::{Deserializer, Serializer}; -use satrs_minisim::eps::SwitchMap; +use satrs_minisim::eps::{SwitchMap, SwitchMapWrapper}; use serde::{Deserialize, Serialize}; use std::{ collections::HashMap, net::{SocketAddr, 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, - bank: HashMap, -} - -#[allow(dead_code)] -fn random_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_named(&human_group_json).unwrap(); - println!("human group msg pack size: {:?}", human_group_rmp_vec.len()); -} - -#[allow(dead_code)] -fn send_back_weird_stuff(buf: &[u8], received: usize, socket: &UdpSocket, src: SocketAddr) { - 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_named(&human_attempt_2).expect("k32k323k2"); - socket - .send_to(&send_back_human, src) - .expect("sending back failed"); +#[derive(Clone, PartialEq, Eq, Default, Serialize, Deserialize)] +pub struct SwitchSet { + pub valid: bool, + pub switch_map: SwitchMap, } pub struct UdpServer { @@ -129,9 +48,13 @@ fn main() { .expect("receive call failed"); udp_server.last_sender = Some(src); println!("received {} bytes from {:?}", received, src); - let switch_map_off = SwitchMap::default(); + let switch_map_off = SwitchMapWrapper::default(); + let switch_set = SwitchSet { + valid: true, + switch_map: switch_map_off.0.clone(), + }; let switch_map_off_json = - serde_json::to_string(&switch_map_off).expect("json serialization failed"); + serde_json::to_string(&switch_set).expect("json serialization failed"); println!("sending back reply: {}", switch_map_off_json); udp_server.send_back_reply(switch_map_off_json.as_bytes()); }