From e1cacce3b054a48d8b65bb0fd56b3221d804fb06 Mon Sep 17 00:00:00 2001 From: Felix Albrigtsen Date: Thu, 21 Oct 2021 16:12:23 +0200 Subject: [PATCH] Fixed (de)serialization --- src/main.rs | 59 +++++++++++++++++++++++++--------------- src/models/networking.rs | 1 - 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index 41da7ea..68fe472 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ mod models; -use models::{player, board, piece, networking}; +use models::{player, board, piece, networking::Action}; use clap::{Arg, App, SubCommand}; +use serde_json::Value; fn main() { let matches = App::new("Khetamine") @@ -8,37 +9,42 @@ fn main() { .author("oysteikt, felixalb") .about("Play Khet with your friends!") .arg(Arg::with_name("serve") - .short("s") - .long("serve") - .help("Run as game server, your friend connects as client.")) + .short("s") + .long("serve") + .help("Run as game server, your friend connects as client.")) .arg(Arg::with_name("connect") - .short("c") - .help("Connect to a server") - .value_name("ADRESS") - .takes_value(true)) + .short("c") + .help("Connect to a server") + .value_name("ADRESS") + .takes_value(true)) .arg(Arg::with_name("v") - .short("v") - .multiple(true) - .help("Sets the level of verbosity")) + .short("v") + .multiple(true) + .help("Sets the level of verbosity")) .subcommand(SubCommand::with_name("test") - .about("Runs various tests") - .version("1.3") - .author("felixalb")) + .about("Runs various tests") + .version("1.3") + .author("felixalb")) .get_matches(); - - // println!("{}", &serialize_move(models::networking::Action::Rotate { + + // let testMove: Result = deserialize_move(serialize_move( + // models::networking::Action::Rotate { // from: (2, 2), // rot: models::networking::RotationDirection::Positive - // })); - + // } + // )); + // match testMove { + // Ok(act) => { println!("It works! Actions for days"); }, + // Err(e) => { println!("Somethings wrong: {}", e); + // } + // } -} - -fn serialize_move(action: models::networking::Action) -> String { + } + + fn serialize_move(action: models::networking::Action) -> String { let serialized = serde_json::to_string(&action); match serialized { Ok(s) => { - // println!("{}", s); return s; }, Err(_e) => { @@ -47,3 +53,12 @@ fn serialize_move(action: models::networking::Action) -> String { } } } + + fn deserialize_move(json: String) -> Result { + let obj= serde_json::from_str(&json); + + return obj; + + } + + \ No newline at end of file diff --git a/src/models/networking.rs b/src/models/networking.rs index 03de6d6..af27e80 100644 --- a/src/models/networking.rs +++ b/src/models/networking.rs @@ -9,7 +9,6 @@ pub enum RotationDirection { pub type Position = (u8, u8); #[derive(Serialize, Deserialize)] -#[serde(tag = "type")] pub enum Action { Move {from: Position, to: Position}, Rotate {from: Position, rot: RotationDirection},