/
inject.rs
147 lines (129 loc) · 4.31 KB
/
inject.rs
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
use std::borrow::Cow;
use proc_macro2::TokenStream;
use quote::quote;
use syn::{parse_quote, Expr, FnArg, Ident, Stmt, Type};
use crate::{
refident::{MaybeIdent, MaybeType},
resolver::Resolver,
utils::IsLiteralExpression,
};
pub(crate) fn resolve_aruments<'a>(
args: impl Iterator<Item = &'a FnArg>,
resolver: &impl Resolver,
generic_types: &[Ident],
) -> TokenStream {
let define_vars = args.map(|arg| ArgumentResolver::new(resolver, generic_types).resolve(arg));
quote! {
#(#define_vars)*
}
}
struct ArgumentResolver<'resolver, 'idents, 'f, R>
where
R: Resolver + 'resolver,
{
resolver: &'resolver R,
generic_types_names: &'idents [Ident],
magic_conversion: &'f dyn Fn(Cow<Expr>, &Type) -> Expr,
}
impl<'resolver, 'idents, 'f, R> ArgumentResolver<'resolver, 'idents, 'f, R>
where
R: Resolver + 'resolver,
{
fn new(resolver: &'resolver R, generic_types_names: &'idents [Ident]) -> Self {
Self {
resolver,
generic_types_names,
magic_conversion: &handling_magic_conversion_code,
}
}
fn resolve(&self, arg: &FnArg) -> Option<Stmt> {
let ident = arg.maybe_ident()?;
let arg_type = arg.maybe_type()?;
let fixture_name = self.fixture_name(ident);
let mut fixture = self
.resolver
.resolve(&fixture_name)
.map(|e| e.clone())
.unwrap_or_else(|| default_fixture_resolve(&fixture_name));
if fixture.is_literal() && self.type_can_be_get_from_literal_str(arg_type) {
fixture = Cow::Owned((self.magic_conversion)(fixture, arg_type));
}
Some(parse_quote! {
let #ident = #fixture;
})
}
fn fixture_name<'a>(&self, ident: &'a Ident) -> Cow<'a, Ident> {
let id_str = ident.to_string();
if id_str.starts_with("_") && !id_str.starts_with("__") {
Cow::Owned(Ident::new(&id_str[1..], ident.span()))
} else {
Cow::Borrowed(ident)
}
}
fn type_can_be_get_from_literal_str(&self, t: &Type) -> bool {
// Check valid type to apply magic conversion
match t {
Type::ImplTrait(_)
| Type::TraitObject(_)
| Type::Infer(_)
| Type::Group(_)
| Type::Macro(_)
| Type::Never(_)
| Type::Paren(_)
| Type::Verbatim(_)
| Type::Slice(_) => return false,
_ => {}
}
match t.maybe_ident() {
Some(id) => !self.generic_types_names.contains(id),
None => true,
}
}
}
fn default_fixture_resolve(ident: &Ident) -> Cow<Expr> {
Cow::Owned(parse_quote! { #ident::default() })
}
fn handling_magic_conversion_code(fixture: Cow<Expr>, arg_type: &Type) -> Expr {
parse_quote! {
{
struct __Wrap<T>(std::marker::PhantomData<T>);
trait __ViaParseDebug<'a, T> {
fn magic_conversion(&self, input: &'a str) -> T;
}
impl<'a, T> __ViaParseDebug<'a, T> for &&__Wrap<T>
where
T: std::str::FromStr,
T::Err: std::fmt::Debug,
{
fn magic_conversion(&self, input: &'a str) -> T {
T::from_str(input).unwrap()
}
}
trait __ViaParse<'a, T> {
fn magic_conversion(&self, input: &'a str) -> T;
}
impl<'a, T> __ViaParse<'a, T> for &__Wrap<T>
where
T: std::str::FromStr,
{
fn magic_conversion(&self, input: &'a str) -> T {
match T::from_str(input) {
Ok(v) => v,
Err(_) => {
panic!("Cannot parse '{}' to get {}", input, std::stringify!(#arg_type));
}
}
}
}
trait __ViaIdent<'a, T> {
fn magic_conversion(&self, input: &'a str) -> T;
}
impl<'a> __ViaIdent<'a, &'a str> for &&__Wrap<&'a str> {
fn magic_conversion(&self, input: &'a str) -> &'a str {
input
}
}
(&&&__Wrap::<#arg_type>(std::marker::PhantomData)).magic_conversion(#fixture)
}
}
}