seaography_macros/
lib.rs

1#![allow(clippy::collapsible_if)]
2extern crate proc_macro;
3
4use darling::FromDeriveInput;
5use proc_macro::TokenStream;
6use syn::{DeriveInput, ItemImpl};
7
8mod convert_output;
9mod custom_enum;
10mod custom_fields;
11mod custom_input_type;
12mod custom_output_type;
13mod util;
14
15#[derive(Debug, FromDeriveInput)]
16#[darling(attributes(seaography))]
17struct Args {
18    input_type_name: Option<String>,
19    output_type_name: Option<String>,
20    enum_name: Option<String>,
21}
22
23#[proc_macro_derive(CustomEnum, attributes(seaography))]
24pub fn derive_custom_enum(input: TokenStream) -> TokenStream {
25    let derive_input: DeriveInput = syn::parse(input).unwrap();
26    match custom_enum::expand(derive_input) {
27        Ok(token_stream) => token_stream.into(),
28        Err(e) => e.to_compile_error().into(),
29    }
30}
31
32#[proc_macro_derive(CustomInputType, attributes(seaography))]
33pub fn derive_custom_input_type(input: TokenStream) -> TokenStream {
34    let derive_input: DeriveInput = syn::parse(input).unwrap();
35
36    match custom_input_type::expand(derive_input) {
37        Ok(token_stream) => token_stream.into(),
38        Err(e) => e.to_compile_error().into(),
39    }
40}
41
42#[proc_macro_derive(CustomOutputType, attributes(seaography))]
43pub fn derive_custom_output_type(input: TokenStream) -> TokenStream {
44    let derive_input: DeriveInput = syn::parse(input).unwrap();
45    match custom_output_type::expand(derive_input) {
46        Ok(token_stream) => token_stream.into(),
47        Err(e) => e.to_compile_error().into(),
48    }
49}
50
51#[proc_macro_attribute]
52#[allow(non_snake_case)]
53pub fn CustomFields(_input: TokenStream, annotated_item: TokenStream) -> TokenStream {
54    let derive_input: ItemImpl = syn::parse(annotated_item.clone()).unwrap();
55    match custom_fields::expand(derive_input, annotated_item) {
56        Ok(token_stream) => token_stream.into(),
57        Err(e) => e.to_compile_error().into(),
58    }
59}
60
61#[proc_macro_derive(ConvertOutput, attributes(seaography))]
62pub fn derive_convert_output(input: TokenStream) -> TokenStream {
63    let derive_input: DeriveInput = syn::parse(input).unwrap();
64    match convert_output::expand(derive_input) {
65        Ok(token_stream) => token_stream.into(),
66        Err(e) => e.to_compile_error().into(),
67    }
68}