use ingraph_codegen::catalog::ResourceSource;
use crate::{attribute, errors::Errors, value};
#[cfg(test)]
mod fuse_tests;
#[cfg(test)]
mod goodbye_tests;
#[cfg(test)]
mod interface_tests;
#[cfg(test)]
mod origin_tests;
#[cfg(test)]
mod tests;
const FACTORY_PATH: &str = "`factory = \"…\"` must be a path to an initialization function, in a \
string literal — a word that points at something written elsewhere is \
quoted";
const UNKNOWN: &str = "unknown entry in `#[ingraph(…)]` on a graph root's field — it takes \
`resource`, `resource(factory)`, `resource(factory = \"path::to::fn\")`, a \
`teardown` composed with any of them, `interface`, `query`, `fuse`, and \
`origin`, and nothing else: what a NODE is, how it is stored, and whether IT \
says goodbye are written in that node's OWN declaration";
const ORIGIN_IS_BARE: &str = "`origin` is a BARE word — it puts an `#[ingraph::source]` declaration \
on this root's origin tier, which is what mounts it as a HEAD whose \
one out-edge IS its feed, and it says nothing else. WHICH origin this \
is is the field's own type; its face, its command node and what it \
produces are written in that source's OWN declaration";
const FUSE_IS_BARE: &str = "`fuse` is a BARE word — it puts a retrieving declaration on this root's \
index tier, which is what keeps it off the node walks, and it says \
nothing else. The modality trims `fuse(vector)` / `fuse(fulltext)` are \
RETIRED along with the root's own fused doors: a fused read is declared \
by `#[ingraph::search]`, where a recipe LISTS the members it fuses and \
has no door for a trim to select";
#[derive(Debug)]
pub(super) struct Declaration {
pub(super) source: ResourceSource,
pub(super) teardown: bool,
}
#[derive(Debug, Default)]
pub(super) struct Words {
pub(super) resource: Option<Declaration>,
pub(super) interface: bool,
pub(super) query: bool,
pub(super) fuse: bool,
pub(super) origin: bool,
}
pub(super) fn words(field: &syn::Field, errors: &mut Errors) -> Words {
let mut declared = Words::default();
attribute::walk(&field.attrs, errors, |meta, errors| {
if meta.path.is_ident("query") {
if declared.query {
errors.push(syn::Error::new_spanned(
&meta.path,
"`query` is declared more than once on one field — a member joins the root query once",
));
}
declared.query = true;
return Ok(());
}
if meta.path.is_ident("fuse") {
if attribute::has_value(&meta) || attribute::has_block(&meta) {
errors.push(syn::Error::new_spanned(&meta.path, FUSE_IS_BARE));
attribute::skip_entry(&meta);
return Ok(());
}
if core::mem::replace(&mut declared.fuse, true) {
errors.push(syn::Error::new_spanned(
&meta.path,
"`fuse` is declared more than once on one field — a member is on ONE tier",
));
}
return Ok(());
}
if meta.path.is_ident("origin") {
if attribute::has_value(&meta) || attribute::has_block(&meta) {
errors.push(syn::Error::new_spanned(&meta.path, ORIGIN_IS_BARE));
attribute::skip_entry(&meta);
return Ok(());
}
if core::mem::replace(&mut declared.origin, true) {
errors.push(syn::Error::new_spanned(
&meta.path,
"`origin` is declared more than once on one field — a member is on ONE tier",
));
}
return Ok(());
}
if meta.path.is_ident("interface") {
if attribute::has_value(&meta) {
errors.push(syn::Error::new_spanned(
&meta.path,
"`interface` is a bare word — WHICH interface this is is the field's own type, and what \
the interface promises is written in that interface's OWN declaration",
));
attribute::skip_entry(&meta);
return Ok(());
}
if core::mem::replace(&mut declared.interface, true) {
errors.push(syn::Error::new_spanned(
&meta.path,
"`interface` is declared more than once on one field — a member is on ONE tier",
));
}
return Ok(());
}
if !meta.path.is_ident("resource") {
errors.push(attribute::unknown(
&meta,
UNKNOWN,
&["query", "resource", "fuse", "origin"],
));
return Ok(());
}
let mut source = ResourceSource::Section;
let mut teardown = false;
attribute::options(&meta, |inner| {
if inner.path.is_ident("teardown") {
if attribute::has_value(&inner) {
errors.push(syn::Error::new_spanned(
&inner.path,
"`teardown` is a bare word — WHAT this resource says on the way out is its own \
`AsyncResource::goodbye`, so the word states only that this deployment wants it said",
));
attribute::skip_entry(&inner);
return Ok(());
}
if core::mem::replace(&mut teardown, true) {
errors.push(syn::Error::new_spanned(
&inner.path,
"`teardown` is declared more than once on one resource — one declaration says goodbye \
once",
));
}
return Ok(());
}
if !inner.path.is_ident("factory") {
errors.push(attribute::unknown(
&inner,
"unknown entry in `resource(…)` — it takes `factory`, bare for the application's own \
`prepare` closure or `factory = \"path::to::fn\"` for a function the create walk calls \
itself, and `teardown`, which composes with either",
&["factory", "teardown"],
));
return Ok(());
}
if !attribute::has_value(&inner) {
source = ResourceSource::Prepare;
return Ok(());
}
match value::parse_literal::<syn::Path>(&inner, FACTORY_PATH) {
Ok(path) => source = ResourceSource::Initializer(path),
Err(e) => {
errors.push(e);
attribute::skip_entry(&inner);
}
}
Ok(())
})?;
if declared
.resource
.replace(Declaration { source, teardown })
.is_some()
{
errors.push(syn::Error::new_spanned(
&meta.path,
"`resource` is declared more than once on one field — a member is one citizen, and its \
options have ONE source",
));
}
Ok(())
});
if declared.fuse && declared.resource.is_some() {
errors.push(syn::Error::new_spanned(
&field.ty,
"a `resource` field takes no `fuse` — a resource is a shared citizen the launch builds and \
hands out, where a fused query joins the rankings of members that RETRIEVE; the two are \
different tiers of the one list",
));
declared.fuse = false;
}
if declared.fuse && declared.interface {
errors.push(syn::Error::new_spanned(
&field.ty,
"an `interface` field takes no `fuse` — an interface is a contract over members and owns no \
slot, so it has no ranking of its own for a fold to read. The MEMBERS fuse, each under its \
own field on this root",
));
declared.fuse = false;
}
if declared.query && declared.resource.is_some() {
errors.push(syn::Error::new_spanned(
&field.ty,
"a `resource` field takes no `query` — a resource is a shared citizen the launch builds and \
hands out, where a root query folds the fields a NODE answers; the two are different tiers \
of the one list",
));
declared.query = false;
}
if declared.query && declared.origin {
errors.push(syn::Error::new_spanned(
&field.ty,
"an `origin` field takes no `query` — an origin is a HEAD that produces into the graph, and \
it answers nobody: it emits no query unit for a root fold to project. The face it DOES \
publish is its command node, named by `mutation(node = \"…\")` on the source's own \
declaration",
));
declared.query = false;
}
if declared.origin && declared.resource.is_some() {
errors.push(syn::Error::new_spanned(
&field.ty,
"a field is on ONE tier — `origin` names a source the launch STARTS and puts on an engine of \
its own, and `resource` names a shared citizen it builds once and hands out; a member \
wearing both says it is two things",
));
declared.origin = false;
}
if declared.origin && declared.interface {
errors.push(syn::Error::new_spanned(
&field.ty,
"a field is on ONE tier — `origin` names a running source and `interface` names a contract \
over members, which is a description rather than a body; a member wearing both says it is \
two things",
));
declared.origin = false;
}
if declared.origin && declared.fuse {
errors.push(syn::Error::new_spanned(
&field.ty,
"a field is on ONE tier — `origin` names a source that PRODUCES into the graph, and `fuse` \
names a retrieving declaration that owns a corpus to be read from; a member wearing both \
says it is two things",
));
declared.origin = false;
}
if declared.interface && declared.resource.is_some() {
errors.push(syn::Error::new_spanned(
&field.ty,
"a field is on ONE tier — `interface` names a contract over members, and `resource` names a \
shared citizen the launch builds and hands out; a member wearing both says it is two things",
));
declared.interface = false;
}
declared
}