fielder_proc/
lib.rs

1//! # `fielder-proc`
2//!
3//! This crate contains the proc-macros used by
4//! [`fielder`](https://docs.rs/fielder/latest/fielder).
5
6#[doc = include_str!("../README.md")]
7#[cfg(doctest)]
8struct ReadmeDoctest;
9
10use proc_macro::TokenStream;
11use syn::parse_macro_input;
12
13extern crate proc_macro;
14
15mod bitfield;
16
17/// Generate a complex bitfield.
18///
19/// Definitions are similar to [`bitflags`](https://docs.rs/bitflags/latest/bitflags), with some
20/// important differences.
21///
22/// # Example
23///
24/// ```edition2021
25/// use fielder::bitfield;
26///
27/// bitfield! {
28///     // The struct definition can include a visiblity modifier (`pub(crate)`) and must include
29///     // a type (`u8`).
30///     pub(crate) struct Field: u8 {
31///         // Fields can either span a single bit...
32///         FlagOne: 0;
33///         // ...or multiple.
34///         FieldTwo: 1..2;
35///         // Fields can have a value assigned...
36///         FieldThree: 2..3 = 0b11;
37///         // ...or use a default. Fields covering multiple bits will default to `0`, while
38///         // fields covering a single bit will default to `1`.
39///         FieldFourEmpty: 4..5;
40///         // Fields can also overlap, allowing for an enum-like interface.
41///         FieldFourPartial: 4..5 = 0b10;
42///         // Fields act as "counters" when the value is "!0", allowing for the exact bit value
43///         // to be get/set
44///         Rest: 6..7 = !0;
45///     }
46/// }
47/// ```
48#[proc_macro]
49pub fn bitfield(input: TokenStream) -> TokenStream {
50    bitfield::to_tokens(parse_macro_input!(input as bitfield::Bitfield))
51}