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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use attribute_derive::Attribute;
use manyhow::Result;
use proc_macro2::TokenStream;
use quote::quote;
use syn::punctuated::Punctuated;
use syn::token::Paren;
use syn::{DeriveInput, Ident, LitStr, Path, Type, TypeTuple};

use crate::core_path;

#[derive(Attribute)]
#[attribute(ident = view)]
struct ViewAttribute {
    #[attribute(example = "CollectionType")]
    collection: Type,
    #[attribute(example = "KeyType")]
    key: Type,
    #[attribute(example = "\"by-name\"")]
    name: Option<LitStr>,
    #[attribute(example = "ValueType")]
    value: Option<Type>,
    #[attribute(example = "bosaidb::core")]
    core: Option<Path>,
    #[attribute(example = "Format or None")]
    serialization: Option<Path>,
}

pub fn derive(
    DeriveInput {
        attrs,
        ident,
        generics,
        ..
    }: DeriveInput,
) -> Result {
    let ViewAttribute {
        collection,
        key,
        name,
        value,
        core,
        serialization,
    } = ViewAttribute::from_attributes(&attrs)?;

    let core = core.unwrap_or_else(core_path);

    let value = value.unwrap_or_else(|| {
        Type::Tuple(TypeTuple {
            paren_token: Paren::default(),
            elems: Punctuated::new(),
        })
    });

    let name = name
        .as_ref()
        .map_or_else(|| ident.to_string(), LitStr::value);

    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

    let serialization = match serialization {
        Some(serialization) if serialization.is_ident("None") => TokenStream::new(),
        Some(serialization) => quote! {
            impl #impl_generics #core::schema::SerializedView for #ident #ty_generics #where_clause {
                type Format = #serialization;

                fn format() -> Self::Format {
                    #serialization::default()
                }
            }
        },
        None => quote! {
            impl #impl_generics #core::schema::DefaultViewSerialization for #ident #ty_generics #where_clause {}
        },
    };

    Ok(quote! {
        impl #impl_generics #core::schema::View for #ident #ty_generics #where_clause {
            type Collection = #collection;
            type Key = #key;
            type Value = #value;

            fn name(&self) -> #core::schema::Name {
                #core::schema::Name::new(#name)
            }
        }
        #serialization
    })
}

#[derive(Attribute)]
#[attribute(ident = view_schema)]
struct ViewSchemaAttribute {
    #[attribute(example = "ViewType")]
    view: Option<Type>,
    #[attribute(example = "KeyType<'doc>")]
    mapped_key: Option<Type>,
    #[attribute(example = "\"by-name\"")]
    version: Option<u64>,
    #[attribute(example = "Lazy")]
    policy: Option<Ident>,
    #[attribute(example = "bosaidb::core")]
    core: Option<Path>,
}

pub fn derive_schema(
    DeriveInput {
        attrs,
        ident,
        generics,
        ..
    }: DeriveInput,
) -> Result {
    let ViewSchemaAttribute {
        view,
        mapped_key,
        version,
        policy,
        core,
    } = ViewSchemaAttribute::from_attributes(&attrs)?;

    let core = core.unwrap_or_else(core_path);

    let view = view.map_or_else(|| quote!(Self), |ty| quote!(#ty));

    let mapped_key = mapped_key.map_or_else(
        || quote!(<Self as #core::schema::View>::Key),
        |ty| quote!(#ty),
    );

    let version = version.map(|version| {
        quote!(fn version(&self) -> u64 {
            #version
        })
    });

    let policy = policy.map(|policy| {
        quote!(fn update_policy(&self) -> #core::schema::view::ViewUpdatePolicy {
            #core::schema::view::ViewUpdatePolicy::#policy
        })
    });

    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

    Ok(quote! {
        impl #impl_generics #core::schema::ViewSchema for #ident #ty_generics #where_clause {
            type View = #view;
            type MappedKey<'doc> = #mapped_key;

            #version
            #policy
        }
    })
}