#![allow(unused_assignments)]
use cbor::CborTagEncode;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use types;
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug)]
pub struct GetClientKeyResponse {
pub address : types::DhtId,
pub public_sign_key : types::PublicSignKey
}
impl GetClientKeyResponse {
pub fn generate_random() -> GetClientKeyResponse {
GetClientKeyResponse {
address: types::DhtId::generate_random(),
public_sign_key: types::PublicSignKey::generate_random(),
}
}
}
impl Encodable for GetClientKeyResponse {
fn encode<E: Encoder>(&self, e: &mut E)->Result<(), E::Error> {
CborTagEncode::new(5483_001, &(&self.address, &self.public_sign_key)).encode(e)
}
}
impl Decodable for GetClientKeyResponse {
fn decode<D: Decoder>(d: &mut D)->Result<GetClientKeyResponse, D::Error> {
try!(d.read_u64());
let (address, public_sign_key) = try!(Decodable::decode(d));
Ok(GetClientKeyResponse { address: address , public_sign_key: public_sign_key})
}
}
#[cfg(test)]
mod test {
use super::*;
use cbor;
#[test]
fn get_client_key_response_serialisation() {
let obj_before = GetClientKeyResponse::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: GetClientKeyResponse = d.decode().next().unwrap().unwrap();
assert_eq!(obj_before, obj_after);
}
}