use ingraph_codegen::{index::IndexModel, prelude::*};
fn tight(tokens: &proc_macro2::TokenStream) -> String {
tokens.to_string().split_whitespace().collect()
}
fn index(road: proc_macro2::TokenStream) -> IndexModel {
let input: syn::DeriveInput = syn::parse_quote! {
#[ingraph(
upstream(node = "media::Keyframe", clip = "clip"),
dedup(hash),
graphql(async_graphql)
)]
pub struct Shots {
#[ingraph(embedding, knn(searchable(tower = #road)), toy)]
clip: Embedding<f32, 4, Clip>,
}
};
let (host, arguments) = crate::host::split(&input);
crate::index::parse(&host, &arguments, NodeKind::Vector).expect("the declaration is legal")
}
fn sugared(road: proc_macro2::TokenStream) -> proc_macro2::TokenStream {
super::recipes(&index(road))
}
fn handwritten(
arguments: proc_macro2::TokenStream,
item: syn::DeriveInput,
) -> proc_macro2::TokenStream {
let arguments = crate::host::options(arguments);
let host = crate::host::echo(&item);
let expansion = crate::search::parse(&item, &arguments)
.as_ref()
.map_or_else(
syn::Error::to_compile_error,
ingraph_codegen::search::SearchModel::emit,
);
quote::quote!(#host #expansion)
}
fn twin() -> proc_macro2::TokenStream {
handwritten(
quote::quote! {
crate = "::ingraph",
shared(question(type = "<media::ClipText as ::ingraph::SearchTower>::Question")),
graphql(async_graphql)
},
syn::parse_quote! {
pub struct ShotsClipSearch {
#[ingraph(tower(question(type = "media::ClipText", shared)))]
shots: Shots,
}
},
)
}
#[test]
fn the_sugars_expansion_is_the_hand_written_recipes() {
assert_eq!(
tight(&sugared(quote::quote!("media::ClipText"))),
tight(&twin()),
"the forged recipe is not the hand-written one",
);
}
#[test]
fn a_different_tower_forges_a_different_expansion() {
assert_ne!(
tight(&sugared(quote::quote!("media::ClipImage"))),
tight(&twin()),
"the expansion does not depend on the tower the election named",
);
}
#[test]
fn an_election_with_no_road_forges_no_recipe() {
let input: syn::DeriveInput = syn::parse_quote! {
#[ingraph(upstream(node = "media::Keyframe", clip = "clip"), dedup(hash))]
pub struct Shots {
#[ingraph(embedding, knn, toy)]
clip: Embedding<f32, 4, Clip>,
}
};
let (host, arguments) = crate::host::split(&input);
let model = crate::index::parse(&host, &arguments, NodeKind::Vector).expect("legal");
assert!(
super::recipes(&model).is_empty(),
"a declaration that wrote no road forged a recipe anyway",
);
}
#[test]
fn the_forged_declaration_names_the_index_and_the_column() {
let model = index(quote::quote!("media::ClipText"));
let elected = &model.embeddings[0];
let road = elected.tower_road.as_ref().expect("a road");
let (arguments, item) = super::declaration(&model, elected, road);
assert_eq!(item.ident, "ShotsClipSearch");
let syn::Data::Struct(data) = &item.data else {
panic!("the forged declaration is a struct")
};
let member = data.fields.iter().next().expect("one member");
assert_eq!(member.ident.as_ref().expect("a named member"), "shots");
let typed = quote::quote!(#member).to_string();
assert!(typed.contains("Shots"), "{typed}");
let head = tight("e::quote!(#(#arguments)*));
assert!(head.contains("SearchTower>::Question"), "{head}");
assert!(head.contains("graphql(async_graphql)"), "{head}");
}
#[test]
fn an_undressed_index_forges_an_undressed_recipe() {
let input: syn::DeriveInput = syn::parse_quote! {
#[ingraph(upstream(node = "media::Keyframe", clip = "clip"), dedup(hash))]
pub struct Shots {
#[ingraph(embedding, knn(searchable(tower = "media::ClipText")), toy)]
clip: Embedding<f32, 4, Clip>,
}
};
let (host, arguments) = crate::host::split(&input);
let model = crate::index::parse(&host, &arguments, NodeKind::Vector).expect("legal");
let elected = &model.embeddings[0];
let (head, _) = super::declaration(
&model,
elected,
elected.tower_road.as_ref().expect("a road"),
);
assert!(
!tight("e::quote!(#(#head)*)).contains("graphql"),
"an index that named no backend forged a dressed recipe",
);
}