From c14dcf1998f5ba1ffea813da4f793fc3bde8c2b7 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Mon, 26 Feb 2018 08:51:08 -0600 Subject: -figured out way to deserialize using serde_erased --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + src/astroid.rs | 7 +++---- src/connection.rs | 2 +- src/lib.rs | 1 + src/mass.rs | 7 ++++++- src/navigation.rs | 28 ++++++++++++++++++++++------ src/ship.rs | 10 ++++------ 8 files changed, 48 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8172406..099afcc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,14 @@ name = "dtoa" version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "erased-serde" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "itoa" version = "0.3.4" @@ -89,6 +97,7 @@ name = "space" version = "0.1.0" dependencies = [ "downcast 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "erased-serde 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", @@ -131,6 +140,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum downcast 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "242756f88358d71447a37780b71b1cb180a276bd214765ee2d81dfe7c90e59bb" "checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab" +"checksum erased-serde 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c564e32677839f1c551664c478e079c9b128a1a2d223180bffb2ddfabeded0be" "checksum itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8324a32baf01e2ae060e9de58ed0bc2320c9a2833491ee36cd3b4c414de4db8c" "checksum libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "1e5d97d6708edaa407429faa671b942dc0f2727222fb6b6539bf1db936e4b121" "checksum num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31" diff --git a/Cargo.toml b/Cargo.toml index ce4e48e..9eb8bce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ authors = ["tom barrett "] [dependencies] serde = "1.0" serde_derive = "1.0" +erased-serde = "0.3" serde_json = "1.0" termion = "1.5.1" downcast = "0.8" diff --git a/src/astroid.rs b/src/astroid.rs index 0eed4ff..6aa1807 100644 --- a/src/astroid.rs +++ b/src/astroid.rs @@ -1,9 +1,10 @@ -use mass::Mass; +use mass::{Mass, Type}; extern crate serde_json; #[derive(Serialize, Deserialize, Debug)] pub struct Astroid { name : String, + t : Type, location : (f64, f64, f64), } @@ -11,6 +12,7 @@ impl Mass for Astroid { fn new(name : &str, location : (f64, f64, f64)) -> Astroid { Astroid { name : String::from(name), + t : Type::Astroid, location : location, } } @@ -30,7 +32,4 @@ impl Mass for Astroid { fn serialize(&self) ->String { serde_json::to_string(self).unwrap() } - - fn deserialize(&mut self, data : &str) { - } } diff --git a/src/connection.rs b/src/connection.rs index 59031c6..9cf7923 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -92,7 +92,7 @@ impl Connection { let mut send = String::new(); for mass in within_range { send.push_str(&mass.serialize()); - send.push_str(","); + send.push_str(";"); } send.push_str("\n"); match self.stream.write(send.as_bytes()) { diff --git a/src/lib.rs b/src/lib.rs index 0611f67..b661cad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +extern crate erased_serde; #[macro_use] extern crate serde_derive; #[macro_use] diff --git a/src/mass.rs b/src/mass.rs index 0a49dc6..71723cd 100644 --- a/src/mass.rs +++ b/src/mass.rs @@ -6,7 +6,12 @@ pub trait Mass : Any { fn location(&self) -> (f64, f64, f64); fn set_location(&mut self, location : (f64, f64, f64)); fn serialize(&self) -> String; - fn deserialize(&mut self, data : &str); +} + +#[derive(Serialize, Deserialize, Debug)] +pub enum Type { + Ship, + Astroid, } downcast!(Mass); diff --git a/src/navigation.rs b/src/navigation.rs index 02156d3..bcb0c4d 100644 --- a/src/navigation.rs +++ b/src/navigation.rs @@ -2,23 +2,39 @@ use std::net::TcpStream; use std::io::BufRead; use std::io::BufReader; +extern crate erased_serde; extern crate serde_json; +use std::collections::BTreeMap; + use mass::Mass; use ship::Ship; +use astroid::Astroid; + +use erased_serde::Deserializer; pub fn Navigation(mut stream :TcpStream, mut buff_r : BufReader){ loop { let mut data = String::new(); buff_r.read_line(&mut data).unwrap(); - let string_masses = data.split(","); - //let masses : Vec> = Vec::new(); + + let string_masses = data.split(";"); + let mut masses : Vec> = Vec::new(); for string_mass in string_masses { - //let mass = Box::new(Ship::new("",(0,0,0))); - //mass.deserialize(string_mass); + if string_mass.len() == 1 { + break; + } + let json = &mut serde_json::de::Deserializer::from_slice(string_mass.as_bytes()); + let mut deserialized : Box = Box::new(Deserializer::erase(json)); - //let mass : Box = serde_json::from_str(string_mass).unwrap(); - //masses.push(Box::new(mass)); + if string_mass.contains("Ship") { + let mass : Ship = erased_serde::deserialize(&mut deserialized).unwrap(); + masses.push(Box::new(mass)); + } + else { + let mass : Astroid = erased_serde::deserialize(&mut deserialized).unwrap(); + masses.push(Box::new(mass)); + } } } } diff --git a/src/ship.rs b/src/ship.rs index e5186c8..078c2c1 100644 --- a/src/ship.rs +++ b/src/ship.rs @@ -1,10 +1,11 @@ -use mass::Mass; +use mass::{Mass, Type}; extern crate serde_json; #[derive(Serialize, Deserialize, Debug)] pub struct Ship { name : String, location : (f64, f64, f64), + t : Type, r : f64, } @@ -19,6 +20,7 @@ impl Mass for Ship { Ship { name : String::from(name), location : location, + t : Type::Ship, r : 100.0, } } @@ -35,11 +37,7 @@ impl Mass for Ship { self.location = location; } - fn serialize(&self) ->String { + fn serialize(&self) -> String { serde_json::to_string(self).unwrap() } - - fn deserialize(&mut self, data : &str) { - //self = serde_json::from_str(data).unwrap(); - } } -- cgit v1.2.3