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; mod models;
use models::{player, board, piece, networking}; use models::{player, board, piece, networking::Action};
use clap::{Arg, App, SubCommand}; use clap::{Arg, App, SubCommand};
use serde_json::Value;
fn main() { fn main() {
let matches = App::new("Khetamine") let matches = App::new("Khetamine")
@ -8,37 +9,42 @@ fn main() {
.author("oysteikt, felixalb") .author("oysteikt, felixalb")
.about("Play Khet with your friends!") .about("Play Khet with your friends!")
.arg(Arg::with_name("serve") .arg(Arg::with_name("serve")
.short("s") .short("s")
.long("serve") .long("serve")
.help("Run as game server, your friend connects as client.")) .help("Run as game server, your friend connects as client."))
.arg(Arg::with_name("connect") .arg(Arg::with_name("connect")
.short("c") .short("c")
.help("Connect to a server") .help("Connect to a server")
.value_name("ADRESS") .value_name("ADRESS")
.takes_value(true)) .takes_value(true))
.arg(Arg::with_name("v") .arg(Arg::with_name("v")
.short("v") .short("v")
.multiple(true) .multiple(true)
.help("Sets the level of verbosity")) .help("Sets the level of verbosity"))
.subcommand(SubCommand::with_name("test") .subcommand(SubCommand::with_name("test")
.about("Runs various tests") .about("Runs various tests")
.version("1.3") .version("1.3")
.author("felixalb")) .author("felixalb"))
.get_matches(); .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), // from: (2, 2),
// rot: models::networking::RotationDirection::Positive // 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); let serialized = serde_json::to_string(&action);
match serialized { match serialized {
Ok(s) => { Ok(s) => {
// println!("{}", s);
return s; return s;
}, },
Err(_e) => { 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); pub type Position = (u8, u8);
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum Action { pub enum Action {
Move {from: Position, to: Position}, Move {from: Position, to: Position},
Rotate {from: Position, rot: RotationDirection}, Rotate {from: Position, rot: RotationDirection},