gerb 0.0.1-alpha+2023-04-27

Font editor for UFO 3 fonts.
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
/*
 * gerb
 *
 * Copyright 2022 - Manos Pitsidianakis
 *
 * This file is part of gerb.
 *
 * gerb is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * gerb is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gerb. If not, see <http://www.gnu.org/licenses/>.
 */

/* [ref:TODO] Unsolved problems in API types:
 *
 * - declare custom __repr__ and __str__ in macro decl
 * - declare methods
 * - declare class methods
 */

//! Wrapper types to expose to Python.
//!
//! Since pyo3 is not send safe, we cannot pass `GObjects` through channels. An
//! alternative is to create types with the same name and process all attribute get/set calls or
//! method calls via a message passing channel to the main thread.
//!
//! The singleton object in every python session is [`crate::api::Gerb`]. It contains the channel
//! receivers and senders.
//!
//! The API types will all need a `Py<Gerb>` reference in order to access the
//! API channel from the python thread to the main thread. `Py<_>` means a shared reference that is
//! owned by the Python side of things and can only be accessed with a `Python<'_>` token.
//!
//! That means every object is defined as:
//!
//! ```ignore
//! pub struct Object {
//!     pub(in crate::api) __gerb: Py<Gerb>,
//! }
//! ```
//!
//! and any kind of data access goes through the `__gerb` value as a JSON serialization of
//! [`crate::api::Request`]. [`Gerb`] will reply with a [`crate::api::Response`] JSON string.
//!
//! So any API type function has to perform this dance:
//!
//! ```ignore
//! // class method or static method or getter or setter.
//! fn method_name(&self, /* python arguments */, py: Python<'_>) -> PyResult</* result */> {
//!     self.__gerb
//!         .as_ref(py)
//!         .borrow()
//!         .__send_rcv(
//!             serde_json::to_string(&Request::/* Appropriate Request variant for what we want to do */)
//!             .unwrap(),
//!             py,
//!         )?
//!         .extract(py)
//! }
//! ```
//!
//! Unfortunately there's no succinct way to do this with the `pyo3` derive macros because objects
//! might have a lot of attributes and we have to write a getter method and a setter method for all
//! of them, and since the `#[pymethods]` is a proc_macro we cannot generate the methods inside of
//! it because of the way macros expand (in specific, this hypothetical macro won't have access to
//! the derive's macro attributes like `#[getter]` and `#[setter]` etc.).
//!
//! So this really ugly module is a manual implementation of a python class for `pyo3` that
//! generates all methods for any given number of fields.

use super::*;
use std::path::PathBuf;

/// Generate a getter method definiton that returns the method's get trampoline (defined elsewhere)
#[macro_export]
macro_rules! generate_getter_method_def {
    ($struct:tt, $($field:ident, $docstr:literal, $ty:ty),*) => {
        $(
            _pyo3::class::PyMethodDefType::Getter({
                _pyo3::class::PyGetterDef::new(
                    concat!(stringify!($field), "\0"),
                    _pyo3::impl_::pymethods::PyGetter({
                        unsafe extern "C" fn trampoline(
                            slf: *mut _pyo3::ffi::PyObject,
                            closure: *mut ::std::os::raw::c_void,
                        ) -> *mut _pyo3::ffi::PyObject
                        {
                            _pyo3::impl_::trampoline::getter(
                                slf,
                                closure,
                                $field::get_tramp,
                            )
                        }
                        trampoline
                    }),
                    concat!($docstr, "\0"),
                )
            })
        )*

    }
 }

/// Generate a setter method definiton that returns the method's set trampoline (defined elsewhere)
///
/// Only wrapped types (class objects that correspond to GObjects) can have setter methods.
#[macro_export]
macro_rules! generate_setter_method_def {
    ($struct:tt, $($field:ident, $docstr:literal, $ty:ty),*) => {

        $(
            _pyo3::class::PyMethodDefType::Setter({
                _pyo3::class::PySetterDef::new(
                    concat!(stringify!($field), "\0"),
                    _pyo3::impl_::pymethods::PySetter({
                        unsafe extern "C" fn trampoline(
                            slf: *mut _pyo3::ffi::PyObject,
                            value: *mut _pyo3::ffi::PyObject,
                            closure: *mut ::std::os::raw::c_void,
                        ) -> ::core::ffi::c_int
                        {
                            _pyo3::impl_::trampoline::setter(
                                slf,
                                value,
                                closure,
                                $field::set_tramp,
                            )
                        }
                        trampoline
                    }),
                    concat!($docstr, "\0"),
                )
            })
        )*

    }
 }

/// Generate method trampolines. The alternative cases are:
///
/// - `export` returns a read-only value to python.
/// - `wrap` returns a python class object that contains a unique Uuid corresponding
///   to a GObject
#[macro_export]
macro_rules! generate_field_tramp {
    ($struct:tt, export $attr_name:ident, $parent_type:ty, { $($wrapper_ty:tt)+ }) => {
        #[doc(hidden)]
        mod $attr_name {
            use super::*;
            use ::pyo3 as _pyo3;
            use ::glib::StaticType;

            pub(super) unsafe fn get_tramp(
                _py: _pyo3::Python<'_>,
                _slf: *mut _pyo3::ffi::PyObject,
            ) -> _pyo3::PyResult<*mut _pyo3::ffi::PyObject> {
                let _cell = _py
                    .from_borrowed_ptr::<_pyo3::PyAny>(_slf)
                    .downcast::<_pyo3::PyCell<$struct>>()?;
                let _ref = _cell.try_borrow()?;
                let _slf: &$struct = &*_ref;
                let item = self::getter(_slf, _py);
                _pyo3::callback::convert(_py, item)
            }

            fn getter(self_: &$struct, py: _pyo3::Python<'_>) -> _pyo3::PyResult<$($wrapper_ty)*>
            {
                let val: _pyo3::Py<_pyo3::PyAny> = $crate::api::Gerb::get_field_value(
                    &self_.__gerb.as_ref(py).borrow(),
                    self_.__id,
                    <$parent_type>::static_type().name(),
                    stringify!($attr_name),
                    py,
                )?;
                val.extract(py)
            }
        }
    };
    ($struct:tt, wrap_dict $attr_name:ident, $parent_type:ty, { $($wrapper_ty:tt)+ }) => {
        #[doc(hidden)]
        mod $attr_name {
            use super::*;
            use ::pyo3 as _pyo3;
            use ::glib::StaticType;

            pub(super) unsafe fn get_tramp(
                _py: _pyo3::Python<'_>,
                _slf: *mut _pyo3::ffi::PyObject,
            ) -> _pyo3::PyResult<*mut _pyo3::ffi::PyObject> {
                let _cell = _py
                    .from_borrowed_ptr::<_pyo3::PyAny>(_slf)
                    .downcast::<_pyo3::PyCell<$struct>>()?;
                let _ref = _cell.try_borrow()?;
                let _slf: &$struct = &*_ref;
                let item = self::getter(_slf, _py)?;
                _pyo3::callback::convert(_py, item)
            }

            fn getter(self_: &$struct, py: _pyo3::Python<'_>) -> _pyo3::PyResult<::indexmap::IndexMap<::std::string::String, $($wrapper_ty)*>> {
                let val: _pyo3::Py<_pyo3::PyAny> = $crate::api::Gerb::get_field_value(
                    &self_.__gerb.as_ref(py).borrow(),
                    self_.__id,
                    <$parent_type>::static_type().name(),
                    stringify!($attr_name),
                    py,
                )?;
                let extracted: ::indexmap::IndexMap<::std::string::String, $crate::api::PyUuid> =  val.extract(py)?;
                Ok(extracted.into_iter().map(
                    |(k, v)| (k, $($wrapper_ty)* {
                        __id: v.0,
                        __gerb: self_.__gerb.clone(),
                    })).collect())
            }
        }
    };
    ($struct:tt, wrap $attr_name:ident, $parent_type:ty, { $($wrapper_ty:tt)+ }) => {
        #[doc(hidden)]
        mod $attr_name {
            use super::*;
            use ::pyo3 as _pyo3;
            use ::glib::StaticType;

            pub(super) unsafe fn get_tramp(
                _py: _pyo3::Python<'_>,
                _slf: *mut _pyo3::ffi::PyObject,
            ) -> _pyo3::PyResult<*mut _pyo3::ffi::PyObject> {
                let _cell = _py
                    .from_borrowed_ptr::<_pyo3::PyAny>(_slf)
                    .downcast::<_pyo3::PyCell<$struct>>()?;
                let _ref = _cell.try_borrow()?;
                let _slf: &$struct = &*_ref;
                let item = self::getter(_slf, _py);
                _pyo3::callback::convert(_py, item)
            }

            fn getter(self_: &$struct, py: _pyo3::Python<'_>) -> _pyo3::PyResult<$($wrapper_ty)*> {
                let __id: $crate::prelude::Uuid = $crate::api::Gerb::get_field_id(
                    &self_.__gerb.as_ref(py).borrow(),
                    self_.__id,
                    <$parent_type>::static_type().name(),
                    stringify!($attr_name),
                    py,
                )?;
                Ok($($wrapper_ty)* {
                    __id,
                    __gerb: self_.__gerb.clone(),
                })
            }
        }
    };
}

/// Expand a wrapper type definition to its full implementation.
///
/// # Examples
///
/// ```rust
/// # #[macro_use] extern crate gerb;
/// mod example {
/// # use gerb::generate_py_class;
/// # use gerb::prelude::*;
/// generate_py_class!(
///     #[docstring = "String returned by help()"]
///     struct PythonTypeName {
///         type PARENT_TYPE = gerb::prelude::Project;
///
///         #[property_name=NAME] // Gobject's property name
///         #[docstring = ""]
///         title: String, // field name exposed to python and the type
///                        // returned by the glib property
///         #[property_name=MODIFIED]
///         #[docstring = ""]
///         flag: bool,
///     },
/// );
/// }
/// ```
///
/// ## Exposing objects wrapped in a python class:
///
/// ```rust
/// # #[macro_use] extern crate gerb;
/// mod example {
/// # use gerb::prelude::*;
/// # generate_py_class!(
/// #     #[docstring = " "]
/// #     struct FieldPythonTypeName {
/// #         type PARENT_TYPE = gerb::prelude::Project;
/// #     },
/// # );
/// generate_py_class!(
///     #[docstring = "String returned by help()"]
///     struct PythonTypeName {
///         type PARENT_TYPE = gerb::prelude::Project;
///
///         #[property_name=NAME] // Gobject's property name
///         #[docstring = " "]
///         title: String,
///     },
///     // FieldPythonTypeName must be a struct that is generated by
///     // generate_py_class!
///     wrap { field_name_python_sees: FieldPythonTypeName },
/// );
/// }
/// ```
///
/// ## Exporting read-only data as native python types
///
/// ```rust
/// #[macro_use] extern crate gerb;
/// mod example {
/// use gerb::prelude::*;
/// use std::path::PathBuf;
/// generate_py_class!(
///     #[docstring = "String returned by help()"]
///     struct PythonTypeName {
///         type PARENT_TYPE = gerb::prelude::Project;
///
///         #[property_name=NAME] // Gobject's property name
///         #[docstring = " "]
///         title: String,
///     },
///     // Option<_> means that it can be None in Python
///      export { path: Option<PathBuf> },
/// );
/// }
/// ```
#[macro_export]
macro_rules! generate_py_class {
    (
        #[docstring=$classdocstr:literal]
        struct $struct:tt {
            type PARENT_TYPE = $parent_type:ty;

            $(
                #[property_name=$property:ident]
                #[docstring=$docstr:literal]
                $field:ident: $field_ty:ty,
            )*
        },
        // wrapper_ty must be a rust type, but the :ty fragment specifier cannot be used as a
        // struct constructor. For example, if the type name is Example, this won't work:
        // ```
        // macro_rules! ___ {
        // ($type_name:ty) => {
        //  struct $type_name {
        //    ...
        //  }
        //
        //  const _ = $type_name {
        //
        //  };
        // ```
        //
        // So instead we match token trees (:tt) and allow more than one because generics
        // like `Option<T>` are multiple tokens: [Option, <, T, >] and matching them with a
        // single :tt will fail.
        $(
            $verb:tt { $attr_name:ident: $($wrapper_ty:tt)+ },
        )*
    ) => {
        pub struct $struct {
            pub __id: $crate::prelude::Uuid,
            pub __gerb: ::pyo3::Py<$crate::api::Gerb>,
        }

        // pyo3 boilerplate taken from pyo3's own pyclass macro.
        //
        // You can ignore this if you are not interested in the implementation details.
        const _: () = {
            use ::pyo3 as _pyo3;

            unsafe impl _pyo3::type_object::PyTypeInfo for $struct {
                type AsRefTarget = _pyo3::PyCell<Self>;
                const NAME: &'static str = stringify!($struct);
                const MODULE: ::std::option::Option<&'static str> = ::core::option::Option::None;

                #[inline]
                fn type_object_raw(py: _pyo3::Python<'_>) -> *mut _pyo3::ffi::PyTypeObject {
                    use _pyo3::type_object::LazyStaticType;
                    static TYPE_OBJECT: LazyStaticType = LazyStaticType::new();
                    TYPE_OBJECT.get_or_init::<Self>(py)
                }
            }

            impl _pyo3::PyClass for $struct {
                type Frozen = _pyo3::pyclass::boolean_struct::False;
            }

            impl<'a, 'py> _pyo3::impl_::extract_argument::PyFunctionArgument<'a, 'py> for &'a $struct {
                type Holder = ::std::option::Option<_pyo3::PyRef<'py, $struct>>;

                #[inline]
                fn extract(
                    obj: &'py _pyo3::PyAny,
                    holder: &'a mut Self::Holder,
                ) -> _pyo3::PyResult<Self> {
                    _pyo3::impl_::extract_argument::extract_pyclass_ref(obj, holder)
                }
            }

            impl<'a, 'py> _pyo3::impl_::extract_argument::PyFunctionArgument<'a, 'py> for &'a mut $struct {
                type Holder = ::std::option::Option<_pyo3::PyRefMut<'py, $struct>>;

                #[inline]
                fn extract(
                    obj: &'py _pyo3::PyAny,
                    holder: &'a mut Self::Holder,
                ) -> _pyo3::PyResult<Self> {
                    _pyo3::impl_::extract_argument::extract_pyclass_ref_mut(obj, holder)
                }
            }

            impl _pyo3::IntoPy<_pyo3::PyObject> for $struct {
                fn into_py(self, py: _pyo3::Python) -> _pyo3::PyObject {
                    _pyo3::IntoPy::into_py(_pyo3::Py::new(py, self).unwrap(), py)
                }
            }

            impl _pyo3::impl_::pyclass::PyClassImpl for $struct {
                const DOC: &'static str = concat!($classdocstr,"\0");
                const IS_BASETYPE: bool = false;
                const IS_SUBCLASS: bool = false;
                const IS_MAPPING: bool = false;
                const IS_SEQUENCE: bool = false;
                type Layout = _pyo3::PyCell<Self>;
                type BaseType = _pyo3::PyAny;
                type ThreadChecker = _pyo3::impl_::pyclass::ThreadCheckerStub<$struct>;
                type PyClassMutability =
                    <<_pyo3::PyAny as
                    _pyo3::impl_::pyclass::PyClassBaseType>::PyClassMutability
                    as _pyo3::impl_::pycell::PyClassMutability>::MutableChild;
                type Dict = _pyo3::impl_::pyclass::PyClassDummySlot;
                type WeakRef = _pyo3::impl_::pyclass::PyClassDummySlot;
                type BaseNativeType = _pyo3::PyAny;

                fn items_iter() -> _pyo3::impl_::pyclass::PyClassItemsIter {
                    use _pyo3::impl_::pyclass::*;
                    let collector = PyClassImplCollector::<Self>::new();
                    static INTRINSIC_ITEMS: PyClassItems = PyClassItems {
                        methods: &[_pyo3::class::PyMethodDefType::Getter({
                            _pyo3::class::PyGetterDef::new(
                                "__gerb\0",
                                _pyo3::impl_::pymethods::PyGetter({
                                    unsafe extern "C" fn trampoline(
                                        slf: *mut _pyo3::ffi::PyObject,
                                        closure: *mut ::std::os::raw::c_void,
                                    ) -> *mut _pyo3::ffi::PyObject {
                                        _pyo3::impl_::trampoline::getter(
                                            slf,
                                            closure,
                                            $struct::__pymethod_get___gerb__,
                                        )
                                    }
                                    trampoline
                                }),
                                "\0",
                            )
                        })],
                        slots: &[],
                    };
                    PyClassItemsIter::new(&INTRINSIC_ITEMS, collector.py_methods())
                }
            }

            #[doc(hidden)]
            #[allow(non_snake_case)]
            impl $struct {
                unsafe fn __pymethod_get___gerb__(
                    _py: _pyo3::Python<'_>,
                    _slf: *mut _pyo3::ffi::PyObject,
                ) -> _pyo3::PyResult<*mut _pyo3::ffi::PyObject> {
                    let _cell = _py
                        .from_borrowed_ptr::<_pyo3::PyAny>(_slf)
                        .downcast::<_pyo3::PyCell<$struct>>()?;
                    let _ref = _cell.try_borrow()?;
                    let _slf: &$struct = &*_ref;
                    let item = ::std::clone::Clone::clone(&(_slf.__gerb));
                    let item: _pyo3::Py<_pyo3::PyAny> = _pyo3::IntoPy::into_py(item, _py);
                    ::std::result::Result::Ok(_pyo3::conversion::IntoPyPointer::into_ptr(item))
                }
            }
        };

        // Here we define the methods, the getter/setters and the corresponding trampolines
        const _: () = {
            use ::pyo3 as _pyo3;

            impl _pyo3::impl_::pyclass::PyMethods<$struct> for _pyo3::impl_::pyclass::PyClassImplCollector<$struct> {
                fn py_methods(self) -> &'static _pyo3::impl_::pyclass::PyClassItems {
                    static ITEMS: _pyo3::impl_::pyclass::PyClassItems =
                        _pyo3::impl_::pyclass::PyClassItems {
                            methods: &[
                                $(generate_getter_method_def!($struct, $field, $docstr, $field_ty),)*
                                $(generate_setter_method_def!($struct, $field, $docstr, $field_ty),)*
                                $(generate_getter_method_def!($struct, $attr_name, " ", $($wrapper_ty)*),)*
                            ],
                            slots: &[{
                                unsafe extern "C" fn trampoline(
                                    _slf: *mut _pyo3::ffi::PyObject,
                                ) -> *mut _pyo3::ffi::PyObject {
                                    _pyo3::impl_::trampoline::reprfunc(
                                        _slf,
                                        $struct::__pymethod___repr____,
                                    )
                                }
                                _pyo3::ffi::PyType_Slot {
                                    slot: _pyo3::ffi::Py_tp_repr,
                                    pfunc: trampoline as _pyo3::ffi::reprfunc as _,
                                }
                            }],
                        };
                    &ITEMS
                }
            }

            $(
                /* Hey what's this module doing here??
                 *
                 * Rust can't create arbitrary identifiers in macros.
                 * Which sucks, because we can't generate functions like this:
                 *
                 * ```
                 * fn get_$field(_) -> _ {
                 *  ...
                 * }
                 * ```
                 *
                 * But we need a trampoline function *and* a {get,set} function for every field,
                 * so their names must be unique.
                 *
                 * There are two options:
                 *
                 * - pass those unique names in the macro invocation, e.g.
                 *   generate_those_funcs!(my_name, get_my_name, get_my_name_trampoline)
                 * - or just use an identifier you already know is unique for each field: its
                 *   own identity.
                 *
                 * Since $field is an :ident fragment specifier it can be used to identify
                 * things, obviously. So the generated functions are namespaced in a module
                 * called $field and problem solved.
                 */
                #[doc(hidden)]
                mod $field {
                    use super::*;
                    use ::pyo3 as _pyo3;
                    use ::glib::StaticType;

                    pub(super) unsafe fn get_tramp (
                        _py: _pyo3::Python<'_>,
                        _slf: *mut _pyo3::ffi::PyObject,
                    ) -> _pyo3::PyResult<*mut _pyo3::ffi::PyObject> {
                        let _cell = _py
                            .from_borrowed_ptr::<_pyo3::PyAny>(_slf)
                            .downcast::<_pyo3::PyCell<$struct>>()?;
                        let _ref = _cell.try_borrow()?;
                        let _slf: &$struct = &*_ref;
                        let item = self::getter(_slf, _py);
                        _pyo3::callback::convert(_py, item)
                    }

                    pub(super) unsafe fn set_tramp (
                        _py: _pyo3::Python<'_>,
                        _slf: *mut _pyo3::ffi::PyObject,
                        _value: *mut _pyo3::ffi::PyObject,
                    ) -> _pyo3::PyResult<::core::ffi::c_int> {
                        let _cell = _py
                            .from_borrowed_ptr::<_pyo3::PyAny>(_slf)
                            .downcast::<_pyo3::PyCell<$struct>>()?;
                        let mut _ref = _cell.try_borrow_mut()?;
                        let _slf: &mut $struct = &mut *_ref;
                        let _value = _py
                            .from_borrowed_ptr_or_opt(_value)
                            .ok_or_else(|| {
                                _pyo3::exceptions::PyAttributeError::new_err("can't delete attribute")
                            })?;
                        let _val = _pyo3::FromPyObject::extract(_value)?;

                        let item = self::setter(_slf, _val, _py);
                        _pyo3::callback::convert(_py, item)
                    }

                    fn getter(self_: &$struct, py: _pyo3::Python<'_>) -> _pyo3::PyResult<$field_ty> {
                        self_.__gerb
                            .as_ref(py)
                            .borrow()
                            .__send_rcv(
                                serde_json::to_string(&{
                                    $crate::api::Request::ObjectProperty {
                                        type_name: <$parent_type>::static_type().name().to_string(),
                                        id: self_.__id,
                                        property: <$parent_type>::$property.to_string(),
                                        action: $crate::api::Action::Get,
                                    }
                                })
                                .unwrap(),
                                py,
                            )?
                            .extract(py)
                    }

                    fn setter(self_: &mut $struct, value: $field_ty, py: _pyo3::Python<'_>) -> _pyo3::PyResult<()> {
                        self_.__gerb
                            .as_ref(py)
                            .borrow()
                            .__send_rcv(
                                serde_json::json!{
                                    $crate::api::Request::ObjectProperty {
                                        type_name: <$parent_type>::static_type().name().to_string(),
                                        id: self_.__id,
                                        property: <$parent_type>::$property.to_string(),
                                        action: $crate::api::Action::Set {
                                            value: $crate::serde_json::to_string(&$crate::serde_json::json! { value }).unwrap(),
                                        },
                                    }
                                }.to_string()
                                ,
                                py,
                            )?;
                        Ok(())
                    }
                }
            )*

            $(
                generate_field_tramp!($struct, $verb $attr_name, $parent_type, { $($wrapper_ty)* });
            )*

            #[doc(hidden)]
            #[allow(non_snake_case)]
            impl $struct {
                unsafe fn __pymethod___repr____(
                    _py: _pyo3::Python<'_>,
                    _raw_slf: *mut _pyo3::ffi::PyObject,
                ) -> _pyo3::PyResult<*mut _pyo3::ffi::PyObject> {
                    let _slf = _raw_slf;
                    let _cell = _py
                        .from_borrowed_ptr::<_pyo3::PyAny>(_slf)
                        .downcast::<_pyo3::PyCell<$struct>>()?;
                    let _ref = _cell.try_borrow()?;
                    let _slf: &$struct = &*_ref;
                    _pyo3::callback::convert(_py, $struct::__repr__(_slf))
                }
            }
        };

        impl $struct {
            fn __repr__(&self) -> ::pyo3::PyResult<String> {
                Ok(format!("<{} instance, id: {}>", stringify!($struct), self.__id))
            }
        }
    };
}

generate_py_class!(
    #[docstring = "The currently loaded project."]
    struct Project {
        type PARENT_TYPE = ProjectParent;

        #[property_name=NAME]
        #[docstring = " "]
        name: String,
        #[property_name=MODIFIED]
        #[docstring = " "]
        modified: bool,
    },
    wrap { font_info:  FontInfo },
    wrap { default_layer: Layer },
    export { path: PathBuf },
);

generate_py_class!(
    #[docstring = "Global settings."]
    struct Settings {
        type PARENT_TYPE = SettingsParent;

        #[property_name=HANDLE_SIZE]
        #[docstring = " "]
        handle_size: f64,
        #[property_name=LINE_WIDTH]
        #[docstring = " "]
        line_width: f64,
        #[property_name=GUIDELINE_WIDTH]
        #[docstring = " "]
        guideline_width: f64,
        #[property_name=WARP_CURSOR]
        #[docstring = " "]
        warp_cursor: bool,
    },
    // [ref:settings_path()_sync_return_value]
    export { path: Option<PathBuf> },
);

generate_py_class!(
    #[docstring = "Font info"]
    struct FontInfo {
        type PARENT_TYPE = FontInfoParent;

        #[property_name=FAMILY_NAME]
        #[docstring = " "]
        family_name: String,
        #[property_name=STYLE_NAME]
        #[docstring = " "]
        style_name: String,
        #[property_name=STYLE_MAP_FAMILY_NAME]
        #[docstring = " "]
        style_map_family_name: String,
        #[property_name=STYLE_MAP_STYLE_NAME]
        #[docstring = " "]
        style_map_style_name: String,
        #[property_name=COPYRIGHT]
        #[docstring = " "]
        copyright: String,
        #[property_name=TRADEMARK]
        #[docstring = " "]
        trademark: String,
        #[property_name=NOTE]
        #[docstring = " "]
        note: String,
        #[property_name=UNITS_PER_EM]
        #[docstring = " "]
        units_per_em: f64,
        #[property_name=X_HEIGHT]
        #[docstring = " "]
        x_height: f64,
        #[property_name=ASCENDER]
        #[docstring = " "]
        ascender: f64,
        #[property_name=DESCENDER]
        #[docstring = " "]
        descender: f64,
        #[property_name=CAP_HEIGHT]
        #[docstring = " "]
        cap_height: f64,
        #[property_name=ITALIC_ANGLE]
        #[docstring = " "]
        italic_angle: f64,
        #[property_name=YEAR]
        #[docstring = " "]
        year: u64,
        #[property_name=VERSION_MAJOR]
        #[docstring = " "]
        version_major: i64,
        #[property_name=VERSION_MINOR]
        #[docstring = " "]
        version_minor: u64,
    },
    export { path: PathBuf },
    export { modified: bool },
);

generate_py_class!(
    #[docstring = "Layers. <https://unifiedfontobject.org/versions/ufo3/layercontents.plist/>"]
    struct Layer {
        type PARENT_TYPE = LayerParent;

        #[property_name=NAME]
        #[docstring = " "]
        name: String,
        #[property_name=DIR_NAME]
        #[docstring = " "]
        dir_name: String,
    },
    export { path: PathBuf },
    export { modified: bool },
    wrap_dict { glyphs: Glyph },
);

generate_py_class!(
    #[docstring = ""]
    struct Glyph {
        type PARENT_TYPE = crate::prelude::GlyphMetadata;

        #[property_name=WIDTH]
        #[docstring = " "]
        width: f64,
        #[property_name=NAME]
        #[docstring = " "]
        name: String,
        #[property_name=RELATIVE_PATH]
        #[docstring = " "]
        relative_path: Option<PathBuf>,
        #[property_name=FILENAME]
        #[docstring = " "]
        filename: Option<PathBuf>,
    },
    export { modified: bool },
    //export { unicode: Vec<String> },
);