use ingraph_codegen::model::NodeKind;
use quote::ToTokens;
use super::{Face, emit};
fn structural(mode: super::Mode) -> ingraph_codegen::standalone::StandaloneModel {
match mode {
super::Mode::Struct(model) => *model,
super::Mode::Composed(_) | super::Mode::Element(_) => {
panic!("a declaration with no marker is the struct mode")
}
}
}
fn tight(tokens: impl ToTokens) -> String {
tokens.to_token_stream().to_string().replace(' ', "")
}
fn base_of(ty: &syn::Type) -> String {
match ty {
syn::Type::Path(path) => path
.path
.segments
.last()
.map_or_else(String::new, |segment| segment.ident.to_string()),
_ => String::new(),
}
}
fn method(items: &[syn::Item], name: &str) -> String {
for item in items {
let syn::Item::Impl(block) = item else {
continue;
};
for member in &block.items {
if let syn::ImplItem::Fn(function) = member
&& function.sig.ident == name
{
return tight(function);
}
}
}
panic!("no `{name}` in the emitted block");
}
fn refused<T>(result: syn::Result<T>) -> syn::Error {
match result {
Ok(_) => panic!("the declaration was accepted where a refusal was expected"),
Err(refused) => refused,
}
}
fn joined(items: &[syn::Item]) -> String {
items.iter().map(tight).collect()
}
fn bare(input: &syn::DeriveInput, face: Face) -> proc_macro2::TokenStream {
let model = super::parse(input, face).expect("a declaration the bare door accepts");
emit(&model, face)
}
fn worded(input: &syn::DeriveInput) -> proc_macro2::TokenStream {
let (host, options) = crate::host::split(input);
crate::parse::parse(&host, &options, NodeKind::Record)
.expect("a declaration the grammar word accepts")
.emit()
}
fn items(expansion: &proc_macro2::TokenStream, name: &str) -> Vec<syn::Item> {
let file: syn::File = syn::parse2(expansion.clone()).expect("an expansion is a file");
let mut found = Vec::new();
collect(&file.items, name, &mut found);
found
}
fn collect(items: &[syn::Item], name: &str, found: &mut Vec<syn::Item>) {
for item in items {
match item {
syn::Item::Struct(declared) if declared.ident == name => found.push(item.clone()),
syn::Item::Enum(declared) if declared.ident == name => found.push(item.clone()),
syn::Item::Impl(block) if block.trait_.is_none() && base_of(&block.self_ty) == name => {
found.push(item.clone());
}
syn::Item::Const(constant) => {
if let syn::Expr::Block(block) = &*constant.expr {
let nested: Vec<syn::Item> = block
.block
.stmts
.iter()
.filter_map(|stmt| match stmt {
syn::Stmt::Item(item) => Some(item.clone()),
_ => None,
})
.collect();
collect(&nested, name, found);
}
}
_ => {}
}
}
}
#[test]
fn the_filter_face_is_one_emission() {
let word: syn::DeriveInput = syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
pub struct Cue {
#[ingraph(id)]
id: u64,
#[ingraph(string)]
title: String,
#[ingraph(scalar)]
mark: i64,
}
};
let bare_door: syn::DeriveInput = syn::parse_quote! {
pub struct Cue {
id: u64,
#[ingraph(string)]
title: String,
#[ingraph(scalar)]
mark: i64,
}
};
let worded = joined(&items(&worded(&word), "CueFilter"));
assert!(worded.contains("structCueFilter"), "{worded}");
assert!(worded.contains("fnwith_title"), "{worded}");
assert_eq!(
worded,
joined(&items(&bare(&bare_door, Face::Filter), "CueFilter")),
);
}
#[test]
fn the_order_by_enum_is_one_emission() {
let word: syn::DeriveInput = syn::parse_quote! {
#[ingraph(sqlite)]
pub struct Cue {
#[ingraph(id)]
id: u64,
#[ingraph(string)]
title: String,
#[ingraph(scalar)]
mark: i64,
}
};
let bare_door: syn::DeriveInput = syn::parse_quote! {
pub struct Cue {
id: u64,
title: String,
mark: i64,
}
};
let worded = items(&worded(&word), "CueOrderBy");
let bare = items(&bare(&bare_door, Face::OrderBy), "CueOrderBy");
assert_eq!(worded.len(), 2, "an order-by is an enum and its methods");
assert_eq!(bare.len(), 2, "an order-by is an enum and its methods");
assert!(
tight(&worded[0]).contains("enumCueOrderBy"),
"the enum is present"
);
assert_eq!(tight(&worded[0]), tight(&bare[0]));
assert_eq!(method(&worded, "direction"), method(&bare, "direction"));
assert_eq!(method(&worded, "identity"), method(&bare, "identity"));
}
#[test]
fn the_comparison_face_is_one_emission() {
let word: syn::DeriveInput = syn::parse_quote! {
#[ingraph(sqlite)]
pub struct Cue {
#[ingraph(id)]
id: u64,
#[ingraph(string)]
title: String,
}
};
let bare_door: syn::DeriveInput = syn::parse_quote! {
#[ingraph(sqlite)]
pub struct Cue {
id: u64,
#[ingraph(string)]
title: String,
}
};
let worded = items(&worded(&word), "CueSqliteComparison");
let bare = items(&bare(&bare_door, Face::Comparison), "CueSqliteComparison");
assert_eq!(worded.len(), 2, "a comparison is a struct and its methods");
assert_eq!(bare.len(), 2, "a comparison is a struct and its methods");
assert!(
tight(&worded[0]).contains("structCueSqliteComparison"),
"the struct is present"
);
assert_eq!(tight(&worded[0]), tight(&bare[0]));
assert_eq!(tight(&worded[1]), tight(&bare[1]));
}
#[test]
fn a_records_word_refuses_by_name() {
let input: syn::DeriveInput = syn::parse_quote! {
pub struct Cue {
#[ingraph(id)]
id: u64,
}
};
let refused = refused(super::parse(&input, Face::Filter));
let sentence = refused.to_string();
assert!(sentence.contains("`id`"), "{sentence}");
assert!(sentence.contains("RECORD"), "{sentence}");
assert!(sentence.contains("#[ingraph::record"), "{sentence}");
}
#[test]
fn an_orphan_field_conversion_refuses_by_name() {
let input: syn::DeriveInput = syn::parse_quote! {
#[ingraph(sqlite)]
pub struct Cue {
#[ingraph(sqlite(type = "i64", try_into = "cross"))]
mark: u64,
}
};
let refused = refused(super::parse(&input, Face::Comparison));
let sentence = refused.to_string();
assert!(sentence.contains("try_into"), "{sentence}");
assert!(
sentence.contains("filter = \\\"path::to::Filter\\\"") || sentence.contains("filter ="),
"{sentence}"
);
}
#[test]
fn the_same_conversion_under_a_hook_is_accepted() {
let input: syn::DeriveInput = syn::parse_quote! {
#[ingraph(sqlite, filter = "CueFilter")]
pub struct Cue {
#[ingraph(sqlite(type = "i64", try_into = "cross"))]
mark: u64,
}
};
super::parse(&input, Face::Comparison).expect("a conversion under a hook is legal");
}
#[test]
fn a_field_naming_an_undeclared_database_refuses() {
let input: syn::DeriveInput = syn::parse_quote! {
#[ingraph(sqlite)]
pub struct Cue {
#[ingraph(postgres(type = "i64"))]
mark: u64,
}
};
let refused = refused(super::parse(&input, Face::Comparison));
let sentence = refused.to_string();
assert!(sentence.contains("postgres"), "{sentence}");
assert!(sentence.contains("the head's store words"), "{sentence}");
}
#[test]
fn a_wrong_container_word_refuses() {
let input: syn::DeriveInput = syn::parse_quote! {
#[ingraph(filters(rename = "CueWhere"))]
pub struct Cue {
mark: u64,
}
};
let sentence = refused(super::parse(&input, Face::Filter)).to_string();
assert!(sentence.contains("unknown entry"), "{sentence}");
assert!(sentence.contains("filter(rename"), "{sentence}");
}
#[test]
fn the_two_filter_words_are_told_apart_by_their_shape() {
let renamed: syn::DeriveInput = syn::parse_quote! {
#[ingraph(filter(rename = "CueWhere"))]
pub struct Cue {
mark: u64,
}
};
let model = structural(super::parse(&renamed, Face::Filter).expect("a rename block"));
assert_eq!(model.filter.safe().to_string(), "CueWhere");
assert!(model.hook.is_none());
let hooked: syn::DeriveInput = syn::parse_quote! {
#[ingraph(sqlite, filter = "CueWhere")]
pub struct Cue {
mark: u64,
}
};
let model = structural(super::parse(&hooked, Face::Comparison).expect("a hook"));
assert_eq!(model.filter.safe().to_string(), "CueFilter");
assert_eq!(tight(model.hook.as_ref().expect("a hook")), "CueWhere");
}
#[test]
fn the_declarations_shape_is_refused_at_the_door() {
let positional: syn::DeriveInput = syn::parse_quote!(
pub struct Cue(u64);
);
let sentence = refused(super::parse(&positional, Face::Filter)).to_string();
assert!(sentence.contains("NAMED fields"), "{sentence}");
let generic: syn::DeriveInput = syn::parse_quote! {
pub struct Cue<T> {
mark: T,
}
};
let sentence = refused(super::parse(&generic, Face::OrderBy)).to_string();
assert!(sentence.contains("CONCRETE"), "{sentence}");
let vocabulary: syn::DeriveInput = syn::parse_quote! {
pub enum Cue {
One,
}
};
let sentence = refused(super::parse(&vocabulary, Face::Comparison)).to_string();
assert!(sentence.contains("structs only"), "{sentence}");
}