Fixed (de)serialization

This commit is contained in:
Felix Albrigtsen 2021-10-21 16:12:23 +02:00
parent ba7b52364d
commit e1cacce3b0
2 changed files with 37 additions and 23 deletions

View File

@ -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")
@ -26,19 +27,24 @@ fn main() {
.author("felixalb"))
.get_matches();
// println!("{}", &serialize_move(models::networking::Action::Rotate {
// let testMove: Result<Action, serde_json::Error> = 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<Action, serde_json::Error> {
let obj= serde_json::from_str(&json);
return obj;
}

View File

@ -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},