use ingraph_codegen::{
composed::{ComposedField, ComposedKind, ComposedModel},
element::{ElementFamily, ElementModel},
model::DatabaseKind,
name::RustSafeName,
standalone::{StandaloneColumn, StandaloneModel, StandaloneStored},
};
use quote::ToTokens as _;
use crate::{
attribute::{self, set_once},
errors::Errors,
parse, value,
};
#[cfg(test)]
mod composed_tests;
#[cfg(test)]
mod element_tests;
#[cfg(test)]
mod tests;
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum Face {
Filter,
Comparison,
OrderBy,
}
impl Face {
const fn word(self) -> &'static str {
match self {
Self::Filter => "#[derive(ingraph::Filter)]",
Self::Comparison => "#[derive(ingraph::Comparison)]",
Self::OrderBy => "#[derive(ingraph::OrderBy)]",
}
}
}
const CRATE_PATH: &str =
"`#[ingraph(crate = \"…\")]` must be a path to the `ingraph` crate, in a string literal";
const CRATE_ONCE: &str = "`crate` is declared more than once — one declaration names one crate";
const HOOK_PATH: &str = "`#[ingraph(filter = \"…\")]` must be a path to the filter type this comparison is crossed into \
from, in a string literal";
pub(crate) enum Mode {
Struct(Box<StandaloneModel>),
Element(Box<ElementModel>),
Composed(Box<ComposedModel>),
}
pub(crate) fn parse(input: &syn::DeriveInput, face: Face) -> syn::Result<Mode> {
let mut errors = Errors::default();
let options = options(&input.attrs, face, &mut errors);
concrete(&input.generics, face, &mut errors);
if let Some(marker) = options.composed.as_ref() {
let model = composed(input, &options, marker, face, &mut errors);
return match model {
Some(model) => errors.into_result(Mode::Composed(Box::new(model))),
None => Err(errors.into_result(()).err().unwrap_or_else(|| {
syn::Error::new_spanned(
&input.ident,
format!(
"{word} could not read this declaration as a composed filter",
word = face.word()
),
)
})),
};
}
if let Some(operator) = options.operator.as_ref() {
let model = element(input, &options, operator, face, &mut errors);
return match model {
Some(model) => errors.into_result(Mode::Element(Box::new(model))),
None => Err(errors.into_result(()).err().unwrap_or_else(|| {
syn::Error::new_spanned(
&input.ident,
format!(
"{word} could not read this declaration as an element",
word = face.word()
),
)
})),
};
}
a_struct_publishes_no_scalar(&options, face, &mut errors);
let fields = match fields(input, face) {
Ok(fields) => fields,
Err(terminal) => return Err(errors.seal(terminal)),
};
let databases = options.databases.clone();
let columns = fields
.named
.iter()
.filter_map(|field| column(field, face, &databases, options.hook.is_some(), &mut errors))
.collect();
let subject = input.ident.clone();
let model = StandaloneModel {
vis: input.vis.clone(),
krate: options
.krate
.unwrap_or_else(|| syn::parse_quote!(::ingraph)),
filter: named_or(
options.filter.as_ref(),
&ingraph_codegen::model::default_filter_name(&subject),
),
order_by: named_or(
options.order_by.as_ref(),
&ingraph_codegen::model::default_order_by_name(&subject),
),
comparison: named_or(options.comparison.as_ref(), &subject),
databases: options.databases,
columns,
hook: options.hook,
subject,
};
errors.into_result(Mode::Struct(Box::new(model)))
}
fn a_struct_publishes_no_scalar(options: &Options, face: Face, errors: &mut Errors) {
let Some(at) = options.wire_name.as_ref() else {
return;
};
errors.push(syn::Error::new(
at.span(),
format!(
"`graphql(rename = \"…\")` names the SCALAR an element publishes, and a {word} over a struct \
mints none — its wire face is the filter input object, whose name is \
`filter(rename = \"…\")`'s. Write `operator(<family>)` to declare this type an ELEMENT, which \
is the reading that gives it a scalar of its own, or drop the word.",
word = face.word()
),
));
}
pub(crate) fn emit(mode: &Mode, face: Face) -> proc_macro2::TokenStream {
match (mode, face) {
(Mode::Element(model), _) => model.emit(),
(Mode::Composed(model), _) => model.emit(),
(Mode::Struct(model), Face::Filter) => model.emit_filter(),
(Mode::Struct(model), Face::Comparison) => model.emit_comparison(),
(Mode::Struct(model), Face::OrderBy) => model.emit_order_by(),
}
}
fn composed(
input: &syn::DeriveInput,
options: &Options,
marker: &Composed,
face: Face,
errors: &mut Errors,
) -> Option<ComposedModel> {
if face != Face::Filter {
errors.push(syn::Error::new_spanned(
marker.at.clone(),
format!(
"`composed` declares a FILTER and reaches no face {word} emits — its fields are operator \
slots rather than columns, so there is no comparison to mint from them and no term to \
order by. Write `#[derive(ingraph::Filter)]` for the wire face, or drop the marker.",
word = face.word()
),
));
return None;
}
if let Some(operator) = options.operator.as_ref() {
errors.push(syn::Error::new_spanned(
operator.at.clone(),
"`operator(…)` and `composed` are the two ELEMENT and FILTER readings of one declaration and \
no declaration is both — `operator(…)` says this type crosses the wire as ONE OPERAND and \
mints a filter over it, `composed` says this type IS a filter and its fields are the \
operator slots. Keep the one this declaration is.",
));
return None;
}
beyond(options, errors, COMPOSED_MODE);
let fields = match fields(input, face) {
Ok(fields) => fields,
Err(terminal) => {
errors.push(terminal);
return None;
}
};
if fields.named.is_empty() {
errors.push(syn::Error::new_spanned(
&input.ident,
"a COMPOSED filter's fields ARE its vocabulary, and this declaration has none — a filter with \
no slot is a predicate that can ask nothing, and the input object it would publish has no \
field, which is not valid SDL. Write the operator slots, or drop the marker.",
));
return None;
}
let declared = fields
.named
.iter()
.filter_map(|field| composed_field(field, errors))
.collect::<Vec<_>>();
duplicates(&declared, errors);
let subject = input.ident.clone();
Some(ComposedModel {
krate: options
.krate
.clone()
.unwrap_or_else(|| syn::parse_quote!(::ingraph)),
sdl: options.filter.as_ref().map_or_else(
|| ingraph_codegen::composed::default_sdl_name(&subject),
RustSafeName::from_ident,
),
shorthand: marker.shorthand.as_ref().is_none_or(|flag| flag.value),
fields: declared,
subject,
})
}
fn composed_field(field: &syn::Field, errors: &mut Errors) -> Option<ComposedField> {
let member = field.ident.clone()?;
let words = words(field, errors);
if words.flatten.is_some() {
if let Some(rename) = words.rename.as_ref() {
errors.push(syn::Error::new_spanned(
rename,
format!(
"`{member}` is SPLICED, so it has no name of its own for `rename` to move — its \
component's fields become this filter's fields, each under the name that component \
publishes. Rename the slot inside the component, or drop `flatten` and hold it as a \
nested slot.",
),
));
}
if optional(&field.ty) {
errors.push(syn::Error::new_spanned(
&field.ty,
format!(
"`{member}` is SPLICED and a spliced component is written BARE — the `Option` on a slot is \
an operator the client did not write, and a component has no such absence: its fields \
become this filter's, each carrying its own. Drop the `Option`, or drop `flatten` and let \
the field be a nested slot.",
),
));
return None;
}
return Some(ComposedField {
member,
ty: field.ty.clone(),
kind: ComposedKind::Spliced,
condition: ingraph_codegen::model::Condition::always(),
});
}
if !optional(&field.ty) {
errors.push(syn::Error::new_spanned(
&field.ty,
format!(
"`{member}` is an operator SLOT and every slot is an `Option` — the absence is the meaning, \
a slot that is not set being an operator the client did not write. Write \
`Option<{spelled}>`, or move the field out of the filter if it is not a slot.",
spelled = field.ty.to_token_stream(),
),
));
return None;
}
let wire = words.rename.as_ref().map_or_else(
|| ingraph_codegen::composed::wire_name(&member),
syn::LitStr::value,
);
Some(ComposedField {
member,
ty: field.ty.clone(),
kind: ComposedKind::Slot(wire),
condition: ingraph_codegen::model::Condition::always(),
})
}
struct Words {
rename: Option<syn::LitStr>,
flatten: Option<syn::Path>,
}
fn words(field: &syn::Field, errors: &mut Errors) -> Words {
let mut words = Words {
rename: None,
flatten: None,
};
attribute::walk(&field.attrs, errors, |meta, errors| {
if meta.path.is_ident("rename") {
match value::literal(&meta, SLOT_NAME) {
Ok(name) => set_once(
&mut words.rename,
name,
&meta.path,
"`rename` is declared more than once — one slot has one name",
errors,
),
Err(e) => errors.push(e),
}
return Ok(());
}
if meta.path.is_ident("flatten") {
if attribute::has_value(&meta) || attribute::has_block(&meta) {
errors.push(attribute::named(
&meta,
"`flatten` is written BARE — it says this field is a component whose fields become this \
filter's, and the component decides what they are called. There is nothing left for a \
value or a block to say."
.to_owned(),
));
}
set_once(
&mut words.flatten,
meta.path.clone(),
&meta.path,
"`flatten` is declared more than once — one field is spliced once",
errors,
);
return Ok(());
}
errors.push(attribute::unknown(
&meta,
"a COMPOSED filter's field takes `rename = \"…\"` and `flatten`, and nothing else — the field \
IS the operator, so what it is read as is its own type and the only things left to say are \
what a client calls it and whether its component's fields are spliced into this filter. The \
interpretation words, the storage words and the `graphql(…)` family are a COLUMN's, and a \
slot is not one.",
&["rename", "flatten"],
));
Ok(())
});
if let Some(name) = words.rename.as_ref() {
crate::wire::refuse_invalid_name(&name.value(), name, errors);
}
words
}
const SLOT_NAME: &str = "a slot's `rename` must be the name a client writes, in a string literal";
const SHORTHAND_FLAG: &str = "`composed(shorthand = …)` must be `true` or `false` — whether a bare value at this filter's \
position is read as its first published slot";
const COMPOSED_MODE: &str = "COMPOSED";
const ELEMENT_MODE: &str = "ELEMENT";
fn duplicates(fields: &[ComposedField], errors: &mut Errors) {
let mut seen: Vec<(&syn::Ident, &str)> = Vec::new();
for field in fields {
let ComposedKind::Slot(wire) = &field.kind else {
continue;
};
if let Some((earlier, _)) = seen.iter().find(|(_, taken)| taken == wire) {
errors.push(syn::Error::new_spanned(
&field.member,
format!(
"`{member}` and `{earlier}` are both the slot `{wire}` — a filter publishes one field per \
name, so the second registration would replace the first and the parse would read one \
key twice. Rename one with `#[ingraph(rename = \"…\")]`.",
member = field.member,
),
));
} else {
seen.push((&field.member, wire));
}
}
}
fn optional(ty: &syn::Type) -> bool {
let syn::Type::Path(path) = ty else {
return false;
};
if path.qself.is_some() {
return false;
}
path.path.segments.last().is_some_and(|last| {
last.ident == "Option"
&& matches!(&last.arguments, syn::PathArguments::AngleBracketed(arguments)
if arguments.args.len() == 1)
})
}
struct Operator {
word: Option<syn::Ident>,
at: proc_macro2::TokenStream,
}
fn element(
input: &syn::DeriveInput,
options: &Options,
operator: &Operator,
face: Face,
errors: &mut Errors,
) -> Option<ElementModel> {
if face != Face::Filter {
errors.push(syn::Error::new_spanned(
operator.at.clone(),
format!(
"`operator(…)` declares an ELEMENT and reaches no face {word} emits — an element is ONE \
value, so it has no columns to compare and no terms to order by. Write \
`#[derive(ingraph::Filter)]` for the element's own filter, or drop the marker.",
word = face.word()
),
));
return None;
}
let family = family(operator, errors)?;
let (member, inner) = single(input, errors)?;
beyond(options, errors, ELEMENT_MODE);
wordless(input, errors);
let subject = input.ident.clone();
match options.wire_name.as_ref() {
Some(word) => crate::wire::refuse_invalid_name(&word.value(), word, errors),
None => crate::wire::refuse_invalid_name(
&syn::ext::IdentExt::unraw(&subject).to_string(),
&subject,
errors,
),
}
Some(ElementModel {
vis: input.vis.clone(),
krate: options
.krate
.clone()
.unwrap_or_else(|| syn::parse_quote!(::ingraph)),
filter: named_or(
options.filter.as_ref(),
&ingraph_codegen::model::default_filter_name(&subject),
),
family,
member,
inner,
subject,
wire_name: options.wire_name.as_ref().map(syn::LitStr::value),
})
}
fn family(operator: &Operator, errors: &mut Errors) -> Option<ElementFamily> {
let Some(word) = operator.word.as_ref() else {
return Some(ElementFamily::Scalar);
};
let spelled = word.to_string();
let spelled = spelled.strip_prefix("r#").unwrap_or(&spelled);
if let Some(family) = ElementFamily::ALL
.into_iter()
.find(|family| family.word() == spelled)
{
return Some(family);
}
errors.push(syn::Error::new_spanned(
word.to_token_stream(),
format!(
"`{spelled}` is not a family an ELEMENT may be read under — `operator(…)` takes {list}. \
`bool` and `duration` are not among them because their filter kinds carry no operand \
parameter, so there is no reading of them over a declared element; `list` and `set` are \
collection POSITIONS rather than elements, and what a collection holds is the question its \
element answers.{near}",
list = ElementFamily::ALL
.map(|family| format!("`{}`", family.word()))
.join(", "),
near = crate::suggest::did_you_mean(spelled, ElementFamily::ALL.map(ElementFamily::word)),
),
));
None
}
fn single(input: &syn::DeriveInput, errors: &mut Errors) -> Option<(syn::Member, syn::Type)> {
let syn::Data::Struct(data) = &input.data else {
errors.push(syn::Error::new_spanned(
&input.ident,
"`operator(…)` declares an ELEMENT, which is a struct holding ONE value — an enum is a \
vocabulary and takes `#[derive(ingraph::Enum)]`, which mints the whole citizenship a \
vocabulary needs.",
));
return None;
};
let mut fields = data.fields.iter();
let (Some(field), None) = (fields.next(), fields.next()) else {
errors.push(syn::Error::new_spanned(
&input.ident,
format!(
"an ELEMENT holds ONE value and this declaration holds {count} — `operator(…)` says the \
whole declaration crosses the wire as one operand, and with {count} fields there is no \
one field for that to be. Write the element as a newtype over the value it carries, or \
drop the marker and take the struct-mode filter over these fields.",
count = data.fields.len()
),
));
return None;
};
let member = field.ident.as_ref().map_or_else(
|| {
syn::Member::Unnamed(syn::Index {
index: 0,
span: proc_macro2::Span::call_site(),
})
},
|ident| syn::Member::Named(ident.clone()),
);
Some((member, field.ty.clone()))
}
fn beyond(options: &Options, errors: &mut Errors, mode: &str) {
let (marker, subject) = match mode {
COMPOSED_MODE => ("composed", "a composed filter"),
_ => ("operator(…)", "an element"),
};
let mut refuse = |at: proc_macro2::TokenStream, word: &str, why: &str| {
errors.push(syn::Error::new_spanned(
at,
format!(
"`{word}` reaches no face the {mode} mode emits — {why}. Drop the word, or drop `{marker}` \
and take the struct-mode faces."
),
));
};
if let Some(at) = options.order_by.as_ref() {
refuse(
at.to_token_stream(),
"order_by(rename = \"…\")",
match mode {
COMPOSED_MODE => {
"a composed filter's fields are operator slots rather than columns, so \
there is nothing to order by"
}
_ => "an element is ONE value, so there is nothing to order it by",
},
);
}
if mode == COMPOSED_MODE
&& let Some(at) = options.wire_name.as_ref()
{
refuse(
at.to_token_stream(),
"graphql(rename = \"…\")",
"a composed filter IS an input object rather than an operand, so it mints no scalar for a \
schema to name; the one name a client reads here is the object's, and \
`filter(rename = \"…\")` moves it",
);
}
if let Some(at) = options.comparison.as_ref() {
refuse(
at.to_token_stream(),
"comparison(rename = \"…\")",
match mode {
COMPOSED_MODE => {
"a composed filter reaches a dialect through the lowering its author \
wrote, so no comparison type is minted here to name"
}
_ => {
"an element's filter crosses through the framework filter it holds, so no comparison \
type is minted here to name"
}
},
);
}
if let Some(at) = options.hook.as_ref() {
refuse(
at.to_token_stream(),
"filter = \"…\"",
"the hook names the seat a COMPARISON is crossed from, and this door emits none",
);
}
if let Some(at) = options.persist_at.as_ref() {
refuse(
at.clone(),
&at.to_string(),
&format!(
"the databases decide how many comparisons a struct-mode declaration mints, and {subject} \
mints none"
),
);
}
}
fn wordless(input: &syn::DeriveInput, errors: &mut Errors) {
let syn::Data::Struct(data) = &input.data else {
return;
};
for field in &data.fields {
for attr in &field.attrs {
if attr.path().is_ident("ingraph") {
errors.push(syn::Error::new_spanned(
attr.to_token_stream(),
"an ELEMENT's field takes no `#[ingraph(…)]` — the field is the value the element \
carries rather than a column, so what it is read as is the container's `operator(…)` \
word and what it crosses as is its own type.",
));
}
}
}
}
struct Options {
krate: Option<syn::Path>,
filter: Option<syn::Ident>,
order_by: Option<syn::Ident>,
comparison: Option<syn::Ident>,
databases: Vec<DatabaseKind>,
persist_at: Option<proc_macro2::TokenStream>,
hook: Option<syn::Path>,
operator: Option<Operator>,
composed: Option<Composed>,
wire_name: Option<syn::LitStr>,
}
struct Composed {
at: proc_macro2::TokenStream,
shorthand: Option<syn::LitBool>,
}
fn options(attrs: &[syn::Attribute], face: Face, errors: &mut Errors) -> Options {
let mut options = Options {
krate: None,
filter: None,
order_by: None,
comparison: None,
databases: Vec::new(),
persist_at: None,
hook: None,
operator: None,
composed: None,
wire_name: None,
};
let mut named = Vec::new();
attribute::walk(attrs, errors, |meta, errors| {
if meta.path.is_ident("crate") {
match value::parse_literal::<syn::Path>(&meta, CRATE_PATH) {
Ok(path) => set_once(&mut options.krate, path, &meta.path, CRATE_ONCE, errors),
Err(e) => errors.push(e),
}
return Ok(());
}
if meta.path.is_ident("filter") {
if attribute::has_value(&meta) {
return match value::parse_literal::<syn::Path>(&meta, HOOK_PATH) {
Ok(path) => {
set_once(
&mut options.hook,
path,
&meta.path,
"`filter = \"…\"` is declared more than once — one declaration names one seat",
errors,
);
Ok(())
}
Err(e) => {
errors.push(e);
Ok(())
}
};
}
return attribute::rename_block(&meta, &mut options.filter, "filter", errors);
}
if meta.path.is_ident("order_by") {
return attribute::rename_block(&meta, &mut options.order_by, "order_by", errors);
}
if meta.path.is_ident("graphql") {
return attribute::named_block(
&meta,
&mut options.wire_name,
"graphql",
"schema type",
errors,
|option| value::literal(option, attribute::SDL_NAME),
);
}
if meta.path.is_ident("comparison") {
return attribute::rename_block(&meta, &mut options.comparison, "comparison", errors);
}
if meta.path.is_ident("composed") {
let at = meta.path.to_token_stream();
if attribute::has_value(&meta) {
errors.push(attribute::named(
&meta,
"`composed` is a MARKER and takes no value — it says this declaration IS a filter, the \
field table being the struct body itself. Write it bare, or with the one word its block \
takes: `composed(shorthand = false)`, which declines the bare-value reading for a filter \
with no slot a bare value obviously means."
.to_owned(),
));
return Ok(());
}
let mut shorthand = None;
let outcome = attribute::options(&meta, |inner| {
if inner.path.is_ident("shorthand") {
match value::boolean(&inner, SHORTHAND_FLAG) {
Ok(flag) => set_once(
&mut shorthand,
flag,
&inner.path,
"`shorthand` is declared more than once — one filter either takes the bare value or \
refuses it",
errors,
),
Err(e) => errors.push(e),
}
return Ok(());
}
errors.push(attribute::unknown(
&inner,
"`composed(…)` takes `shorthand = <bool>` and nothing else — the field table is the \
struct body itself, so what a slot is called is a FIELD's `#[ingraph(rename = \"…\")]`, \
what a component splices is that field's `#[ingraph(flatten)]`, and what the input \
object is called is the container's `filter(rename = \"…\")`.",
&["shorthand"],
));
Ok(())
});
if let Err(e) = outcome {
errors.push(e);
}
set_once(
&mut options.composed,
Composed { at, shorthand },
&meta.path,
"`composed` is declared more than once — one declaration is one filter",
errors,
);
return Ok(());
}
if meta.path.is_ident("operator") {
let at = meta.path.to_token_stream();
let mut word = None;
let outcome = attribute::options(&meta, |inner| {
match inner.path.get_ident() {
Some(ident) if !attribute::has_value(&inner) && !attribute::has_block(&inner) => {
set_once(
&mut word,
ident.clone(),
&inner.path,
"`operator(…)` names ONE family — an element is read under one",
errors,
);
}
_ => errors.push(attribute::unknown(
&inner,
"`operator(…)` takes ONE BARE family word and nothing else — the interpretation \
vocabulary's own words, written without a value or a block",
&[],
)),
}
Ok(())
});
if let Err(e) = outcome {
errors.push(e);
}
set_once(
&mut options.operator,
Operator {
word,
at: at.clone(),
},
&meta.path,
"`operator(…)` is declared more than once — one declaration is one element",
errors,
);
return Ok(());
}
if let Some(kind) = DatabaseKind::ALL
.into_iter()
.find(|kind| meta.path.is_ident(kind.as_str()))
{
options.persist_at = Some(meta.path.to_token_stream());
if attribute::has_value(&meta) || attribute::has_block(&meta) {
errors.push(attribute::named(
&meta,
format!(
"`{db}` is named BARE here — a standalone face emits ONE comparison per database and \
has no row to refine, so there is nothing for a block to say. The per-database \
refinements are a FIELD's, inside `persist({db}(…))` on the field.",
db = kind.as_str()
),
));
return Ok(());
}
if named.contains(&kind) {
errors.push(syn::Error::new_spanned(
meta.path.to_token_stream(),
format!(
"`{}` is named more than once — one declaration names one database once",
kind.as_str()
),
));
} else {
named.push(kind);
}
return Ok(());
}
if meta.path.is_ident("persist") {
errors.push(attribute::named(
&meta,
"`persist(…)` is gone — a standalone face names its databases BARE, so write \
`#[ingraph(postgres)]`. It is the record's own unwrap: storage is the essence of \
what these faces are emitted FOR, and an essence does not wrap."
.to_owned(),
));
attribute::skip_entry(&meta);
return Ok(());
}
errors.push(attribute::unknown(
&meta,
&format!(
"unknown entry in `#[ingraph(…)]` on a {word} declaration — a standalone face takes its \
DATABASES bare (`sqlite`, `postgres`, `mysql`, `mongo`), and `crate = \"…\"`, \
`filter(rename = \"…\")`, `comparison(rename = \"…\")`, `order_by(rename = \"…\")`, \
`graphql(rename = \"…\")`, the conversion hook `filter = \"…\"`, the element marker \
`operator(<family>)` and the composed marker `composed`, bare or as \
`composed(shorthand = <bool>)`",
word = face.word()
),
&[
"crate",
"filter",
"comparison",
"order_by",
"graphql",
"operator",
"composed",
]
.into_iter()
.chain(DatabaseKind::ALL.into_iter().map(DatabaseKind::as_str))
.collect::<Vec<_>>(),
));
Ok(())
});
options.databases = named;
options
}
fn column(
field: &syn::Field,
face: Face,
databases: &[DatabaseKind],
hooked: bool,
errors: &mut Errors,
) -> Option<StandaloneColumn> {
let member = field.ident.clone()?;
let read = parse::standalone_field(&field.attrs, errors);
for (word, at) in &read.beyond {
errors.push(syn::Error::new_spanned(
at.to_token_stream(),
format!(
"`{word}` is a RECORD's word and reaches no face a {door} emits — it declares an identity, \
an address segment, an accessor's projection, a physical layout or a construction \
protocol, and a bare struct has none of those. Write the field under \
`#[ingraph::record(…)]` if that is what it is.",
door = face.word()
),
));
}
if !hooked {
for (word, at) in &read.conversions {
errors.push(syn::Error::new_spanned(
at.to_token_stream(),
format!(
"`{word}` refines a CROSSING and this declaration declares none — a conversion is legal \
only under the container's own hook, which is `#[ingraph(filter = \"path::to::Filter\")]` \
naming the filter seat the crossing starts from. Write the hook, or drop the word."
),
));
}
}
for database in &read.per_db {
if !databases.contains(&database.kind) {
errors.push(syn::Error::new_spanned(
database.at.to_token_stream(),
format!(
"`{db}` is not one of this declaration's databases — a per-database refinement refines a \
comparison, and the comparisons are the ones the head's store words name. Add `{db}` \
there, or drop the block.",
db = database.kind.as_str()
),
));
}
}
let stored = databases
.iter()
.map(|kind| {
read
.per_db
.iter()
.find(|database| database.kind == *kind)
.map_or(
StandaloneStored {
ty: None,
flatten: false,
},
|database| StandaloneStored {
ty: database.stored.clone(),
flatten: database.flatten,
},
)
})
.collect();
let name = read.rename.as_ref().map_or_else(
|| RustSafeName::from_ident(&member),
|rename| RustSafeName::new(rename.value(), rename.span()),
);
Some(StandaloneColumn {
member,
name,
domain: field.ty.clone(),
wire: read.wire,
marker: read.marker,
stored,
})
}
fn fields(input: &syn::DeriveInput, face: Face) -> Result<&syn::FieldsNamed, syn::Error> {
match &input.data {
syn::Data::Struct(data) => match &data.fields {
syn::Fields::Named(named) => Ok(named),
syn::Fields::Unnamed(_) | syn::Fields::Unit => Err(syn::Error::new_spanned(
&input.ident,
format!(
"{word} takes a struct with NAMED fields — every face it emits is one member per field, \
and a positional field has no member name to take.",
word = face.word()
),
)),
},
syn::Data::Enum(_) | syn::Data::Union(_) => Err(syn::Error::new_spanned(
&input.ident,
format!("{word} supports structs only", word = face.word()),
)),
}
}
fn concrete(generics: &syn::Generics, face: Face, errors: &mut Errors) {
if generics.params.is_empty() {
return;
}
errors.push(syn::Error::new_spanned(
generics.to_token_stream(),
format!(
"{word} takes a CONCRETE struct — the faces it emits name this type in a `where` clause and \
in a map row, and a parameter would have to be threaded through every one of them.",
word = face.word()
),
));
}
fn named_or(written: Option<&syn::Ident>, default: &syn::Ident) -> RustSafeName {
RustSafeName::from_ident(written.unwrap_or(default))
}