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
62
63
64
65
66
67
68
69
70
71
72
73
74
/*! Typing struct.
*/

use super::*;

use nom::number::complete::le_u8;

/// Typing status of user
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TypingStatus {
    /// Not typing
    NotTyping = 0,
    /// Typing
    Typing,
}

impl FromBytes for TypingStatus {
    named!(from_bytes<TypingStatus>,
        switch!(le_u8,
            0 => value!(TypingStatus::NotTyping) |
            1 => value!(TypingStatus::Typing)
        )
    );
}

/** Typing is a struct that holds typing status of user.

This packet is used to transmit sender's typing status to a friend.

Serialized form:

Length    | Content
--------- | ------
`1`       | `0x33`
`1`       | Typing status(0 = not typing, 1 = typing)

*/
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Typing(TypingStatus);

impl FromBytes for Typing {
    named!(from_bytes<Typing>, do_parse!(
        tag!("\x33") >>
        status: call!(TypingStatus::from_bytes) >>
        (Typing(status))
    ));
}

impl ToBytes for Typing {
    fn to_bytes<'a>(&self, buf: (&'a mut [u8], usize)) -> Result<(&'a mut [u8], usize), GenError> {
        do_gen!(buf,
            gen_be_u8!(0x33) >>
            gen_be_u8!(self.0 as u8)
        )
    }
}

impl Typing {
    /// Create new Typing object.
    pub fn new(status: TypingStatus) -> Self {
        Typing(status)
    }
}

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

    encode_decode_test!(
        tox_crypto::crypto_init().unwrap(),
        typing_encode_decode,
        Typing::new(TypingStatus::Typing)
    );
}