bsn1_serde_macros/
lib.rs

1// Copyright 2023-2024 Shin Yoshida
2//
3// "GPL-3.0-only"
4//
5// This is part of BSN1_SERDE
6//
7// BSN1_SERDE is free software: you can redistribute it and/or modify it under the terms of the
8// GNU General Public License as published by the Free Software Foundation, version 3.
9//
10// BSN1_SERDE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
11// even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License along with this program. If
15// not, see <https://www.gnu.org/licenses/>.
16
17#![deny(missing_docs)]
18#![doc = include_str!("../README.md")]
19
20mod attribute;
21mod data_container;
22mod de;
23mod generics;
24mod ser;
25
26use attribute::Attribute;
27use data_container::DataContainer;
28use proc_macro::TokenStream;
29
30/// Derive macro to implement `ser::Serialize` trait for struct or enum.
31///
32/// User can customize the implementation by attributes `bsn1_serde`.
33///
34/// All the fields of the struct or enum must implement `ser::Serialize`
35/// unless the field is annotated with `#[bsn1_serde(to = "...")]` or `#[bsn1_serde(into = "...")]`
36#[doc = include_str!("../annotation_bsn1_serde.md")]
37#[proc_macro_derive(Serialize, attributes(bsn1_serde))]
38pub fn serialize(input: TokenStream) -> TokenStream {
39    let ast = syn::parse_macro_input!(input as syn::DeriveInput);
40
41    match ser::do_serialize(ast) {
42        Ok(ts) => ts.into(),
43        Err(e) => e.to_compile_error().into(),
44    }
45}
46
47/// Derive macro to implement `de::Deserialize` trait for struct or enum.
48///
49/// User can customize the implementation by attributes `bsn1_serde`.
50///
51/// All the fields of the struct or enum must implement `de::Deserialize`
52/// unless the field is annotated with `#[bsn1_serde(from = "...")]` or
53/// `#[bsn1_serde(try_from = "...")]`
54#[doc = include_str!("../annotation_bsn1_serde.md")]
55#[proc_macro_derive(Deserialize, attributes(bsn1_serde))]
56pub fn deserialize(input: TokenStream) -> TokenStream {
57    let ast = syn::parse_macro_input!(input as syn::DeriveInput);
58
59    match de::do_deserialize(ast) {
60        Ok(ts) => ts.into(),
61        Err(e) => e.to_compile_error().into(),
62    }
63}