derive_attr_parser/lib.rs
1//! # Simple parser for derive attributes
2//! * "Copy from Giants". The code mostly copied from <https://github.com/serde-rs/serde/tree/master/serde_derive>.
3//! * "Copy from Copier of Giants". The recommendation is to copy the code into your project for better control.<https://github.com/nealmi/derive-attr-parse>
4//!
5//! ### Typical usage in proc macro
6//! ```
7//! let input = parse_macro_input!(inputTokenStream as DeriveInput);
8//! let ctx = derive_attr_parser::Ctxt::new();
9//! const DEMO: derive_attr_parser::Symbol = derive_attr_parser::Symbol("demo");
10//! let container = derive_attr_parser::from_ast(&ctx, input, DEMO);
11//! ```
12//!### Full Demo Derive Code
13//!
14//! ```rust
15//! extern crate proc_macro;
16//!
17//! use quote::quote;
18//! use syn::{parse_macro_input, DeriveInput};
19//! use derive_attr_parser::{Ctxt, from_ast, Symbol};
20//!
21//! #[proc_macro_derive(DemoDerive, attributes(demo))]
22//! pub fn simuples(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
23//! let mut input = parse_macro_input!(input as DeriveInput);
24//! demo_expand(&mut input)
25//! .unwrap_or_else(syn::Error::into_compile_error)
26//! .into()
27//! }
28//! const DEMO: Symbol = Symbol("demo");
29//! fn demo_expand(input: &mut syn::DeriveInput) -> syn::Result<proc_macro2::TokenStream> {
30//! let ctx = Ctxt::new();
31//! let cont = from_ast(&ctx, input, DEMO);
32//! ctx.check()?;
33//! eprintln!("{cont:#?}");
34//!
35//! //Do something with the info.
36//! //eg. Generate System Dynamics Code.
37//! Ok(quote!())
38//! }
39//! ```
40//! Look into [`Container`], [`Field`], [`Val`]
41//! ### Usage of Demo Derive
42//! ```rust
43//!#[derive(DemoDerive)]
44//! #[demo(
45//! name = "test",
46//! method = "system_dynamics",
47//! ode_solver = "eula",
48//! input_name = "BassInput",
49//! output_name = "BassOutput"
50//! )]
51//!#[demo(input(name = "BassInput", ty = "struct"))]
52//!#[demo(output(name = "BassOutput", ty = "struct"))]
53//! pub struct Bass {
54//! #[demo(param(val = "10_000_f64"), input(from = "total_population"))]
55//! total_population: f64,
56//! #[demo(param(val = "0.015_f64"))]
57//! ad_effectiveness: f64,
58//! #[demo(param(val = "100_f64"))]
59//! contact_rate: f64,
60//! #[demo(param(val = "0.011_f64"))]
61//! sales_fraction: f64,
62//! #[demo(var(val = "potential_clients * ad_effectiveness"))]
63//! sales_from_ad: f64,
64//! #[demo(var(
65//! val = "clients * contact_rate * sales_fraction * potential_clients / total_population"
66//! ))]
67//! sales_from_wom: f64,
68//!
69//! #[demo(stock(val = "total_population"), output(to = "potential_clients"))]
70//! potential_clients: f64,
71//! #[demo(stock, output(to = "clients"))]
72//! clients: f64,
73//!
74//! #[demo(
75//! flow(
76//! from = "potential_clients",
77//! to = "clients",
78//! val = "sales_from_ad + sales_from_wom"
79//! ),
80//! output(to = "sales")
81//! )]
82//! sales: f64,
83//!}
84//! ```
85//!
86mod internals;
87
88pub use internals::ast::*;
89pub use internals::ctxt::Ctxt;
90pub use internals::parse::from_ast;