1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*! InnerOnionResponse enum
*/

use super::*;

use tox_binary_io::*;

/** Onion responses that can be enclosed in onion packets and sent through onion
path.

Onion allows only two types of packets to be sent as a response through onion
paths: `OnionAnnounceResponse` and `OnionDataResponse`.
*/
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum InnerOnionResponse {
    /// [`OnionAnnounceResponse`](./struct.OnionAnnounceResponse.html) structure.
    OnionAnnounceResponse(OnionAnnounceResponse),
    /// [`OnionDataResponse`](./struct.OnionDataResponse.html) structure.
    OnionDataResponse(OnionDataResponse)
}

impl ToBytes for InnerOnionResponse {
    fn to_bytes<'a>(&self, buf: (&'a mut [u8], usize)) -> Result<(&'a mut [u8], usize), GenError> {
        match *self {
            InnerOnionResponse::OnionAnnounceResponse(ref inner) => inner.to_bytes(buf),
            InnerOnionResponse::OnionDataResponse(ref inner) => inner.to_bytes(buf),
        }
    }
}

impl FromBytes for InnerOnionResponse {
    named!(from_bytes<InnerOnionResponse>, alt!(
        map!(OnionAnnounceResponse::from_bytes, InnerOnionResponse::OnionAnnounceResponse) |
        map!(OnionDataResponse::from_bytes, InnerOnionResponse::OnionDataResponse)
    ));
}

#[cfg(test)]
mod tests {
    use super::*;

    encode_decode_test!(
        tox_crypto::crypto_init().unwrap(),
        inner_onion_announce_response_encode_decode,
        InnerOnionResponse::OnionAnnounceResponse(OnionAnnounceResponse {
            sendback_data: 12345,
            nonce: gen_nonce(),
            payload: vec![42; 123]
        })
    );

    encode_decode_test!(
        tox_crypto::crypto_init().unwrap(),
        inner_onion_data_response_encode_decode,
        InnerOnionResponse::OnionDataResponse(OnionDataResponse {
            nonce: gen_nonce(),
            temporary_pk: gen_keypair().0,
            payload: vec![42; 123]
        })
    );
}