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

Alorel / macroific-rs / 11195403850

05 Oct 2024 06:27PM UTC coverage: 75.693%. First build
11195403850

Pull #14

github

web-flow
Merge 622ba6823 into de4fadcc0
Pull Request #14: v2

299 of 358 new or added lines in 13 files covered. (83.52%)

928 of 1226 relevant lines covered (75.69%)

50.96 hits per line

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

86.49
/modules/macroific-core/src/elements/attributed.rs
1
use proc_macro2::TokenStream;
2
use quote::ToTokens;
3
use syn::parse::{Parse, ParseStream};
4
use syn::Attribute;
5

6
/// Something that has [`Attribute`]s. Use when you case about processing that has attributes such
7
/// as doc comments, but aren't picky about what they're attached to.
8
///
9
/// Note that the item's `attributes` property will be an empty array - all attributes will be
10
/// contained on the `Attributed` instance.
11
///
12
/// ```
13
/// # use syn::{parse_quote, parse::{Parse, ParseStream}};
1✔
14
/// # use quote::{quote, ToTokens};
15
/// # use proc_macro2::TokenStream;
16
/// # use macroific_core as macroific;
17
/// # use macroific::elements::Attributed;
18
/// #
19
/// // Attributes can only be parsed using a ParseStream - wrapper struct created only to gain
20
/// // access to it. Note that `T` defaults to `TokenStream`.
21
/// struct MyStruct<T>(Attributed<T>);
22
/// impl<T: Parse> Parse for MyStruct<T> {
23
///     fn parse(input: ParseStream) -> syn::Result<Self> {
24
///        Attributed::parse_outer(input).map(Self)
2✔
25
///     }
2✔
26
/// }
2✔
27
///
28
/// let input_stream = quote! {
29
///   #[cfg(feature = "foo")]
1✔
30
///   fn foo() {}
1✔
31
/// };
1✔
32
///
1✔
33
/// let expect_attrs: Vec<syn::Attribute> = vec![parse_quote!(#[cfg(feature = "foo")])];
1✔
34
///
1✔
35
/// // `fn foo() {}` parsed as a `TokenStream`
1✔
36
/// let as_stream: MyStruct<TokenStream> = syn::parse2(input_stream.clone()).unwrap();
1✔
37
///
1✔
38
/// assert_eq!(as_stream.0.attributes, expect_attrs);
1✔
39
/// assert_eq!(as_stream.0.data.to_string(), "fn foo () { }");
1✔
40
///
1✔
41
/// // Alternatively, parse the data as some specific type - use this to, for example, verify that
42
/// // an attribute is attached to a `pub struct` without needing to parse the rest of the struct.
43
///
44
/// let as_fn: MyStruct<syn::ItemFn> = syn::parse2(input_stream).unwrap();
45
///
1✔
46
/// assert_eq!(as_fn.0.attributes, expect_attrs);
1✔
47
/// assert_eq!(as_fn.0.to_token_stream().to_string(), as_stream.0.to_token_stream().to_string());
1✔
48
/// ```
1✔
49
pub struct Attributed<T = TokenStream> {
1✔
50
    /// Collected attributes.
51
    pub attributes: Vec<Attribute>,
52

53
    /// The data following the attributes.
54
    pub data: T,
55
}
56

57
impl<T: Parse> Attributed<T> {
58
    /// Parse using [`Attribute::parse_outer`].
59
    #[allow(clippy::missing_errors_doc)]
60
    pub fn parse_outer(input: ParseStream) -> syn::Result<Self> {
2✔
61
        Ok(Self {
2✔
62
            attributes: Attribute::parse_outer(input)?,
2✔
63
            data: input.parse()?,
2✔
64
        })
65
    }
2✔
66

67
    /// Parse using [`Attribute::parse_inner`].
68
    #[allow(clippy::missing_errors_doc)]
NEW
69
    pub fn parse_inner(input: ParseStream) -> syn::Result<Self> {
×
NEW
70
        Ok(Self {
×
NEW
71
            attributes: Attribute::parse_outer(input)?,
×
NEW
72
            data: input.parse()?,
×
73
        })
NEW
74
    }
×
75
}
76

77
impl<T: ToTokens> ToTokens for Attributed<T> {
78
    fn to_tokens(&self, tokens: &mut TokenStream) {
2✔
79
        for attr in &self.attributes {
4✔
80
            attr.to_tokens(tokens);
2✔
81
        }
2✔
82

83
        self.data.to_tokens(tokens);
2✔
84
    }
2✔
85
}
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

© 2026 Coveralls, Inc