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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
// Take a look at the license at the top of the repository in the LICENSE file.

mod boxed_derive;
mod clone;
mod clone_old;
mod closure;
mod closure_old;
mod derived_properties_attribute;
mod downgrade_derive;
mod enum_derive;
mod error_domain_derive;
mod flags_attribute;
mod object_interface_attribute;
mod object_subclass_attribute;
mod properties;
mod shared_boxed_derive;
mod value_delegate_derive;
mod variant_derive;

mod utils;

use flags_attribute::AttrInput;
use proc_macro::{TokenStream, TokenTree};
use proc_macro2::Span;
use syn::{parse_macro_input, DeriveInput};
use utils::{parse_nested_meta_items_from_stream, NestedMetaItem};

/// Macro for passing variables as strong or weak references into a closure.
///
/// This macro can be useful in combination with closures, e.g. signal handlers, to reduce the
/// boilerplate required for passing strong or weak references into the closure. It will
/// automatically create the new reference and pass it with the same name into the closure.
///
/// If upgrading the weak reference to a strong reference inside the closure is failing, the
/// closure is immediately returning an optional default return value. If none is provided, `()` is
/// returned.
///
/// **⚠️ IMPORTANT ⚠️**
///
/// `glib` needs to be in scope, so unless it's one of the direct crate dependencies, you need to
/// import it because `clone!` is using it. For example:
///
/// ```rust,ignore
/// use gtk::glib;
/// ```
///
/// ### Debugging
///
/// In case something goes wrong inside the `clone!` macro, we use the [`g_debug`] macro. Meaning
/// that if you want to see these debug messages, you'll have to set the `G_MESSAGES_DEBUG`
/// environment variable when running your code (either in the code directly or when running the
/// binary) to either "all" or [`CLONE_MACRO_LOG_DOMAIN`]:
///
/// [`g_debug`]: ../glib/macro.g_debug.html
/// [`CLONE_MACRO_LOG_DOMAIN`]: ../glib/constant.CLONE_MACRO_LOG_DOMAIN.html
///
/// ```rust,ignore
/// use glib::CLONE_MACRO_LOG_DOMAIN;
///
/// std::env::set_var("G_MESSAGES_DEBUG", CLONE_MACRO_LOG_DOMAIN);
/// std::env::set_var("G_MESSAGES_DEBUG", "all");
/// ```
///
/// Or:
///
/// ```bash
/// $ G_MESSAGES_DEBUG=all ./binary
/// ```
///
/// ### Passing a strong reference
///
/// ```
/// use glib;
/// use glib_macros::clone;
/// use std::rc::Rc;
///
/// let v = Rc::new(1);
/// let closure = clone!(@strong v => move |x| {
///     println!("v: {}, x: {}", v, x);
/// });
///
/// closure(2);
/// ```
///
/// ### Passing a weak reference
///
/// ```
/// use glib;
/// use glib_macros::clone;
/// use std::rc::Rc;
///
/// let u = Rc::new(2);
/// let closure = clone!(@weak u => move |x| {
///     println!("u: {}, x: {}", u, x);
/// });
///
/// closure(3);
/// ```
///
/// #### Allowing a nullable weak reference
///
/// In some cases, even if the weak references can't be retrieved, you might want to still have
/// your closure called. In this case, you need to use `@weak-allow-none`:
///
/// ```
/// use glib;
/// use glib_macros::clone;
/// use std::rc::Rc;
///
/// let closure = {
///     // This `Rc` won't be available in the closure because it's dropped at the end of the
///     // current block
///     let u = Rc::new(2);
///     clone!(@weak-allow-none u => @default-return false, move |x| {
///         // We need to use a Debug print for `u` because it'll be an `Option`.
///         println!("u: {:?}, x: {}", u, x);
///         true
///     })
/// };
///
/// assert_eq!(closure(3), true);
/// ```
///
/// ### Creating owned values from references (`ToOwned`)
///
/// ```
/// use glib;
/// use glib_macros::clone;
///
/// let v = "123";
/// let closure = clone!(@to-owned v => move |x| {
///     // v is passed as `String` here
///     println!("v: {}, x: {}", v, x);
/// });
///
/// closure(2);
/// ```
///
/// ### Renaming variables
///
/// ```
/// use glib;
/// use glib_macros::clone;
/// use std::rc::Rc;
///
/// let v = Rc::new(1);
/// let u = Rc::new(2);
/// let closure = clone!(@strong v as y, @weak u => move |x| {
///     println!("v as y: {}, u: {}, x: {}", y, u, x);
/// });
///
/// closure(3);
/// ```
///
/// ### Providing a default return value if upgrading a weak reference fails
///
/// You can do it in two different ways:
///
/// Either by providing the value yourself using `@default-return`:
///
/// ```
/// use glib;
/// use glib_macros::clone;
/// use std::rc::Rc;
///
/// let v = Rc::new(1);
/// let closure = clone!(@weak v => @default-return false, move |x| {
///     println!("v: {}, x: {}", v, x);
///     true
/// });
///
/// // Drop value so that the weak reference can't be upgraded.
/// drop(v);
///
/// assert_eq!(closure(2), false);
/// ```
///
/// Or by using `@default-panic` (if the value fails to get upgraded, it'll panic):
///
/// ```should_panic
/// # use glib;
/// # use glib_macros::clone;
/// # use std::rc::Rc;
/// # let v = Rc::new(1);
/// let closure = clone!(@weak v => @default-panic, move |x| {
///     println!("v: {}, x: {}", v, x);
///     true
/// });
/// # drop(v);
/// # assert_eq!(closure(2), false);
/// ```
///
/// ### Errors
///
/// Here is a list of errors you might encounter:
///
/// **Missing `@weak` or `@strong`**:
///
/// ```compile_fail
/// # use glib;
/// # use glib_macros::clone;
/// # use std::rc::Rc;
/// let v = Rc::new(1);
///
/// let closure = clone!(v => move |x| println!("v: {}, x: {}", v, x));
/// # drop(v);
/// # closure(2);
/// ```
///
/// **Passing `self` as an argument**:
///
/// ```compile_fail
/// # use glib;
/// # use glib_macros::clone;
/// # use std::rc::Rc;
/// #[derive(Debug)]
/// struct Foo;
///
/// impl Foo {
///     fn foo(&self) {
///         let closure = clone!(@strong self => move |x| {
///             println!("self: {:?}", self);
///         });
///         # closure(2);
///     }
/// }
/// ```
///
/// If you want to use `self` directly, you'll need to rename it:
///
/// ```
/// # use glib;
/// # use glib_macros::clone;
/// # use std::rc::Rc;
/// #[derive(Debug)]
/// struct Foo;
///
/// impl Foo {
///     fn foo(&self) {
///         let closure = clone!(@strong self as this => move |x| {
///             println!("self: {:?}", this);
///         });
///         # closure(2);
///     }
/// }
/// ```
///
/// **Passing fields directly**
///
/// ```compile_fail
/// # use glib;
/// # use glib_macros::clone;
/// # use std::rc::Rc;
/// #[derive(Debug)]
/// struct Foo {
///     v: Rc<usize>,
/// }
///
/// impl Foo {
///     fn foo(&self) {
///         let closure = clone!(@strong self.v => move |x| {
///             println!("self.v: {:?}", v);
///         });
///         # closure(2);
///     }
/// }
/// ```
///
/// You can do it by renaming it:
///
/// ```
/// # use glib;
/// # use glib_macros::clone;
/// # use std::rc::Rc;
/// # struct Foo {
/// #     v: Rc<usize>,
/// # }
/// impl Foo {
///     fn foo(&self) {
///         let closure = clone!(@strong self.v as v => move |x| {
///             println!("self.v: {}", v);
///         });
///         # closure(2);
///     }
/// }
/// ```
#[proc_macro]
pub fn clone(item: TokenStream) -> TokenStream {
    if cfg!(feature = "unstable-clone-syntax") {
        // Check if this is an old-style clone macro invocation.
        // These always start with an '@' punctuation.
        let Some(first) = item.clone().into_iter().next() else {
            return syn::Error::new(Span::call_site(), "expected a closure or async block")
                .to_compile_error()
                .into();
        };

        match first {
            TokenTree::Punct(ref p) if p.to_string() == "@" => clone_old::clone_inner(item),
            _ => clone::clone_inner(item),
        }
    } else {
        clone_old::clone_inner(item)
    }
}

/// Macro for creating a [`Closure`] object. This is a wrapper around [`Closure::new`] that
/// automatically type checks its arguments at run-time.
///
/// A `Closure` takes [`Value`] objects as inputs and output. This macro will automatically convert
/// the inputs to Rust types when invoking its callback, and then will convert the output back to a
/// `Value`. All inputs must implement the [`FromValue`] trait, and outputs must either implement
/// the [`ToValue`] trait or be the unit type `()`. Type-checking of inputs is done at run-time; if
/// incorrect types are passed via [`Closure::invoke`] then the closure will panic. Note that when
/// passing input types derived from [`Object`] or [`Interface`], you must take care to upcast to
/// the exact object or interface type that is being received.
///
/// Similarly to [`clone!`](crate::clone!), this macro can be useful in combination with signal
/// handlers to reduce boilerplate when passing references. Unique to `Closure` objects is the
/// ability to watch an object using a the `@watch` directive. Only an [`Object`] value can be
/// passed to `@watch`, and only one object can be watched per closure. When an object is watched,
/// a weak reference to the object is held in the closure. When the object is destroyed, the
/// closure will become invalidated: all signal handlers connected to the closure will become
/// disconnected, and any calls to [`Closure::invoke`] on the closure will be silently ignored.
/// Internally, this is accomplished using [`Object::watch_closure`] on the watched object.
///
/// The `@weak-allow-none` and `@strong` captures are also supported and behave the same as in
/// [`clone!`](crate::clone!), as is aliasing captures with the `as` keyword. Notably, these
/// captures are able to reference `Rc` and `Arc` values in addition to `Object` values.
///
/// [`Closure`]: ../glib/closure/struct.Closure.html
/// [`Closure::new`]: ../glib/closure/struct.Closure.html#method.new
/// [`Closure::new_local`]: ../glib/closure/struct.Closure.html#method.new_local
/// [`Closure::invoke`]: ../glib/closure/struct.Closure.html#method.invoke
/// [`Value`]: ../glib/value/struct.Value.html
/// [`FromValue`]: ../glib/value/trait.FromValue.html
/// [`ToValue`]: ../glib/value/trait.ToValue.html
/// [`Interface`]: ../glib/object/struct.Interface.html
/// [`Object`]: ../glib/object/struct.Object.html
/// [`Object::watch_closure`]: ../glib/object/trait.ObjectExt.html#tymethod.watch_closure
/// **⚠️ IMPORTANT ⚠️**
///
/// `glib` needs to be in scope, so unless it's one of the direct crate dependencies, you need to
/// import it because `closure!` is using it. For example:
///
/// ```rust,ignore
/// use gtk::glib;
/// ```
///
/// ### Using as a closure object
///
/// ```
/// use glib_macros::closure;
///
/// let concat_str = closure!(|s: &str| s.to_owned() + " World");
/// let result = concat_str.invoke::<String>(&[&"Hello"]);
/// assert_eq!(result, "Hello World");
/// ```
///
/// ### Connecting to a signal
///
/// For wrapping closures that can't be sent across threads, the
/// [`closure_local!`](crate::closure_local!) macro can be used. It has the same syntax as
/// `closure!`, but instead uses [`Closure::new_local`] internally.
///
/// ```
/// use glib;
/// use glib::prelude::*;
/// use glib_macros::closure_local;
///
/// let obj = glib::Object::new::<glib::Object>();
/// obj.connect_closure(
///     "notify", false,
///     closure_local!(|_obj: glib::Object, pspec: glib::ParamSpec| {
///         println!("property notify: {}", pspec.name());
///     }));
/// ```
///
/// ### Object Watching
///
/// ```
/// use glib;
/// use glib::prelude::*;
/// use glib_macros::closure_local;
///
/// let closure = {
///     let obj = glib::Object::new::<glib::Object>();
///     let closure = closure_local!(@watch obj => move || {
///         obj.type_().name()
///     });
///     assert_eq!(closure.invoke::<String>(&[]), "GObject");
///     closure
/// };
/// // `obj` is dropped, closure invalidated so it always does nothing and returns None
/// closure.invoke::<()>(&[]);
/// ```
///
/// `@watch` has special behavior when connected to a signal:
///
/// ```
/// use glib;
/// use glib::prelude::*;
/// use glib_macros::closure_local;
///
/// let obj = glib::Object::new::<glib::Object>();
/// {
///     let other = glib::Object::new::<glib::Object>();
///     obj.connect_closure(
///         "notify", false,
///         closure_local!(@watch other as b => move |a: glib::Object, pspec: glib::ParamSpec| {
///             let value = a.property_value(pspec.name());
///             b.set_property(pspec.name(), &value);
///         }));
///     // The signal handler will disconnect automatically at the end of this
///     // block when `other` is dropped.
/// }
/// ```
///
/// ### Weak and Strong References
///
/// ```
/// use glib;
/// use glib::prelude::*;
/// use glib_macros::closure;
/// use std::sync::Arc;
///
/// let closure = {
///     let a = Arc::new(String::from("Hello"));
///     let b = Arc::new(String::from("World"));
///     let c = "!";
///     let closure = closure!(@strong a, @weak-allow-none b, @to-owned c => move || {
///         // `a` is Arc<String>, `b` is Option<Arc<String>>, `c` is a `String`
///         format!("{} {}{}", a, b.as_ref().map(|b| b.as_str()).unwrap_or_else(|| "Moon"), c)
///     });
///     assert_eq!(closure.invoke::<String>(&[]), "Hello World!");
///     closure
/// };
/// // `a`, `c` still kept alive, `b` is dropped
/// assert_eq!(closure.invoke::<String>(&[]), "Hello Moon!");
/// ```
#[proc_macro]
pub fn closure(item: TokenStream) -> TokenStream {
    if cfg!(feature = "unstable-clone-syntax") {
        // Check if this is an old-style closure macro invocation.
        // These always start with an '@' punctuation.
        let Some(first) = item.clone().into_iter().next() else {
            return syn::Error::new(Span::call_site(), "expected a closure")
                .to_compile_error()
                .into();
        };

        match first {
            TokenTree::Punct(ref p) if p.to_string() == "@" => {
                closure_old::closure_inner(item, "new")
            }
            _ => closure::closure_inner(item, "new"),
        }
    } else {
        closure_old::closure_inner(item, "new")
    }
}

/// The same as [`closure!`](crate::closure!) but uses [`Closure::new_local`] as a constructor.
/// This is useful for closures which can't be sent across threads. See the documentation of
/// [`closure!`](crate::closure!) for details.
///
/// [`Closure::new_local`]: ../glib/closure/struct.Closure.html#method.new_local
#[proc_macro]
pub fn closure_local(item: TokenStream) -> TokenStream {
    if cfg!(feature = "unstable-clone-syntax") {
        // Check if this is an old-style closure macro invocation.
        // These always start with an '@' punctuation.
        let Some(first) = item.clone().into_iter().next() else {
            return syn::Error::new(Span::call_site(), "expected a closure")
                .to_compile_error()
                .into();
        };

        match first {
            TokenTree::Punct(ref p) if p.to_string() == "@" => {
                closure_old::closure_inner(item, "new_local")
            }
            _ => closure::closure_inner(item, "new_local"),
        }
    } else {
        closure_old::closure_inner(item, "new_local")
    }
}

/// Derive macro for register a Rust enum in the GLib type system and derive the
/// the [`glib::Value`] traits.
///
/// # Example
///
/// ```
/// use glib::prelude::*;
/// use glib::subclass::prelude::*;
///
/// #[derive(Debug, Copy, Clone, PartialEq, Eq, glib::Enum)]
/// #[enum_type(name = "MyEnum")]
/// enum MyEnum {
///     Val,
///     #[enum_value(name = "My Val")]
///     ValWithCustomName,
///     #[enum_value(name = "My Other Val", nick = "other")]
///     ValWithCustomNameAndNick,
/// }
/// ```
///
/// An enum can be registered as a dynamic type by setting the derive macro
/// helper attribute `enum_dynamic`:
/// ```ignore
/// use glib::prelude::*;
/// use glib::subclass::prelude::*;
///
/// #[derive(Debug, Copy, Clone, PartialEq, Eq, glib::Enum)]
/// #[enum_type(name = "MyEnum")]
/// #[enum_dynamic]
/// enum MyEnum {
///     ...
/// }
/// ```
///
/// As a dynamic type, an enum must be explicitly registered when the system
/// loads the implementation (see [`TypePlugin`] and [`TypeModule`].
/// Therefore, whereas an enum can be registered only once as a static type,
/// it can be registered several times as a dynamic type.
///
/// An enum registered as a dynamic type is never unregistered. The system
/// calls [`TypePluginExt::unuse`] to unload the implementation. If the
/// [`TypePlugin`] subclass is a [`TypeModule`], the enum registered as a
/// dynamic type is marked as unloaded and must be registered again when the
/// module is reloaded.
///
/// The derive macro helper attribute `enum_dynamic` provides two behaviors
/// when registering an enum as a dynamic type:
///
/// - lazy registration: by default an enum is registered as a dynamic type
/// when the system loads the implementation (e.g. when the module is loaded).
/// Optionally setting `lazy_registration` to `true` postpones registration on
/// the first use (when `static_type()` is called for the first time):
/// ```ignore
/// #[derive(Debug, Copy, Clone, PartialEq, Eq, glib::Enum)]
/// #[enum_type(name = "MyEnum")]
/// #[enum_dynamic(lazy_registration = true)]
/// enum MyEnum {
///     ...
/// }
/// ```
///
/// - registration within [`TypeModule`] subclass or within [`TypePlugin`]
/// subclass: an enum is usually registered as a dynamic type within a
/// [`TypeModule`] subclass:
/// ```ignore
/// #[derive(Debug, Copy, Clone, PartialEq, Eq, glib::Enum)]
/// #[enum_type(name = "MyModuleEnum")]
/// #[enum_dynamic]
/// enum MyModuleEnum {
///     ...
/// }
/// ...
/// #[derive(Default)]
/// pub struct MyModule;
/// ...
/// impl TypeModuleImpl for MyModule {
///     fn load(&self) -> bool {
///         // registers enums as dynamic types.
///         let my_module = self.obj();
///         let type_module: &glib::TypeModule = my_module.upcast_ref();
///         MyModuleEnum::on_implementation_load(type_module)
///     }
///     ...
/// }
/// ```
///
/// Optionally setting `plugin_type` allows to register an enum as a dynamic
/// type within a [`TypePlugin`] subclass that is not a [`TypeModule`]:
/// ```ignore
/// #[derive(Debug, Copy, Clone, PartialEq, Eq, glib::Enum)]
/// #[enum_type(name = "MyPluginEnum")]
/// #[enum_dynamic(plugin_type = MyPlugin)]
/// enum MyPluginEnum {
///     ...
/// }
/// ...
/// #[derive(Default)]
/// pub struct MyPlugin;
/// ...
/// impl TypePluginImpl for MyPlugin {
///     fn use_plugin(&self) {
///         // register enums as dynamic types.
///         let my_plugin = self.obj();
///         MyPluginEnum::on_implementation_load(my_plugin.as_ref());
///     }
///     ...
/// }
/// ```
///
/// [`glib::Value`]: ../glib/value/struct.Value.html
/// [`TypePlugin`]: ../glib/gobject/type_plugin/struct.TypePlugin.html
/// [`TypeModule`]: ../glib/gobject/type_module/struct.TypeModule.html
/// [`TypePluginExt::unuse`]: ../glib/gobject/type_plugin/trait.TypePluginExt.
#[proc_macro_derive(Enum, attributes(enum_type, enum_dynamic, enum_value))]
pub fn enum_derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    enum_derive::impl_enum(&input)
        .unwrap_or_else(syn::Error::into_compile_error)
        .into()
}

/// Attribute macro for defining flags using the `bitflags` crate.
/// This macro will also define a `GFlags::type_` function and
/// the [`glib::Value`] traits.
///
/// The expected `GType` name has to be passed as macro attribute.
/// The name and nick of each flag can also be optionally defined.
/// Default name is the flag identifier in CamelCase and default nick
/// is the identifier in kebab-case.
/// Combined flags should not be registered with the `GType` system
/// and so needs to be tagged with the `#[flags_value(skip)]` attribute.
///
/// # Example
///
/// ```
/// use glib::prelude::*;
/// use glib::subclass::prelude::*;
///
/// #[glib::flags(name = "MyFlags")]
/// enum MyFlags {
///     #[flags_value(name = "Flag A", nick = "nick-a")]
///     A = 0b00000001,
///     #[flags_value(name = "Flag B")]
///     B = 0b00000010,
///     #[flags_value(skip)]
///     AB = Self::A.bits() | Self::B.bits(),
///     C = 0b00000100,
/// }
/// ```
///
/// The flags can be registered as a dynamic type by setting the macro helper
/// attribute `flags_dynamic`:
/// ```ignore
/// use glib::prelude::*;
/// use glib::subclass::prelude::*;
///
/// #[glib::flags(name = "MyFlags")]
/// #[flags_dynamic]
/// enum MyFlags {
///     ...
/// }
/// ```
///
/// As a dynamic type, the flags must be explicitly registered when the system
/// loads the implementation (see [`TypePlugin`] and [`TypeModule`].
/// Therefore, whereas the flags can be registered only once as a static type,
/// they can be registered several times as a dynamic type.
///
/// The flags registered as a dynamic type are never unregistered. The system
/// calls [`TypePluginExt::unuse`] to unload the implementation. If the
/// [`TypePlugin`] subclass is a [`TypeModule`], the flags registered as a
/// dynamic type are marked as unloaded and must be registered again when the
/// module is reloaded.
///
/// The macro helper attribute `flags_dynamic` provides two behaviors when
/// registering the flags as a dynamic type:
///
/// - lazy registration: by default the flags are registered as a dynamic type
/// when the system loads the implementation (e.g. when the module is loaded).
/// Optionally setting `lazy_registration` to `true` postpones registration on
/// the first use (when `static_type()` is called for the first time):
/// ```ignore
/// #[glib::flags(name = "MyFlags")]
/// #[flags_dynamic(lazy_registration = true)]
/// enum MyFlags {
///     ...
/// }
/// ```
///
/// - registration within [`TypeModule`] subclass or within [`TypePlugin`]
/// subclass: the flags are usually registered as a dynamic type within a
/// [`TypeModule`] subclass:
/// ```ignore
/// #[glib::flags(name = "MyModuleFlags")]
/// #[flags_dynamic]
/// enum MyModuleFlags {
///     ...
/// }
/// ...
/// #[derive(Default)]
/// pub struct MyModule;
/// ...
/// impl TypeModuleImpl for MyModule {
///     fn load(&self) -> bool {
///         // registers flags as dynamic types.
///         let my_module = self.obj();
///         let type_module: &glib::TypeModule = my_module.upcast_ref();
///         MyModuleFlags::on_implementation_load(type_module)
///     }
///     ...
/// }
/// ```
///
/// Optionally setting `plugin_type` allows to register the flags as a dynamic
/// type within a [`TypePlugin`] subclass that is not a [`TypeModule`]:
/// ```ignore
/// #[glib::flags(name = "MyModuleFlags")]
/// #[flags_dynamic(plugin_type = MyPlugin)]
/// enum MyModuleFlags {
///     ...
/// }
/// ...
/// #[derive(Default)]
/// pub struct MyPlugin;
/// ...
/// impl TypePluginImpl for MyPlugin {
///     fn use_plugin(&self) {
///         // register flags as dynamic types.
///         let my_plugin = self.obj();
///         MyPluginFlags::on_implementation_load(my_plugin.as_ref());
///     }
///     ...
/// }
/// ```
///
/// [`glib::Value`]: ../glib/value/struct.Value.html
/// [`TypePlugin`]: ../glib/gobject/type_plugin/struct.TypePlugin.html
/// [`TypeModule`]: ../glib/gobject/type_module/struct.TypeModule.html
/// [`TypePluginExt::unuse`]: ../glib/gobject/type_plugin/trait.TypePluginExt.
#[proc_macro_attribute]
pub fn flags(attr: TokenStream, item: TokenStream) -> TokenStream {
    let mut name = NestedMetaItem::<syn::LitStr>::new("name")
        .required()
        .value_required();

    if let Err(e) = parse_nested_meta_items_from_stream(attr.into(), &mut [&mut name]) {
        return e.to_compile_error().into();
    }

    let attr_meta = AttrInput {
        enum_name: name.value.unwrap(),
    };

    syn::parse::<syn::ItemEnum>(item)
        .map_err(|_| syn::Error::new(Span::call_site(), flags_attribute::WRONG_PLACE_MSG))
        .map(|mut input| flags_attribute::impl_flags(attr_meta, &mut input))
        .unwrap_or_else(syn::Error::into_compile_error)
        .into()
}

/// Derive macro for defining a GLib error domain and its associated
/// [`ErrorDomain`] trait.
///
/// # Example
///
/// ```
/// use glib::prelude::*;
/// use glib::subclass::prelude::*;
///
/// #[derive(Debug, Copy, Clone, glib::ErrorDomain)]
/// #[error_domain(name = "ex-foo")]
/// enum Foo {
///     Blah,
///     Baaz,
/// }
/// ```
///
/// [`ErrorDomain`]: ../glib/error/trait.ErrorDomain.html
#[proc_macro_derive(ErrorDomain, attributes(error_domain))]
pub fn error_domain_derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    error_domain_derive::impl_error_domain(&input)
        .unwrap_or_else(syn::Error::into_compile_error)
        .into()
}

/// Derive macro for defining a [`BoxedType`]`::type_` function and
/// the [`glib::Value`] traits. Optionally, the type can be marked as
/// `nullable` to get an implementation of `glib::value::ToValueOptional`.
///
/// # Example
///
/// ```
/// use glib::prelude::*;
/// use glib::subclass::prelude::*;
///
/// #[derive(Clone, Debug, PartialEq, Eq, glib::Boxed)]
/// #[boxed_type(name = "MyBoxed")]
/// struct MyBoxed(String);
///
/// #[derive(Clone, Debug, PartialEq, Eq, glib::Boxed)]
/// #[boxed_type(name = "MyNullableBoxed", nullable)]
/// struct MyNullableBoxed(String);
/// ```
///
/// [`BoxedType`]: ../glib/subclass/boxed/trait.BoxedType.html
/// [`glib::Value`]: ../glib/value/struct.Value.html
#[proc_macro_derive(Boxed, attributes(boxed_type))]
pub fn boxed_derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    boxed_derive::impl_boxed(&input)
        .unwrap_or_else(syn::Error::into_compile_error)
        .into()
}

/// Derive macro for defining a [`SharedType`]`::get_type` function and
/// the [`glib::Value`] traits. Optionally, the type can be marked as
/// `nullable` to get an implementation of `glib::value::ToValueOptional`.
///
/// # Example
///
/// ```
/// use glib::prelude::*;
/// use glib::subclass::prelude::*;
///
/// #[derive(Clone, Debug, PartialEq, Eq)]
/// struct MySharedInner {
///   foo: String,
/// }
///
/// #[derive(Clone, Debug, PartialEq, Eq, glib::SharedBoxed)]
/// #[shared_boxed_type(name = "MySharedBoxed")]
/// struct MySharedBoxed(std::sync::Arc<MySharedInner>);
///
/// #[derive(Clone, Debug, PartialEq, Eq, glib::SharedBoxed)]
/// #[shared_boxed_type(name = "MyNullableSharedBoxed", nullable)]
/// struct MyNullableSharedBoxed(std::sync::Arc<MySharedInner>);
/// ```
///
/// [`SharedType`]: ../glib/subclass/shared/trait.SharedType.html
/// [`glib::Value`]: ../glib/value/struct.Value.html
#[proc_macro_derive(SharedBoxed, attributes(shared_boxed_type))]
pub fn shared_boxed_derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    shared_boxed_derive::impl_shared_boxed(&input)
        .unwrap_or_else(syn::Error::into_compile_error)
        .into()
}

/// Macro for boilerplate of [`ObjectSubclass`] implementations.
///
/// This adds implementations for the `type_data()` and `type_()` methods,
/// which should probably never be defined differently.
///
/// It provides default values for the `Instance`, `Class`, and `Interfaces`
/// type parameters. If these are present, the macro will use the provided value
/// instead of the default.
///
/// Usually the defaults for `Instance` and `Class` will work. `Interfaces` is
/// necessary for types that implement interfaces.
///
/// ```ignore
/// type Instance = glib::subclass::basic::InstanceStruct<Self>;
/// type Class = glib::subclass::basic::ClassStruct<Self>;
/// type Interfaces = ();
/// ```
///
/// If no `new()` or `with_class()` method is provide, the macro adds a `new()`
/// implementation calling `Default::default()`. So the type needs to implement
/// `Default`, or this should be overridden.
///
/// ```ignore
/// fn new() -> Self {
///     Default::default()
/// }
/// ```
///
/// An object subclass can be registered as a dynamic type by setting the macro
/// helper attribute `object_class_dynamic`:
/// ```ignore
/// #[derive(Default)]
/// pub struct MyType;
///
/// #[glib::object_subclass]
/// #[object_subclass_dynamic]
/// impl ObjectSubclass for MyType { ... }
/// ```
///
/// As a dynamic type, an object subclass must be explicitly registered when
/// the system loads the implementation (see [`TypePlugin`] and [`TypeModule`].
/// Therefore, whereas an object subclass can be registered only once as a
/// static type, it can be registered several times as a dynamic type.
///
/// An object subclass registered as a dynamic type is never unregistered. The
/// system calls [`TypePluginExt::unuse`] to unload the implementation. If the
/// [`TypePlugin`] subclass is a [`TypeModule`], the object subclass registered
/// as a dynamic type is marked as unloaded and must be registered again when
/// the module is reloaded.
///
/// The macro helper attribute `object_class_dynamic` provides two behaviors
/// when registering an object subclass as a dynamic type:
///
/// - lazy registration: by default an object subclass is registered as a
/// dynamic type when the system loads the implementation (e.g. when the module
/// is loaded). Optionally setting `lazy_registration` to `true` postpones
/// registration on the first use (when `static_type()` is called for the first
/// time):
/// ```ignore
/// #[derive(Default)]
/// pub struct MyType;
///
/// #[glib::object_subclass]
/// #[object_subclass_dynamic(lazy_registration = true)]
/// impl ObjectSubclass for MyType { ... }
/// ```
///
/// - registration within [`TypeModule`] subclass or within [`TypePlugin`]
/// subclass: an object subclass is usually registered as a dynamic type within
/// a [`TypeModule`] subclass:
/// ```ignore
/// #[derive(Default)]
/// pub struct MyModuleType;
///
/// #[glib::object_subclass]
/// #[object_subclass_dynamic]
/// impl ObjectSubclass for MyModuleType { ... }
/// ...
/// #[derive(Default)]
/// pub struct MyModule;
/// ...
/// impl TypeModuleImpl for MyModule {
///     fn load(&self) -> bool {
///         // registers object subclasses as dynamic types.
///         let my_module = self.obj();
///         let type_module: &glib::TypeModule = my_module.upcast_ref();
///         MyModuleType::on_implementation_load(type_module)
///     }
///     ...
/// }
/// ```
///
/// Optionally setting `plugin_type` allows to register an object subclass as a
/// dynamic type within a [`TypePlugin`] subclass that is not a [`TypeModule`]:
/// ```ignore
/// #[derive(Default)]
/// pub struct MyPluginType;
///
/// #[glib::object_subclass]
/// #[object_subclass_dynamic(plugin_type = MyPlugin)]
/// impl ObjectSubclass for MyPluginType { ... }
/// ...
/// #[derive(Default)]
/// pub struct MyPlugin;
/// ...
/// impl TypePluginImpl for MyPlugin {
///     fn use_plugin(&self) {
///         // register object subclasses as dynamic types.
///         let my_plugin = self.obj();
///         MyPluginType::on_implementation_load(my_plugin.as_ref());
///     }
///     ...
/// }
/// ```
///
/// [`ObjectSubclass`]: ../glib/subclass/types/trait.ObjectSubclass.html
/// [`TypePlugin`]: ../glib/gobject/type_plugin/struct.TypePlugin.html
/// [`TypeModule`]: ../glib/gobject/type_module/struct.TypeModule.html
/// [`TypePluginExt::unuse`]: ../glib/gobject/type_plugin/trait.TypePluginExt.html#method.unuse
#[proc_macro_attribute]
pub fn object_subclass(_attr: TokenStream, item: TokenStream) -> TokenStream {
    syn::parse::<syn::ItemImpl>(item)
        .map_err(|_| {
            syn::Error::new(
                Span::call_site(),
                object_subclass_attribute::WRONG_PLACE_MSG,
            )
        })
        .and_then(|mut input| object_subclass_attribute::impl_object_subclass(&mut input))
        .unwrap_or_else(syn::Error::into_compile_error)
        .into()
}

/// Macro for boilerplate of [`ObjectInterface`] implementations.
///
/// This adds implementations for the `get_type()` method, which should probably never be defined
/// differently.
///
/// It provides default values for the `Prerequisites` type parameter. If this present, the macro
/// will use the provided value instead of the default.
///
/// `Prerequisites` is interfaces for types that require a specific base class or interfaces.
///
/// ```ignore
/// type Prerequisites = ();
/// ```
///
/// An object interface can be registered as a dynamic type by setting the
/// macro helper attribute `object_interface_dynamic`:
/// ```ignore
/// pub struct MyInterface {
///     parent: glib::gobject_ffi::GTypeInterface,
/// }
/// #[glib::object_interface]
/// #[object_interface_dynamic]
/// unsafe impl ObjectInterface for MyInterface { ... }
/// ```
///
/// As a dynamic type, an object interface must be explicitly registered when
/// the system loads the implementation (see [`TypePlugin`] and [`TypeModule`].
/// Therefore, whereas an object interface can be registered only once as a
/// static type, it can be registered several times as a dynamic type.
///
/// An object interface registered as a dynamic type is never unregistered. The
/// system calls [`TypePluginExt::unuse`] to unload the implementation. If the
/// [`TypePlugin`] subclass is a [`TypeModule`], the object interface
/// registered as a dynamic type is marked as unloaded and must be registered
/// again when the module is reloaded.
///
/// The macro helper attribute `object_interface_dynamic` provides two
/// behaviors when registering an object interface as a dynamic type:
///
/// - lazy registration: by default an object interface is registered as a
/// dynamic type when the system loads the implementation (e.g. when the module
/// is loaded). Optionally setting `lazy_registration` to `true` postpones
/// registration on the first use (when `type_()` is called for the first time):
/// ```ignore
/// pub struct MyInterface {
///     parent: glib::gobject_ffi::GTypeInterface,
/// }
/// #[glib::object_interface]
/// #[object_interface_dynamic(lazy_registration = true)]
/// unsafe impl ObjectInterface for MyInterface { ... }
/// ```
///
/// - registration within [`TypeModule`] subclass or within [`TypePlugin`]
/// subclass: an object interface is usually registered as a dynamic type
/// within a [`TypeModule`] subclass:
/// ```ignore
/// pub struct MyModuleInterface {
///     parent: glib::gobject_ffi::GTypeInterface,
/// }
/// #[glib::object_interface]
/// #[object_interface_dynamic]
/// unsafe impl ObjectInterface for MyModuleInterface { ... }
/// ...
/// #[derive(Default)]
/// pub struct MyModule;
/// ...
/// impl TypeModuleImpl for MyModule {
///     fn load(&self) -> bool {
///         // registers object interfaces as dynamic types.
///         let my_module = self.obj();
///         let type_module: &glib::TypeModule = my_module.upcast_ref();
///         MyModuleInterface::on_implementation_load(type_module)
///     }
///     ...
/// }
/// ```
///
/// Optionally setting `plugin_type` allows to register an object interface as
/// a dynamic type within a [`TypePlugin`] subclass that is not a [`TypeModule`]:
/// ```ignore
/// pub struct MyPluginInterface {
///     parent: glib::gobject_ffi::GTypeInterface,
/// }
/// #[glib::object_interface]
/// #[object_interface_dynamic(plugin_type = MyPlugin)]
/// unsafe impl ObjectInterface for MyPluginInterface { ... }
/// ...
/// #[derive(Default)]
/// pub struct MyPlugin;
/// ...
/// impl TypePluginImpl for MyPlugin {
///     fn use_plugin(&self) {
///         // register object interfaces as dynamic types.
///         let my_plugin = self.obj();
///         MyPluginInterface::on_implementation_load(my_plugin.as_ref());
///     }
///     ...
/// }
/// ```
///
/// [`ObjectInterface`]: ../glib/subclass/interface/trait.ObjectInterface.html
/// [`TypePlugin`]: ../glib/gobject/type_plugin/struct.TypePlugin.html
/// [`TypeModule`]: ../glib/gobject/type_module/struct.TypeModule.html
/// [`TypePluginExt::unuse`]: ../glib/gobject/type_plugin/trait.TypePluginExt.html#method.unuse///
#[proc_macro_attribute]
pub fn object_interface(_attr: TokenStream, item: TokenStream) -> TokenStream {
    syn::parse::<syn::ItemImpl>(item)
        .map_err(|_| {
            syn::Error::new(
                Span::call_site(),
                object_interface_attribute::WRONG_PLACE_MSG,
            )
        })
        .and_then(|mut input| object_interface_attribute::impl_object_interface(&mut input))
        .unwrap_or_else(syn::Error::into_compile_error)
        .into()
}

/// Macro for deriving implementations of [`glib::clone::Downgrade`] and
/// [`glib::clone::Upgrade`] traits and a weak type.
///
/// # Examples
///
/// ## New Type Idiom
///
/// ```rust,ignore
/// #[derive(glib::Downgrade)]
/// pub struct FancyLabel(gtk::Label);
///
/// impl FancyLabel {
///     pub fn new(label: &str) -> Self {
///         Self(gtk::LabelBuilder::new().label(label).build())
///     }
///
///     pub fn flip(&self) {
///         self.0.set_angle(180.0 - self.0.angle());
///     }
/// }
///
/// let fancy_label = FancyLabel::new("Look at me!");
/// let button = gtk::ButtonBuilder::new().label("Click me!").build();
/// button.connect_clicked(clone!(@weak fancy_label => move || fancy_label.flip()));
/// ```
///
/// ## Generic New Type
///
/// ```rust,ignore
/// #[derive(glib::Downgrade)]
/// pub struct TypedEntry<T>(gtk::Entry, std::marker::PhantomData<T>);
///
/// impl<T: ToString + FromStr> for TypedEntry<T> {
///     // ...
/// }
/// ```
///
/// ## Structures and Enums
///
/// ```rust,ignore
/// #[derive(Clone, glib::Downgrade)]
/// pub struct ControlButtons {
///     pub up: gtk::Button,
///     pub down: gtk::Button,
///     pub left: gtk::Button,
///     pub right: gtk::Button,
/// }
///
/// #[derive(Clone, glib::Downgrade)]
/// pub enum DirectionButton {
///     Left(gtk::Button),
///     Right(gtk::Button),
///     Up(gtk::Button),
///     Down(gtk::Button),
/// }
/// ```
///
/// [`glib::clone::Downgrade`]: ../glib/clone/trait.Downgrade.html
/// [`glib::clone::Upgrade`]: ../glib/clone/trait.Upgrade.html
#[proc_macro_derive(Downgrade)]
pub fn downgrade(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    downgrade_derive::impl_downgrade(input)
}

/// Derive macro for serializing/deserializing custom structs/enums as [`glib::Variant`]s.
///
/// # Example
///
/// ```
/// use glib::prelude::*;
///
/// #[derive(Debug, PartialEq, Eq, glib::Variant)]
/// struct Foo {
///     some_string: String,
///     some_int: i32,
/// }
///
/// let v = Foo { some_string: String::from("bar"), some_int: 1 };
/// let var = v.to_variant();
/// assert_eq!(var.get::<Foo>(), Some(v));
/// ```
///
/// When storing `Vec`s of fixed size types it is a good idea to wrap these in
/// `glib::FixedSizeVariantArray` as serialization/deserialization will be more efficient.
///
/// # Example
///
/// ```
/// use glib::prelude::*;
///
/// #[derive(Debug, PartialEq, Eq, glib::Variant)]
/// struct Foo {
///     some_vec: glib::FixedSizeVariantArray<Vec<u32>, u32>,
///     some_int: i32,
/// }
///
/// let v = Foo { some_vec: vec![1u32, 2u32].into(), some_int: 1 };
/// let var = v.to_variant();
/// assert_eq!(var.get::<Foo>(), Some(v));
/// ```
///
/// Enums are serialized as a tuple `(sv)` with the first value as a [kebab case] string for the
/// enum variant, or just `s` if this is a C-style enum. Some additional attributes are supported
/// for enums:
/// - `#[variant_enum(repr)]` to serialize the enum variant as an integer type instead of `s`.  The
/// `#[repr]` attribute must also be specified on the enum with a sized integer type, and the type
/// must implement `Copy`.
/// - `#[variant_enum(enum)]` uses [`EnumClass`] to serialize/deserialize as nicks. Meant for use
/// with [`glib::Enum`](Enum).
/// - `#[variant_enum(flags)]` uses [`FlagsClass`] to serialize/deserialize as nicks. Meant for use
/// with [`glib::flags`](macro@flags).
/// - `#[variant_enum(enum, repr)]` serializes as `i32`. Meant for use with [`glib::Enum`](Enum).
/// The type must also implement `Copy`.
/// - `#[variant_enum(flags, repr)]` serializes as `u32`. Meant for use with
/// [`glib::flags`](macro@flags).
///
/// # Example
///
/// ```
/// use glib::prelude::*;
///
/// #[derive(Debug, PartialEq, Eq, glib::Variant)]
/// enum Foo {
///     MyA,
///     MyB(i32),
///     MyC { some_int: u32, some_string: String }
/// }
///
/// let v = Foo::MyC { some_int: 1, some_string: String::from("bar") };
/// let var = v.to_variant();
/// assert_eq!(var.child_value(0).str(), Some("my-c"));
/// assert_eq!(var.get::<Foo>(), Some(v));
///
/// #[derive(Debug, Copy, Clone, PartialEq, Eq, glib::Variant)]
/// #[variant_enum(repr)]
/// #[repr(u8)]
/// enum Bar {
///     A,
///     B = 3,
///     C = 7
/// }
///
/// let v = Bar::B;
/// let var = v.to_variant();
/// assert_eq!(var.get::<u8>(), Some(3));
/// assert_eq!(var.get::<Bar>(), Some(v));
///
/// #[derive(Debug, Copy, Clone, PartialEq, Eq, glib::Enum, glib::Variant)]
/// #[variant_enum(enum)]
/// #[enum_type(name = "MyEnum")]
/// enum MyEnum {
///     Val,
///     #[enum_value(name = "My Val")]
///     ValWithCustomName,
///     #[enum_value(name = "My Other Val", nick = "other")]
///     ValWithCustomNameAndNick,
/// }
///
/// let v = MyEnum::ValWithCustomNameAndNick;
/// let var = v.to_variant();
/// assert_eq!(var.str(), Some("other"));
/// assert_eq!(var.get::<MyEnum>(), Some(v));
/// ```
///
/// [`glib::Variant`]: ../glib/variant/struct.Variant.html
/// [`EnumClass`]: ../glib/struct.EnumClass.html
/// [`FlagsClass`]: ../glib/struct.FlagsClass.html
/// [kebab case]: https://docs.rs/heck/0.4.0/heck/trait.ToKebabCase.html
#[proc_macro_derive(Variant, attributes(variant_enum))]
pub fn variant_derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    variant_derive::impl_variant(input)
        .unwrap_or_else(syn::Error::into_compile_error)
        .into()
}
#[proc_macro]
pub fn cstr_bytes(item: TokenStream) -> TokenStream {
    syn::parse::Parser::parse2(
        |stream: syn::parse::ParseStream<'_>| {
            let literal = stream.parse::<syn::LitStr>()?;
            stream.parse::<syn::parse::Nothing>()?;
            let bytes = std::ffi::CString::new(literal.value())
                .map_err(|e| syn::Error::new_spanned(&literal, format!("{e}")))?
                .into_bytes_with_nul();
            let bytes = proc_macro2::Literal::byte_string(&bytes);
            Ok(quote::quote! { #bytes }.into())
        },
        item.into(),
    )
    .unwrap_or_else(|e| e.into_compile_error().into())
}

/// This macro enables you to derive object properties in a quick way.
///
/// # Supported `#[property]` attributes
/// | Attribute | Description | Default | Example |
/// | --- | --- | --- | --- |
/// | `name = "literal"` | The name of the property | field ident where `_` (leading and trailing `_` are trimmed) is replaced into `-` | `#[property(name = "prop-name")]` |
/// | `type = expr` | The type of the property | inferred | `#[property(type = i32)]` |
/// | `get [= expr]` | Specify that the property is readable and use [`PropertyGet::get`] [or optionally set a custom internal getter] | | `#[property(get)]`, `#[property(get = get_prop)]`, or `[property(get = \|_\| 2)]` |
/// | `set [= expr]` | Specify that the property is writable and use [`PropertySet::set`] [or optionally set a custom internal setter] | | `#[property(set)]`, `#[property(set = set_prop)]`, or `[property(set = \|_, val\| {})]` |
/// | `override_class = expr` | The type of class of which to override the property from | | `#[property(override_class = SomeClass)]` |
/// | `override_interface = expr` | The type of interface of which to override the property from | | `#[property(override_interface = SomeInterface)]` |
/// | `nullable` | Whether to use `Option<T>` in the generated setter method |  | `#[property(nullable)]` |
/// | `member = ident` | Field of the nested type where property is retrieved and set | | `#[property(member = author)]` |
/// | `construct` | Specify that the property is construct property. Ensures that the property is always set during construction (if not explicitly then the default value is used). The use of a custom internal setter is supported. | | `#[property(get, construct)]` or `#[property(get, set = set_prop, construct)]` |
/// | `construct_only` | Specify that the property is construct only. This will not generate a public setter and only allow the property to be set during object construction. The use of a custom internal setter is supported. | | `#[property(get, construct_only)]` or `#[property(get, set = set_prop, construct_only)]` |
/// | `builder(<required-params>)[.ident]*` | Used to input required params or add optional Param Spec builder fields | | `#[property(builder(SomeEnum::default()))]`, `#[builder().default_value(1).minimum(0).maximum(5)]`, etc.  |
/// | `default` | Sets the `default_value` field of the Param Spec builder | | `#[property(default = 1)]` |
/// | `<optional-pspec-builder-fields> = expr` | Used to add optional Param Spec builder fields | | `#[property(minimum = 0)` , `#[property(minimum = 0, maximum = 1)]`, etc. |
/// | `<optional-pspec-builder-fields>` | Used to add optional Param Spec builder fields | | `#[property(explicit_notify)]` , `#[property(construct_only)]`, etc. |
///
/// ## Using Rust keywords as property names
/// You might hit a roadblock when declaring properties with this macro because you want to use a name that happens to be a Rust keyword. This may happen with names like `loop`, which is a pretty common name when creating things like animation handlers.
/// To use those names, you can make use of the raw identifier feature of Rust. Simply prefix the identifier name with `r#` in the struct declaration. Internally, those `r#`s are stripped so you can use its expected name in [`ObjectExt::property`] or within GtkBuilder template files.
///
/// # Generated methods
/// The following methods are generated on the wrapper type specified on `#[properties(wrapper_type = ...)]`:
/// * `$property()`, when the property is readable
/// * `set_$property()`, when the property is writable and not construct-only
/// * `connect_$property_notify()`
/// * `notify_$property()`
///
/// ## Extension trait
/// You can choose to move the method definitions to a trait by using `#[properties(wrapper_type = super::MyType, ext_trait = MyTypePropertiesExt)]`.
/// The trait name is optional, and defaults to `MyTypePropertiesExt`, where `MyType` is extracted from the wrapper type.
/// Note: The trait is defined in the same module where the `#[derive(Properties)]` call happens, and is implemented on the wrapper type.
///
/// Notice: You can't reimplement the generated methods on the wrapper type, unless you move them to a trait.
/// You can change the behavior of the generated getter/setter methods by using a custom internal getter/setter.
///
/// # Internal getters and setters
/// By default, they are generated for you. However, you can use a custom getter/setter
/// by assigning an expression to `get`/`set` `#[property]` attributes: `#[property(get = |_| 2, set)]` or `#[property(get, set = custom_setter_func)]`.
///
/// # Supported types
/// Every type implementing the trait [`Property`] is supported.
/// The type `Option<T>` is supported as a property only if `Option<T>` implements [`ToValueOptional`].
/// Optional types also require the `nullable` attribute: without it, the generated setter on the wrapper type
/// will take `T` instead of `Option<T>`, preventing the user from ever calling the setter with a `None` value.
///
/// ## Adding support for custom types
/// ### Types wrapping an existing <code>T: [ToValue] + [HasParamSpec]</code>
/// If you have declared a newtype as
/// ```rust
/// struct MyInt(i32);
/// ```
/// you can use it as a property by deriving [`ValueDelegate`].
///
/// ### Types with inner mutability
/// The trait [`Property`] must be implemented.
/// The traits [`PropertyGet`] and [`PropertySet`] should be implemented to enable the Properties macro
/// to generate a default internal getter/setter.
/// If possible, implementing [`PropertySetNested`] is preferred over `PropertySet`, because it
/// enables this macro to access the contained type and provide access to its fields,
/// using the `member = $structfield` syntax.
///
/// ### Types without [`HasParamSpec`][HasParamSpec]
/// If you have encountered a type <code>T: [ToValue]</code>, inside the gtk-rs crate, which doesn't implement [`HasParamSpec`][HasParamSpec],
/// then it's a bug and you should report it.
/// If you need to support a `ToValue` type with a [`ParamSpec`] not provided by gtk-rs, then you need to
/// implement `HasParamSpec` on that type.
///
/// # Example
/// ```
/// use std::cell::RefCell;
/// use glib::prelude::*;
/// use glib::subclass::prelude::*;
/// use glib_macros::Properties;
///
/// #[derive(Default, Clone)]
/// struct Author {
///     name: String,
///     nick: String,
/// }
///
/// pub mod imp {
///     use std::rc::Rc;
///
///     use super::*;
///
///     #[derive(Properties, Default)]
///     #[properties(wrapper_type = super::Foo)]
///     pub struct Foo {
///         #[property(get, set = Self::set_fizz)]
///         fizz: RefCell<String>,
///         #[property(name = "author-name", get, set, type = String, member = name)]
///         #[property(name = "author-nick", get, set, type = String, member = nick)]
///         author: RefCell<Author>,
///         #[property(get, set, explicit_notify, lax_validation)]
///         custom_flags: RefCell<String>,
///         #[property(get, set, minimum = 0, maximum = 3)]
///         numeric_builder: RefCell<u32>,
///         #[property(get, set, builder('c'))]
///         builder_with_required_param: RefCell<char>,
///         #[property(get, set, nullable)]
///         optional: RefCell<Option<String>>,
///         #[property(get, set)]
///         smart_pointer: Rc<RefCell<String>>,
///     }
///     
///     #[glib::derived_properties]
///     impl ObjectImpl for Foo {}
///
///     #[glib::object_subclass]
///     impl ObjectSubclass for Foo {
///         const NAME: &'static str = "MyFoo";
///         type Type = super::Foo;
///     }
///
///     impl Foo {
///         fn set_fizz(&self, value: String) {
///             *self.fizz.borrow_mut() = format!("custom set: {}", value);
///         }
///     }
/// }
///
/// glib::wrapper! {
///     pub struct Foo(ObjectSubclass<imp::Foo>);
/// }
///
/// fn main() {
///   let myfoo: Foo = glib::object::Object::new();
///
///   myfoo.set_fizz("test value");
///   assert_eq!(myfoo.fizz(), "custom set: test value".to_string());
/// }
/// ```
///
/// [`Property`]: ../glib/property/trait.Property.html
/// [`PropertyGet`]: ../glib/property/trait.PropertyGet.html
/// [`PropertyGet::get`]: ../glib/property/trait.PropertyGet.html#tymethod.get
/// [`PropertySet`]: ../glib/property/trait.PropertySet.html
/// [`PropertySet::set`]: ../glib/property/trait.PropertySet.html#tymethod.set
/// [`PropertySetNested`]: ../glib/property/trait.PropertySetNested.html
/// [`ObjectExt::property`]: ../glib/object/trait.ObjectExt.html#tymethod.property
/// [HasParamSpec]: ../glib/trait.HasParamSpec.html
/// [`ParamSpec`]: ../glib/struct.ParamSpec.html
/// [`ToValueOptional`]: ../glib/value/trait.ToValueOptional.html
/// [ToValue]: ../glib/value/trait.ToValue.html
#[allow(clippy::needless_doctest_main)]
#[proc_macro_derive(Properties, attributes(properties, property))]
pub fn derive_props(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as properties::PropsMacroInput);
    properties::impl_derive_props(input)
}

/// When applied to `ObjectImpl`
/// ```ignore
/// #[glib::derived_properties]
/// impl ObjectImpl for CustomObject
/// ```
/// this macro generates
/// ```ignore
/// impl ObjectImpl for CustomObject {
///     fn properties() -> &'static [glib::ParamSpec] {
///         Self::derived_properties()
///     }
///     fn set_property(&self, id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
///         self.derived_set_property(id, value, pspec)
///     }
///     fn property(&self, id: usize, pspec: &glib::ParamSpec) -> glib::Value {
///         self.derived_property(id, pspec)
///     }
/// }
/// ```
#[proc_macro_attribute]
pub fn derived_properties(_attr: TokenStream, item: TokenStream) -> TokenStream {
    syn::parse::<syn::ItemImpl>(item)
        .map_err(|_| {
            syn::Error::new(
                Span::call_site(),
                derived_properties_attribute::WRONG_PLACE_MSG,
            )
        })
        .and_then(|input| derived_properties_attribute::impl_derived_properties(&input))
        .unwrap_or_else(syn::Error::into_compile_error)
        .into()
}

/// # Example
/// ```
/// use glib::prelude::*;
/// use glib::ValueDelegate;
///
/// #[derive(ValueDelegate, Debug, PartialEq)]
/// struct MyInt(i32);
///
/// let myv = MyInt(2);
/// let convertedv = myv.to_value();
/// assert_eq!(convertedv.get::<MyInt>(), Ok(myv));
///
///
/// #[derive(ValueDelegate, Debug, PartialEq)]
/// #[value_delegate(from = u32)]
/// enum MyEnum {
///     Zero,
///     NotZero(u32)
/// }
///
/// impl From<u32> for MyEnum {
///     fn from(v: u32) -> Self {
///         match v {
///             0 => MyEnum::Zero,
///             x => MyEnum::NotZero(x)
///         }
///     }
/// }
/// impl<'a> From<&'a MyEnum> for u32 {
///     fn from(v: &'a MyEnum) -> Self {
///         match v {
///             MyEnum::Zero => 0,
///             MyEnum::NotZero(x) => *x
///         }
///     }
/// }
/// impl From<MyEnum> for u32 {
///     fn from(v: MyEnum) -> Self {
///         match v {
///             MyEnum::Zero => 0,
///             MyEnum::NotZero(x) => x
///         }
///     }
/// }
///
/// let myv = MyEnum::NotZero(34);
/// let convertedv = myv.to_value();
/// assert_eq!(convertedv.get::<MyEnum>(), Ok(myv));
///
///
/// // If you want your type to be usable inside an `Option`, you can derive `ToValueOptional`
/// // by adding `nullable` as follows
/// #[derive(ValueDelegate, Debug, PartialEq)]
/// #[value_delegate(nullable)]
/// struct MyString(String);
///
/// let myv = Some(MyString("Hello world".to_string()));
/// let convertedv = myv.to_value();
/// assert_eq!(convertedv.get::<Option<MyString>>(), Ok(myv));
/// let convertedv = None::<MyString>.to_value();
/// assert_eq!(convertedv.get::<Option<MyString>>(), Ok(None::<MyString>));
/// ```
#[proc_macro_derive(ValueDelegate, attributes(value_delegate))]
pub fn derive_value_delegate(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as value_delegate_derive::ValueDelegateInput);
    value_delegate_derive::impl_value_delegate(input).unwrap()
}