1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//! A proc macro crate for deriving [`ReadFixed`] and [`WriteFixed`]
//! 
//! This crate, `fixcol-derive` contains the proc-macro derive functionality for
//! the [`fixcol`](../fixcol) crate. Unless you are planning on contributing to 
//! `fixcol`, you probably want one of the following.
//!
//!  - [Crate](https://crates.io/crates/fixcol)
//!  - [Documentation](https://docs.rs/fixcol/latest/fixcol)
//!  - [Repository](https://github.com/BrianLondon/fixcol)
//! 
//! [`ReadFixed`]: fixcol::ReadFixed
//! [`WriteFixed`]: fixcol::WriteFixed
mod attrs;
mod enums;
mod error;
mod fields;
mod structs;

extern crate proc_macro;
extern crate proc_macro2;
extern crate quote;
extern crate syn;

use enums::enum_write;
use error::MacroError;
use proc_macro::TokenStream;
use quote::quote;
use syn::spanned::Spanned;
use syn::{Data, DataEnum, DataStruct, DeriveInput};

use crate::enums::enum_read;
use crate::structs::{struct_read, struct_write};

/// Derive proc-macro for ReadFixed
///
/// See documentation on [`ReadFixed`] for a full description.
///
/// [`ReadFixed`]: fixcol::ReadFixed
#[proc_macro_derive(ReadFixed, attributes(fixcol))]
pub fn read_fixed_impl(input: TokenStream) -> TokenStream {
    let ast: DeriveInput = syn::parse(input).unwrap();

    let name = &ast.ident;
    let attrs = &ast.attrs;
    let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();

    let function_impl_result = match ast.data {
        Data::Struct(DataStruct { fields, .. }) => struct_read(name, attrs, fields),
        Data::Enum(DataEnum { variants, .. }) => enum_read(name, attrs, variants.iter().collect()),
        Data::Union(u) => Err(MacroError::new(
            "Deriving ReadFixed on unions is not supported",
            u.union_token.span(),
        )),
    };

    let gen = match function_impl_result {
        Ok(function_impl) => {
            quote! {
                impl #impl_generics fixcol::ReadFixed for #name #ty_generics #where_clause {
                    #function_impl
                }
            }
        }
        Err(err) => quote! { #err },
    };

    // println!("{}", gen);

    gen.into()
}

/// Derive proc-macro for WriteFixed
///
/// See [`WriteFixed`] for a complete discuassion.
///
/// [`WriteFixed`]: fixcol::WriteFixed
#[proc_macro_derive(WriteFixed, attributes(fixcol))]
pub fn write_fixed_impl(input: TokenStream) -> TokenStream {
    let ast: DeriveInput = syn::parse(input).unwrap();

    let name = &ast.ident;
    let attrs = &ast.attrs;
    let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();

    let function_impl_result = match ast.data {
        Data::Struct(DataStruct { fields, .. }) => struct_write(name, attrs, fields),
        Data::Enum(DataEnum { variants, .. }) => {
            enum_write(name, attrs, &variants.iter().collect::<Vec<_>>())
        }
        Data::Union(u) => Err(MacroError::new(
            "Deriving WriteFixed on unions is not supported",
            u.union_token.span(),
        )),
    };

    let gen = match function_impl_result {
        Ok(function_impl) => {
            quote! {
                impl #impl_generics fixcol::WriteFixed for #name #ty_generics #where_clause {
                    #function_impl
                }
            }
        }
        Err(err) => quote! { #err },
    };

    // println!("{}", gen);

    gen.into()
}

#[cfg(test)]
mod tests {
    // use super::*;

    #[test]
    fn macro_test() {
        assert!(true);
    }
}