use ingraph_codegen::prefix::EmissionPrefix;
use ingraph_codegen::resource::{ResourceKind, ResourceMode, ResourceModel};
use crate::{attribute, errors::Errors, value};
#[cfg(test)]
mod kind_tests;
#[cfg(test)]
mod tests;
pub(crate) fn parse(
input: &syn::DeriveInput,
arguments: &[syn::Attribute],
) -> syn::Result<ResourceModel> {
let mut errors = Errors::default();
crate::host::refuse_a_moved_option_block(&input.attrs, &mut errors);
let Words {
krate,
kind,
asynchronous,
} = options(arguments, &mut errors);
concrete(&input.generics, &mut errors);
if let Some((builtin, at)) = kind.as_ref() {
if let Some(word) = asynchronous.as_ref() {
errors.push(syn::Error::new_spanned(
word,
format!(
"`{word}` already knows which face its engine wears — a BUILTIN kind word CARRIES the \
mode, and `async` beside it is a second opinion that could only disagree with it. The \
mode word is the FOREIGN form's, where the faces are hand-written and nothing but the \
declaration can see which ones they are",
word = builtin.word()
),
));
}
mints_its_own_engine(*builtin, at, input, &mut errors);
}
let kind = kind.map(|(kind, _)| kind);
let mode = match (kind, asynchronous.is_some()) {
(Some(kind), _) => kind.mode(),
(None, true) => ResourceMode::Async,
(None, false) => ResourceMode::Block,
};
errors.into_result(ResourceModel {
prefix: EmissionPrefix::default(),
krate: krate.unwrap_or_else(|| syn::parse_quote!(::ingraph)),
name: input.ident.clone(),
kind,
mode,
})
}
struct Words {
krate: Option<syn::Path>,
kind: Option<(ResourceKind, syn::Path)>,
asynchronous: Option<syn::Path>,
}
fn options(attrs: &[syn::Attribute], errors: &mut Errors) -> Words {
let mut krate: Option<syn::Path> = None;
let mut kind: Option<(ResourceKind, syn::Path)> = None;
let mut asynchronous: 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) => {
if krate.replace(path).is_some() {
errors.push(syn::Error::new_spanned(
&meta.path,
"`crate` is declared more than once — one declaration names one crate",
));
}
}
Err(e) => errors.push(e),
}
return Ok(());
}
if meta.path.is_ident("async") {
if attribute::has_value(&meta) || attribute::has_block(&meta) {
errors.push(attribute::named(
&meta,
"`async` is a bare word here — it names the FACE this resource's engine impls wear, and \
the impls themselves are the author's own, so there is nothing to configure"
.to_owned(),
));
return Ok(());
}
attribute::set_once(
&mut asynchronous,
meta.path.clone(),
&meta.path,
"`async` is declared more than once — one declaration wears one face",
errors,
);
return Ok(());
}
if let Some(named) = meta.path.get_ident().map(ToString::to_string)
&& let Some(builtin) = ResourceKind::from_word(&named)
{
if attribute::has_value(&meta) || attribute::has_block(&meta) {
errors.push(attribute::named(
&meta,
format!(
"`{named}` is a bare word — it names WHICH framework engine this resource is, and how \
one is tuned is the engine's own options, which reach it through the `[resources.…]` \
section its mount names rather than through this attribute"
),
));
return Ok(());
}
attribute::set_once(
&mut kind,
(builtin, meta.path.clone()),
&meta.path,
"a kind word is declared more than once — one resource is one engine",
errors,
);
return Ok(());
}
errors.push(attribute::unknown(&meta, &unknown(), &vocabulary()));
Ok(())
});
Words {
krate,
kind,
asynchronous,
}
}
fn vocabulary() -> Vec<&'static str> {
let mut words = vec!["crate", "async"];
words.extend_from_slice(ResourceKind::WORDS);
words
}
fn unknown() -> String {
let kinds = ResourceKind::WORDS
.iter()
.map(|word| format!("`{word}`"))
.collect::<Vec<_>>()
.join(", ");
format!(
"unknown entry in `#[ingraph(…)]` on a resource — it takes `crate = \"…\"`, the `async` word, \
and one BUILTIN KIND WORD ({kinds}), and nothing else. How a FOREIGN one is BUILT is the \
`AsyncResource` impl's (`type Options`, `build`), and where its options come from is the \
launch's"
)
}
fn concrete(generics: &syn::Generics, errors: &mut Errors) {
if generics.params.is_empty() {
return;
}
errors.push(syn::Error::new_spanned(
&generics.params,
"a resource takes no generic parameter — the declaration registers ONE type as a citizen of \
the walk, and a parameter is not a type until it is substituted, so a generic one would name \
a different resource per instantiation",
));
}
fn mints_its_own_engine(
kind: ResourceKind,
at: &syn::Path,
input: &syn::DeriveInput,
errors: &mut Errors,
) {
let holds = match &input.data {
syn::Data::Struct(data) => !matches!(data.fields, syn::Fields::Unit),
syn::Data::Enum(_) | syn::Data::Union(_) => true,
};
if !holds {
return;
}
errors.push(syn::Error::new_spanned(
at,
format!(
"`{word}` MINTS this resource's engine, so the declaration it takes holds nothing of its own \
— write `struct {name};` and let the expansion put the engine inside it. A declaration that \
already holds something is the FOREIGN form's shape: drop the kind word, and the engine \
impls beside it are yours to write",
word = kind.word(),
name = input.ident
),
));
}