firebase_rs_sdk/firestore/value/
bytes_value.rs

1use base64::{engine::general_purpose::STANDARD_NO_PAD, Engine as _};
2
3#[derive(Clone, Debug, PartialEq, Eq, Hash)]
4pub struct BytesValue(Vec<u8>);
5
6impl BytesValue {
7    pub fn new(bytes: Vec<u8>) -> Self {
8        Self(bytes)
9    }
10
11    pub fn from_base64(value: &str) -> Result<Self, base64::DecodeError> {
12        STANDARD_NO_PAD.decode(value).map(Self)
13    }
14
15    pub fn to_base64(&self) -> String {
16        STANDARD_NO_PAD.encode(&self.0)
17    }
18
19    pub fn as_slice(&self) -> &[u8] {
20        &self.0
21    }
22}
23
24impl From<Vec<u8>> for BytesValue {
25    fn from(value: Vec<u8>) -> Self {
26        Self::new(value)
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33
34    #[test]
35    fn base64_roundtrip() {
36        let bytes = BytesValue::new(vec![1, 2, 3, 4]);
37        let encoded = bytes.to_base64();
38        let decoded = BytesValue::from_base64(&encoded).unwrap();
39        assert_eq!(decoded.as_slice(), &[1, 2, 3, 4]);
40    }
41}