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
use polodb_bson::Document;
use crate::DbResult;
use std::io::Write;

#[derive(Eq, PartialEq, Copy, Clone)]
pub enum SerializeType {
    Default,
    Legacy
}

pub(crate) fn serialize(ty: SerializeType, doc: &Document, buf: &mut Vec<u8>) -> DbResult<()> {
    match ty {
        SerializeType::Default => {
            doc.to_msgpack(buf)?;
        },
        SerializeType::Legacy => {
            let tmp = doc.to_bytes()?;
            buf.write(&tmp)?;
        }
    }
    Ok(())
}

pub(crate) fn deserialize(ty: SerializeType, buf: &mut &[u8]) -> DbResult<Document> {
    match ty {
        SerializeType::Default => {
            let doc = Document::from_msgpack(buf)?;
            Ok(doc)
        },
        SerializeType::Legacy => {
            let doc = Document::from_bytes(buf)?;
            Ok(doc)
        },
    }
}