#![allow(unused_assignments)]
extern crate rand;
use cbor::CborTagEncode;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use types;
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ConnectSuccess {
pub peer_id : types::DhtId,
pub peer_fob : types::PublicPmid
}
impl ConnectSuccess {
pub fn generate_random() -> ConnectSuccess {
ConnectSuccess {
peer_id: types::DhtId::generate_random(),
peer_fob: types::PublicPmid::generate_random(),
}
}
}
impl Encodable for ConnectSuccess {
fn encode<E: Encoder>(&self, e: &mut E)->Result<(), E::Error> {
CborTagEncode::new(5483_001, &(&self.peer_id,
&self.peer_fob)).encode(e)
}
}
impl Decodable for ConnectSuccess {
fn decode<D: Decoder>(d: &mut D)->Result<ConnectSuccess, D::Error> {
use types::DhtId;
try!(d.read_u64());
let (peer_id, peer_fob): (DhtId, types::PublicPmid) = try!(Decodable::decode(d));
Ok(ConnectSuccess { peer_id: peer_id,
peer_fob: peer_fob})
}
}
#[cfg(test)]
mod test {
extern crate cbor;
use super::*;
#[test]
fn connect_success_serialisation() {
let obj_before = ConnectSuccess::generate_random();
let mut e = cbor::Encoder::from_memory();
e.encode(&[&obj_before]).unwrap();
let mut d = cbor::Decoder::from_bytes(e.as_bytes());
let obj_after: ConnectSuccess = d.decode().next().unwrap().unwrap();
assert_eq!(obj_before, obj_after);
}
}