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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use crate::{Error, Result};
use std::fmt;

//#[derive(Clone)]
pub enum BulkString {
    Str(&'static str),
    String(String),
    Binary(Vec<u8>),
    Integer(i64),
    Nil,
}

impl BulkString {
    pub fn len(&self) -> usize {
        match self {
            BulkString::Str(s) => s.len(),
            BulkString::String(s) => s.len(),
            BulkString::Binary(s) => s.len(),
            BulkString::Integer(_) => unimplemented!(),
            BulkString::Nil => unimplemented!(),
        }
    }

    pub fn as_bytes(&self) -> &[u8] {
        match self {
            BulkString::Str(s) => s.as_bytes(),
            BulkString::String(s) => s.as_bytes(),
            BulkString::Binary(s) => &s,
            BulkString::Integer(_) => unimplemented!(),
            BulkString::Nil => unimplemented!(),
        }
    }
}

impl From<&'static str> for BulkString {
    fn from(str: &'static str) -> Self {
        Self::Str(str)
    }
}

impl From<String> for BulkString {
    fn from(string: String) -> Self {
        Self::String(string)
    }
}

impl From<Vec<u8>> for BulkString {
    fn from(binary: Vec<u8>) -> Self {
        Self::Binary(binary)
    }
}

impl From<i64> for BulkString {
    fn from(i: i64) -> Self {
        Self::Integer(i)
    }
}

impl From<u64> for BulkString {
    fn from(u: u64) -> Self {
        Self::Integer(u as i64)
    }
}

impl From<i32> for BulkString {
    fn from(i: i32) -> Self {
        Self::Integer(i as i64)
    }
}

impl From<u32> for BulkString {
    fn from(u: u32) -> Self {
        Self::Integer(u as i64)
    }
}

impl From<isize> for BulkString {
    fn from(i: isize) -> Self {
        Self::Integer(i as i64)
    }
}

impl From<usize> for BulkString {
    fn from(u: usize) -> Self {
        Self::Integer(u as i64)
    }
}

impl From<f32> for BulkString {
    fn from(f: f32) -> Self {
        Self::String(f.to_string())
    }
}

impl From<f64> for BulkString {
    fn from(f: f64) -> Self {
        Self::String(f.to_string())
    }
}

impl From<BulkString> for Result<String> {
    fn from(bs: BulkString) -> Self {
        match bs {
            BulkString::Str(s) => Ok(s.to_owned()),
            BulkString::String(s) => Ok(s),
            BulkString::Binary(s) => String::from_utf8(s).map_err(|e| Error::Parse(e.to_string())),
            BulkString::Integer(i) => Ok(i.to_string()),
            BulkString::Nil => Ok(String::from("")),
        }
    }
}

impl ToString for BulkString {
    fn to_string(&self) -> String {
        match self {
            BulkString::Str(s) => (*s).to_owned(),
            BulkString::String(s) => s.clone(),
            BulkString::Binary(s) => String::from_utf8_lossy(s).into_owned(),
            BulkString::Integer(i) => i.to_string(),
            BulkString::Nil => String::from(""),
        }
    }
}

impl fmt::Debug for BulkString {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Str(arg0) => f.debug_tuple("Str").field(arg0).finish(),
            Self::String(arg0) => f.debug_tuple("String").field(arg0).finish(),
            Self::Binary(arg0) => f
                .debug_tuple("Binary")
                .field(&String::from_utf8_lossy(arg0).into_owned())
                .finish(),
            Self::Integer(arg0) => f.debug_tuple("Integer").field(arg0).finish(),
            Self::Nil => write!(f, "Nil"),
        }
    }
}

/// Initialize a Bulksring as [BulkString::Nil](crate::resp::BulkString::Nil)
impl Default for BulkString {
    fn default() -> Self {
        BulkString::Nil
    }
}