summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2018-02-26 08:51:08 -0600
committertom barrett <spalf0@gmail.com>2018-02-26 08:51:08 -0600
commitc14dcf1998f5ba1ffea813da4f793fc3bde8c2b7 (patch)
treef650e9bdf008a785b4627db04446c717f530e7c5 /src
parent6634f73a6768f61f928d51941be5e970e6011f07 (diff)
-figured out way to deserialize using serde_erased
Diffstat (limited to 'src')
-rw-r--r--src/astroid.rs7
-rw-r--r--src/connection.rs2
-rw-r--r--src/lib.rs1
-rw-r--r--src/mass.rs7
-rw-r--r--src/navigation.rs28
-rw-r--r--src/ship.rs10
6 files changed, 37 insertions, 18 deletions
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<TcpStream>){
loop {
let mut data = String::new();
buff_r.read_line(&mut data).unwrap();
- let string_masses = data.split(",");
- //let masses : Vec<Box<Mass>> = Vec::new();
+
+ let string_masses = data.split(";");
+ let mut masses : Vec<Box<Mass>> = 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<Deserializer> = Box::new(Deserializer::erase(json));
- //let mass : Box<Mass> = 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();
- }
}