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
//! This crate provides the derive macro for Soars.

mod fields;
mod zst;

use fields::{fields_struct, FieldKind};
use proc_macro::TokenStream;
use proc_macro2::{Ident, Span, TokenStream as TokenStream2};
use quote::{quote, quote_spanned};
use std::collections::HashSet;
use syn::{parse_macro_input, Attribute, Data, DeriveInput, Fields};
use zst::{zst_struct, ZstKind};

#[proc_macro_derive(Soars, attributes(align, soa_derive))]
pub fn soa(input: TokenStream) -> TokenStream {
    let input: DeriveInput = parse_macro_input!(input);
    let span = input.ident.span();
    match soa_inner(input) {
        Ok(tokens) => tokens,
        Err(e) => match e {
            SoarsError::NotAStruct => quote_spanned! {
                span => compile_error!("Soars only applies to structs");
            },
            SoarsError::Syn(e) => e.into_compile_error(),
        },
    }
    .into()
}

fn soa_inner(input: DeriveInput) -> Result<TokenStream2, SoarsError> {
    let DeriveInput {
        ident,
        vis,
        data,
        attrs,
        generics: _,
    } = input;

    let soa_derive = SoaDerive::try_from(attrs)?;
    match data {
        Data::Struct(strukt) => match strukt.fields {
            Fields::Named(fields) => Ok(fields_struct(
                ident,
                vis,
                fields.named,
                FieldKind::Named,
                soa_derive,
            )?),
            Fields::Unnamed(fields) => Ok(fields_struct(
                ident,
                vis,
                fields.unnamed,
                FieldKind::Unnamed,
                soa_derive,
            )?),
            Fields::Unit => Ok(zst_struct(ident, vis, ZstKind::Unit)),
        },
        Data::Enum(_) | Data::Union(_) => Err(SoarsError::NotAStruct),
    }
}

#[derive(Debug, Clone)]
enum SoarsError {
    NotAStruct,
    Syn(syn::Error),
}

impl From<syn::Error> for SoarsError {
    fn from(value: syn::Error) -> Self {
        Self::Syn(value)
    }
}

#[derive(Debug, Clone, Default)]
struct SoaDerive {
    derives: HashSet<syn::Path>,
}

impl SoaDerive {
    fn into_derive(self) -> TokenStream2 {
        let Self { derives } = self;
        let derives = derives.into_iter();
        quote! {
            #[derive(#(#derives),*)]
        }
    }

    fn insert(&mut self, derive: &str) {
        self.derives.insert(syn::Path::from(syn::PathSegment {
            ident: Ident::new(derive, Span::call_site()),
            arguments: syn::PathArguments::None,
        }));
    }
}

impl TryFrom<Vec<Attribute>> for SoaDerive {
    type Error = syn::Error;

    fn try_from(value: Vec<Attribute>) -> Result<Self, Self::Error> {
        let mut out = Self::default();
        for attr in value {
            if attr.path().is_ident("soa_derive") {
                let _ = attr.parse_nested_meta(|meta| {
                    out.derives.insert(meta.path);
                    Ok(())
                });
            }
        }
        Ok(out)
    }
}