MCPcopy Create free account
hub / github.com/HFQR/xitca-web / state

Function state

codegen/src/state.rs:5–40  ·  view source on GitHub ↗
(input: DeriveInput)

Source from the content-addressed store, hash-verified

3use syn::{Data, DeriveInput, Error};
4
5pub(crate) fn state(input: DeriveInput) -> Result<TokenStream, Error> {
6 let ty_ident = &input.ident;
7 let _generics = &input.generics;
8
9 let Data::Struct(ref ty) = input.data else {
10 return Err(Error::new(ty_ident.span(), "expect Struct"));
11 };
12
13 let fields = ty
14 .fields
15 .iter()
16 .enumerate()
17 .filter(|(_, field)| {
18 field.attrs.iter().any(|attr| {
19 attr.path()
20 .segments
21 .first()
22 .filter(|seg| seg.ident.to_string().as_str() == "borrow")
23 .is_some()
24 })
25 })
26 .map(|(_, field)| {
27 let ident = field.ident.as_ref().unwrap();
28 let ty = &field.ty;
29
30 quote! {
31 impl ::xitca_web::handler::state::BorrowState<#ty> for #ty_ident {
32 fn borrow(&self) -> &#ty {
33 &self.#ident
34 }
35 }
36 }
37 });
38
39 Ok(quote! { #(#fields)* }.into())
40}

Callers 2

from_codeMethod · 0.85
state_implFunction · 0.85

Calls 5

pathMethod · 0.80
mapMethod · 0.45
iterMethod · 0.45
as_strMethod · 0.45
as_refMethod · 0.45

Tested by

no test coverage detected