-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathlib.rs
More file actions
80 lines (73 loc) · 2.85 KB
/
lib.rs
File metadata and controls
80 lines (73 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! This crate provides derive macros used for the EosVariant and
//! FunctionalVariant enums in FeOs. The macros implement
//! the boilerplate for the EquationOfState and HelmholtzEnergyFunctional traits.
use components::expand_components;
use dft::expand_helmholtz_energy_functional;
use ideal_gas::expand_ideal_gas;
use proc_macro::TokenStream;
use residual::expand_residual;
use syn::{parse_macro_input, DeriveInput};
mod components;
mod dft;
mod ideal_gas;
mod residual;
fn implement(name: &str, variant: &syn::Variant, opts: &[&'static str]) -> syn::Result<bool> {
let syn::Variant { attrs, .. } = variant;
let mut implement = Ok(false);
for attr in attrs.iter() {
if attr.path.is_ident("implement") {
if let Ok(syn::Meta::List(list)) = attr.parse_meta() {
for meta in list.nested {
if let syn::NestedMeta::Meta(syn::Meta::Path(path)) = meta {
// check if all keywords are valid, return error if not
if !opts.iter().any(|s| path.is_ident(s)) {
let opts = opts.join(", ");
return Err(syn::Error::new_spanned(
path,
format!("expected one of: {}", opts),
));
}
// "name" is present
if path.is_ident(name) {
implement = Ok(true)
}
}
}
} else {
return Err(syn::Error::new_spanned(
&attr.tokens,
"expected 'implement(optional_trait, ...)'",
));
}
}
}
implement
}
#[proc_macro_derive(Components)]
pub fn derive_components(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
expand_components(input)
.unwrap_or_else(syn::Error::into_compile_error)
.into()
}
#[proc_macro_derive(IdealGas)]
pub fn derive_ideal_gas(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
expand_ideal_gas(input)
.unwrap_or_else(syn::Error::into_compile_error)
.into()
}
#[proc_macro_derive(Residual, attributes(implement))]
pub fn derive_residual(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
expand_residual(input)
.unwrap_or_else(syn::Error::into_compile_error)
.into()
}
#[proc_macro_derive(HelmholtzEnergyFunctional, attributes(implement))]
pub fn derive_helmholtz_energy_functional(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
expand_helmholtz_energy_functional(input)
.unwrap_or_else(syn::Error::into_compile_error)
.into()
}