microtype_macro/
lib.rs

1//! proc-macro crate for `microtype`
2
3#![warn(clippy::all)]
4#![deny(missing_docs)]
5
6use codegen::codegen;
7use parse::MicrotypeMacro;
8use syn::parse_macro_input;
9
10use crate::model::flatten;
11
12extern crate proc_macro;
13
14mod parse;
15mod model;
16mod codegen;
17
18
19/// Macro to create microtype wrappers
20/// 
21/// See crate-level documentation for a more thorough explanation
22///
23/// Example usage:
24/// ```ignore
25/// # use microtype::microtype;
26/// microtype! {
27///   #[derive(Debug, Clone)]  // attributes on the outer type apply to all types in this block
28///   String {
29///     #[derive(PartialEq)]  // attributes can also be applied to a single microtype
30///     Email,
31///
32///     NotPartialEqString,
33///   }
34///
35///   // secret microtypes have extra restrictions to prevent accidental misuse of sensitive data
36///   #[secret]
37///   String {
38///     Password
39///   }
40///   
41///   // `#[secret(serialize)]` can be used to give a secret microtype a `Serialize` implementation
42///   #[secret(serialize)]
43///   String {
44///     SessionToken
45///   }
46/// }
47/// ```
48#[proc_macro]
49pub fn microtype(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
50    let microtype = parse_macro_input!(tokens as MicrotypeMacro);
51    let microtypes = flatten(microtype);
52    codegen(microtypes).into()
53}
54