sqlite_collections/format/
raw.rs

1use rusqlite::{types::FromSql, ToSql};
2use std::{borrow::Borrow, convert::Infallible, marker::PhantomData, ops::Deref};
3
4use crate::db;
5
6use super::Format;
7
8/// A plain SQL format, just serializing and deserializing raw sql types
9/// directly.
10///
11/// This is the easiest way of working with this library, and is a great choice
12/// for keys.
13pub struct Raw<Out, In = <Out as Deref>::Target>(PhantomData<Out>, PhantomData<In>)
14where
15    In: ToSql + ?Sized + ToOwned<Owned = Out>,
16    Out: ToSql + FromSql + Clone,
17    Out: Borrow<In>;
18
19impl<Out, In> Format for Raw<Out, In>
20where
21    In: ToSql + ?Sized + ToOwned<Owned = Out>,
22    Out: ToSql + FromSql + Clone,
23    Out: Borrow<In>,
24{
25    type Out = Out;
26    type In = In;
27    type Buffer = Out;
28    type SerializeError = Infallible;
29    type DeserializeError = Infallible;
30
31    fn sql_type() -> &'static str {
32        db::any()
33    }
34
35    fn serialize(object: &Self::In) -> Result<Self::Buffer, Self::SerializeError> {
36        Ok(object.to_owned())
37    }
38
39    fn deserialize(data: &Self::Buffer) -> Result<Self::Out, Self::DeserializeError> {
40        Ok(data.clone())
41    }
42}