identity_diff_derive/
lib.rs

1// Copyright 2020-2022 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4#![doc = include_str!("./../README.md")]
5
6use crate::model::InputModel;
7use crate::utils::extract_option_segment;
8use crate::utils::parse_from_into;
9use crate::utils::should_ignore;
10use proc_macro::TokenStream;
11use quote::quote;
12use syn::parse_macro_input;
13use syn::DeriveInput;
14
15mod impls;
16mod model;
17mod utils;
18
19/// Entry point for the `Diff` derive proc macro.  `Diff` implements the `Diff` trait from the `identity_diff` crate on
20/// any Enum or Struct type.  Contains and optional attribute `should_ignore` which will ignore an appended field.
21#[proc_macro_derive(Diff, attributes(diff))]
22pub fn derive_diff(input: TokenStream) -> TokenStream {
23  let input = parse_macro_input!(input as DeriveInput);
24  internal(input)
25}
26
27/// Function for dealing with the internal logic of the macro.
28fn internal(input: DeriveInput) -> TokenStream {
29  let model: InputModel = InputModel::parse(&input);
30  // debug implementation derivation.
31  let debug = model.impl_debug();
32  // diff type derivation.
33  let diff = model.impl_diff();
34  // diff trait implementation derivation.
35  let diff_typ = model.derive_diff();
36
37  let from_into = model.impl_from_into();
38
39  let output = quote! {
40      #diff_typ
41      #debug
42      #diff
43      #from_into
44  };
45
46  // for debugging.
47  // println!("{}", from_into);
48  // println!("{}", diff_typ);
49  // println!("{}", debug);
50  // println!("{}", diff);
51
52  // A hack.
53  TokenStream::from(output)
54}