sqlite_collections/format/
parse.rs

1use std::{convert::Infallible, marker::PhantomData, str::FromStr};
2
3use super::Format;
4
5/// A format that uses ToString and FromStr to parse to and from a string representation.
6///
7/// This is a good choice if you have a type that has non-structured string
8/// serialization needs, like for Uuids or IP addresses.
9pub struct Parse<Out, In = Out>(PhantomData<Out>, PhantomData<In>)
10where
11    In: ?Sized + ToString,
12    Out: FromStr,
13    <Out as FromStr>::Err: std::error::Error;
14
15impl<Out, In> Format for Parse<Out, In>
16where
17    In: ?Sized + ToString,
18    Out: FromStr,
19    <Out as FromStr>::Err: std::error::Error,
20{
21    type Out = Out;
22    type In = In;
23    type Buffer = String;
24    type SerializeError = Infallible;
25    type DeserializeError = <Out as FromStr>::Err;
26
27    fn sql_type() -> &'static str {
28        "TEXT"
29    }
30
31    fn serialize(object: &Self::In) -> Result<Self::Buffer, Self::SerializeError> {
32        Ok(object.to_string())
33    }
34
35    fn deserialize(data: &Self::Buffer) -> Result<Self::Out, Self::DeserializeError> {
36        data.parse()
37    }
38}