use crate::schema::ast::{LazyAst, SchemaAst, SchemaKind};
use crate::schema::data::EffectData;
use crate::schema::parse::Schema;
pub trait HasSchema {
type A: 'static;
type I: 'static;
type E: EffectData + 'static;
fn schema() -> Schema<Self::A, Self::I, Self::E>
where
Self: Sized;
}
pub fn ast_ref<T: HasSchema + 'static>() -> SchemaAst {
let name: String = std::any::type_name::<T>()
.chars()
.map(|c| {
if c.is_ascii_alphanumeric() || matches!(c, '_' | '.' | ':' | '-') {
c
} else {
'_'
}
})
.collect();
SchemaAst::new(SchemaKind::Lazy(LazyAst::new(Some(name), || {
T::schema().ast().clone()
})))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::schema::parse::{Schema, i64};
struct OnlyInt;
impl HasSchema for OnlyInt {
type A = i64;
type I = i64;
type E = ();
fn schema() -> Schema<Self::A, Self::I, Self::E> {
i64::<()>()
}
}
#[test]
fn manual_impl_returns_schema() {
let s = OnlyInt::schema();
assert_eq!(s.decode(3_i64).unwrap(), 3);
}
struct Generic<T>(std::marker::PhantomData<T>);
impl<T: 'static> HasSchema for Generic<T> {
type A = i64;
type I = i64;
type E = ();
fn schema() -> Schema<Self::A, Self::I, Self::E> {
i64::<()>()
}
}
#[test]
fn ast_ref_is_a_named_lazy_with_a_fragment_safe_name() {
let SchemaKind::Lazy(lazy) = ast_ref::<OnlyInt>().kind else {
panic!("expected a lazy node");
};
assert!(lazy.name().unwrap().ends_with("::OnlyInt"));
assert_eq!(lazy.force().kind, SchemaKind::Integer);
let SchemaKind::Lazy(lazy) = ast_ref::<Generic<OnlyInt>>().kind else {
panic!("expected a lazy node");
};
let name = lazy.name().unwrap();
assert!(
name.contains("Generic_") && name.ends_with("OnlyInt_"),
"{name}"
);
}
}