use crate::Instance;
use arcstr::ArcStr;
use serde::{Deserialize, Serialize};
use std::convert::Infallible;
pub trait Schema {
type Primitive: Primitive + Sized;
}
pub trait Primitive {}
impl<T> Primitive for T {}
pub trait FromSchema<S: Schema + ?Sized>: Schema {
type Error;
fn convert_primitive(
primitive: <S as Schema>::Primitive,
) -> Result<<Self as Schema>::Primitive, Self::Error>;
fn convert_instance(
instance: &mut Instance,
primitive: &<S as Schema>::Primitive,
) -> Result<(), Self::Error>;
}
impl<S: Schema + ?Sized> FromSchema<S> for S {
type Error = Infallible;
fn convert_primitive(
primitive: <S as Schema>::Primitive,
) -> Result<<Self as Schema>::Primitive, Self::Error> {
Ok(primitive)
}
fn convert_instance(
_instance: &mut Instance,
_primitive: &<S as Schema>::Primitive,
) -> Result<(), Self::Error> {
Ok(())
}
}
pub struct NoSchema;
#[derive(
Copy, Clone, Eq, PartialEq, Debug, Default, Hash, Serialize, Deserialize, thiserror::Error,
)]
#[error("attempted to convert a library containing primitives to/from `NoSchema`")]
pub struct NoSchemaError;
#[derive(Clone, Serialize, Deserialize)]
pub struct NoPrimitive(());
impl Schema for NoSchema {
type Primitive = NoPrimitive;
}
pub struct StringSchema;
impl Schema for StringSchema {
type Primitive = ArcStr;
}
impl FromSchema<NoSchema> for StringSchema {
type Error = NoSchemaError;
fn convert_primitive(
_primitive: <NoSchema as Schema>::Primitive,
) -> Result<<Self as Schema>::Primitive, Self::Error> {
Err(NoSchemaError)
}
fn convert_instance(
_instance: &mut Instance,
_primitive: &<NoSchema as Schema>::Primitive,
) -> Result<(), Self::Error> {
Err(NoSchemaError)
}
}