sark-gen 0.11.0

Sark proc-macro generators
Documentation
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::parse::{Parse, ParseStream};
use syn::{Error, Fields, Ident, ItemStruct, LitByteStr, LitStr, Result, Token, Type};

use crate::util::{AttributeSliceExt, TypeExt};

#[derive(Clone, Copy)]
pub(super) enum Mode {
    Json,
    Raw,
}

impl Parse for Mode {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        if input.is_empty() {
            return Ok(Self::Json);
        }
        let ident = input.parse::<Ident>()?;
        if ident == "json" {
            if !input.is_empty() {
                return Err(Error::new_spanned(
                    input.parse::<TokenStream>()?,
                    "#[sark_gen::response] supports only `json` or `raw`",
                ));
            }
            return Ok(Self::Json);
        }
        if ident == "raw" {
            if !input.is_empty() {
                input.parse::<Token![,]>()?;
                if !input.is_empty() {
                    let extra = input.parse::<Ident>()?;
                    return Err(Error::new_spanned(
                        extra,
                        "#[sark_gen::response] supports only `json` or `raw`",
                    ));
                }
            }
            return Ok(Self::Raw);
        }
        Err(Error::new_spanned(
            ident,
            "#[sark_gen::response] supports only `json` or `raw`",
        ))
    }
}

impl Mode {
    pub(super) fn expand(self, mut st: ItemStruct) -> Result<TokenStream> {
        let public_name = st.ident.clone();
        let vis = st.vis.clone();
        let static_headers = st.attrs.static_headers()?;
        st.attrs.retain(|attr| {
            !attr.path().is_ident("header") && !attr.path().is_ident("header_static")
        });
        // Static handlers consume their response value during macro expansion, so the
        // source-level response struct can legitimately have no runtime constructor.
        st.attrs.push(syn::parse_quote!(#[allow(dead_code)]));
        let has_borrowed = match &st.fields {
            Fields::Named(fields) => fields
                .named
                .iter()
                .any(|field| field.ty.has_borrowed_bytes()),
            _ => false,
        };
        let inner_name = public_name.clone();
        let fields = match &mut st.fields {
            Fields::Named(fields) => &mut fields.named,
            _ => {
                return Err(Error::new_spanned(
                    st.struct_token,
                    "#[sark_gen::response] requires a struct with named fields",
                ));
            }
        };

        let mut status_ident = None::<syn::Ident>;
        let mut body_ident = None::<syn::Ident>;
        let mut body_ty = None::<Type>;
        let mut body_is_static_slice = false;
        let mut dynamic = Vec::new();
        let mut all_fields = Vec::new();
        for field in fields.iter_mut() {
            let ident = field
                .ident
                .clone()
                .ok_or_else(|| Error::new(Span::call_site(), "named field required"))?;
            all_fields.push(ident.clone());
            if ident == "status" {
                status_ident = Some(ident.clone());
            }
            if ident == "body" {
                body_ident = Some(ident.clone());
                body_ty = Some(field.ty.clone());
                body_is_static_slice = field.ty.is_static_byte_slice();
            }
            if let Some(header) = field.attrs.header_name()? {
                dynamic.push((ident.clone(), header));
            }
            field.attrs.retain(|attr| !attr.path().is_ident("header"));
        }
        if has_borrowed
            && !st
                .generics
                .params
                .iter()
                .any(|p| matches!(p, syn::GenericParam::Lifetime(lt) if lt.lifetime.ident == "req"))
        {
            st.generics.params.insert(0, syn::parse_quote!('req));
        }
        let status_ident = status_ident.ok_or_else(|| {
            Error::new(
                Span::call_site(),
                "#[sark_gen::response] requires `status` field",
            )
        })?;
        let body_ident = body_ident.ok_or_else(|| {
            Error::new(
                Span::call_site(),
                "#[sark_gen::response] requires `body` field",
            )
        })?;
        let body_ty = body_ty.ok_or_else(|| {
            Error::new(
                Span::call_site(),
                "#[sark_gen::response] requires `body` field",
            )
        })?;

        let header_count = dynamic.len();
        let headers = HeaderEmit::new(has_borrowed, &dynamic, &static_headers)?;

        let body_build = match self {
            Mode::Json => quote! {
                let __resp_body = ::sark::json::JsonBody::new(#body_ident);
            },
            Mode::Raw => quote! {
                let __resp_body = #body_ident;
            },
        };

        let (impl_generics, ty_lifetime, serve_lt) = if has_borrowed {
            (quote!(<'req>), quote!(<'req>), quote!('req))
        } else {
            (quote!(), quote!(), quote!('static))
        };
        let fixed_ret = match self {
            Mode::Json => quote! {
                ::sark::sark_core::http::EncodedResponse<
                    #serve_lt,
                    ::sark::json::JsonBody<#body_ty>,
                    #header_count,
                >
            },
            Mode::Raw if has_borrowed => {
                quote!(::sark::sark_core::http::FixedResponse<'req, #header_count>)
            }
            Mode::Raw => {
                quote!(::sark::sark_core::http::FixedResponse<'static, #header_count>)
            }
        };
        let destructure = quote! { let Self { #( #all_fields, )* } = self; };
        let headers_build = headers.build_expr();
        let static_wire = &headers.static_wire;
        let response_ctor = match self {
            Mode::Json => quote!(::sark::sark_core::http::EncodedResponse::direct),
            Mode::Raw => quote!(::sark::sark_core::http::FixedResponse::direct),
        };
        let into_fixed_body = quote! {
            #destructure
            #body_build
            #headers_build
            #response_ctor(#status_ident, #static_wire, __resp_headers, __resp_body)
        };
        let fixed_api = if matches!(self, Mode::Raw) && body_is_static_slice {
            quote!()
        } else {
            quote! {
                impl #impl_generics #inner_name #ty_lifetime {
                    #vis fn into_fixed(self) -> #fixed_ret {
                        #into_fixed_body
                    }
                }

                impl #impl_generics From<#inner_name #ty_lifetime> for #fixed_ret {
                    fn from(value: #inner_name #ty_lifetime) -> #fixed_ret {
                        value.into_fixed()
                    }
                }
            }
        };
        let static_slice_emit = if matches!(self, Mode::Raw) && body_is_static_slice {
            quote! {
                impl #impl_generics #inner_name #ty_lifetime {
                    #vis fn into_static_response(
                        self,
                    ) -> ::sark::sark_core::http::StaticResponseInner<#serve_lt, #header_count> {
                        #destructure
                        #headers_build
                        ::sark::sark_core::http::StaticResponseInner::direct(
                            #status_ident,
                            #static_wire,
                            __resp_headers,
                            #body_ident,
                        )
                    }
                }

            }
        } else {
            quote!()
        };
        let native_body_kind = if body_is_static_slice {
            quote!(::sark::sark_core::http::body_kind::ResponseKind::Static)
        } else {
            quote!(::sark::sark_core::http::body_kind::ResponseKind::Inline)
        };
        let owned_shape_impl = if !has_borrowed && st.generics.params.is_empty() {
            let (owned_shape, into_shape) = if matches!(self, Mode::Json) {
                (quote!(#fixed_ret), quote!(self.into_fixed()))
            } else if body_is_static_slice {
                (
                    quote!(::sark::sark_core::http::StaticResponseInner<'static, #header_count>),
                    quote!(self.into_static_response()),
                )
            } else {
                (quote!(#fixed_ret), quote!(self.into_fixed()))
            };
            quote! {
                impl ::sark::sark_core::http::__private::GeneratedResponse for #inner_name {
                    type Shape = #owned_shape;

                    const BODY_KIND: ::sark::sark_core::http::body_kind::ResponseKind =
                        #native_body_kind;

                    fn into_owned_shape(self) -> Self::Shape {
                        #into_shape
                    }
                }
            }
        } else {
            quote!()
        };
        let native_response_impl = if matches!(self, Mode::Json) {
            quote! {
                impl #impl_generics ::sark::service::manifold::NativeResponse<#serve_lt>
                    for #inner_name #ty_lifetime
                {
                    type Kind = ::sark::service::manifold::Sync;
                    type Shape = #fixed_ret;
                    type Stream = ::sark::sark_core::http::NeverStream;

                    const BODY_KIND: ::sark::sark_core::http::body_kind::ResponseKind =
                        #native_body_kind;

                    fn into_route_response(self) -> Self::Shape {
                        self.into_fixed()
                    }
                }
            }
        } else if has_borrowed {
            if body_is_static_slice {
                quote! {
                    impl<'req> ::sark::service::manifold::NativeResponse<'req>
                        for #inner_name<'req>
                    {
                        type Kind = ::sark::service::manifold::Sync;
                        type Shape = ::sark::sark_core::http::StaticResponseInner<'req, #header_count>;
                        type Stream = ::sark::sark_core::http::NeverStream;

                        const BODY_KIND: ::sark::sark_core::http::body_kind::ResponseKind =
                            #native_body_kind;

                        fn into_route_response(self) -> Self::Shape {
                            self.into_static_response()
                        }
                    }
                }
            } else {
                quote! {
                    impl<'req> ::sark::service::manifold::NativeResponse<'req>
                        for #inner_name<'req>
                    {
                        type Kind = ::sark::service::manifold::Sync;
                        type Shape = ::sark::sark_core::http::FixedResponse<'req, #header_count>;
                        type Stream = ::sark::sark_core::http::NeverStream;

                        const BODY_KIND: ::sark::sark_core::http::body_kind::ResponseKind =
                            #native_body_kind;

                        fn into_route_response(self) -> Self::Shape {
                            self.into_fixed()
                        }
                    }
                }
            }
        } else {
            let (native_shape, into_native) = if body_is_static_slice {
                (
                    quote!(::sark::sark_core::http::StaticResponseInner<'req, #header_count>),
                    quote!(self.into_static_response()),
                )
            } else {
                (
                    quote!(::sark::sark_core::http::FixedResponse<'req, #header_count>),
                    quote!(self.into_fixed()),
                )
            };
            quote! {
                impl<'req> ::sark::service::manifold::NativeResponse<'req> for #inner_name {
                    type Kind = ::sark::service::manifold::Sync;
                    type Shape = #native_shape;
                    type Stream = ::sark::sark_core::http::NeverStream;

                    const BODY_KIND: ::sark::sark_core::http::body_kind::ResponseKind =
                        #native_body_kind;

                    fn into_route_response(self) -> Self::Shape {
                        #into_native
                    }
                }
            }
        };

        Ok(quote! {
            #st

            #fixed_api
            #static_slice_emit

            #owned_shape_impl

            #native_response_impl
        })
    }
}

struct HeaderEmit {
    headers_path: TokenStream,
    dyn_items: Vec<TokenStream>,
    static_wire: LitByteStr,
}

impl HeaderEmit {
    fn new(
        has_borrowed: bool,
        dynamic: &[(syn::Ident, LitStr)],
        static_headers: &[(LitStr, LitStr)],
    ) -> Result<Self> {
        let header_count = dynamic.len();
        if header_count > usize::from(u8::MAX) {
            return Err(Error::new(
                dynamic[usize::from(u8::MAX)].1.span(),
                "response supports at most 255 dynamic headers",
            ));
        }
        let (item_path, headers_path) = if has_borrowed {
            (
                quote!(::sark::sark_core::http::HeaderItem::<'req>),
                quote!(::sark::sark_core::http::Headers::<'req, #header_count>),
            )
        } else {
            (
                quote!(::sark::sark_core::http::HeaderItem),
                quote!(::sark::sark_core::http::Headers::<'static, #header_count>),
            )
        };
        let mut dyn_items = Vec::with_capacity(dynamic.len());
        for (ident, header_name) in dynamic {
            validate_header_name(header_name)?;
            dyn_items.push(quote! {
                #item_path::from_value(
                    const {
                        ::sark::sark_core::http::HeaderNameToken::new(#header_name)
                    },
                    #ident,
                )
            });
        }
        let mut wire = Vec::new();
        for (name, value) in static_headers {
            validate_header_name(name)?;
            validate_header_value(value)?;
            let name = name.value();
            let value = value.value();
            wire.extend_from_slice(name.as_bytes());
            wire.extend_from_slice(b": ");
            wire.extend_from_slice(value.as_bytes());
            wire.extend_from_slice(b"\r\n");
        }
        let static_wire = LitByteStr::new(&wire, Span::call_site());
        Ok(Self {
            headers_path,
            dyn_items,
            static_wire,
        })
    }

    fn build_expr(&self) -> TokenStream {
        let headers_path = &self.headers_path;
        let items = &self.dyn_items;
        quote! {
            let __resp_headers = #headers_path::from_items([
                #( #items, )*
            ]);
        }
    }
}

fn validate_header_name(name: &LitStr) -> Result<()> {
    match sark_protocol::validate_response_header_name(&name.value()) {
        Ok(()) => Ok(()),
        Err(sark_protocol::ResponseHeaderNameError::Empty) => Err(Error::new(
            name.span(),
            "response header name must not be empty",
        )),
        Err(sark_protocol::ResponseHeaderNameError::InvalidByte { index, byte }) => {
            Err(Error::new(
                name.span(),
                format!(
                    "response header name contains invalid HTTP token byte 0x{byte:02x} at byte {index}"
                ),
            ))
        }
        Err(sark_protocol::ResponseHeaderNameError::Managed) => Err(Error::new(
            name.span(),
            format!(
                "response header `{}` is managed by Sark and cannot be overridden",
                name.value()
            ),
        )),
    }
}

fn validate_header_value(value: &LitStr) -> Result<()> {
    match sark_protocol::validate_header_value(value.value().as_bytes()) {
        Ok(()) => Ok(()),
        Err(error) => Err(Error::new(
            value.span(),
            format!(
                "static response header value contains CR/LF at byte {}",
                error.index
            ),
        )),
    }
}