1use crate::Instance;
5use arcstr::ArcStr;
6use serde::{Deserialize, Serialize};
7use std::convert::Infallible;
8
9pub trait Schema {
12 type Primitive: Primitive + Sized;
14}
15
16pub trait Primitive {}
18
19impl<T> Primitive for T {}
20
21pub trait FromSchema<S: Schema + ?Sized>: Schema {
23 type Error;
25
26 fn convert_primitive(
28 primitive: <S as Schema>::Primitive,
29 ) -> Result<<Self as Schema>::Primitive, Self::Error>;
30
31 fn convert_instance(
34 instance: &mut Instance,
35 primitive: &<S as Schema>::Primitive,
36 ) -> Result<(), Self::Error>;
37}
38
39impl<S: Schema + ?Sized> FromSchema<S> for S {
40 type Error = Infallible;
41
42 fn convert_primitive(
43 primitive: <S as Schema>::Primitive,
44 ) -> Result<<Self as Schema>::Primitive, Self::Error> {
45 Ok(primitive)
46 }
47
48 fn convert_instance(
49 _instance: &mut Instance,
50 _primitive: &<S as Schema>::Primitive,
51 ) -> Result<(), Self::Error> {
52 Ok(())
53 }
54}
55
56pub struct NoSchema;
58
59#[derive(
61 Copy, Clone, Eq, PartialEq, Debug, Default, Hash, Serialize, Deserialize, thiserror::Error,
62)]
63#[error("attempted to convert a library containing primitives to/from `NoSchema`")]
64pub struct NoSchemaError;
65
66#[derive(Clone, Serialize, Deserialize)]
71pub struct NoPrimitive(());
72
73impl Schema for NoSchema {
74 type Primitive = NoPrimitive;
75}
76
77pub struct StringSchema;
79
80impl Schema for StringSchema {
81 type Primitive = ArcStr;
82}
83
84impl FromSchema<NoSchema> for StringSchema {
85 type Error = NoSchemaError;
86
87 fn convert_primitive(
88 _primitive: <NoSchema as Schema>::Primitive,
89 ) -> Result<<Self as Schema>::Primitive, Self::Error> {
90 Err(NoSchemaError)
91 }
92 fn convert_instance(
93 _instance: &mut Instance,
94 _primitive: &<NoSchema as Schema>::Primitive,
95 ) -> Result<(), Self::Error> {
96 Err(NoSchemaError)
97 }
98}