pivotal_tracker_derive/
lib.rs

1use branded::{get_branded_impls, get_branded_type};
2use proc_macro::TokenStream;
3use quote::quote;
4use syn::{parse_macro_input, DeriveInput};
5
6extern crate proc_macro;
7
8mod branded;
9
10#[proc_macro_derive(Branded)]
11pub fn derive_branded(item: TokenStream) -> TokenStream {
12	// Parse the input tokens into a syntax tree
13	let input = parse_macro_input!(item as DeriveInput);
14	let ident = &input.ident;
15	let type_ident = get_branded_type(&input);
16
17	// Build the output, possibly using quasi-quotation
18	let tokens = get_branded_impls(ident, type_ident);
19
20	// Hand the output tokens back to the compiler
21	TokenStream::from(tokens)
22}
23
24#[proc_macro_derive(BrandedInt)]
25pub fn derive_branded_int(item: TokenStream) -> TokenStream {
26	// Parse the input tokens into a syntax tree
27	let input = parse_macro_input!(item as DeriveInput);
28	let ident = &input.ident;
29	let type_ident = get_branded_type(&input);
30	let branded_impls = get_branded_impls(ident, type_ident);
31
32	// Build the output, possibly using quasi-quotation
33	let tokens = quote! {
34		#branded_impls
35
36		impl std::fmt::Display for #ident {
37			fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38				write!(f, "{}", self.0)
39			}
40		}
41	};
42
43	// Hand the output tokens back to the compiler
44	TokenStream::from(tokens)
45}