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
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use bytes::{BufMut, Bytes, BytesMut};
use std::error::Error;
use std::fmt;

#[derive(Debug)]
pub enum BulkStringError {
    InvalidValue,
    InvalidFirstChar,
    InvalidLength,
    InvalidLengthSeparator,
    InvalidTerminate,
    LengthsNotMatch,
}

impl fmt::Display for BulkStringError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            BulkStringError::InvalidValue => {
                write!(f, "[BulkStringError] Invalid value.")
            }
            BulkStringError::InvalidFirstChar => {
                write!(f, "[BulkStringError] Invalid first char.")
            }
            BulkStringError::InvalidLength => {
                write!(f, "[BulkStringError] Invalid length.")
            }
            BulkStringError::InvalidLengthSeparator => {
                write!(f, "[BulkStringError] Invalid length separator.")
            }
            BulkStringError::InvalidTerminate => {
                write!(f, "[BulkStringError] Invalid terminate.")
            }
            BulkStringError::LengthsNotMatch => {
                write!(f, "[BulkStringError] Lengths do not match.")
            }
        }
    }
}

impl Error for BulkStringError {}

#[derive(Debug, PartialEq)]
pub struct BulkString(Bytes);

impl BulkString {
    pub fn new(input: &[u8]) -> BulkString {
        let length_string = input.len().to_string();
        let mut bytes = BytesMut::with_capacity(input.len() + length_string.len() + 5);
        bytes.put_u8(0x24); // "$"
        bytes.put_slice(length_string.as_bytes());
        bytes.put_u8(0x0d); // CR
        bytes.put_u8(0x0a); // LF
        bytes.put_slice(input);
        bytes.put_u8(0x0d); // CR
        bytes.put_u8(0x0a); // LF
        BulkString(bytes.freeze())
    }

    #[inline]
    pub fn bytes(&self) -> Bytes {
        self.0.clone()
    }

    #[inline]
    pub fn len(&self) -> usize {
        self.0.len()
    }

    pub fn parse(
        input: &[u8],
        start: &mut usize,
        end: &usize,
    ) -> Result<BulkString, BulkStringError> {
        let mut index = *start;
        if index >= *end || input[index] != 0x24 {
            return Err(BulkStringError::InvalidFirstChar);
        }
        index += 1;
        if index + 1 >= *end
            || (input[index] == 0x30 && input[index + 1] >= 0x30 && input[index + 1] <= 0x39)
        {
            return Err(BulkStringError::InvalidLength);
        }
        while index < *end && input[index] >= 0x30 && input[index] <= 0x39 {
            index += 1;
        }
        if index + 1 >= *end || input[index] != 0x0d || input[index + 1] != 0x0a {
            return Err(BulkStringError::InvalidLengthSeparator);
        }
        let length = unsafe {
            String::from_utf8_unchecked(input[*start + 1..index].to_vec())
                .parse::<usize>()
                .unwrap()
        };
        index += 2;
        let value_start_index = index;
        while index < *end
            && index - value_start_index <= length
            && input[index] != 0x0d
            && input[index] != 0x0a
        {
            index += 1;
        }
        if length != index - value_start_index {
            return Err(BulkStringError::LengthsNotMatch);
        }
        if index + 1 >= *end || input[index] != 0x0d || input[index + 1] != 0x0a {
            return Err(BulkStringError::InvalidTerminate);
        }
        let value = Self::new(&input[value_start_index..index]);
        *start = index + 2;
        Ok(value)
    }
}

#[cfg(test)]
mod tests_bulk_string {
    use crate::BulkString;

    #[test]
    fn test_parse() {
        let string = "$6\r\nfoobar\r\n:100\r\n";
        let mut cursor = 0;
        assert_eq!(
            BulkString::parse(string.as_bytes(), &mut cursor, &string.len()).unwrap(),
            BulkString::new(b"foobar")
        );
        assert_eq!(cursor, 12);
    }
}