mybatis_drive/types/
bytes.rs

1use std::ops::{Deref, DerefMut};
2use rbson::Bson;
3use serde::{Deserializer, Serializer};
4use serde::de::Error;
5
6/// Rbatis Bytes
7#[derive(Debug, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
8pub struct Bytes {
9    pub inner: Vec<u8>,
10}
11
12impl From<rbson::Binary> for Bytes {
13    fn from(arg: rbson::Binary) -> Self {
14        Self {
15            inner: arg.bytes
16        }
17    }
18}
19
20impl From<&rbson::Binary> for Bytes {
21    fn from(arg: &rbson::Binary) -> Self {
22        Self {
23            inner: arg.bytes.clone()
24        }
25    }
26}
27
28impl From<&[u8]> for Bytes {
29    fn from(arg: &[u8]) -> Self {
30        Self {
31            inner: arg.to_owned()
32        }
33    }
34}
35
36impl From<Vec<u8>> for Bytes {
37    fn from(arg: Vec<u8>) -> Self {
38        Self {
39            inner: arg
40        }
41    }
42}
43
44impl From<&Vec<u8>> for Bytes {
45    fn from(arg: &Vec<u8>) -> Self {
46        Self {
47            inner: arg.to_owned()
48        }
49    }
50}
51
52impl serde::Serialize for Bytes {
53    #[inline]
54    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
55        serializer.serialize_bytes(&self.inner)
56    }
57}
58
59impl<'de> serde::Deserialize<'de> for Bytes {
60    #[inline]
61    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
62        let bson = Bson::deserialize(deserializer)?;
63        match bson {
64            Bson::Binary(data) => {
65                return Ok(Bytes {
66                    inner: data.bytes,
67                });
68            }
69            Bson::String(data) => {
70                return match base64::decode(data) {
71                    Ok(v) => {
72                        Ok(Bytes {
73                            inner: v,
74                        })
75                    }
76                    Err(e) => {
77                        return Err(D::Error::custom(e.to_string()));
78                    }
79                };
80            }
81            _ => {
82                Err(D::Error::custom("deserialize unsupported bson type!"))
83            }
84        }
85    }
86}
87
88impl Bytes {
89    pub fn new(arg: Vec<u8>) -> Bytes {
90        Bytes {
91            inner: arg
92        }
93    }
94}
95
96impl Deref for Bytes {
97    type Target = Vec<u8>;
98
99    fn deref(&self) -> &Self::Target {
100        &self.inner
101    }
102}
103
104impl DerefMut for Bytes {
105    fn deref_mut(&mut self) -> &mut Self::Target {
106        &mut self.inner
107    }
108}
109
110
111#[cfg(test)]
112mod test {
113    use rbson::Bson;
114    use crate::types::Bytes;
115
116    #[test]
117    fn test_ser_de() {
118        let b = Bytes::from("111".as_bytes());
119        let bsons = rbson::to_bson(&b).unwrap();
120        match &bsons {
121            rbson::Bson::Binary(b) => {
122                assert_eq!(b.subtype, rbson::spec::BinarySubtype::Generic);
123                println!("yes is BinarySubtype::Generic");
124            }
125            _ => {}
126        }
127        let b_de: Bytes = rbson::from_bson(bsons).unwrap();
128        assert_eq!(b, b_de);
129        assert_eq!(b.inner, b_de.inner);
130    }
131}