use ingraph_codegen::{
index::{Dedup, ElectionLabel, Fold, IndexModel},
prelude::*,
};
use quote::ToTokens as _;
const DERIVED: [NodeKind; 3] = [NodeKind::Fulltext, NodeKind::Vector, NodeKind::Blob];
pub(super) fn parse(input: &syn::DeriveInput, kind: NodeKind) -> syn::Result<IndexModel> {
let (host, arguments) = crate::host::split(input);
super::parse(&host, &arguments, kind)
}
pub(super) fn fulltext(input: &syn::DeriveInput) -> syn::Result<IndexModel> {
parse(input, NodeKind::Fulltext)
}
fn refusals(input: syn::DeriveInput) -> String {
refused(input, NodeKind::Fulltext)
}
pub(super) fn refused(input: syn::DeriveInput, kind: NodeKind) -> String {
parse(&input, kind)
.expect_err("the declaration is refused")
.into_iter()
.map(|e| e.to_string())
.collect::<Vec<_>>()
.join("\n")
}
fn document() -> syn::DeriveInput {
syn::parse_quote! {
#[ingraph(upstream(node = "asr::Segment", title = "speaker", body = "text"), dedup(hash))]
pub struct SearchDocument {
#[ingraph(searchable)]
title: String,
#[ingraph(searchable)]
body: String,
}
}
}
fn blob_document() -> syn::DeriveInput {
syn::parse_quote! {
#[ingraph(upstream(node = "asr::Segment", title = "speaker", body = "text"))]
pub struct Artifact { title: String, body: String }
}
}
fn vector_document() -> syn::DeriveInput {
syn::parse_quote! {
#[ingraph(
upstream(
node = "asr::Segment",
caption_embedding = "caption_embedding",
image_embedding = "image_embedding",
),
dedup(hash),
)]
pub struct SegmentEmbedding {
caption_embedding: Embedding<f32, 4>,
#[ingraph(embedding, knn)]
image_embedding: Embedding<f32, 4>,
}
}
}
#[test]
fn an_index_is_its_upstream_and_its_fields_in_order() {
let m = fulltext(&document()).expect("a derived index");
assert_eq!(m.index, "SearchDocument");
assert_eq!(m.kind, NodeKind::Fulltext);
assert_eq!(
m.source.node.to_token_stream().to_string(),
"asr :: Segment"
);
assert_eq!(
m.fields
.iter()
.map(|column| column.name.to_string())
.collect::<Vec<_>>(),
["title", "body"]
);
}
#[test]
fn the_projections_are_ordered_by_the_declaration() {
let m = fulltext(&syn::parse_quote! {
#[ingraph(upstream(node = "asr::Segment", body = "text", title = "speaker"), dedup(hash))]
struct SearchDocument {
#[ingraph(searchable)] title: String,
#[ingraph(searchable)] body: String,
}
})
.expect("a derived index");
assert_eq!(
m.source
.projections
.iter()
.map(|projected| (projected.field.to_string(), projected.from.to_string()))
.collect::<Vec<_>>(),
[
("title".to_owned(), "speaker".to_owned()),
("body".to_owned(), "text".to_owned()),
]
);
}
#[test]
fn the_three_projection_words_reach_the_model() {
let m = fulltext(&syn::parse_quote! {
#[ingraph(upstream(node = "a::A", w = "w", tags = "tags", note = "note", plain = "plain"), dedup(hash))]
struct D {
#[ingraph(copy)] w: u32,
#[ingraph(repeated)] tags: Vec<String>,
#[ingraph(optional)] note: Option<String>,
#[ingraph(searchable)] plain: String,
}
})
.expect("a derived index");
let project = |name: &str| {
m.fields
.iter()
.find(|column| column.name == name)
.unwrap_or_else(|| panic!("`{name}` is a field"))
.project
};
assert_eq!(project("w"), Projection::Copied);
assert_eq!(project("tags"), Projection::Repeated);
assert_eq!(project("note"), Projection::Optional);
assert_eq!(project("plain"), Projection::Borrowed);
}
#[test]
fn the_crate_path_defaults_and_is_moveable() {
let m = fulltext(&document()).expect("the default");
assert_eq!(m.krate.to_token_stream().to_string(), ":: ingraph");
let m = fulltext(&syn::parse_quote! {
#[ingraph(crate = "shim::vendored", upstream(node = "a::A", w = "w"), dedup(hash))]
struct D { #[ingraph(searchable)] w: String }
})
.expect("a re-export shim");
assert_eq!(m.krate.to_token_stream().to_string(), "shim :: vendored");
}
#[test]
fn the_three_storages_share_one_grammar() {
for kind in DERIVED {
let written = match kind {
NodeKind::Vector => vector_document(),
NodeKind::Fulltext => document(),
_ => blob_document(),
};
let m = parse(&written, kind).expect("one grammar");
assert_eq!(m.kind, kind);
assert_eq!(m.source.projections.len(), 2);
assert_eq!(!m.embeddings.is_empty(), kind == NodeKind::Vector);
assert_eq!(!m.searchable.is_empty(), kind == NodeKind::Fulltext);
}
}
#[test]
fn every_spelling_of_an_identity_is_refused_by_name() {
let e = refusals(syn::parse_quote! {
#[ingraph(
subscribe = "a::A",
key(rename = "DKey"),
lineage(fields(a)),
upstream(node = "a::A", a = "a"),
)]
struct D { #[ingraph(id)] a: u32 }
});
for rule in [
"`subscribe` is a BARE word here",
"does not name its key type, because it declares none",
"does not declare a lineage",
"because it mints none",
] {
assert!(e.contains(rule), "missing `{rule}` in: {e}");
}
}
#[test]
fn every_family_a_derived_index_does_not_claim_is_refused_by_name() {
let e = refusals(syn::parse_quote! {
#[ingraph(
persist(sqlite),
derived(captions = "a::Caption"),
implements(Searchable),
vector(lance),
blob(fs),
upstream(node = "a::A", a = "a"))]
struct D { a: u32 }
});
for rule in [
"a derived index stores no rows",
"a derived index declares no reverse positions",
"an interface's members are NODES WITH ROWS",
"a full-text index owns no embedding index",
"a full-text index owns no artifact family",
] {
assert!(e.contains(rule), "missing `{rule}` in: {e}");
}
assert!(
e.contains("One node holds ONE storage"),
"and every one of them carries the ruling it follows from: {e}"
);
}
#[test]
fn a_nodes_own_storage_block_is_the_wrong_grain_for_its_engines() {
for kind in DERIVED {
let block: proc_macro2::TokenStream = kind.as_str().parse().expect("a word");
let e = refused(
syn::parse_quote! {
#[ingraph(#block(engine), upstream(node = "a::A", a = "a"))]
struct D { a: u32 }
},
kind,
);
assert!(
e.contains("is not where") && e.contains("a slot is one FIELD in one engine"),
"{}: {e}",
kind.as_str(),
);
assert!(
!e.contains("has not arrived"),
"{}: and it is no longer a wait: {e}",
kind.as_str(),
);
}
}
#[test]
fn a_derived_index_names_one_upstream() {
let e = refusals(syn::parse_quote! { struct D { a: u32 } });
assert!(
e.contains("draws its documents from an upstream")
&& e.contains("without an upstream it has no key to be addressed by either"),
"{e}"
);
let e = refusals(syn::parse_quote! {
#[ingraph(upstream(a = "a"), dedup(hash))]
struct D { a: u32 }
});
assert!(
e.contains("`upstream(…)` needs `node = \"path::To::Upstream\"`"),
"{e}"
);
}
#[test]
fn a_source_with_no_node_still_reports_its_projections() {
let e = refusals(syn::parse_quote! {
#[ingraph(upstream(missing = "a"), dedup(hash))]
struct D { a: u32 }
});
assert!(
e.contains("`missing` is not one of `D`'s") && e.contains("`a` is read out of nothing"),
"{e}"
);
}
#[test]
fn a_second_source_is_the_multi_source_shape() {
let e = refusals(syn::parse_quote! {
#[ingraph(upstream(node = "a::A", w = "w"), upstream(node = "b::B", w = "w"), dedup(hash))]
struct D { w: u32 }
});
assert!(
e.contains("a second `upstream(…)` is the MULTI-UPSTREAM shape")
&& e.contains("becomes an enum over both upstreams' keys"),
"{e}"
);
}
#[test]
fn every_field_is_projected_and_every_projection_names_a_field() {
let e = refusals(syn::parse_quote! {
#[ingraph(upstream(node = "a::A", caption = "text"), dedup(hash))]
struct D { body: String }
});
assert!(
e.contains("`caption` is not one of `D`'s"),
"the entry names no field: {e}"
);
assert!(
e.contains("`body` is read out of nothing"),
"and the field is named by no entry: {e}"
);
}
#[test]
fn a_field_is_projected_once() {
let e = refusals(syn::parse_quote! {
#[ingraph(upstream(node = "a::A", w = "w", w = "v"), dedup(hash))]
struct D { w: u32 }
});
assert!(e.contains("`w` is projected more than once"), "{e}");
}
#[test]
fn several_upstream_fields_are_a_named_refuse_until() {
let e = refusals(syn::parse_quote! {
#[ingraph(upstream(node = "a::A", body = ["text", "notes"]), dedup(hash))]
struct D { body: String }
});
assert!(
e.contains("reads SEVERAL of the upstream's fields into one of this declaration's")
&& e.contains("one goes in as a list, another joined on a separator"),
"{e}"
);
}
#[test]
fn a_field_cannot_be_called_node() {
let e = refusals(syn::parse_quote! {
#[ingraph(upstream(node = "a::A", w = "w"), dedup(hash))]
struct D { node: String, w: u32 }
});
assert!(
e.contains("`node` cannot be a full-text index's field"),
"{e}"
);
assert!(
!e.contains("`node` is read out of nothing"),
"and it is not ALSO reported as unprojected, which would send the author to write the one \
entry that cannot exist: {e}"
);
}
#[test]
fn a_declaration_that_already_carries_a_key_has_its_storage() {
let e = refusals(syn::parse_quote! {
#[ingraph(upstream(node = "a::A", w = "w"), dedup(hash))]
struct D { w: u32, __ig_key__: SegmentKey }
});
assert!(
e.contains("already carries a storage macro's injected key")
&& e.contains("One node holds ONE storage"),
"{e}"
);
}
#[test]
fn an_unelected_field_has_no_index_for_an_engine_to_hold() {
let e = refusals(syn::parse_quote! {
#[ingraph(upstream(node = "a::A", w = "w"), dedup(hash))]
struct D { #[ingraph(lexical(each))] w: u32 }
});
assert!(
e.contains("names `lexical` and is not elected")
&& e.contains("there is no index for that engine to hold"),
"{e}"
);
assert!(
e.contains("#[ingraph(searchable)]"),
"and the refusal offers the marker, which is the repair usually meant: {e}"
);
}
#[test]
fn the_shape_rules_are_the_tiers_own() {
let e = refusals(syn::parse_quote! { enum D { A } });
assert!(
e.contains("`#[ingraph::fulltext]` supports structs only"),
"{e}"
);
let e = refusals(syn::parse_quote! { struct D(u32); });
assert!(e.contains("`upstream(…)` fills a field BY NAME"), "{e}");
let e = refusals(syn::parse_quote! {
#[ingraph(upstream(node = "a::A", w = "w"), dedup(hash))]
struct D<T> { w: T }
});
assert!(
e.contains("generic full-text indexes are not supported"),
"{e}"
);
let e = refusals(syn::parse_quote! {
#[ingraph(upstream(node = "a::A"), dedup(hash))]
struct D {}
});
assert!(e.contains("with no fields indexes nothing"), "{e}");
}
#[test]
fn the_emission_rules_hold_over_an_index_too() {
let e = refusals(syn::parse_quote! {
#[ingraph(upstream(node = "a::A", __w__ = "w"), dedup(hash))]
struct D { __w__: u32 }
});
assert!(
e.contains("__w__"),
"the framework namespace is fenced: {e}"
);
let e = refusals(syn::parse_quote! {
#[ingraph(upstream(node = "a::A", key = "k", new = "n"), dedup(hash))]
struct D { key: String, #[ingraph(copy)] new: u32 }
});
assert!(
e.contains("would declare `key_ref` twice")
&& e.contains("the borrow of the upstream key it carries"),
"the carried key's borrow is a name a field can collide with: {e}"
);
assert!(
e.contains("would declare `new` twice") && e.contains("the type's own constructor"),
"and so is the constructor, which this tier emits and a record does not: {e}"
);
}
#[test]
fn the_dedup_word_reaches_the_model() {
for kind in [NodeKind::Fulltext, NodeKind::Vector] {
for (written, chosen) in [("hash", Dedup::Hash), ("eq", Dedup::Eq)] {
let word = syn::Ident::new(written, proc_macro2::Span::call_site());
let input: syn::DeriveInput = if kind == NodeKind::Vector {
syn::parse_quote! {
#[ingraph(upstream(node = "a::A", body = "text", vec = "vec"), dedup(#word))]
struct D { body: String, #[ingraph(embedding, knn)] vec: Embedding<f32, 4> }
}
} else {
syn::parse_quote! {
#[ingraph(upstream(node = "a::A", body = "text"), dedup(#word))]
struct D { #[ingraph(searchable)] body: String }
}
};
let m = parse(&input, kind).expect("a derived index");
assert_eq!(m.dedup, chosen, "{kind:?} {written}");
}
}
}
#[test]
fn the_knn_marker_elects_the_field_it_is_written_on() {
let m = parse(&vector_document(), NodeKind::Vector).expect("an embedding index");
let [elected] = &m.embeddings[..] else {
panic!("one marker, one election: {}", m.embeddings.len());
};
assert_eq!(elected.field, "image_embedding");
assert_eq!(elected.label, ElectionLabel::Embedding);
assert_eq!(
elected.ty.to_token_stream().to_string(),
"Embedding < f32 , 4 >",
);
}
#[test]
fn several_fields_may_wear_the_marker() {
let m = parse(
&syn::parse_quote! {
#[ingraph(
upstream(
node = "a::A",
caption_embedding = "caption_embedding",
image_embedding = "image_embedding",
),
dedup(hash),
)]
struct D {
#[ingraph(embedding, knn)]
caption_embedding: Embedding<f32, 4>,
#[ingraph(embedding, knn)]
image_embedding: Embedding<f32, 8>,
}
},
NodeKind::Vector,
)
.expect("two elections are one declaration");
assert_eq!(
m.embeddings
.iter()
.map(|elected| (
elected.field.to_string(),
elected.ty.to_token_stream().to_string()
))
.collect::<Vec<_>>(),
[
(
"caption_embedding".to_owned(),
"Embedding < f32 , 4 >".to_owned(),
),
(
"image_embedding".to_owned(),
"Embedding < f32 , 8 >".to_owned(),
),
],
"each election carries its own field's type, in declared order",
);
}
#[test]
fn every_way_a_label_can_be_wrong_is_refused() {
let labelled = |label: proc_macro2::TokenStream| {
refused(
syn::parse_quote! {
#[ingraph(upstream(node = "a::A", vec = "vec"), dedup(hash))]
struct D {
#[ingraph(#label, knn)]
vec: Vec<Embedding<f32, 4>>,
}
},
NodeKind::Vector,
)
};
let bare = labelled(quote::quote!(list));
assert!(
bare.contains("`list` names a COLLECTION and a collection has elements")
&& bare.contains("`list(embedding)`"),
"a container word written bare is told what it holds: {bare}"
);
let nested = labelled(quote::quote!(list(list(embedding))));
assert!(
nested.contains("no backend carries a collection of collections")
&& nested.contains("`(owner, ordinal)`"),
"the nested refusal is inherited, with this family's own reason: {nested}"
);
let valued = labelled(quote::quote!(embedding(list)));
assert!(
valued.contains("`embedding` names ONE embedding and one embedding has no elements"),
"a value word carries no element: {valued}"
);
let reserved = labelled(quote::quote!(set(embedding)));
assert!(
reserved.contains("`set` is not the container word")
&& reserved.contains("ORDINALS")
&& reserved.contains("reserved for a true unordered container"),
"the near miss is told apart from the word it nearly is: {reserved}"
);
let unknown = labelled(quote::quote!(list(scalar)));
assert!(
unknown.contains("the element of an election's container is `embedding`"),
"and an unknown element names the vocabulary: {unknown}"
);
let twice = labelled(quote::quote!(embedding, list(embedding)));
assert!(
twice.contains("`vec` is labelled more than once"),
"one label, one field: {twice}"
);
}
#[test]
fn a_label_is_refused_on_the_kinds_that_elect_nothing() {
for kind in [NodeKind::Fulltext, NodeKind::Blob] {
let e = refused(
syn::parse_quote! {
#[ingraph(upstream(node = "a::A", body = "text"))]
struct D {
#[ingraph(embedding)]
body: String,
}
},
kind,
);
assert!(
e.contains("is labelled as an embedding field") && e.contains("the `vector` kind's own"),
"{kind:?}: {e}"
);
}
}
#[test]
fn a_retrieving_declaration_states_how_it_tells_two_keys_apart() {
for (kind, written) in [
(
NodeKind::Fulltext,
quote::quote! {
#[ingraph(upstream(node = "a::A", body = "text"))]
struct D { #[ingraph(searchable)] body: String }
},
),
(
NodeKind::Vector,
quote::quote! {
#[ingraph(upstream(node = "a::A", vec = "vec"))]
struct D {
#[ingraph(embedding, knn)]
vec: Embedding<f32, 4>,
}
},
),
] {
let e = refused(syn::parse_quote!(#written), kind);
assert!(
e.contains("does not say HOW it tells two of its keys apart")
&& e.contains("`dedup(hash)`")
&& e.contains("`PartialEq` alone")
&& e.contains("There is no default"),
"{kind:?}: {e}"
);
}
}
#[test]
fn the_semantics_word_carries_its_kinds_implementation() {
let m = parse(
&syn::parse_quote! {
#[ingraph(upstream(node = "a::A", frames = "frames"), dedup(hash))]
struct D {
#[ingraph(list(embedding), knn(semantics(fold = "app::PositionWeighted")))]
frames: Vec<Embedding<f32, 4>>,
}
},
NodeKind::Vector,
)
.expect("a set election with a declared fold");
assert!(
matches!(
m.embeddings[0].fold.as_ref(),
Some(Fold::Custom(path)) if path.to_token_stream().to_string() == "app :: PositionWeighted"
),
"the custom rides as a PATH, carried verbatim",
);
}
#[test]
fn an_election_with_no_semantics_word_declares_no_fold() {
let m = parse(
&syn::parse_quote! {
#[ingraph(upstream(node = "a::A", frames = "frames"), dedup(hash))]
struct D {
#[ingraph(list(embedding), knn)]
frames: Vec<Embedding<f32, 4>>,
}
},
NodeKind::Vector,
)
.expect("a set election");
assert!(m.embeddings[0].fold.is_none());
}
#[test]
fn the_shorthand_names_a_shipped_citizen_by_its_word() {
for (written, expected) in [
(quote::quote!(semantics(fold(sum))), Fold::Sum),
(quote::quote!(semantics(fold(max))), Fold::Max),
] {
let m = parse(
&syn::parse_quote! {
#[ingraph(upstream(node = "a::A", frames = "frames"), dedup(hash))]
struct D {
#[ingraph(list(embedding), knn(#written))]
frames: Vec<Embedding<f32, 4>>,
}
},
NodeKind::Vector,
)
.expect("a set election with a shorthand fold");
assert!(
matches!(
(m.embeddings[0].fold.as_ref(), &expected),
(Some(Fold::Sum), Fold::Sum) | (Some(Fold::Max), Fold::Max)
),
"{written} reaches the model as its citizen",
);
}
}
#[test]
fn every_way_the_shorthand_can_be_wrong_is_refused() {
let written = |word: proc_macro2::TokenStream| {
refused(
syn::parse_quote! {
#[ingraph(upstream(node = "a::A", frames = "frames"), dedup(hash))]
struct D {
#[ingraph(list(embedding), knn(semantics(#word)))]
frames: Vec<Embedding<f32, 4>>,
}
},
NodeKind::Vector,
)
};
let mean = written(quote::quote!(fold(mean)));
assert!(
mean.contains("`mean` is WITHDRAWN")
&& mean.contains("INCLUDING the empty ones")
&& mean.contains("fold = \"app::MyMean\""),
"the withdrawal note carries the road to a custom: {mean}"
);
let weighted = written(quote::quote!(fold(weighted)));
assert!(
weighted.contains("`weighted` ships and cannot be named here")
&& weighted.contains("AT CONSTRUCTION")
&& weighted.contains("pass a `Weighted` at the query"),
"the constructor-checked citizen is sent to the query layer: {weighted}"
);
let unknown = written(quote::quote!(fold(rrf)));
assert!(
unknown.contains("unknown fold in `fold(…)`")
&& unknown.contains("`sum` or `max`")
&& unknown.contains("fold = \"app::MyFold\""),
"an unknown word names the vocabulary and the escape: {unknown}"
);
let carried = written(quote::quote!(fold(sum(k = 60))));
assert!(
carried.contains("a shipped fold is named by a BARE WORD"),
"a citizen takes no arguments: {carried}"
);
for both in [
quote::quote!(fold(sum), fold = "app::MyFold"),
quote::quote!(fold(sum), fold(max)),
quote::quote!(fold = "app::One", fold = "app::Two"),
] {
let twice = written(both);
assert!(
twice.contains("`fold` is declared more than once on one election"),
"one election, one declaration-level fold: {twice}"
);
}
}
#[test]
fn a_label_on_a_field_no_marker_elects_is_carried_rather_than_refused() {
let m = parse(
&syn::parse_quote! {
#[ingraph(upstream(node = "a::A", a = "a", b = "b"), dedup(hash))]
struct D {
#[ingraph(embedding, knn)]
a: Embedding<f32, 4>,
#[ingraph(list(embedding))]
b: Vec<Embedding<f32, 4>>,
}
},
NodeKind::Vector,
)
.expect("a labelled payload field is a forward declaration");
assert_eq!(m.embeddings.len(), 1, "one marker, one election");
assert_eq!(m.embeddings[0].field, "a");
let [labelled] = &m.labelled[..] else {
panic!("the payload label is carried: {}", m.labelled.len());
};
assert_eq!(labelled.field, "b");
assert_eq!(labelled.label, ElectionLabel::List);
}
#[test]
fn every_way_the_semantics_word_can_be_wrong_is_refused() {
let written = |word: proc_macro2::TokenStream, label: proc_macro2::TokenStream| {
refused(
syn::parse_quote! {
#[ingraph(upstream(node = "a::A", frames = "frames"), dedup(hash))]
struct D {
#[ingraph(#label, knn(#word))]
frames: Vec<Embedding<f32, 4>>,
}
},
NodeKind::Vector,
)
};
let single = written(
quote::quote!(semantics(fold = "app::MyFold")),
quote::quote!(embedding),
);
assert!(
single.contains("is labelled `embedding` and `semantics(fold = …)` is the SET election's word")
&& single.contains("nothing for a fold to fold"),
"{single}"
);
let unkeyed = written(
quote::quote!(semantics = "app::MyFold"),
quote::quote!(list(embedding)),
);
assert!(
unkeyed.contains("takes a semantic KIND and the type implementing it"),
"the key is the kind: {unkeyed}"
);
let unknown = written(
quote::quote!(semantics(rank = "app::MyRank")),
quote::quote!(list(embedding)),
);
assert!(
unknown.contains("the kinds are `fold`"),
"an unknown kind names the vocabulary: {unknown}"
);
let bare = written(
quote::quote!(semantics(fold = app::MyFold)),
quote::quote!(list(embedding)),
);
assert!(
bare.contains("QUOTED because it refers to a type this declaration did not mint"),
"mint bare, refer quoted: {bare}"
);
let twice = written(
quote::quote!(semantics(fold = "a::One", fold = "a::Two")),
quote::quote!(list(embedding)),
);
assert!(
twice.contains("`fold` is declared more than once on one election"),
"{twice}"
);
}
#[test]
fn every_way_an_election_can_be_wrong_is_refused() {
for (kind, written) in [
(
NodeKind::Vector,
syn::parse_quote! {
#[ingraph(upstream(node = "a::A", vec = "vec"), dedup(hash))]
struct D { vec: Embedding<f32, 4> }
},
),
(
NodeKind::Fulltext,
syn::parse_quote! {
#[ingraph(upstream(node = "a::A", body = "text"), dedup(hash))]
struct D { body: String }
},
),
] {
let missing: syn::DeriveInput = written;
let missing = refused(missing, kind);
assert!(
missing.contains("does not say WHICH of its fields it indexes")
&& missing.contains("elects at least one")
&& missing.contains("Mandatory even where exactly one")
&& missing.contains("`D` declares"),
"{kind:?}: one candidate is still not an election: {missing}"
);
}
let unlabelled = refused(
syn::parse_quote! {
#[ingraph(upstream(node = "a::A", vec = "vec"), dedup(hash))]
struct D { #[ingraph(knn)] vec: Embedding<f32, 4> }
},
NodeKind::Vector,
);
assert!(
unlabelled.contains("`vec` is marked `knn` and carries no LABEL")
&& unlabelled.contains("`#[ingraph(list(embedding), knn)]`")
&& unlabelled.contains("there is no default"),
"an elected field states its multiplicity: {unlabelled}"
);
for (kind, marker) in [
(NodeKind::Vector, quote::quote!(knn, knn)),
(NodeKind::Fulltext, quote::quote!(searchable, searchable)),
] {
let e = refused(
syn::parse_quote! {
#[ingraph(upstream(node = "a::A", a = "a"), dedup(hash))]
struct D { #[ingraph(#marker)] a: Embedding<f32, 4> }
},
kind,
);
assert!(
e.contains("is declared more than once on `a` — one marker, one field"),
"{kind:?}: {e}"
);
}
}
#[test]
fn each_marker_is_refused_on_the_kinds_that_do_not_take_it() {
for (marker, owner, kind) in [
(quote::quote!(knn), "vector", NodeKind::Fulltext),
(quote::quote!(knn), "vector", NodeKind::Blob),
(quote::quote!(searchable), "fulltext", NodeKind::Vector),
(quote::quote!(searchable), "fulltext", NodeKind::Blob),
] {
let e = refused(
syn::parse_quote! {
#[ingraph(upstream(node = "a::A", body = "text"), dedup(hash))]
struct D { #[ingraph(#marker)] body: String }
},
kind,
);
assert!(
e.contains(&format!("The marker is the `{owner}` kind's own")),
"{kind:?} is told which kind carries it: {e}"
);
assert!(
e.contains("elects `body` into"),
"and the refusal names the field it was written on: {e}"
);
}
}
#[test]
fn the_retired_head_word_teaches_the_marker_that_replaced_it() {
for (kind, successor) in [
(NodeKind::Vector, "`#[ingraph(knn)]` on the field itself"),
(NodeKind::Fulltext, "`#[ingraph(searchable)]` on it"),
(NodeKind::Blob, "elects nothing under any spelling"),
] {
let e = refused(
syn::parse_quote! {
#[ingraph(upstream(node = "a::A", body = "text"), embedding(body), dedup(hash))]
struct D { body: String }
},
kind,
);
assert!(
e.contains("`embedding(<field>)` has retired"),
"{kind:?}: {e}"
);
assert!(e.contains(successor), "{kind:?}: {e}");
assert!(
e.contains("reported at the TYPE its author wrote"),
"and the sentence says what the move bought: {e}"
);
}
}
#[test]
fn a_declaration_that_names_no_mechanism_hashes() {
assert_eq!(
fulltext(&document()).expect("a derived index").dedup,
Dedup::Hash
);
assert_eq!(Dedup::default(), Dedup::Hash);
}
#[test]
fn an_unknown_mechanism_is_refused_by_name() {
let e = refusals(syn::parse_quote! {
#[ingraph(upstream(node = "a::A", body = "text"), dedup(ord))]
struct D { body: String }
});
assert!(e.contains("`ord` is not a dedup mechanism"), "{e}");
assert!(e.contains("`hash` or `eq`"), "{e}");
assert!(e.contains("Hash + Eq") && e.contains("PartialEq"), "{e}");
}
#[test]
fn one_index_dedupes_one_way() {
let e = refusals(syn::parse_quote! {
#[ingraph(upstream(node = "a::A", body = "text"), dedup(hash, eq))]
struct D { body: String }
});
assert!(
e.contains("names ONE mechanism and `hash` was already named"),
"{e}"
);
let e = refusals(syn::parse_quote! {
#[ingraph(upstream(node = "a::A", body = "text"), dedup(hash), dedup(eq))]
struct D { body: String }
});
assert!(e.contains("`dedup` is declared more than once"), "{e}");
}
#[test]
fn an_artifact_family_does_not_dedup() {
let e = refused(
syn::parse_quote! {
#[ingraph(upstream(node = "a::A", payload = "bytes"), dedup(hash))]
struct D { payload: String }
},
NodeKind::Blob,
);
assert!(
e.contains("`dedup(…)` is a RETRIEVE's word and an artifact family does not retrieve"),
"{e}"
);
}
#[test]
fn the_unknown_entry_message_lists_what_the_kind_takes() {
for (kind, listed) in [
(NodeKind::Fulltext, true),
(NodeKind::Vector, true),
(NodeKind::Blob, false),
] {
let e = refused(
syn::parse_quote! {
#[ingraph(upstream(node = "a::A", body = "text"), rank = "score")]
struct D { body: String }
},
kind,
);
assert_eq!(e.contains("`dedup(…)`"), listed, "{kind:?}: {e}");
}
}