async_debug_derive/
lib.rs

1#![warn(rustdoc::missing_crate_level_docs)]
2#![warn(missing_docs)]
3
4//! Derive macro for [async-debug](https://crates.io/crates/async-debug)
5
6mod common;
7mod enums;
8mod fields;
9mod structs;
10#[cfg(test)]
11mod tests;
12mod zip_result;
13
14extern crate proc_macro;
15
16use proc_macro2::{Span, TokenStream};
17use syn::{parse2, Data, DataEnum, DataStruct, DeriveInput, Error};
18
19use self::{enums::AsyncDebugEnum, structs::AsyncDebugStruct};
20
21type Result<T> = std::result::Result<T, Error>;
22
23#[proc_macro_derive(AsyncDebug, attributes(async_debug))]
24/// `AsyncDebug` proc macro
25///
26/// This macro will use the `#[async_debug()]` attribute on properties of the struct or enum.
27/// Attribute arguments can include:
28///   async_call = some_function  - Call this async function to render the value
29///   clone                       - Call `.clone()` on the value (exclusive of copy)
30///   copy                        - Dereference the value to take a copy (exclusive of clone)
31///   ty = SomeType               - Use this type as the type of thie property on the generated `*AsyncDebug` struct/enum.
32pub fn async_debug(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
33    match async_debug_impl(input.into()) {
34        Ok(output) => output.into(),
35        Err(err) => err.to_compile_error().into(),
36    }
37}
38
39fn async_debug_impl(input: TokenStream) -> Result<TokenStream> {
40    let input: DeriveInput = parse2(input)?;
41
42    match &input.data {
43        Data::Struct(DataStruct { fields, .. }) => {
44            AsyncDebugStruct::new(&input, fields)?.to_token_stream()
45        }
46        Data::Enum(DataEnum { variants, .. }) => {
47            let variants = variants.iter().cloned().collect();
48
49            AsyncDebugEnum::new(&input, variants)?.to_token_stream()
50        }
51        Data::Union(..) => Err(Error::new(Span::call_site(), "unions are not supported")),
52    }
53}