use super::{Dedup, Elected, ElectionLabel, IndexGraphql, IndexModel, Projected, Upstream};
use crate::model::{Column, GraphqlKind, NodeKind, Projection};
use crate::prefix::EmissionPrefix;
fn ident(name: &str) -> syn::Ident {
syn::Ident::new(name, proc_macro2::Span::call_site())
}
fn column(name: &str, ty: &str) -> Column {
Column {
name: ident(name),
source: ident(name),
ty: syn::parse_str(ty).expect("a type"),
project: Projection::Borrowed,
}
}
fn projected(field: &str, ty: &str) -> Projected {
Projected {
field: ident(field),
from: ident(field),
ty: syn::parse_str(ty).expect("a type"),
}
}
fn document(searchable: &[&str]) -> IndexModel {
IndexModel {
prefix: EmissionPrefix::default(),
numbers: crate::index::Numbers::default(),
field_numbers: Vec::new(),
stores: Vec::new(),
extension: None,
data: false,
keys: false,
subscribe: false,
graphql: Some(IndexGraphql::of(GraphqlKind::AsyncGraphql)),
rename: None,
slot: None,
index: ident("SearchDocument"),
kind: NodeKind::Fulltext,
vis: syn::Visibility::Public(syn::token::Pub::default()),
krate: syn::parse_quote!(::ingraph),
source: Upstream {
node: syn::parse_quote!(asr::Segment),
projections: vec![
projected("body", "String"),
projected("speaker", "String"),
projected("notes", "String"),
],
},
dedup: Dedup::Hash,
embeddings: Vec::new(),
labelled: Vec::new(),
searchable: searchable.iter().copied().map(ident).collect(),
engines: Vec::new(),
fields: vec![
column("body", "String"),
column("speaker", "String"),
column("notes", "String"),
],
}
}
fn retrieving(file: &syn::File) -> Vec<syn::ImplItemFn> {
fn methods(items: &[syn::Item], found: &mut Vec<syn::ImplItemFn>) {
for item in items {
match item {
syn::Item::Impl(declared) if declared.trait_.is_none() => {
found.extend(declared.items.iter().filter_map(|inner| match inner {
syn::ImplItem::Fn(method) if reads(&method.sig.ident) => Some(method.clone()),
_ => None,
}));
}
syn::Item::Macro(gated) => {
if let Ok(inside) = syn::parse2::<syn::File>(gated.mac.tokens.clone()) {
methods(&inside.items, found);
}
}
_ => {}
}
if !found.is_empty() {
return;
}
}
}
let mut found = Vec::new();
for item in &file.items {
let syn::Item::Const(anonymous) = item else {
continue;
};
let syn::Expr::Block(block) = &*anonymous.expr else {
continue;
};
for statement in &block.block.stmts {
match statement {
syn::Stmt::Item(item) => methods(std::slice::from_ref(item), &mut found),
syn::Stmt::Macro(gated) => {
if let Ok(inside) = syn::parse2::<syn::File>(gated.mac.tokens.clone()) {
methods(&inside.items, &mut found);
}
}
_ => {}
}
if !found.is_empty() {
break;
}
}
}
found
}
fn reads(name: &syn::Ident) -> bool {
let name = name.to_string();
name.starts_with("retrieve") || name.starts_with("get_")
}
fn emission(model: &IndexModel) -> syn::File {
syn::parse2(model.emit()).expect("the emission is a parsable file")
}
fn subject(model: &IndexModel) -> String {
let file = emission(model);
let mut said = String::new();
for method in &retrieving(&file) {
for attribute in &method.attrs {
if attribute.path().is_ident("doc") {
said.push_str("e::ToTokens::to_token_stream(attribute).to_string());
said.push('\n');
}
}
}
said
}
#[test]
fn the_full_text_subject_is_the_elected_fields_and_not_every_projection() {
let said = subject(&document(&["body", "notes"]));
assert!(
said.contains("in `body`, `notes`"),
"the subject names its elected fields, in declared order: {said}",
);
assert!(
!said.contains("speaker"),
"and a payload field is in no subject at all: {said}",
);
}
#[test]
fn a_single_election_is_named_too() {
let said = subject(&document(&["notes"]));
assert!(said.contains("in `notes`"), "{said}");
assert!(!said.contains("`body`"), "{said}");
assert!(!said.contains("speaker"), "{said}");
}
#[test]
fn the_full_text_election_grows_a_pair_beside_the_whole_subject_one() {
assert_eq!(
doors(&document(&["body", "notes"])),
[
"retrieve_keys",
"retrieve",
"retrieve_keys_by_body",
"retrieve_by_body",
"retrieve_keys_by_notes",
"retrieve_by_notes",
],
"two elections: the whole-subject pair, then one pair each in declared order",
);
assert_eq!(
doors(&document(&["notes"])),
[
"retrieve_keys",
"retrieve",
"retrieve_keys_by_notes",
"retrieve_by_notes",
],
"and ONE election is still two pairs — the whole-subject door is not the field's door",
);
assert!(
!doors(&document(&["body", "notes"]))
.iter()
.any(|door| door.contains("speaker")),
"a payload field is behind no door, which is the exclusion the subject states in prose",
);
}
#[test]
fn a_per_field_door_asks_its_own_column_and_the_whole_subject_door_asks_all() {
let file = emission(&document(&["body", "notes"]));
let body = |name: &str| {
retrieving(&file)
.into_iter()
.find(|method| method.sig.ident == name)
.map(|method| quote::ToTokens::to_token_stream(&method.block).to_string())
.unwrap_or_else(|| panic!("`{name}` is emitted"))
};
let whole = body("retrieve_keys");
assert!(
whole.contains("FulltextSubset :: All"),
"the whole-subject door asks over everything the slot holds: {whole}",
);
assert!(!whole.contains("These"), "and narrows nothing: {whole}");
for (door, column, other) in [
("retrieve_keys_by_body", "body", "notes"),
("retrieve_keys_by_notes", "notes", "body"),
] {
let said = body(door);
assert!(
said.contains("FulltextSubset :: These"),
"`{door}` narrows: {said}",
);
assert!(
said.contains(&format!("FulltextColumn :: new (\"{column}\")")),
"`{door}` names its own column: {said}",
);
assert!(
!said.contains(&format!("\"{other}\"")),
"`{door}` names no other election's: {said}",
);
}
}
fn embeddings(elected: &[&str]) -> IndexModel {
IndexModel {
prefix: EmissionPrefix::default(),
stores: Vec::new(),
extension: None,
data: false,
keys: false,
subscribe: false,
index: ident("SegmentEmbedding"),
kind: NodeKind::Vector,
source: Upstream {
node: syn::parse_quote!(asr::Segment),
projections: vec![
projected("image_embedding", "Embedding<f32, 4>"),
projected("caption_embedding", "Embedding<f32, 4>"),
projected("audio_embedding", "Embedding<f32, 4>"),
],
},
embeddings: elected
.iter()
.map(|field| {
Elected::new(
ident(field),
ElectionLabel::Embedding,
syn::parse_str("Embedding<f32, 4>").expect("a type"),
None,
)
})
.collect(),
labelled: Vec::new(),
searchable: Vec::new(),
fields: vec![
column("image_embedding", "Embedding<f32, 4>"),
column("caption_embedding", "Embedding<f32, 4>"),
column("audio_embedding", "Embedding<f32, 4>"),
],
..document(&[])
}
}
fn doors(model: &IndexModel) -> Vec<String> {
let file = emission(model);
retrieving(&file)
.iter()
.map(|method| method.sig.ident.to_string())
.collect()
}
#[test]
fn a_payload_embedding_is_behind_no_door() {
assert_eq!(
doors(&embeddings(&["caption_embedding", "audio_embedding"])),
[
"retrieve_keys_by_caption_embedding",
"retrieve_by_caption_embedding",
"get_caption_embedding",
"retrieve_similar_by_caption_embedding",
"retrieve_keys_by_audio_embedding",
"retrieve_by_audio_embedding",
"get_audio_embedding",
"retrieve_similar_by_audio_embedding",
],
"one QUAD per election, in declared order, and nothing at all for `image_embedding`",
);
}