-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.rs
304 lines (278 loc) · 11.3 KB
/
utils.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use std::fmt::Display;
use proc_macro2::{Ident, TokenStream};
use quote::quote;
use syn::{
punctuated::Punctuated, spanned::Spanned, token::Comma, Error, FnArg, GenericArgument, Pat,
ReturnType, Type, TypePath, TypeReference, TypeSlice, TypeTuple,
};
#[derive(Debug)]
pub struct TypescriptArg {
pub ident: Ident,
pub ty: TypescriptType,
}
impl Display for TypescriptArg {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.ident, self.ty)
}
}
impl TryFrom<&FnArg> for TypescriptArg {
type Error = syn::Error;
fn try_from(value: &FnArg) -> Result<Self, Self::Error> {
match value {
FnArg::Receiver(receiver) => Err(Error::new(
receiver.span(),
"Cannot create a typescript type from a receiver argument.",
)),
FnArg::Typed(pat_ty) => {
let ty = match *pat_ty.ty {
syn::Type::Path(ref path) => TypescriptType::try_from(path)?,
syn::Type::Reference(ref type_reference) => TypescriptType::try_from(type_reference)?,
ref unknown => return Err(Error::new(unknown.span(), format!("Cannot create typescript arg from typed argument `{unknown:?}`. Unexpected argument type."))),
};
let ident = match *pat_ty.pat {
syn::Pat::Ident(ref ident_pat) => ident_pat.ident.clone(),
ref unknown => return Err(Error::new(unknown.span(), format!("Cannot create typescript arg from typed argument `{unknown:?}`. Unexpected argument identifier."))),
};
Ok(TypescriptArg { ident, ty })
}
}
}
}
#[derive(Debug, Clone)]
pub enum TypescriptType {
Void,
String,
Number,
Boolean,
Struct(String),
Promise(Box<TypescriptType>),
Array(Box<TypescriptType>),
Option(Box<TypescriptType>),
Tuple(Vec<TypescriptType>),
}
impl TypescriptType {
pub fn wrapped_with_promise(&self) -> Self {
match self {
TypescriptType::Promise(inner) => TypescriptType::Promise(inner.clone()),
other => TypescriptType::Promise(Box::new(other.clone())),
}
}
}
impl Display for TypescriptType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TypescriptType::Void => write!(f, "void"),
TypescriptType::String => write!(f, "string"),
TypescriptType::Number => write!(f, "number"),
TypescriptType::Boolean => write!(f, "boolean"),
TypescriptType::Struct(struct_name) => write!(f, "{}", struct_name),
TypescriptType::Promise(inner) => write!(f, "Promise<{}>", *inner),
TypescriptType::Array(inner) => write!(f, "{}[]", *inner),
TypescriptType::Option(inner) => write!(f, "{}|undefined", *inner),
TypescriptType::Tuple(inner) => {
write!(f, "[")?;
for (i, inner_ty) in inner.into_iter().enumerate() {
write!(f, "{}", inner_ty)?;
if i < inner.len() - 1 {
write!(f, ", ")?;
}
}
write!(f, "]")
}
}
}
}
impl TryFrom<&TypePath> for TypescriptType {
type Error = syn::Error;
fn try_from(value: &TypePath) -> Result<Self, Self::Error> {
let last_segment = value.path.segments.last().ok_or(Error::new(
value.span(),
"Cannot create a typescript type from type. No segments in type path.",
))?;
match last_segment.ident.to_string().as_str() {
"i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" | "u64" | "usize" | "f32" => {
Ok(TypescriptType::Number)
}
"bool" => Ok(TypescriptType::Boolean),
"String" | "str" => Ok(TypescriptType::String),
rs_type_str @ "Option" | rs_type_str @ "Result" | rs_type_str @ "Vec" => {
let inner_type: TypescriptType = match &last_segment.arguments {
// If
syn::PathArguments::AngleBracketed(args) => {
match &args.args[0] {
GenericArgument::Type(ty) => TypescriptType::try_from(ty)?,
ref unknown => {
return Err(Error::new(
args.args[0].span(),
format!("Cannot create a typescript type from {rs_type_str}. No type in result arguments: {unknown:?}."),
))
}
}
},
// If these are parenthesized that means it's a tuple
syn::PathArguments::Parenthesized(args) => {
let tuple_types: Result<Vec<_>, syn::Error> = args.inputs.iter().map(|ty| {
TypescriptType::try_from(ty)
}).collect();
TypescriptType::Tuple(tuple_types?)
},
ref unknown => {
return Err(Error::new(
last_segment.arguments.span(),
format!("Cannot create a typescript type from Result. Unexpected arguments type: {unknown:?}."),
))
}
};
match rs_type_str {
"Option" => Ok(TypescriptType::Option(Box::new(inner_type))),
"Result" => Ok(TypescriptType::Promise(Box::new(inner_type))),
"Vec" => Ok(TypescriptType::Array(Box::new(inner_type))),
_ => panic!("Impossible"),
}
}
class => Ok(TypescriptType::Struct(class.to_string())),
}
}
}
impl TryFrom<&TypeReference> for TypescriptType {
type Error = syn::Error;
fn try_from(value: &TypeReference) -> Result<Self, Self::Error> {
match *value.elem {
Type::Path(ref path) => {
TypescriptType::try_from(path)
},
_ => Err(Error::new(
value.span(),
format!("Cannot create a typescript type from type reference. Unexpected arguments type: {value:?}."),
))
}
}
}
impl TryFrom<&TypeSlice> for TypescriptType {
type Error = syn::Error;
fn try_from(value: &TypeSlice) -> Result<Self, Self::Error> {
let inner_type: Result<TypescriptType, Error> = value.elem.as_ref().try_into();
Ok(TypescriptType::Array(Box::new(inner_type?)))
}
}
impl TryFrom<&TypeTuple> for TypescriptType {
type Error = syn::Error;
fn try_from(value: &TypeTuple) -> Result<Self, Self::Error> {
let inner_types: Result<Vec<_>, Error> =
value.elems.iter().map(TypescriptType::try_from).collect();
Ok(TypescriptType::Tuple(inner_types?))
}
}
impl TryFrom<&syn::Type> for TypescriptType {
type Error = syn::Error;
fn try_from(value: &syn::Type) -> Result<Self, Self::Error> {
match value {
Type::Path(ty_path) => ty_path.try_into(),
Type::Tuple(ty_tuple) => ty_tuple.try_into(),
Type::Reference(ty_reference) => ty_reference.try_into(),
Type::Slice(ty_slice) => ty_slice.try_into(),
ref unknown => Err(Error::new(
unknown.span(),
format!("Cannot create a typescript type from tuple type. Unknown inner type: {unknown:?}."),
))
}
}
}
impl TryFrom<&FnArg> for TypescriptType {
type Error = syn::Error;
fn try_from(value: &FnArg) -> Result<Self, Self::Error> {
match value {
FnArg::Receiver(receiver) => Err(Error::new(
receiver.span(),
"Cannot create a typescript type from a receiver argument.",
)),
FnArg::Typed(pat_ty) => TypescriptType::try_from(&*pat_ty.ty),
}
}
}
impl TryFrom<&ReturnType> for TypescriptType {
type Error = syn::Error;
fn try_from(value: &ReturnType) -> Result<Self, Self::Error> {
match value {
ReturnType::Default => Ok(TypescriptType::Void),
ReturnType::Type(_, ty) => TypescriptType::try_from(&**ty),
}
}
}
#[derive(Debug)]
pub struct ApiMethodArgs {
pub world_ident: Ident,
pub api_args: Punctuated<FnArg, Comma>,
}
impl TryFrom<&Punctuated<FnArg, Comma>> for ApiMethodArgs {
type Error = Error;
fn try_from(value: &Punctuated<FnArg, Comma>) -> Result<Self, Self::Error> {
if value.is_empty() {
return Err(Error::new(
value.span(),
"Argument must have the at least 1 argument of `world: &mut World`.",
));
}
let mut iter = value.iter();
let first = iter.next().unwrap();
if let FnArg::Receiver(_) = &first {}
let world_ident = match first {
FnArg::Receiver(_) => {
return Err(Error::new_spanned(
first.clone(),
"First argument must be `world: &mut World`. Instead found `self`.",
))
}
FnArg::Typed(ref first_typed) => {
match *first_typed.ty {
Type::Reference(ref reference) => {
if reference.mutability.is_none() {
return Err(Error::new_spanned(reference, format!("First argument must be `world: &mut World`. First argument is a reference but is not mutable {:?}.", reference)));
}
}
ref unknown => {
return Err(Error::new_spanned(
unknown,
"First argument is unexpected type. ",
))
}
}
match *first_typed.pat {
Pat::Ident(ref ident) => {
&ident.ident
}
ref unknown => return Err(Error::new_spanned(unknown, format!("First argument must be `world: &mut World`. First argument has unexpected pattern {:?}.", unknown)))
}
}
};
Ok(ApiMethodArgs {
world_ident: world_ident.clone(),
api_args: iter.cloned().collect(),
})
}
}
impl ApiMethodArgs {
/// Token stream for defining the args on the external api (exposed to js)
pub fn api_args_definition_token_stream(&self) -> TokenStream {
let mut args: Vec<TokenStream> = vec![];
args.push(quote! { &self });
args.extend(self.api_args.iter().map(|fn_arg| {
quote! { #fn_arg }
}));
quote! { #(#args),* }
}
/// Token stream for passing in the args to the inner function (original method).
pub fn original_method_call_args_token_stream(&self) -> TokenStream {
let mut idents = vec![self.world_ident.clone()];
idents.extend(self.api_args.iter().map(|arg| {
let FnArg::Typed(pat_ty) = arg else {
panic!("Impossible")
};
let Pat::Ident(ref pat_ident) = *pat_ty.pat else {
panic!("Impossible");
};
pat_ident.ident.clone()
}));
quote! { #(#idents),* }
}
}