rust_bridge_macros 0.1.0

The macros for the rust_bridge crate
Documentation
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
use quote::{format_ident, quote, ToTokens};
use std::fs::OpenOptions;
use std::io::{Read, Write};
use syn::{visit::Visit, DeriveInput, ItemImpl, Type};

use crate::common::{AttributeArgs, GetImplMethod, SupportedLanguage};
use crate::types::{OutputType, SupportedType};

const STUB_TOP: &str = r#"
# Top of file key: A12BECOD!
from typing import List, Dict, Optional, Self, Any

"#;

/// This function assumes the user has already impliemented:
/// - `FromPyObject` for the wrapped type
/// - `IntoPy<PyObject>` for the wrapped type
pub fn generate_alias_manual(parsed: DeriveInput) -> proc_macro::TokenStream {
    let name_ident = format_ident!("{}Python", parsed.ident);
    let wrapped_type_ident = parsed.ident;

    let expanded = quote! {
        #[cfg(feature = "python")]
        pub struct #name_ident {
            pub wrapped: #wrapped_type_ident
        }

        #[cfg(feature = "python")]
        impl rust_bridge::python::CustomInto<#name_ident> for #wrapped_type_ident {
            fn custom_into(self) -> #name_ident {
                #name_ident {
                    wrapped: self,
                }
            }
        }

        #[cfg(feature = "python")]
        impl rust_bridge::python::CustomInto<#wrapped_type_ident> for #name_ident {
            fn custom_into(self) -> #wrapped_type_ident {
                self.wrapped
            }
        }

        // From Python to Rust
        #[cfg(feature = "python")]
        impl pyo3::conversion::FromPyObject<'_> for #name_ident {
            fn extract(obj: &pyo3::PyAny) -> pyo3::PyResult<Self> {
                Ok(Self {
                    wrapped: obj.extract()?
                })
            }
        }

        // From Rust to Python
        #[cfg(feature = "python")]
        impl pyo3::conversion::IntoPy<pyo3::PyObject> for #name_ident {
            fn into_py(self, py: pyo3::Python) -> pyo3::PyObject {
                use pyo3::conversion::ToPyObject;
                self.wrapped.into_py(py)
            }
        }
    };

    proc_macro::TokenStream::from(expanded)
}

pub fn generate_python_alias(parsed: DeriveInput) -> proc_macro::TokenStream {
    let name_ident = format_ident!("{}Python", parsed.ident);
    let wrapped_type_ident = parsed.ident;
    let wrapped_type_name = wrapped_type_ident.to_string();
    // May also want to put a __print__ method here (if that works) automatically for every CustomDerive struct
    let expanded = quote! {
        #[cfg(feature = "python")]
        #[pyo3::pyclass(name = #wrapped_type_name)]
        #[derive(Clone)]
        pub struct #name_ident {
            pub wrapped: std::boxed::Box<#wrapped_type_ident>
        }

        #[cfg(feature = "python")]
        impl rust_bridge::python::CustomInto<#name_ident> for #wrapped_type_ident {
            fn custom_into(self) -> #name_ident {
                #name_ident {
                    wrapped: std::boxed::Box::new(self),
                }
            }
        }

        #[cfg(feature = "python")]
        impl rust_bridge::python::CustomInto<#wrapped_type_ident> for #name_ident {
            fn custom_into(self) -> #wrapped_type_ident {
                *self.wrapped
            }
        }

        #[cfg(feature = "python")]
        impl rust_bridge::python::CustomInto<&'static mut #wrapped_type_ident> for &mut #name_ident {
            fn custom_into(self) -> &'static mut #wrapped_type_ident {
                // This is how we get around the liftime checker
                unsafe {
                    let ptr = &*self.wrapped as *const #wrapped_type_ident;
                    let ptr = ptr as *mut #wrapped_type_ident;
                    let boxed = Box::from_raw(ptr);
                    Box::leak(boxed)
                }
            }
        }

        #[cfg(feature = "python")]
        impl rust_bridge::python::CustomInto<&'static #wrapped_type_ident> for &mut #name_ident {
            fn custom_into(self) -> &'static #wrapped_type_ident {
                // This is how we get around the liftime checker
                unsafe {
                    let ptr = &*self.wrapped as *const #wrapped_type_ident;
                    let ptr = ptr as *mut #wrapped_type_ident;
                    let boxed = Box::from_raw(ptr);
                    Box::leak(boxed)
                }
            }
        }

        #[cfg(feature = "python")]
        impl rust_bridge::python::CustomInto<#wrapped_type_ident> for &mut #name_ident {
            fn custom_into(self) -> #wrapped_type_ident {
                // This is how we get around the liftime checker
                unsafe {
                    let ptr = &*self.wrapped as *const #wrapped_type_ident;
                    let ptr = ptr as *mut #wrapped_type_ident;
                    let boxed = Box::from_raw(ptr);
                    Box::leak(boxed).to_owned()
                }
            }
        }

        #[cfg(feature = "python")]
        impl rust_bridge::python::CustomInto<&'static #wrapped_type_ident> for &#name_ident {
            fn custom_into(self) -> &'static #wrapped_type_ident {
                // This is how we get around the liftime checker
                unsafe {
                    let ptr = &*self.wrapped as *const #wrapped_type_ident;
                    let ptr = ptr as *mut #wrapped_type_ident;
                    let boxed = Box::from_raw(ptr);
                    Box::leak(boxed)
                }
            }
        }

        #[cfg(feature = "python")]
        impl rust_bridge::python::CustomInto<#wrapped_type_ident> for &#name_ident {
            fn custom_into(self) -> #wrapped_type_ident {
                // This is how we get around the liftime checker
                unsafe {
                    let ptr = &*self.wrapped as *const #wrapped_type_ident;
                    let ptr = ptr as *mut #wrapped_type_ident;
                    let boxed = Box::from_raw(ptr);
                    Box::leak(boxed).to_owned()
                }
            }
        }

        #[cfg(feature = "python")]
        impl pyo3::IntoPy<pyo3::PyObject> for #wrapped_type_ident {
            fn into_py(self, py: pyo3::Python) -> pyo3::PyObject {
                use pyo3::conversion::IntoPy;
                use rust_bridge::python::CustomInto;
                let x: #name_ident = self.custom_into();
                x.into_py(py)
            }
        }
    };
    proc_macro::TokenStream::from(expanded)
}

pub fn generate_python_methods(
    parsed: ItemImpl,
    attribute_args: &AttributeArgs,
) -> proc_macro::TokenStream {
    let mut methods = Vec::new();

    let wrapped_type_ident = match *parsed.self_ty {
        Type::Path(p) => p.path.segments.first().unwrap().ident.clone(),
        _ => panic!("Error getting struct ident for impl block"),
    };
    let name_ident = format_ident!("{}Python", wrapped_type_ident);

    let python_class_name = wrapped_type_ident.to_string();
    let mut stubs = format!("\nclass {}:\n", python_class_name);

    // Iterate over the items - see: https://docs.rs/syn/latest/syn/enum.ImplItem.html
    for item in parsed.items {
        // We only create methods for functions listed in the attribute args
        match &item {
            syn::ImplItem::Fn(f) => {
                let method_name = f.sig.ident.to_string();
                if !attribute_args.should_alias_method(&method_name, SupportedLanguage::Python) {
                    continue;
                }
            }
            _ => continue,
        }

        // Get ImplMethod details - see: https://docs.rs/syn/latest/syn/visit/index.html
        let mut method = GetImplMethod::default();
        method.visit_impl_item(&item);
        if !method.exists {
            continue;
        }
        let method_ident = method.method_ident.clone();

        let (method_arguments, wrapper_arguments, prep_wrapper_arguments) =
            get_method_wrapper_arguments_python(&method);
        let (output_type, convert_from) = match &method.output_type {
            OutputType::Result(v) | OutputType::Other(v) => {
                convert_output_type_convert_from_python(v, &method)
            }
            OutputType::Default => (None, None),
        };

        let some_wrapper_type = match method.receiver.as_ref() {
            Some(r) => {
                let st = r.to_string();
                Some(if st.contains('&') {
                    let st = st.replace("self", &wrapped_type_ident.to_string());
                    let s = syn::parse_str::<syn::Type>(&st).unwrap_or_else(|_| {
                        panic!("Error converting self type to necessary syn type: {:?}", r)
                    });
                    s.to_token_stream()
                } else {
                    quote! { #wrapped_type_ident }
                })
            }
            None => None,
        };

        let signature = quote! {
            pub fn #method_ident<'a>(#(#method_arguments),*) -> #output_type
        };

        let p1 = if method.is_async { "async def" } else { "def" };
        let p2 = match method_ident.to_string().as_str() {
            "new" => "__init__".to_string(),
            _ => method_ident.to_string(),
        };
        let mut p3 = method
            .method_arguments
            .iter()
            .map(|a| {
                format!(
                    "{}: {}",
                    a.0.replace("mut", "").trim(),
                    get_python_type(&a.1)
                )
            })
            .collect::<Vec<String>>();
        p3.insert(0, "self".to_string());
        let p3 = p3.join(", ");
        let p4 = match &method.output_type {
            OutputType::Result(v) | OutputType::Other(v) => get_python_type(v),
            OutputType::Default => "None".to_string(),
        };
        stubs.push_str(&format!("\t{} {}({}) -> {}", p1, p2, p3, p4));
        stubs.push_str("\n\t\t...\n");

        let prepared_wrapper_arguments = quote! {
            #(#prep_wrapper_arguments)*
        };

        // The new function for pyO3 requires some unique syntax
        // The way we use the #convert_from assumes that new has a return type
        let (signature, middle) = if method_ident == "new" {
            let signature = quote! {
                #[new]
                #signature
            };
            let middle = if method.is_async {
                quote! {
                    crate::get_or_set_runtime().block_on(#wrapped_type_ident::new(#(#wrapper_arguments),*))
                }
            } else {
                quote! {
                    #wrapped_type_ident::new(#(#wrapper_arguments),*)
                }
            };
            let middle = if let OutputType::Result(_r) = method.output_type {
                quote! {
                    let x = match #middle {
                        Ok(m) => m,
                        Err(e) => return Err(pyo3::PyErr::new::<pyo3::exceptions::PyException, _>(e.to_string()))
                    };
                }
            } else {
                quote! {
                    let x = #middle;
                }
            };
            let middle = quote! {
                use std::ops::DerefMut;
                use rust_bridge::python::CustomInto;
                #prepared_wrapper_arguments
                #middle
                let x: #convert_from = x.custom_into();
                Ok(x)
            };
            (signature, middle)
        } else {
            let middle = quote! {
                #method_ident(#(#wrapper_arguments),*)
            };

            let middle = if method.is_async {
                quote! {
                    {
                        wrapped.#middle.await
                    }
                }
            } else {
                quote! {
                    wrapped.#middle
                }
            };

            let middle = if let OutputType::Result(_r) = method.output_type {
                quote! {
                    let x = match #middle {
                        Ok(m) => m,
                        Err(e) => return Err(pyo3::PyErr::new::<pyo3::exceptions::PyException, _>(e.to_string()))
                    };
                }
            } else {
                quote! {
                    let x = #middle;
                }
            };
            let middle = if let Some(convert) = convert_from {
                quote! {
                    #middle
                    // let x = <#convert>::from(x);
                    let x: #convert = x.custom_into();
                }
            } else {
                middle
            };
            let middle = if method.is_async {
                quote! {
                    use rust_bridge::python::CustomInto;
                    let mut wrapped: #some_wrapper_type = self.custom_into();
                    #prepared_wrapper_arguments
                    pyo3_asyncio::tokio::future_into_py(py, async move {
                        #middle
                        Ok(x)
                    })
                }
            } else {
                quote! {
                    use rust_bridge::python::CustomInto;
                    let mut wrapped: #some_wrapper_type = self.custom_into();
                    #prepared_wrapper_arguments
                    #middle
                    Ok(x)
                }
            };
            (signature, middle)
        };

        methods.push(quote! {
            #signature {
                #middle
            }
        });
    }

    let path = std::env::var("PYTHON_STUB_FILE");
    if let Ok(path) = path {
        let mut file = OpenOptions::new()
            .create(true)
            .append(true)
            .read(true)
            .open(path)
            .unwrap();
        let mut contents = String::new();
        file.read_to_string(&mut contents)
            .expect("Unable to read stubs file for python");
        if !contents.contains("A12BECOD") {
            file.write_all(STUB_TOP.as_bytes())
                .expect("Unable to write stubs file for python");
        }
        if !contents.contains(&format!("class {}:", python_class_name)) {
            file.write_all(stubs.as_bytes())
                .expect("Unable to write stubs file for python");
        }
    }
    proc_macro::TokenStream::from(quote! {
        #[cfg(feature = "python")]
        #[pyo3::pymethods]
        impl #name_ident {
            #(#methods)*
        }
    })
}

pub fn get_method_wrapper_arguments_python(
    method: &GetImplMethod,
) -> (
    Vec<proc_macro2::TokenStream>,
    Vec<proc_macro2::TokenStream>,
    Vec<proc_macro2::TokenStream>,
) {
    let mut method_arguments = Vec::new();
    let mut wrapper_arguments = Vec::new();
    let mut prep_wrapper_arguments = Vec::new();

    if let Some(_receiver) = &method.receiver {
        method_arguments.push(quote! { &mut self });
    }

    method
        .method_arguments
        .iter()
        .for_each(|(argument_name, argument_type)| {
            let argument_name_ident = format_ident!("{}", argument_name.replace("mut ", ""));
            let (method_argument, wrapper_argument, prep_wrapper_argument) =
                convert_method_wrapper_arguments(
                    argument_name_ident,
                    argument_type,
                    method.is_async,
                );
            method_arguments.push(method_argument);
            wrapper_arguments.push(wrapper_argument);
            prep_wrapper_arguments.push(prep_wrapper_argument);
        });

    let extra_arg = quote! {
        py: pyo3::Python<'a>
    };
    if !method_arguments.is_empty() {
        method_arguments.insert(1, extra_arg);
    } else {
        method_arguments.push(extra_arg);
    }

    (method_arguments, wrapper_arguments, prep_wrapper_arguments)
}

fn convert_method_wrapper_arguments(
    name_ident: syn::Ident,
    ty: &SupportedType,
    _is_async: bool,
) -> (
    proc_macro2::TokenStream,
    proc_macro2::TokenStream,
    proc_macro2::TokenStream,
) {
    let pyo3_type = ty
        .to_type(Some("Python"))
        .expect("Could not parse type in convert_method_wrapper_arguments in python.rs");
    let normal_type = ty
        .to_type(None)
        .expect("Could not parse type in convert_method_wrapper_arguments in python.rs");

    (
        quote! { #name_ident: #pyo3_type },
        quote! { #name_ident },
        quote! {
            let #name_ident: #normal_type = #name_ident.custom_into();
        },
    )
}

fn convert_output_type_convert_from_python(
    ty: &SupportedType,
    method: &GetImplMethod,
) -> (
    Option<proc_macro2::TokenStream>,
    Option<proc_macro2::TokenStream>,
) {
    let (output_type, convert_from) = match ty {
        SupportedType::S => (
            Some(quote! {pyo3::PyResult<Self>}),
            Some(format_ident!("Self").into_token_stream()),
        ),
        t => {
            let ty = t
                .to_type(Some("Python"))
                .expect("Error converting to type in convert_output_type_convert_from_python");
            (Some(quote! {pyo3::PyResult<#ty>}), Some(quote! {#ty}))
        }
    };

    if method.is_async && method.method_ident != "new" {
        (Some(quote! {pyo3::PyResult<&'a pyo3::PyAny>}), convert_from)
    } else {
        (output_type, convert_from)
    }
}

fn get_python_type(ty: &SupportedType) -> String {
    match ty {
        SupportedType::Reference(r) => get_python_type(&r.ty),
        SupportedType::S => "Self".to_string(),
        SupportedType::str | SupportedType::String => "str".to_string(),
        SupportedType::bool => "bool".to_string(),
        SupportedType::Option(o) => format!(
            "Optional[{}] = {}",
            get_python_type(o),
            get_type_for_optional(o)
        ),
        SupportedType::Vec(v) => format!("List[{}]", get_python_type(v)),
        SupportedType::HashMap((k, v)) => {
            format!("Dict[{}, {}]", get_python_type(k), get_python_type(v))
        }
        SupportedType::Tuple(t) => {
            let mut types = Vec::new();
            for ty in t {
                types.push(get_python_type(ty));
            }
            // Rust's unit type is represented as an empty tuple
            if types.is_empty() {
                "None".to_string()
            } else {
                format!("tuple[{}]", types.join(", "))
            }
        }
        SupportedType::i64 | SupportedType::u64 => "int".to_string(),
        SupportedType::f64 => "float".to_string(),
        // Our own types
        SupportedType::CustomType(t) => t.to_string(),
        // Add more types as required
        _ => "Any".to_string(),
    }
}

fn get_type_for_optional(ty: &SupportedType) -> String {
    match ty {
        SupportedType::Reference(r) => get_type_for_optional(&r.ty),
        SupportedType::str | SupportedType::String => {
            "\"Default set in Rust. Please check the documentation.\"".to_string()
        }
        SupportedType::HashMap(_) => "{}".to_string(),
        SupportedType::Vec(_) => "[]".to_string(),
        SupportedType::i64 | SupportedType::u64 => 1.to_string(),
        SupportedType::f64 => 1.0.to_string(),
        SupportedType::bool => "True".to_string(),

        _ => "Any".to_string(),
        // _ => panic!("Type not yet supported for optional python stub: {:?}", ty),
    }
}