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
use rusqlite::{types::FromSql, ToSql};
use std::{borrow::Borrow, fmt::Debug};

/// The trait that tells collections how to serialize and deserialize their
/// types.
/// Serializers are typically built on Serde, but aren't required to be.  For
/// instance, `Ipv4Addr` can be directly stored and loaded as a SQLite INTEGER
/// using the `From` Trait.  There are unit tests for exactly this use-case.
pub trait Serializer: Debug {
    /// A borrowed target, for serialization.  This is exposed to the user of
    /// the collections in order for them to insert or query the collection.
    type TargetBorrowed: ?Sized;

    /// An owned target, for deserialization.  This is exposed to the user in
    /// objects returned to them from queries.
    type Target: Borrow<Self::TargetBorrowed>;

    /// A borrowed buffer, for deserialization.  This is not exposed to the
    /// user.
    type BufferBorrowed: ?Sized + ToSql;

    /// An owned buffer, for serialization.  This is not exposed to the user.
    type Buffer: Borrow<Self::BufferBorrowed> + FromSql;
    type SerializeError: std::error::Error;
    type DeserializeError: std::error::Error;

    /// The column type.  This is not a constant, because you might want to
    /// query the SQLite version to select the best type for the version (for
    /// instance, selecting ANY for a STRICT table only where it's available,
    /// and BLOB otherwise).
    fn sql_type() -> &'static str;

    /// Serialize a borrowed target into a buffer.
    fn serialize(target: &Self::TargetBorrowed) -> Result<Self::Buffer, Self::SerializeError>;

    /// Deserialize a target from a borrowed buffer.
    fn deserialize(data: &Self::BufferBorrowed) -> Result<Self::Target, Self::DeserializeError>;
}

mod direct;
pub use direct::Direct;

#[cfg(feature = "cbor")]
mod cbor;

#[cfg(feature = "cbor")]
pub use cbor::Cbor;

#[cfg(feature = "json")]
mod json;

#[cfg(feature = "json")]
pub use json::Json;

#[cfg(feature = "postcard")]
mod postcard;

#[cfg(feature = "postcard")]
pub use self::postcard::Postcard;