use ingraph_codegen::source::SourceModel;
use crate::{attribute, errors::Errors, value};
#[cfg(test)]
mod tests;
pub(crate) fn parse(
input: &syn::DeriveInput,
arguments: &[syn::Attribute],
) -> syn::Result<SourceModel> {
let mut errors = Errors::default();
crate::host::refuse_a_moved_option_block(&input.attrs, &mut errors);
let (krate, awaited, node) = options(arguments, &mut errors);
concrete(&input.generics, &mut errors);
errors.into_result(SourceModel {
krate: krate.unwrap_or_else(|| syn::parse_quote!(::ingraph)),
name: input.ident.clone(),
awaited,
node,
})
}
fn options(
attrs: &[syn::Attribute],
errors: &mut Errors,
) -> (Option<syn::Path>, bool, Option<syn::Path>) {
let mut krate: Option<syn::Path> = None;
let mut awaited: Option<syn::Path> = None;
let mut node: Option<syn::Path> = None;
let mut mounted_at: Option<syn::Path> = None;
attribute::walk(attrs, errors, |meta, errors| {
if meta.path.is_ident("crate") {
match value::parse_literal::<syn::Path>(
&meta,
"`#[ingraph(crate = \"…\")]` must be a path to the `ingraph` crate, in a string literal",
) {
Ok(path) => attribute::set_once(
&mut krate,
path,
&meta.path,
"`crate` is declared more than once — one declaration names one crate",
errors,
),
Err(e) => refuse_value(&meta, e, errors),
}
return Ok(());
}
if meta.path.is_ident("async") {
attribute::set_once(
&mut awaited,
meta.path.clone(),
&meta.path,
"`async` is declared more than once — one origin wears one face",
errors,
);
attribute::skip_entry(&meta);
return Ok(());
}
if meta.path.is_ident("mutation") {
if mounted_at.is_some() {
errors.push(syn::Error::new_spanned(&meta.path, SECOND_MOUNT));
attribute::skip_entry(&meta);
return Ok(());
}
mounted_at = Some(meta.path.clone());
return mount(&meta, &mut node, errors);
}
errors.push(attribute::unknown(
&meta,
"unknown entry in `#[ingraph::source(…)]` — it takes `async` (which face this origin's body \
wears), `mutation(node = \"…\")` (the node whose fields are its commands) and `crate = \
\"…\"`. WHAT it produces, what it takes to build and how it fails are the `impl`'s, being \
types rather than words",
&["async", "mutation", "crate"],
));
Ok(())
});
(krate, awaited.is_some(), node)
}
fn mount(
meta: &syn::meta::ParseNestedMeta<'_>,
node: &mut Option<syn::Path>,
errors: &mut Errors,
) -> syn::Result<()> {
if !attribute::has_block(meta) || attribute::empty_group(meta) {
errors.push(attribute::unknown(meta, MOUNT_EMPTY, &[]));
return Ok(());
}
let mut node_at: Option<syn::Path> = None;
let walked = meta.parse_nested_meta(|inner| {
if inner.path.is_ident("node") {
node_at = Some(inner.path.clone());
match value::parse_literal::<syn::Path>(&inner, MOUNT_NODE) {
Ok(path) => attribute::set_once(
node,
path,
&inner.path,
"`node` is declared more than once in `mutation(…)` — one block names one command face",
errors,
),
Err(e) => refuse_value(&inner, e, errors),
}
return Ok(());
}
errors.push(attribute::unknown(
&inner,
"unknown entry in `mutation(…)` — it names the NODE this origin is controlled through and \
nothing else. What that node's commands are, which handler runs each and how each fails are \
the node's own declaration",
&["node"],
));
Ok(())
});
if let Err(e) = walked {
errors.push(e);
}
if node.is_none() && node_at.is_none() {
errors.push(syn::Error::new_spanned(&meta.path, MOUNT_EMPTY));
}
Ok(())
}
fn refuse_value(meta: &syn::meta::ParseNestedMeta<'_>, error: syn::Error, errors: &mut Errors) {
errors.push(error);
attribute::skip_entry(meta);
}
fn concrete(generics: &syn::Generics, errors: &mut Errors) {
const WHY: &str = "a source takes no generic parameter — the declaration states ONE bound over \
ONE origin, and a parameter is not a type until it is substituted, so a \
generic one would check the double entry for nobody";
if !generics.params.is_empty() {
errors.push(syn::Error::new_spanned(generics, WHY));
} else if let Some(clause) = &generics.where_clause {
errors.push(syn::Error::new_spanned(clause, WHY));
}
}
const SECOND_MOUNT: &str = "one command face per source — more words go in the node; the day two vocabularies truly \
diverge, mounts will widen";
const MOUNT_EMPTY: &str = "`mutation` names no node — write `mutation(node = \"path::to::Node\")`. An origin nobody \
controls omits the word entirely, which is the declaration whose `type Mutation` is `()`";
const MOUNT_NODE: &str = "`mutation(node = \"…\")` names the node this origin is controlled through — quoted, because \
the word names a TYPE this declaration cannot resolve and the compiler resolves where the \
expansion lands";