Skip to main content

fmi_export_derive/
lib.rs

1#![doc=include_str!( "../README.md")]
2#![deny(clippy::all)]
3
4use proc_macro::TokenStream;
5use proc_macro_error2::proc_macro_error;
6use syn::{DeriveInput, parse_macro_input};
7
8mod codegen;
9mod model;
10mod util;
11
12//#[cfg(test)]
13//mod tests;
14
15use codegen::CodeGenerator;
16use model::Model;
17
18//TODO: move this into `fmi` crate?
19const RUST_FMI_NAMESPACE: uuid::Uuid = uuid::uuid!("6ba7b810-9dad-11d1-80b4-00c04fd430c8");
20
21/// Main derive macro for FMU models
22///
23/// # Example
24///
25/// ```rust,ignore
26/// use fmi_export::FmuModel;
27///
28/// /// Simple bouncing ball model
29/// #[derive(FmuModel, Default)]
30/// #[model()]
31/// struct BouncingBall {
32///     /// Height above ground (state output)
33///     #[variable(causality = Output, start = 1.0)]
34///     h: f64,
35///
36///     /// Velocity of the ball
37///     #[variable(causality = Output, start = 0.0)]
38///     #[alias(name="der(h)", causality = Local, derivative = h)]
39///     v: f64,
40/// }
41/// ```
42#[proc_macro_derive(FmuModel, attributes(model, variable, alias, child))]
43#[proc_macro_error]
44pub fn derive_fmu_model(input: TokenStream) -> TokenStream {
45    let input = parse_macro_input!(input as DeriveInput);
46    let model = Model::from(input);
47
48    proc_macro_error2::abort_if_dirty();
49
50    let code_generator = CodeGenerator::new(model);
51    quote::quote! {
52        #code_generator
53    }
54    .into()
55}