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
use std::convert::From;
use std::io::{Read, Write};

use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};

use crate::{Decodable, Encodable};
use crate::control::variable_header::VariableHeaderError;

/// Keep alive time interval
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub struct KeepAlive(pub u16);

impl Encodable for KeepAlive {
    type Err = VariableHeaderError;

    fn encode<W: Write>(&self, writer: &mut W) -> Result<(), VariableHeaderError> {
        writer.write_u16::<BigEndian>(self.0).map_err(From::from)
    }

    fn encoded_length(&self) -> u32 {
        2
    }
}

impl Decodable for KeepAlive {
    type Err = VariableHeaderError;
    type Cond = ();

    fn decode_with<R: Read>(reader: &mut R, _rest: Option<()>) -> Result<KeepAlive, VariableHeaderError> {
        reader.read_u16::<BigEndian>()
              .map(KeepAlive)
              .map_err(From::from)
    }
}