-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathlib.rs
More file actions
102 lines (93 loc) · 3.45 KB
/
lib.rs
File metadata and controls
102 lines (93 loc) · 3.45 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//! 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.
#![warn(clippy::all)]
use dft::expand_helmholtz_energy_functional;
use functional_contribution::expand_functional_contribution;
use ideal_gas::expand_ideal_gas;
use proc_macro::TokenStream;
use residual::expand_residual;
use subset::expand_subset;
use syn::{parse_macro_input, DeriveInput};
mod dft;
mod functional_contribution;
mod ideal_gas;
mod residual;
mod subset;
// possible additional traits to implement
const OPT_IMPLS: [&str; 7] = [
"molar_weight",
"parameter_info",
"entropy_scaling",
"functional",
"bond_lengths",
"fluid_parameters",
"pair_potential",
];
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(Subset)]
pub fn derive_components(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
expand_subset(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(ResidualDyn, 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(HelmholtzEnergyFunctionalDyn, 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()
}
#[proc_macro_derive(FunctionalContribution)]
pub fn derive_functional_contribution(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
expand_functional_contribution(input)
.unwrap_or_else(syn::Error::into_compile_error)
.into()
}