use ingraph_codegen::{model::Upstreams, prelude::*};
fn operator(input: &syn::DeriveInput) -> syn::Result<RecordModel> {
let (host, options) = crate::host::split(input);
crate::parse::parse(&host, &options, NodeKind::Operator)
}
fn mirrors(model: &RecordModel) -> Vec<(String, String)> {
model
.upstreams
.as_ref()
.and_then(Upstreams::fused)
.expect("the designation reaches the model")
.mirrors
.iter()
.map(|mirror| (mirror.column.to_string(), mirror.from.to_string()))
.collect()
}
fn refusal(input: syn::DeriveInput) -> String {
operator(&input)
.expect_err("the declaration is refused")
.into_iter()
.map(|e| e.to_string())
.collect::<Vec<_>>()
.join(" | ")
}
#[test]
fn a_mirrored_parent_field_may_be_a_keyword_under_both_spellings() {
for written in ["type", "r#type"] {
let entry: proc_macro2::TokenStream = format!(r#"source = "{written}""#)
.parse()
.expect("a lineage field entry");
let model = operator(&syn::parse_quote! {
#[ingraph(
upstream(node = "media::Frame", key),
upstream(node = "asr::Segment"),
lineage(fields(kind(#entry)))
)]
struct Caption {
#[ingraph(id)] id: Uuid7,
kind: Uuid7,
}
})
.expect("a keyword names a field of the parent's key");
assert_eq!(
mirrors(&model),
[("kind".to_owned(), "r#type".to_owned())],
"written as {written:?}, the mapping reads as the raw identifier the emission spells"
);
}
}
#[test]
fn an_ordinary_mirrored_field_stays_an_ordinary_identifier() {
let model = operator(&syn::parse_quote! {
#[ingraph(
upstream(node = "media::Frame", key),
upstream(node = "asr::Segment"),
lineage(fields(frame(source = "id")))
)]
struct Caption {
#[ingraph(id)] id: Uuid7,
frame: Uuid7,
}
})
.expect("an ordinary name names a field of the parent's key");
assert_eq!(mirrors(&model), [("frame".to_owned(), "id".to_owned())]);
}
#[test]
fn a_value_that_names_no_field_is_still_refused() {
for written in ["not an ident", "", "9lives", "a::b", "crate"] {
let entry: proc_macro2::TokenStream = format!(r#"source = "{written}""#)
.parse()
.expect("a lineage field entry");
let refused = refusal(syn::parse_quote! {
#[ingraph(
upstream(node = "media::Frame", key),
upstream(node = "asr::Segment"),
lineage(fields(kind(#entry)))
)]
struct Caption {
#[ingraph(id)] id: Uuid7,
kind: Uuid7,
}
});
assert!(
refused.contains("names ONE field of the parent's key, so its value is a field name"),
"written as {written:?}: {refused}"
);
}
}