moongraph_macros/lib.rs
1//! Provides derive macros for `moongraph::Edges`.
2use syn::DeriveInput;
3
4/// Macro for deriving structs that encode a node's edges/resource usage.
5///
6/// This is the quickest way to get a node up and running that uses a struct as
7/// its edges. Simply add `#[derive(Edges)]` on your own structs if each of the
8/// fields is one of `View`, `ViewMut` or `Move`.
9///
10/// ## Example
11///
12/// ```rust
13/// use moongraph::{Edges, ViewMut, View, Move};
14///
15/// #[derive(Edges)]
16/// struct MyData {
17/// an_f32: ViewMut<f32>,
18/// a_u32: View<u32>,
19/// a_str: Move<&'static str>,
20/// }
21/// ```
22#[proc_macro_derive(Edges, attributes(moongraph))]
23pub fn derive_edges(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
24 let input: DeriveInput = syn::parse_macro_input!(input);
25 let maybe_path = match moongraph_macros_syntax::find_path("moongraph", &input) {
26 Err(e) => return e.into_compile_error().into(),
27 Ok(maybe_path) => maybe_path,
28 };
29 let path = maybe_path.unwrap_or_else(|| {
30 // UNWRAP: safe because we know this will parse
31 let path: syn::Path = syn::parse_str("moongraph").unwrap();
32 path
33 });
34 moongraph_macros_syntax::derive_edges(input, path).into()
35}