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

Alorel / macroific-rs / 11197184821

05 Oct 2024 11:43PM CUT 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

47.32
/modules/macroific-attr-parse/src/parse_option_impls.rs
1
use std::rc::Rc;
2
use std::sync::Arc;
3

4
use proc_macro2::{Literal, Span, TokenStream};
5
use quote::ToTokens;
6
use syn::parse::{Parse, ParseStream};
7
use syn::punctuated::Punctuated;
8
use syn::*;
9

10
use crate::ValueSyntax;
11
use crate::__attr_parse_prelude::*;
12

13
impl ParseOption for bool {
14
    fn from_stream(input: ParseStream) -> Result<Self> {
5✔
15
        input.parse_bool_attr()
5✔
16
    }
5✔
17
}
18

19
impl<T: ParseOption, P: Parse> ParseOption for Punctuated<T, P> {
20
    #[inline]
21
    fn from_stream(input: ParseStream) -> Result<Self> {
3✔
22
        if input.is_empty() {
3✔
23
            return Ok(Self::new());
×
24
        }
3✔
25

3✔
26
        let parse_buf;
3✔
27
        let parse_from: ParseStream;
3✔
28
        match ValueSyntax::from_stream(input) {
3✔
29
            Some(ValueSyntax::Eq) => {
30
                input.parse::<Token![=]>()?;
×
31
                let outer;
32
                parenthesized!(outer in input);
×
33

34
                bracketed!(parse_buf in outer);
×
35
                parse_from = &parse_buf;
×
36
            }
37
            Some(ValueSyntax::Paren) => {
38
                parenthesized!(parse_buf in input);
2✔
39
                parse_from = &parse_buf;
2✔
40
            }
41
            None => {
1✔
42
                parse_from = input;
1✔
43
            }
1✔
44
        };
45

46
        Self::parse_terminated_with(parse_from, ParseOption::from_stream)
3✔
47
    }
3✔
48
}
49

50
impl<T: FromExpr, P: Parse + Default> FromExpr for Punctuated<T, P> {
51
    #[cfg_attr(not(feature = "full"), inline, allow(unused_variables))]
52
    fn from_expr(expr: Expr) -> Result<Self> {
×
53
        #[cfg(feature = "full")]
×
54
        match expr {
×
55
            Expr::Array(ExprArray { elems, .. }) | Expr::Tuple(ExprTuple { elems, .. }) => {
×
56
                let it = elems.into_iter().map(T::from_expr);
×
NEW
57
                it.collect::<Result<_>>()
×
58
            }
×
59
            expr => Err(Error::new_spanned(
×
60
                expr,
×
61
                "Can't parse this type as `Punctuated`",
×
62
            )),
×
63
        }
×
64

×
65
        #[cfg(not(feature = "full"))]
×
66
        Err(Error::new(
×
67
            Span::call_site(),
×
68
            "`full` feature required to parse `Punctuated`",
×
69
        ))
×
70
    }
×
71
}
72

73
impl<T: ParseOption> ParseOption for Option<T> {
74
    fn from_stream(input: ParseStream) -> Result<Self> {
1✔
75
        Ok(Some(T::from_stream(input)?))
1✔
76
    }
1✔
77
}
78

79
impl<T: FromExpr> FromExpr for Option<T> {
80
    fn from_expr(expr: Expr) -> Result<Self> {
1✔
81
        Ok(Some(T::from_expr(expr)?))
1✔
82
    }
1✔
83

84
    #[inline]
85
    fn boolean() -> Option<Self> {
×
86
        Some(T::boolean())
×
87
    }
×
88
}
89

90
impl FromExpr for Expr {
91
    #[inline]
92
    fn from_expr(expr: Expr) -> Result<Self> {
×
93
        Ok(expr)
×
94
    }
×
95
}
96

97
impl FromExpr for TokenStream {
98
    #[inline]
99
    fn from_expr(expr: Expr) -> Result<Self> {
×
100
        Ok(expr.into_token_stream())
×
101
    }
×
102
}
103

104
impl FromExpr for Lit {
105
    fn from_expr(expr: Expr) -> Result<Self> {
2✔
106
        Ok(ExprLit::from_expr(expr)?.lit)
2✔
107
    }
2✔
108
}
109

110
impl FromExpr for bool {
111
    fn from_expr(expr: Expr) -> Result<Self> {
×
112
        Ok(<LitBool>::from_expr(expr)?.value())
×
113
    }
×
114

115
    #[inline]
116
    fn boolean() -> Option<Self> {
1✔
117
        Some(true)
1✔
118
    }
1✔
119
}
120

121
impl FromExpr for LitBool {
122
    fn from_expr(expr: Expr) -> Result<Self> {
×
123
        let lit = Lit::from_expr(expr)?;
×
124
        if let Lit::Bool(lit) = lit {
×
125
            Ok(lit)
×
126
        } else {
127
            Err(Error::new_spanned(lit, "Incompatible literal"))
×
128
        }
129
    }
×
130

131
    fn boolean() -> Option<Self> {
×
132
        Some(Self::new(true, Span::call_site()))
×
133
    }
×
134
}
135

136
impl FromExpr for Path {
137
    fn from_expr(expr: Expr) -> Result<Self> {
×
138
        Ok(<ExprPath>::from_expr(expr)?.path)
×
139
    }
×
140
}
141

142
/// For use within the macro. API subject to change at any time.
143
macro_rules! parse_impl {
144
    (lit $([$base: ty, $lit: ty]),+) => {
145
        $(
146
            impl ParseOption for $base {
147
              fn from_stream(input: ::syn::parse::ParseStream) -> ::syn::Result<Self> {
4✔
148
                  Ok(<$lit as ParseOption>::from_stream(input)?.value())
4✔
149
                }
4✔
150
            }
151

152
            from_expr!(lit_direct_num_ok $base, value => $lit);
153
        )+
154
    };
155
    (new [$($ty: ty),+]) => {
156
        $(
157
          impl<T: ParseOption> ParseOption for $ty {
158
            fn from_stream(input: ParseStream) -> Result<Self> {
×
159
                T::from_stream(input).map(<$ty>::new)
×
160
            }
×
161
          }
162

163
            impl<T: FromExpr> FromExpr for $ty {
164
                fn from_expr(expr: Expr) -> Result<Self> {
×
165
                    T::from_expr(expr).map(<$ty>::new)
×
166
                }
×
167
            }
168
        )+
169
    };
170
    (parse [$($ty: ty),+]) => {
171
        $(
172
          impl ParseOption for $ty {
173
              parse_impl!(parse);
174
          }
175
        )+
176
    };
177
    (parse if $feature: literal [$($ty: ty),+]) => {
178
        $(
179
          #[cfg_attr(doc_cfg, doc(cfg(feature = $feature)))]
180
          impl ParseOption for $ty {
181
              parse_impl!(parse);
182
          }
183
        )+
184
    };
185
    (parse) => {
186
          fn from_stream(input: ::syn::parse::ParseStream) -> ::syn::Result<Self> {
108✔
187
            ValueSyntax::from_stream(input).and_parse(input)
108✔
188
          }
108✔
189
    };
190
    (lit_num [$lit: ty => $($base: ty),+]) => {
191
        $(
192
            impl ParseOption for $base {
193
              fn from_stream(input: ::syn::parse::ParseStream) -> ::syn::Result<Self> {
22✔
194
                <$lit as ParseOption>::from_stream(input)?.base10_parse()
22✔
195
              }
22✔
196
            }
197

198
            from_expr!(lit_direct_num $base, base10_parse => $lit);
199
        )+
200
    };
201
}
202

203
macro_rules! from_expr {
204
    (direct $([$ident: ident => $target: ident]),+) => {
205
        $(
206
            impl FromExpr for $target {
207
                fn from_expr(expr: Expr) -> Result<Self> {
2✔
208
                    if let Expr::$ident(expr) = expr {
2✔
209
                        Ok(expr)
2✔
210
                    } else {
211
                        Err(Error::new_spanned(expr, "Incompatible expression"))
×
212
                    }
213
                }
2✔
214
            }
215
        )+
216
    };
217
    (lit $([$ident: ident => $target: ident]),+) => {
218
        $(
219
               impl FromExpr for $target {
220
                    fn from_expr(expr: Expr) -> Result<Self> {
2✔
221
                        let lit = Lit::from_expr(expr)?;
2✔
222
                        if let Lit::$ident(lit) = lit {
2✔
223
                            Ok(lit)
2✔
224
                        } else {
225
                            Err(Error::new_spanned(lit, "Incompatible literal"))
×
226
                        }
227
                    }
2✔
228
                }
229
            )+
230
    };
231
    (lit_direct_num $base: ty, $fn: ident => $lit: ty) => {
232
        impl FromExpr for $base {
233
            fn from_expr(expr: Expr) -> ::syn::Result<Self> {
1✔
234
                <$lit>::from_expr(expr)?.$fn()
1✔
235
            }
1✔
236
        }
237
    };
238
    (lit_direct_num_ok $base: ty, $fn: ident => $lit: ty) => {
239
        impl FromExpr for $base {
240
            fn from_expr(expr: Expr) -> ::syn::Result<Self> {
1✔
241
                Ok(<$lit>::from_expr(expr)?.$fn())
1✔
242
            }
1✔
243
        }
244
    };
245
    (tokenise [$($ident: ident),+]) => {
246
        $(
247
            impl FromExpr for $ident {
248
                fn from_expr(expr: Expr) -> Result<Self> {
×
249
                    parse2(expr.into_token_stream())
×
250
                }
×
251
            }
252
        )+
253
    };
254
}
255

256
from_expr!(tokenise [Lifetime, LifetimeParam, BoundLifetimes, TypeParamBound, TraitBound, TypeParam, GenericParam, WherePredicate]);
257
from_expr!(tokenise [Ident, Type, TypeArray, TypeBareFn, TypeGroup, TypeImplTrait, TypeInfer, TypeMacro, TypeNever, TypeParen, TypePath, TypePtr, TypeReference, TypeSlice, TypeTraitObject, TypeTuple, AngleBracketedGenericArguments, ConstParam, Abi, BareFnArg, Meta, MetaList, MetaNameValue, Visibility]);
258
from_expr!(lit [Str => LitStr], [ByteStr => LitByteStr], [Byte => LitByte], [Char => LitChar], [Int => LitInt], [Float => LitFloat], [Verbatim => Literal]);
259
from_expr!(direct [Array => ExprArray], [Assign => ExprAssign], [Async => ExprAsync], [Await => ExprAwait], [Binary => ExprBinary], [Block => ExprBlock], [Break => ExprBreak], [Call => ExprCall], [Cast => ExprCast], [Closure => ExprClosure], [Const => ExprConst], [Continue => ExprContinue], [Field => ExprField], [ForLoop => ExprForLoop], [Group => ExprGroup], [If => ExprIf], [Infer => ExprInfer], [Index => ExprIndex], [Let => ExprLet], [Lit => ExprLit], [Loop => ExprLoop], [Macro => ExprMacro], [Match => ExprMatch], [MethodCall => ExprMethodCall], [Paren => ExprParen], [Path => ExprPath], [Range => ExprRange], [Reference => ExprReference], [Repeat => ExprRepeat], [Return => ExprReturn], [Struct => ExprStruct], [Try => ExprTry], [TryBlock => ExprTryBlock], [Tuple => ExprTuple], [Unary => ExprUnary], [Unsafe => ExprUnsafe], [While => ExprWhile], [Yield => ExprYield]);
260

261
parse_impl!(lit [String, LitStr], [char, LitChar]);
262

263
parse_impl!(lit_num [LitFloat => f32, f64]);
264
parse_impl!(lit_num [LitInt => u8, i8, u16, i16, u32, i32, u64, i64, usize, isize]);
265

266
parse_impl!(parse [Expr, AngleBracketedGenericArguments, ConstParam, Abi, BareFnArg, Ident, Path, Meta, MetaList, MetaNameValue, Visibility]);
267
parse_impl!(parse [Lifetime, LifetimeParam, BoundLifetimes, TypeParamBound, TraitBound, TypeParam, GenericParam, WherePredicate]);
268
parse_impl!(parse [Lit, LitBool, LitByteStr, LitByte, LitStr, LitChar, LitInt, LitFloat, Literal]);
269
parse_impl!(parse [Type, TypeArray, TypeBareFn, TypeGroup, TypeImplTrait, TypeInfer, TypeMacro, TypeNever, TypeParen, TypePath, TypePtr, TypeReference, TypeSlice, TypeTraitObject, TypeTuple]);
270

271
parse_impl!(new [Box<T>, Rc<T>, Arc<T>]);
272

273
#[cfg(feature = "full")]
274
parse_impl!(parse if "full" [ExprArray, ExprAssign, ExprAsync, ExprAwait, ExprBinary, ExprBlock, ExprBreak, ExprCall, ExprCast, ExprClosure, ExprConst, ExprContinue, ExprField, ExprForLoop, ExprIf, ExprIndex, ExprInfer, ExprLet, ExprLit, ExprLoop, ExprMacro, ExprMatch, ExprMethodCall, ExprParen, ExprPath, ExprRange, ExprReference, ExprRepeat, ExprReturn, ExprStruct, ExprTry, ExprTryBlock, ExprTuple, ExprUnary, ExprUnsafe, ExprWhile, ExprYield]);
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