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
79
80
81
82
83
84
85
86
use super::{ProtocolError, Result};

pub struct MessageHead {
    pub count: usize,
}

impl MessageHead {
    pub fn from_buf(buf: Vec<u8>) -> Result<MessageHead> {
        if buf[0] == b'*' {
            Ok(MessageHead {
                count: std::str::from_utf8(&buf[1..])?.trim().parse()?,
            })
        } else {
            log::info!("Error buffer is {:?}", std::str::from_utf8(&buf)?);
            Err(ProtocolError::GrammarCheckFailed(
                "* should be the first character of a message",
            ))
        }
    }
    pub fn into_bytes(self) -> Vec<u8> {
        format!("*{}\r\n", self.count).into_bytes()
    }
}

pub struct PartHead {
    pub size: usize,
}

impl PartHead {
    pub fn from_buf(buf: Vec<u8>) -> Result<PartHead> {
        if buf[0] == b'$' {
            Ok(PartHead {
                size: std::str::from_utf8(&buf[1..])?.trim().parse()?,
            })
        } else {
            Err(ProtocolError::GrammarCheckFailed(
                "$ should be the first character of a message part",
            ))
        }
    }
    pub fn into_bytes(self) -> Vec<u8> {
        format!("${}\r\n", self.size).into_bytes()
    }
}

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

    #[test]
    fn message_head() {
        let message_head = MessageHead::from_buf(b"*100\r\n".to_vec()).unwrap();
        assert_eq!(message_head.count, 100);
        assert_eq!(message_head.into_bytes(), b"*100\r\n".to_vec());
    }

    #[test]
    fn part_head() {
        let part_head = PartHead::from_buf(b"$100\r\n".to_vec()).unwrap();
        assert_eq!(part_head.size, 100);
        assert_eq!(part_head.into_bytes(), b"$100\r\n".to_vec());
    }

    #[test]
    fn wrong_message_head() {
        match MessageHead::from_buf(b"$100\r\r".to_vec()) {
            Err(err) => assert_eq!(
                err.description(),
                "* should be the first character of a message"
            ),
            Ok(_) => assert!(false, "should throw an error"),
        }
    }

    #[test]
    fn wrong_part_head() {
        match PartHead::from_buf(b"*100\r\r".to_vec()) {
            Err(err) => assert_eq!(
                err.description(),
                "$ should be the first character of a message part"
            ),
            Ok(_) => assert!(false, "should throw an error"),
        }
    }
}