Fixed (de)serialization
This commit is contained in:
parent
ba7b52364d
commit
e1cacce3b0
29
src/main.rs
29
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")
|
||||
|
@ -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;
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -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},
|
||||
|
|
Loading…
Reference in New Issue