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
75
76
77
78
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use packet_encoding::{Decodable, Encodable};

use crate::frame::FrameParseError;

/// The length (in bytes) of a Flow Control Command. Both Flow Control On and Off commands
/// contain no parameters.
/// Defined in GSM 7.10 Section 5.4.6.3.5 & 5.4.6.3.6.
const FLOW_CONTROL_COMMAND_LENGTH: usize = 0;

/// The Flow Control Command is used to enable/disable aggregate flow.
/// The command contains no parameters.
/// Defined in GSM 7.10 Section 5.4.6.3.5 & 5.4.6.3.6.
#[derive(Clone, Debug, PartialEq)]
pub struct FlowControlParams {}

impl Decodable for FlowControlParams {
    type Error = FrameParseError;

    fn decode(buf: &[u8]) -> Result<Self, FrameParseError> {
        if buf.len() != FLOW_CONTROL_COMMAND_LENGTH {
            return Err(FrameParseError::InvalidBufferLength(
                FLOW_CONTROL_COMMAND_LENGTH,
                buf.len(),
            ));
        }
        Ok(Self {})
    }
}

impl Encodable for FlowControlParams {
    type Error = FrameParseError;

    fn encoded_len(&self) -> usize {
        FLOW_CONTROL_COMMAND_LENGTH
    }

    fn encode(&self, buf: &mut [u8]) -> Result<(), FrameParseError> {
        if buf.len() < self.encoded_len() {
            return Err(FrameParseError::BufferTooSmall);
        }
        Ok(())
    }
}

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

    use assert_matches::assert_matches;

    #[test]
    fn test_decode_flow_control_invalid_buf() {
        let buf = [0x00, 0x01, 0x02];
        assert_matches!(
            FlowControlParams::decode(&buf[..]),
            Err(FrameParseError::InvalidBufferLength(FLOW_CONTROL_COMMAND_LENGTH, 3))
        );
    }

    #[test]
    fn test_decode_flow_control() {
        let buf = [];
        assert_eq!(FlowControlParams::decode(&buf[..]).unwrap(), FlowControlParams {});
    }

    #[test]
    fn test_encode_flow_control() {
        let mut buf = [];
        let expected: [u8; 0] = [];
        let command = FlowControlParams {};
        assert!(command.encode(&mut buf[..]).is_ok());
        assert_eq!(buf, expected);
    }
}