prost_dto_core/
lib.rs

1use darling::FromDeriveInput;
2use proc_macro2::TokenStream;
3use syn::DeriveInput;
4mod attributes;
5mod enum_codegen;
6mod proto_conv;
7mod struct_codegen;
8mod utils;
9
10use self::attributes::{Direction, FromProstInfo, IntoProstInfo, ProstInfo};
11
12pub fn derive_into_prost(input: DeriveInput) -> TokenStream {
13    let into_info = match IntoProstInfo::from_derive_input(&input) {
14        Ok(info) => info,
15        Err(e) => {
16            return e.write_errors();
17        }
18    };
19    derive_prost(Direction::IntoProst(into_info), input)
20}
21
22pub fn derive_from_prost(input: DeriveInput) -> TokenStream {
23    let from_info = match FromProstInfo::from_derive_input(&input) {
24        Ok(info) => info,
25        Err(e) => {
26            return e.write_errors();
27        }
28    };
29    derive_prost(Direction::FromProst(from_info), input)
30}
31
32fn derive_prost(
33    direction: Direction<FromProstInfo, IntoProstInfo>,
34    input: DeriveInput,
35) -> TokenStream {
36    let tokens = ProstInfo::from_derive_input(&input)
37        .and_then(|info| proto_conv::expand_proto_conv(direction, info, input));
38
39    match tokens {
40        Ok(tokens) => tokens,
41        Err(e) => e.write_errors(),
42    }
43}