Skip to main content

autumn_macros_support/
lib.rs

1//! Shared implementation helpers for Autumn's proc-macro crates.
2//!
3//! This is a plain library — **not** a proc-macro crate. It holds the
4//! parsing, path-rewriting, schema, and naming helpers used by
5//! `autumn-macros`, `autumn-macros-model`, and `autumn-macros-repository`.
6//! Each proc-macro dylib links its own copy, so the logic is written once
7//! but the per-dylib compile cost stays proportional to the macros that
8//! dylib actually registers.
9//!
10//! One exception: the `analysers` module (feature `analysers`) holds the
11//! bodies of `#[query_budget]` and `#[agent_operable]`, which only
12//! `autumn-macros` registers. They are here because `autumn-web` waits for
13//! the proc-macro crate, and Cargo builds a library in parallel with other
14//! crates (#2795).
15//!
16//! Users should not depend on this crate directly — use `autumn-web`
17//! instead, which re-exports everything.
18
19#[cfg(any(test, feature = "analysers"))]
20mod agent_authority;
21pub mod cascade;
22pub mod crate_path;
23pub mod graph;
24pub mod naming;
25#[doc(hidden)]
26pub mod parse;
27#[cfg(any(test, feature = "analysers"))]
28mod query_budget;
29pub mod schema;
30pub mod wire;
31
32/// The two largest macro analysers. `autumn-macros` registers them.
33///
34/// They live in this library, not in the proc-macro crate. Cargo builds a
35/// library in parallel with other crates. It builds a proc-macro crate late,
36/// and `autumn-web` waits for that crate (#2795).
37#[cfg(any(test, feature = "analysers"))]
38#[doc(hidden)]
39pub mod analysers {
40    use proc_macro2::TokenStream;
41
42    /// The body of `#[query_budget]`.
43    #[must_use]
44    pub fn query_budget(attr: TokenStream, item: TokenStream) -> TokenStream {
45        crate::query_budget::query_budget_macro(attr, item)
46    }
47
48    /// The body of `#[agent_operable]`.
49    #[must_use]
50    pub fn agent_operable(attr: TokenStream, item: TokenStream) -> TokenStream {
51        crate::agent_authority::agent_operable_macro(attr, item)
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    /// The entry points give the same tokens as the analysers they wrap.
58    #[test]
59    fn the_entry_points_run_the_analysers() {
60        let attr = || quote::quote!(2);
61        let item = || {
62            quote::quote!(
63                async fn list() {}
64            )
65        };
66        assert_eq!(
67            super::analysers::query_budget(attr(), item()).to_string(),
68            crate::query_budget::query_budget_macro(attr(), item()).to_string()
69        );
70        let attr = proc_macro2::TokenStream::new;
71        assert_eq!(
72            super::analysers::agent_operable(attr(), item()).to_string(),
73            crate::agent_authority::agent_operable_macro(attr(), item()).to_string()
74        );
75    }
76
77    /// A large analyser in the proc-macro crate makes every cold build wait.
78    #[test]
79    fn the_large_analysers_are_not_in_the_proc_macro_crate() {
80        let macros =
81            std::path::Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/../autumn-macros/src"));
82        for file in ["query_budget.rs", "agent_authority.rs"] {
83            assert!(!macros.join(file).exists(), "{file} is in autumn-macros");
84            let here = std::path::Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/src")).join(file);
85            assert!(here.exists(), "{file} is not in autumn-macros-support");
86        }
87    }
88}