<commit_before>use std::boxed::Box;
use hash;
use rpc;

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
pub struct Rpc {
   destination : Destination,
   payload     : Payload,
}

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
pub enum Destination {
   Address(String),
   Id(hash::Hash),
}

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
pub enum Payload {
   Ping,
   PingResponse,
   Store(StorePayload),
   FindNode(FindNodePayload),
   FindNodeResponse(FindNodeResponsePayload),
   FindValue(FindValuePayload),
   FindValueResponse(FindValueResponsePayload),
}

impl Rpc {
   pub fn ping(destination: Destination) -> Rpc {
      Rpc {
         destination : destination,
         payload     : Payload::Ping,
      }
   }
}

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
pub struct StorePayload;

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
pub struct FindNodePayload;

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
pub struct FindValuePayload;

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
pub struct FindNodeResponsePayload;

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
pub struct FindValueResponsePayload;

#[cfg(test)]
mod tests {
    use super::*;
    use hash::Hash;
    use bincode::serde::{serialize, deserialize};
    use bincode::SizeLimit;
    use node::SOCKET_BUFFER_SIZE_BYTES;

    #[test]
    fn serdes_for_ping() {
       let destination = Destination::Id(Hash::random());
       let ping = Rpc::ping(destination);
       let serialized_ping = serialize(&ping, SizeLimit::Bounded(SOCKET_BUFFER_SIZE_BYTES as u64)).unwrap();
       let deserialized_ping: Rpc = deserialize(&serialized_ping).unwrap();

       assert_eq!(ping, deserialized_ping);
    }
}<commit_msg>Expose serdes methods at rpc level<commit_after>use std::boxed::Box;
use bincode::serde;
use bincode;
use node;
use hash;
use rpc;

#[derive(Eq, PartialEq, Debug)]
pub struct Rpc {
   destination : Destination,
   payload     : Payload,
}

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
pub enum Destination {
   Address(String),
   Id(hash::Hash),
   Unknown,
}

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
pub enum Payload {
   Ping,
   PingResponse,
   Store(StorePayload),
   FindNode(FindNodePayload),
   FindNodeResponse(FindNodeResponsePayload),
   FindValue(FindValuePayload),
   FindValueResponse(FindValueResponsePayload),
}

impl Rpc {
   pub fn ping(destination: Destination) -> Rpc {
      Rpc {
         destination : destination,
         payload     : Payload::Ping,
      }
   }

   pub fn serialized_payload(&self) -> Vec<u8> {
       serde::serialize(&self.payload, bincode::SizeLimit::Bounded(node::SOCKET_BUFFER_SIZE_BYTES as u64)).unwrap()
   }

   pub fn from_payload(serialized_payload: &[u8]) -> serde::DeserializeResult<Rpc> {
       let payload: Payload = try!(serde::deserialize(serialized_payload));
       Ok(Rpc { 
          destination : Destination::Unknown,
          payload : payload,
       })
   }
}

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
pub struct StorePayload;

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
pub struct FindNodePayload;

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
pub struct FindValuePayload;

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
pub struct FindNodeResponsePayload;

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
pub struct FindValueResponsePayload;

#[cfg(test)]
mod tests {
    use super::*;
    use hash::Hash;
    use bincode::serde::{serialize, deserialize};

    #[test]
    fn serdes_for_ping() {
       let destination = Destination::Id(Hash::random());
       let ping = Rpc::ping(destination);
       let serialized_payload = ping.serialized_payload();
       let deserialized_ping = Rpc::from_payload(&serialized_payload).unwrap();

       assert_eq!(ping.payload, deserialized_ping.payload);
       assert_eq!(Destination::Unknown, deserialized_ping.destination);
    }
}
{'fraction_non_alphanumeric': 0.12406203101550775, 'fraction_numerical': 0.0015007503751875938, 'mean_word_length': 4.4334239130434785, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7609448', 'n_tokens_mistral': 1225, 'n_tokens_neox': 1277, 'n_words': 286}