sqlite_collections/format.rs
1use rusqlite::{types::FromSql, ToSql};
2
3/// The trait that tells collections how to serialize and deserialize their
4/// types.
5/// Serializers are typically built on Serde, but aren't required to be. For
6/// instance, `Ipv4Addr` can be directly stored and loaded as a SQLite INTEGER
7/// using the `From` Trait, and [`Parse`] can be used for types that implement
8/// Display and FromStr.
9///
10/// [`Parse`]: struct.Parse.html
11pub trait Format {
12 /// The type that is passed into function calls, for insertion, checking, etc.
13 /// This type is serialized.
14 type In: ?Sized;
15
16 /// The type that is returned from function calls.
17 /// This type is deserialized.
18 type Out;
19
20 /// An owned buffer, for serialization and deserialization. This is not
21 /// exposed to the user.
22 type Buffer: ToSql + FromSql;
23
24 type SerializeError: std::error::Error;
25 type DeserializeError: std::error::Error;
26
27 /// The column type. This is not a constant, because you might want to
28 /// query the SQLite version to select the best type for the version (for
29 /// instance, selecting ANY for a STRICT table only where it's available,
30 /// and BLOB otherwise).
31 fn sql_type() -> &'static str;
32
33 /// Serialize a borrowed target into a buffer.
34 fn serialize(target: &Self::In) -> Result<Self::Buffer, Self::SerializeError>;
35
36 /// Deserialize a target from a borrowed buffer.
37 fn deserialize(data: &Self::Buffer) -> Result<Self::Out, Self::DeserializeError>;
38}
39
40mod raw;
41pub use raw::Raw;
42
43mod parse;
44pub use parse::Parse;
45
46#[cfg(feature = "cbor")]
47mod cbor;
48
49#[cfg(feature = "cbor")]
50pub use cbor::Cbor;
51
52#[cfg(feature = "json")]
53mod json;
54
55#[cfg(feature = "json")]
56pub use json::Json;
57
58#[cfg(feature = "postcard")]
59mod postcard;
60
61#[cfg(feature = "postcard")]
62pub use self::postcard::Postcard;
63
64#[cfg(feature = "msgpack")]
65mod msgpack;
66
67#[cfg(feature = "msgpack")]
68pub use self::msgpack::Msgpack;