• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Alorel / macroific-rs / 11197184821

05 Oct 2024 11:43PM UTC coverage: 81.468%. First build
11197184821

Pull #14

github

web-flow
Merge c8342d59b into 697e66c3a
Pull Request #14: v2

430 of 476 new or added lines in 16 files covered. (90.34%)

1099 of 1349 relevant lines covered (81.47%)

46.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.4
/modules/macroific-macro/src/attr_parse/attr_options.rs
1
use syn::{DeriveInput, Token};
2

3
use macroific_core::core_ext::*;
4

5
use super::{
6
    Delimiter, Fields, Generics, Group, Ident, ParseStream, Render, ToTokens, TokenStream, PRIVATE,
7
    RESULT,
8
};
9
use ::syn::parse::Parse;
10
use quote::{quote, TokenStreamExt};
11

12
impl Render for AttrOptionsDerive {
13
    const TRAIT_NAME: &'static str = "AttributeOptions";
14

15
    #[inline]
16
    fn generics(&self) -> &Generics {
9✔
17
        &self.generics
9✔
18
    }
9✔
19

20
    #[inline]
21
    fn ident(&self) -> &Ident {
9✔
22
        &self.ident
9✔
23
    }
9✔
24

25
    #[inline]
26
    fn fields(&self) -> &Fields {
9✔
27
        &self.fields
9✔
28
    }
9✔
29

NEW
30
    fn render_empty_body(ending: Option<Group>) -> TokenStream {
×
NEW
31
        quote! {
×
NEW
32
            #[inline]
×
NEW
33
            fn from_attr(_: ::syn::Attribute) -> ::syn::Result<Self> {
×
NEW
34
                #RESULT::Ok(Self #ending)
×
NEW
35
            }
×
NEW
36

×
NEW
37
            #[inline]
×
NEW
38
            fn from_iter(_: ::proc_macro2::Span, _: impl ::core::iter::IntoIterator<Item = ::syn::Attribute>) -> ::syn::Result<Self> {
×
NEW
39
                #RESULT::Ok(Self #ending)
×
NEW
40
            }
×
NEW
41
        }
×
NEW
42
    }
×
43
}
44

45
pub struct AttrOptionsDerive {
46
    ident: Ident,
47
    generics: Generics,
48
    fields: Fields,
49
}
50

51
impl Parse for AttrOptionsDerive {
52
    fn parse(input: ParseStream) -> ::syn::Result<Self> {
9✔
53
        let DeriveInput {
54
            ident,
9✔
55
            generics,
9✔
56
            data,
9✔
57
            ..
58
        } = input.parse()?;
9✔
59

60
        Ok(Self {
61
            ident,
9✔
62
            generics,
9✔
63
            fields: data.try_into()?,
9✔
64
        })
65
    }
9✔
66
}
67

68
impl ToTokens for AttrOptionsDerive {
NEW
69
    fn to_tokens(&self, _: &mut TokenStream) {
×
NEW
70
        unimplemented!("Use to_token_stream")
×
71
    }
72

73
    fn to_token_stream(&self) -> TokenStream {
9✔
74
        let fields = match self.named_fields() {
9✔
75
            Ok(fields) => fields,
9✔
76
            Err(delim) => return self.render_empty(delim),
×
77
        };
78

79
        let mut tokens = self.impl_generics();
9✔
80

81
        let span_arg_name = if fields.iter().any(move |f| f.opts.omit_default()) {
15✔
82
            Ident::create("attributes_span")
7✔
83
        } else {
84
            Ident::create("_")
2✔
85
        };
86

87
        let fn_body = Group::new(Delimiter::Brace, {
9✔
88
            let indexed_fields = super::indexed_fields(fields);
9✔
89
            let nones = super::nones(fields);
9✔
90

9✔
91
            let matches = indexed_fields.clone()
9✔
92
                .map(move |(option_var_name, field)| {
112✔
93
                    let mut stream = field.resolved_label().into_token_stream();
112✔
94
                    <Token![=>]>::default().to_tokens(&mut stream);
112✔
95

112✔
96
                    stream.append(Group::new(
112✔
97
                        Delimiter::Brace,
112✔
98
                        quote! { #PRIVATE::decode_attr_options_field(&mut #option_var_name, ident, meta.input) },
112✔
99
                    ));
112✔
100

112✔
101
                    stream
112✔
102
                });
112✔
103

9✔
104
            let unwraps = super::unwraps(indexed_fields, &span_arg_name);
9✔
105

9✔
106
            quote! {
9✔
107
                #nones
9✔
108

9✔
109
                for attr in attributes {
9✔
110
                    attr.parse_nested_meta(|meta| {
9✔
111
                        let ident = #PRIVATE::get_attr_ident(&meta.path)?;
9✔
112

9✔
113
                        match ::std::string::ToString::to_string(ident).as_str() {
9✔
114
                            #(#matches)*
9✔
115
                            other => #RESULT::Err(::syn::Error::new(::syn::spanned::Spanned::span(ident), ::std::format!("Unrecognised attribute: `{}`", other))),
9✔
116
                        }
9✔
117
                    })?;
9✔
118
                }
9✔
119

9✔
120
                #RESULT::Ok(Self #unwraps )
9✔
121
            }
9✔
122
        });
123

124
        // Struct body
125
        tokens.append(Group::new(Delimiter::Brace, {
9✔
126
            let mut signature = quote! {
9✔
127
                fn from_iter(#span_arg_name: ::proc_macro2::Span, attributes: impl ::core::iter::IntoIterator<Item = ::syn::Attribute>) -> ::syn::Result<Self>
9✔
128
            };
9✔
129
            signature.append(fn_body);
9✔
130
            signature
9✔
131
        }));
9✔
132

9✔
133
        tokens
9✔
134
    }
9✔
135
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc