luminance_derive/
lib.rs

1//! Derive procedural macros for [luminance].
2//!
3//! This crate exports several macros used to ease development with [luminance]. You are
4//! strongly advised to read the documentation of [luminance] in the first place.
5//!
6//! # `Vertex`
7//!
8//! This macro allows to derive the [`Vertex`] trait for a custom `struct` type.
9//!
10//! [See the full documentation here](https://docs.rs/luminance/latest/luminance/#vertex)
11//!
12//! # `Semantics`
13//!
14//! This macro allows to derive the [`Semantics`] trait for a custom `enum` type.
15//!
16//! [See the full documentation here](https://docs.rs/luminance/latest/luminance/#semantics)
17//!
18//! # `UniformInterface`
19//!
20//! This macro allows to derive the [`UniformInterface`] trait for a custom `struct` type.
21//!
22//! [See the full documentation here](https://docs.rs/luminance/latest/luminance/#uniform-interface)
23//!
24//! [luminance]: https://crates.io/crates/luminance
25//! [`Vertex`]: https://docs.rs/luminance/latest/luminance/vertex/trait.Vertex.html
26//! [`Semantics`]: https://docs.rs/luminance/latest/luminance/vertex/trait.Semantics.html
27
28extern crate proc_macro;
29
30mod attrib;
31mod semantics;
32mod uniform_interface;
33mod vertex;
34
35use crate::semantics::generate_enum_semantics_impl;
36use crate::uniform_interface::generate_uniform_interface_impl;
37use crate::vertex::generate_vertex_impl;
38use proc_macro::TokenStream;
39use syn::{self, parse_macro_input, Data, DeriveInput};
40
41#[proc_macro_derive(Vertex, attributes(vertex))]
42pub fn derive_vertex(input: TokenStream) -> TokenStream {
43  let di: DeriveInput = parse_macro_input!(input);
44
45  match di.data {
46    // for now, we only handle structs
47    Data::Struct(struct_) => match generate_vertex_impl(di.ident, di.attrs.iter(), struct_) {
48      Ok(impl_) => impl_,
49      Err(e) => panic!("{}", e),
50    },
51
52    _ => panic!("only structs are currently supported for deriving Vertex"),
53  }
54}
55
56#[proc_macro_derive(Semantics, attributes(sem))]
57pub fn derive_semantics(input: TokenStream) -> TokenStream {
58  let di: DeriveInput = parse_macro_input!(input);
59
60  match di.data {
61    // for now, we only handle enums
62    Data::Enum(enum_) => match generate_enum_semantics_impl(di.ident, enum_) {
63      Ok(impl_) => impl_,
64      Err(e) => panic!("{}", e),
65    },
66
67    _ => panic!("only enums are currently supported for deriving VertexAttribSem"),
68  }
69}
70
71#[proc_macro_derive(UniformInterface, attributes(uniform))]
72pub fn derive_uniform_interface(input: TokenStream) -> TokenStream {
73  let di: DeriveInput = parse_macro_input!(input);
74
75  match di.data {
76    // for now, we only handle structs
77    Data::Struct(struct_) => match generate_uniform_interface_impl(di.ident, struct_) {
78      Ok(impl_) => impl_,
79      Err(e) => panic!("{}", e),
80    },
81
82    _ => panic!("only structs are currently supported for deriving UniformInterface"),
83  }
84}