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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
use proc_macro::TokenStream;
use quote::{quote, ToTokens};
use syn::{
parse, parse_macro_input, parse_str, punctuated::Punctuated, token::Comma,
AttributeArgs, FnArg, GenericArgument, ImplItem, ItemImpl, Meta, NestedMeta, Path,
PathArguments, Type, TypePath,
};
mod libs;
use libs::intertrait_macros::{
args::{Casts, Flag, Targets},
gen_caster::generate_caster,
};
const NO_INTERFACE_ARG_ERR_MESSAGE: &str =
"Expected a argument specifying a interface trait";
const INVALID_ARG_ERR_MESSAGE: &str = "Invalid argument passed";
const INVALID_ITEM_TYPE_ERR_MESSAGE: &str =
"The attached to item is not a trait implementation";
const IMPL_NO_NEW_METHOD_ERR_MESSAGE: &str =
"The attached to trait implementation is missing a new method";
const IMPL_NEW_METHOD_SELF_PARAM_ERR_MESSAGE: &str =
"The new method of the attached to trait implementation cannot have a self parameter";
const IMPL_NEW_METHOD_BOX_PARAMS_ERR_MESSAGE: &str =
"All parameters of the new method of the attached to trait implementation must be std::boxed::Box";
fn path_to_string(path: &Path) -> String
{
return path
.segments
.pairs()
.fold(String::new(), |mut acc, segment_pair| {
let segment_ident = &segment_pair.value().ident;
acc.push_str(segment_ident.to_string().as_str());
let opt_colon_two = segment_pair.punct();
match opt_colon_two {
Some(colon_two) => {
acc.push_str(colon_two.to_token_stream().to_string().as_str())
}
None => {}
}
acc
});
}
fn get_fn_args_has_self(fn_args: &Punctuated<FnArg, Comma>) -> bool
{
return fn_args.iter().any(|arg| match arg {
FnArg::Receiver(_) => true,
&_ => false,
});
}
fn get_fn_arg_type_paths(fn_args: &Punctuated<FnArg, Comma>) -> Vec<TypePath>
{
return fn_args.iter().fold(Vec::<TypePath>::new(), |mut acc, arg| {
match arg {
FnArg::Typed(typed_fn_arg) => match typed_fn_arg.ty.as_ref() {
Type::Path(arg_type_path) => acc.push(arg_type_path.clone()),
&_ => {}
},
FnArg::Receiver(_receiver_fn_arg) => {}
}
acc
});
}
fn get_dependency_types(item_impl: &ItemImpl) -> Vec<Type>
{
let impl_items = &item_impl.items;
let opt_new_method_impl_item = impl_items.iter().find(|item| match item {
ImplItem::Method(method_item) => method_item.sig.ident == "new",
&_ => false,
});
let new_method_impl_item = match opt_new_method_impl_item {
Some(item) => match item {
ImplItem::Method(method_item) => method_item,
&_ => panic!("{}", IMPL_NO_NEW_METHOD_ERR_MESSAGE),
},
None => panic!("{}", IMPL_NO_NEW_METHOD_ERR_MESSAGE),
};
let new_method_inputs = &new_method_impl_item.sig.inputs;
if get_fn_args_has_self(new_method_inputs) {
panic!("{}", IMPL_NEW_METHOD_SELF_PARAM_ERR_MESSAGE)
}
let new_method_arg_type_paths = get_fn_arg_type_paths(new_method_inputs);
return new_method_arg_type_paths.iter().fold(
Vec::<Type>::new(),
|mut acc, arg_type_path| {
let arg_type_path_string = path_to_string(&arg_type_path.path);
if arg_type_path_string != "Box"
&& arg_type_path_string != "std::boxed::Box"
&& arg_type_path_string != "boxed::Box"
{
panic!("{}", IMPL_NEW_METHOD_BOX_PARAMS_ERR_MESSAGE);
}
// Assume the type path has a last segment.
// The Box check wouldn't pass if it didn't
let last_path_segment = arg_type_path.path.segments.last().unwrap();
match &last_path_segment.arguments {
PathArguments::AngleBracketed(angle_bracketed_generic_args) => {
let generic_args = &angle_bracketed_generic_args.args;
let opt_first_generic_arg = generic_args.first();
// Assume a first generic argument exists because Box requires one
let first_generic_arg = opt_first_generic_arg.as_ref().unwrap();
match first_generic_arg {
GenericArgument::Type(first_generic_arg_type) => {
acc.push(first_generic_arg_type.clone());
}
&_ => {}
}
}
&_ => {}
}
acc
},
);
}
/// Makes a struct injectable. Therefore usable with `DIContainer`.
///
/// # Arguments
///
/// * A interface trait the struct implements.
///
/// # Examples
/// ```
/// trait IConfigReader
/// {
/// fn read_config() -> Config;
/// }
///
/// struct ConfigReader {}
///
/// #[injectable(IConfigReader)]
/// impl IConfigReader for ConfigReader
/// {
/// fn read_config() -> Config
/// {
/// // Stuff here
/// }
/// }
/// ```
#[proc_macro_attribute]
pub fn injectable(args_stream: TokenStream, impl_stream: TokenStream) -> TokenStream
{
let args = parse_macro_input!(args_stream as AttributeArgs);
if args.is_empty() {
panic!("{}", NO_INTERFACE_ARG_ERR_MESSAGE);
}
if args.len() > 1 {
panic!("Only a single argument is expected");
}
let interface_path = match &args[0] {
NestedMeta::Meta(arg_meta) => match arg_meta {
Meta::Path(path_arg) => path_arg,
&_ => panic!("{}", INVALID_ARG_ERR_MESSAGE),
},
&_ => panic!("{}", INVALID_ARG_ERR_MESSAGE),
};
let item_impl: ItemImpl = match parse(impl_stream) {
Ok(impl_parsed) => impl_parsed,
Err(_) => {
panic!("{}", INVALID_ITEM_TYPE_ERR_MESSAGE)
}
};
let self_type = item_impl.self_ty.as_ref();
let self_type_path = match self_type {
Type::Path(path_self_type) => path_self_type.path.clone(),
&_ => parse_str("invalid_type").unwrap(),
};
let dependency_types = get_dependency_types(&item_impl);
quote! {
#item_impl
impl syrette::interfaces::injectable::Injectable for #self_type_path {
fn resolve(
di_container: &syrette::DIContainer
) -> error_stack::Result<Box<Self>, syrette::errors::injectable::ResolveError>
{
use error_stack::ResultExt;
return Ok(Box::new(Self::new(
#(di_container.get::<#dependency_types>()
.change_context(syrette::errors::injectable::ResolveError)
.attach_printable(format!(
"Unable to resolve a dependency of {}",
std::any::type_name::<#self_type_path>()
))?,
)*
)));
}
}
syrette::castable_to!(#self_type_path => #interface_path);
}
.into()
}
#[doc(hidden)]
#[proc_macro]
pub fn castable_to(input: TokenStream) -> TokenStream
{
let Casts {
ty,
targets: Targets { flags, paths },
} = parse_macro_input!(input);
paths
.iter()
.map(|t| generate_caster(&ty, t, flags.contains(&Flag::Sync)))
.collect::<proc_macro2::TokenStream>()
.into()
}
|