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
use std::ops::{Deref, DerefMut};
use rbson::Bson;
use serde::{Deserializer, Serializer};
use serde::de::Error;

/// Rbatis Bytes
#[derive(Debug, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Bytes {
    pub inner: Vec<u8>,
}

impl From<rbson::Binary> for Bytes {
    fn from(arg: rbson::Binary) -> Self {
        Self {
            inner: arg.bytes
        }
    }
}

impl From<&rbson::Binary> for Bytes {
    fn from(arg: &rbson::Binary) -> Self {
        Self {
            inner: arg.bytes.clone()
        }
    }
}

impl From<&[u8]> for Bytes {
    fn from(arg: &[u8]) -> Self {
        Self {
            inner: arg.to_owned()
        }
    }
}

impl From<Vec<u8>> for Bytes {
    fn from(arg: Vec<u8>) -> Self {
        Self {
            inner: arg
        }
    }
}

impl From<&Vec<u8>> for Bytes {
    fn from(arg: &Vec<u8>) -> Self {
        Self {
            inner: arg.to_owned()
        }
    }
}

impl serde::Serialize for Bytes {
    #[inline]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
        serializer.serialize_bytes(&self.inner)
    }
}

impl<'de> serde::Deserialize<'de> for Bytes {
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
        let bson = Bson::deserialize(deserializer)?;
        match bson {
            Bson::Binary(data) => {
                return Ok(Bytes {
                    inner: data.bytes,
                });
            }
            Bson::String(data) => {
                return match base64::decode(data) {
                    Ok(v) => {
                        Ok(Bytes {
                            inner: v,
                        })
                    }
                    Err(e) => {
                        return Err(D::Error::custom(e.to_string()));
                    }
                };
            }
            _ => {
                Err(D::Error::custom("deserialize unsupported bson type!"))
            }
        }
    }
}

impl Bytes {
    pub fn new(arg: Vec<u8>) -> Bytes {
        Bytes {
            inner: arg
        }
    }
}

impl Deref for Bytes {
    type Target = Vec<u8>;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl DerefMut for Bytes {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}


#[cfg(test)]
mod test {
    use rbson::Bson;
    use crate::types::Bytes;

    #[test]
    fn test_ser_de() {
        let b = Bytes::from("111".as_bytes());
        let bsons = rbson::to_bson(&b).unwrap();
        match &bsons {
            rbson::Bson::Binary(b) => {
                assert_eq!(b.subtype, rbson::spec::BinarySubtype::Generic);
                println!("yes is BinarySubtype::Generic");
            }
            _ => {}
        }
        let b_de: Bytes = rbson::from_bson(bsons).unwrap();
        assert_eq!(b, b_de);
        assert_eq!(b.inner, b_de.inner);
    }
}