musli/help/
derives.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
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
//! Deriving [`Encode`] and [`Decode`].
//!
//! The [`Encode`] and [`Decode`] derives allows for automatically implementing
//! [`Encode`] and [`Decode`].
//!
//! They come with a number of options for customizing their implementation,
//! detailed below. But first we need to talk about *modes*.
//!
//! <br>
//!
//! #### Modes
//!
//! If you've paid close attention to the [`Encode`] and [`Decode`] traits you
//! might notive that they have an extra parameter called `M`. This stands for
//! "mode".
//!
//! This parameter allows us to have different implementations of these traits
//! for the same type.
//!
//! By default we implements two modes, which each have subtly different default
//! behaviors:
//! * [`Binary`] - which uses indexed fields, the equivalent of
//!   `#[musli(name_type = usize)]`.
//! * [`Text`] - which uses literally text fields by their name, the equivalent
//!   of `#[musli(name_type = str)]`.
//!
//! When it comes to deriving these traits you can scope attributes to apply to
//! any mode including custom local ones. This is done using the `#[musli(mode =
//! ..)]` meta attribute like this:
//!
//! ```
//! use musli::{Encode, Decode};
//! use musli::mode::Binary;
//! use musli::json::Encoding;
//!
//! #[derive(Encode, Decode)]
//! struct Person<'a> {
//!     #[musli(mode = Text, name = "name")]
//!     not_name: &'a str,
//!     age: u32,
//! }
//!
//! const TEXT: Encoding = Encoding::new();
//! const BINARY: Encoding<Binary> = Encoding::new().with_mode();
//!
//! let named = TEXT.to_vec(&Person { not_name: "Aristotle", age: 61 })?;
//! assert_eq!(named.as_slice(), br#"{"name":"Aristotle","age":61}"#);
//!
//! let indexed = BINARY.to_vec(&Person { not_name: "Plato", age: 84 })?;
//! assert_eq!(indexed.as_slice(), br#"{"0":"Plato","1":84}"#);
//! # Ok::<_, musli::json::Error>(())
//! ```
//!
//! So the `#[musli(mode)]` atttribute is supported in any position. And any of
//! its sibling attributes will be added to the given *alternative* mode, rather
//! the [default mode].
//!
//! <br>
//!
//! #### Attributes
//!
//! * [*Meta attributes*](#meta-attributes) which apply to the attribute itself.
//!   It is used to filter what scope the current attribute applies to, such as
//!   only applying to an `Encode` derive using `#[musli(encode_only, ..)]` or a
//!   specific mode such as `#[musli(mode = Json, ..)]`.
//!
//! * [*Container attributes*](#container-attributes) are attributes which apply
//!   to the `struct` or `enum`.
//!
//! * [*Variant attributes*](#variant-attributes) are attributes which apply to
//!   each individual variant in an `enum`.
//!
//! * [*Field attributes*](#field-attributes) are attributes which apply to each
//!   individual field either in a `struct` or an `enum` variant.
//!
//! <br>
//!
//! ## Meta attributes
//!
//! Certain attributes affect which other attributes apply to a given context.
//! These are called *meta* attributes.
//!
//! Meta attributes are applicable to any context, and can be used on
//! containers, variants, and fields.
//!
//! <br>
//!
//! #### `#[musli(mode = <path>)]`
//!
//! The attributes only apply to the given `mode`.
//!
//! The `Person` struct below uses string field names by default when the `Text`
//! mode is enabled, but we can change this behavior only for that particular
//! mode like this:
//!
//! ```
//! use musli::{Encode, Decode};
//! use musli::mode::Text;
//!
//! #[derive(Encode, Decode)]
//! #[musli(mode = Text, name_type = usize)]
//! struct Person<'a> {
//!     name: &'a str,
//!     age: u32,
//! }
//! ```
//!
//! <br>
//!
//! #### `#[musli(encode_only)]`
//!
//! The attributes only apply when implementing the `Encode` trait.
//!
//! An example where this is useful is if you want to apply `#[musli(packed)]`
//! in a different mode, but only for encoding, since decoding packed types is
//! not supported for enums.
//!
//! ```
//! use musli::mode::Binary;
//! use musli::{Decode, Encode};
//!
//! enum Packed {}
//!
//! #[derive(Encode, Decode)]
//! #[musli(mode = Packed, encode_only, packed)]
//! enum Name<'a> {
//!     Full(&'a str),
//!     Given(&'a str),
//! }
//! ```
//!
//! <br>
//!
//! #### `#[musli(decode_only)]`
//!
//! The attributes only apply when implementing the `Decode` trait.
//!
//! ```
//! use musli::{Decode, Encode};
//!
//! #[derive(Encode, Decode)]
//! #[musli(name_all = "name")]
//! struct Name<'a> {
//!     sur_name: &'a str,
//!     #[musli(decode_only, name = "last")]
//!     last_name: &'a str,
//! }
//! ```
//!
//! <br>
//!
//! ## Container attributes
//!
//! Container attributes apply to the container, such as directly on the
//! `struct` or `enum`. Like the uses of `#[musli(packed)]` and
//! `#[musli(name_all = "name")]` here:
//!
//! ```
//! use musli::{Encode, Decode};
//!
//! #[derive(Encode, Decode)]
//! #[musli(packed)]
//! struct Struct {
//!     /* the body of the struct */
//! }
//!
//! #[derive(Encode, Decode)]
//! #[musli(name_all = "name")]
//! enum Enum {
//!     /* the body of the enum */
//! }
//! ```
//!
//! <br>
//!
//! #### `#[musli(name_all = "..")]`
//!
//! Allos for renaming every field in the container. It can take any of the
//! following values:
//!
//! * `index` (default) - the index of the field will be used.
//! * `name` - the literal name of the field will be used.
//! * `PascalCase` - the field will be converted to pascal case.
//! * `camelCase` - the field will be converted to camel case.
//! * `snake_case` - the field will be converted to snake case.
//! * `SCREAMING_SNAKE_CASE` - the field will be converted to screaming snake case.
//! * `kebab-case` - the field will be converted to kebab case.
//! * `SCREAMING-KEBAB-CASE` - the field will be converted to screaming kebab case.
//!
//! ```
//! use musli::{Encode, Decode};
//!
//! #[derive(Encode, Decode)]
//! #[musli(name_all = "PascalCase")]
//! struct PascalCaseStruct {
//!     field_name: u32,
//! }
//!
//! #[derive(Encode, Decode)]
//! #[musli(name_all = "name")]
//! struct NamedStruct {
//!     field1: u32,
//!     field2: u32,
//! }
//! ```
//!
//! If applied to an enum, it will instead rename all variants:
//!
//! ```
//! use musli::{Encode, Decode};
//!
//! #[derive(Encode, Decode)]
//! #[musli(name_all = "PascalCase")]
//! enum PascalCaseEnum {
//!     VariantName {
//!         field_name: u32,
//!     }
//! }
//!
//! #[derive(Encode, Decode)]
//! #[musli(name_all = "name")]
//! enum NamedEnum {
//!     Variant1 {
//!         field1: u32,
//!     },
//!     Variant2 {
//!         field1: u32,
//!     },
//! }
//! ```
//!
//! <br>
//!
//! #### `#[musli(transparent)]`
//!
//! This can only be used on types which have a single field. It will cause that
//! field to define how that variant is encoded or decoded transparently without
//! being treated as a field.
//!
//! ```
//! use musli::{Encode, Decode};
//!
//! #[derive(Encode)]
//! #[musli(transparent)]
//! struct Struct(u32);
//!
//! let data = musli::wire::to_vec(&Struct(42))?;
//! let actual: u32 = musli::wire::from_slice(&data)?;
//! assert_eq!(actual, 42u32);
//! # Ok::<_, musli::wire::Error>(())
//! ```
//!
//! <br>
//!
//! #### `#[musli(packed)]`
//!
//! This attribute will disable all *tagging* and the structure will simply be
//! encoded with one field following another in the order in which they are
//! defined.
//!
//! A caveat of *packed* structures is that they cannot be safely versioned and
//! the two systems communicating through them need to be using strictly
//! synchronized representations.
//!
//! This attribute is useful for performing simple decoding over "raw" bytes
//! when combined with an encoder which does minimal prefixing and packs fields.
//!
//! ```
//! use musli::{Encode, Decode};
//!
//! #[derive(Encode)]
//! #[musli(packed)]
//! struct Struct {
//!     field1: u32,
//!     field2: u32,
//!     field3: u32,
//! }
//!
//! let data = musli::storage::to_vec(&Struct {
//!     field1: 1,
//!     field2: 2,
//!     field3: 3,
//! })?;
//!
//! assert_eq!(data.as_slice(), [1, 2, 3]);
//! # Ok::<_, musli::storage::Error>(())
//! ```
//!
//! <br>
//!
//! #### `#[musli(name_type = ..)]`
//!
//! This indicates which type any contained `#[musli(name = ..)]` attributes
//! should have. Tags can usually be inferred, but specifying this field ensures
//! that all tags have a single well-defined type.
//!
//! The following values are treated specially:
//! * `str` applies `#[musli(name_all = "name")]` by default.
//! * `[u8]` applies `#[musli(name_all = "name")]` by default.
//!
//! ```
//! use core::fmt;
//!
//! use musli::{Encode, Decode};
//!
//! #[derive(Debug, PartialEq, Eq, Encode, Decode)]
//! #[musli(transparent)]
//! struct CustomTag<'a>(&'a [u8]);
//!
//! impl fmt::Display for CustomTag<'_> {
//!     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
//!         fmt::Debug::fmt(self.0, f)
//!     }
//! }
//!
//! #[derive(Encode, Decode)]
//! #[musli(name_type = CustomTag)]
//! struct Struct {
//!     #[musli(name = CustomTag(b"name in bytes"))]
//!     name: String,
//! }
//!
//! #[derive(Encode, Decode)]
//! #[musli(name_type = CustomTag)]
//! enum EnumWithCustomTag {
//!     #[musli(name = CustomTag(b"variant one"))]
//!     Variant1 {
//!         /* .. */
//!     },
//! }
//! ```
//!
//! <br>
//!
//! #### `#[musli(name_method = ..)]`
//!
//! This allows for explicitly setting which method should be used to decode
//! names. Available options are:
//!
//! * `"value"` (default) - the name is decoded as a value.
//! * `"unsized"` - the name is decoded as an unsized value, this is the default
//!   if for example `#[musli(name_type = str)]` is used.
//! * `"unsized_bytes"` - the name is decoded as a unsized bytes, this is the
//!   default if for example `#[musli(name_type = [u8])]` is used.
//!
//! This can be overrided for values which are unsized, but cannot be determined
//! through heuristics. Such a type must also implement [`Decode`] (for
//! `"value"`), `DecodeUnsized`, or `DecodeUnsizedBytes` as appropriate.
//!
//! <br>
//!
//! #### `#[musli(bound = {..})]` and `#[musli(decode_bound = {..})]`
//!
//! These attributes can be used to apply bounds to an [`Encode`] or [`Decode`]
//! implementation.
//!
//! These are necessary to use when a generic container is used to ensure that
//! the given parameter implements the necessary bounds.
//!
//! `#[musli(bound = {..})]` applies to all implementations while
//! `#[musli(decode_bound = {..})]` only applies to the [`Decode`]
//! implementation. The latter allows for using the decode lifetime parameter
//! (which defaults to `'de`).
//!
//! ```
//! use musli::{Decode, Encode};
//! use musli::mode::{Binary, Text};
//!
//! #[derive(Clone, Debug, PartialEq, Encode, Decode)]
//! #[musli(mode = Binary, bound = {T: Encode<Binary>}, decode_bound = {T: Decode<'de, Binary>})]
//! #[musli(mode = Text, bound = {T: Encode<Text>}, decode_bound = {T: Decode<'de, Text>})]
//! pub struct GenericWithBound<T> {
//!     value: T,
//! }
//! ```
//!
//! <br>
//!
//! ## Enum attributes
//!
//! <br>
//!
//! #### `#[musli(tag = ..)]`
//!
//! This attribute causes the enum to be internally tagged, with the given tag.
//! See [enum representations](#enum-representations) for details on this
//! representation.
//!
//! ```
//! # use musli::{Encode, Decode};
//! # #[derive(Encode, Decode)] struct Params;
//! # #[derive(Encode, Decode)] struct Value;
//! #[derive(Encode, Decode)]
//! #[musli(name_all = "name", tag = "type")]
//! enum Message {
//!     Request { id: String, method: String, params: Params },
//!     Response { id: String, result: Value },
//! }
//! ```
//!
//! <br>
//!
//! ## Variant attributes
//!
//! *Variant attributes* are attributes which apply to each individual variant
//! in an `enum`. Like the use of `#[musli(name = ..)]` here:
//!
//! ```
//! use musli::{Encode, Decode};
//!
//! #[derive(Encode, Decode)]
//!#[musli(name_all = "name")]
//! enum Enum {
//!     Variant {
//!         /* variant body */
//!     },
//!     #[musli(name = "Other")]
//!     Something {
//!         /* variant body */
//!     },
//! }
//! ```
//!
//! <br>
//!
//! #### `#[musli(name = ..)]`
//!
//! This allows for renaming a variant from its default value. It can take any
//! value (including complex ones) that can be serialized with the current
//! encoding, such as:
//!
//! * `#[musli(name = 1)]`
//! * `#[musli(name = "Hello World")]`
//! * `#[musli(name = b"box\0")]`
//! * `#[musli(name = SomeStruct { field: 42 })]` (if `SomeStruct` implements
//!   [`Encode`] and [`Decode`] as appropriate).
//!
//! If the type of the tag is ambiguous it can be explicitly specified through
//! the `#[musli(name_type)]` attribute.
//!
//! <br>
//!
//! #### `#[musli(pattern = ..)]`
//!
//! A pattern to match for decoding a variant.
//!
//! This allows for more flexibility when decoding variants.
//!
//! ```
//! use musli::{Encode, Decode};
//!
//! #[derive(Encode, Decode)]
//! enum Enum {
//!     Variant1,
//!     Variant2,
//!     #[musli(mode = Binary, pattern = 2..=4)]
//!     Deprecated,
//! }
//! ```
//!
//! <br>
//!
//! #### `#[musli(name_all = "..")]`
//!
//! Allos for renaming every field in the variant. It can take any of the
//! following values:
//!
//! * `index` (default) - the index of the field will be used.
//! * `name` - the literal name of the field will be used.
//! * `PascalCase` - the field will be converted to pascal case.
//! * `camelCase` - the field will be converted to camel case.
//! * `snake_case` - the field will be converted to snake case.
//! * `SCREAMING_SNAKE_CASE` - the field will be converted to screaming snake case.
//! * `kebab-case` - the field will be converted to kebab case.
//! * `SCREAMING-KEBAB-CASE` - the field will be converted to screaming kebab case.
//!
//! ```
//! use musli::{Encode, Decode};
//!
//! #[derive(Encode, Decode)]
//! enum PascalCaseEnum {
//!     #[musli(name_all = "PascalCase")]
//!     Variant {
//!         field_name: u32,
//!     }
//! }
//!
//! #[derive(Encode, Decode)]
//! enum NamedEnum {
//!     #[musli(name_all = "name")]
//!     Variant {
//!         field1: u32,
//!     },
//!     Variant2 {
//!         field1: u32,
//!     },
//! }
//! ```
//!
//! <br>
//!
//! #### `#[musli(name_type = ..)]`
//!
//! This indicates which type any contained `#[musli(tag = ..)]` attributes
//! should have. Tags can usually be inferred, but specifying this field ensures
//! that all tags have a well-defined type.
//!
//! The following values are treated specially:
//! * `str` applies `#[musli(name_all = "name")]` by default.
//! * `[u8]` applies `#[musli(name_all = "name")]` by default.
//!
//! ```
//! use core::fmt;
//!
//! use musli::{Encode, Decode};
//!
//! #[derive(Debug, PartialEq, Eq, Encode, Decode)]
//! #[musli(transparent)]
//! struct CustomTag<'a>(&'a [u8]);
//!
//! impl fmt::Display for CustomTag<'_> {
//!     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
//!         fmt::Debug::fmt(self.0, f)
//!     }
//! }
//!
//! #[derive(Encode, Decode)]
//! #[musli(name_type = usize)]
//! enum Enum {
//!     #[musli(name = 0usize, name_type = CustomTag)]
//!     Variant {
//!         #[musli(name = CustomTag(b"field1"))]
//!         field1: u32,
//!         #[musli(name = CustomTag(b"field2"))]
//!         field2: u32,
//!     },
//!     #[musli(name = 1usize, name_all = "name")]
//!     Variant2 {
//!         #[musli(name = "field1")]
//!         field1: u32,
//!         #[musli(name = "field2")]
//!         field2: u32,
//!     },
//! }
//! ```
//!
//! <br>
//!
//! #### `#[musli(name_method = ..)]`
//!
//! This allows for explicitly setting which method should be used to decode
//! field names. Available options are:
//!
//! * `"value"` (default) - the name is decoded as a value.
//! * `"unsized"` - the name is decoded as an unsized value, this is the default
//!   if for example `#[musli(name_type = str)]` is used.
//! * `"unsized_bytes"` - the name is decoded as a unsized bytes, this is the
//!   default if for example `#[musli(name_type = [u8])]` is used.
//!
//! This can be overrided for values which are unsized, but cannot be determined
//! through heuristics. Such a type must also implement [`Decode`] (for
//! `"value"`), `DecodeUnsized`, or `DecodeUnsizedBytes` as appropriate.
//!
//! <br>
//!
//! #### `#[musli(transparent)]`
//!
//! This can only be used on variants which have a single field. It will cause
//! that field to define how that variant is encoded or decoded transparently
//! without being treated as a field.
//!
//! <br>
//!
//! #### `#[musli(default)]`
//!
//! This defines the variant that will be used in case no other variant matches.
//! Only one such variant can be defined.
//!
//! ```
//! use musli::{Encode, Decode};
//!
//! #[derive(Debug, PartialEq, Eq, Encode, Decode)]
//! #[musli(name_all = "kebab-case")]
//! enum Animal {
//!     Cat,
//!     Dog,
//!     #[musli(default)]
//!     Unknown,
//! }
//! ```
//!
//! <br>
//!
//! ## Field attributes
//!
//! *Field attributes* are attributes which apply to each individual field
//! either in a `struct` or an `enum` variant. Like the uses of `#[musli(all)]`
//! here:
//!
//! ```
//! use musli::{Encode, Decode};
//!
//! #[derive(Encode, Decode)]
//! #[musli(name_all = "name")]
//! struct Struct {
//!     #[musli(name = "other")]
//!     something: String,
//!     #[musli(skip, default = default_field)]
//!     skipped_field: u32,
//! }
//!
//! fn default_field() -> u32 {
//!     42
//! }
//!
//! #[derive(Encode, Decode)]
//! #[musli(name_all = "name")]
//! enum Enum {
//!     #[musli(name_all = "name")]
//!     Variant {
//!         #[musli(name = "other")]
//!         something: String,
//!     }
//! }
//! ```
//!
//! <br>
//!
//! #### `#[musli(skip)]`
//!
//! This attribute means that the entire field is skipped. If a field is decoded
//! it uses [`Default::default`] to construct the value. Other defaults can be
//! specified with [`#[musli(default = <path>)]`][#muslidefault--path].
//!
//! ```
//! use musli::{Encode, Decode};
//!
//! #[derive(Encode, Decode)]
//! struct Person {
//!     name: String,
//!     #[musli(skip)]
//!     age: Option<u32>,
//!     #[musli(skip, default = default_country)]
//!     country: Option<String>,
//! }
//!
//! fn default_country() -> Option<String> {
//!     Some(String::from("Earth"))
//! }
//! ```
//!
//! <br>
//!
//! #### `#[musli(default [= <path>])]`
//!
//! When a field is absent or disabled with `#[musli(skip)]`, this attribute
//! specifies that a default value should be used instead.
//!
//! If `#[musli(default)]` is specified, the default value is constructed using
//! [`Default::default`].
//!
//! If `#[musli(default = <path>)]` is specified, the default value is
//! constructed by calling the function at `<path>`.
//!
//! ```
//! use musli::{Encode, Decode};
//!
//! #[derive(Encode, Decode)]
//! struct Person {
//!     name: String,
//!     #[musli(default)]
//!     age: Option<u32>,
//!     #[musli(default = default_height)]
//!     height: Option<u32>,
//!     #[musli(skip, default = default_meaning)]
//!     meaning: u32,
//! }
//!
//! fn default_height() -> Option<u32> {
//!     Some(180)
//! }
//!
//! fn default_meaning() -> u32 {
//!     42
//! }
//! ```
//!
//! <br>
//!
//! #### `#[musli(name = ..)]`
//!
//! This allows for renaming a field from its default value. It can take any
//! value (including complex ones) that can be serialized with the current
//! encoding, such as:
//!
//! * `#[musli(name = 1)]`
//! * `#[musli(name = "Hello World")]`
//! * `#[musli(name = b"box\0")]`
//! * `#[musli(name = SomeStruct { field: 42 })]` (if `SomeStruct` implements
//!   [`Encode`] and [`Decode`] as appropriate).
//!
//! If the type of the tag is ambiguous it can be explicitly specified through
//! the `#[musli(name_type)]` variant or container attributes.
//!
//! <br>
//!
//! #### `#[musli(pattern = ..)]`
//!
//! A pattern to match for decoding the given field.
//!
//! This allows for more flexibility when decoding fields.
//!
//! ```
//! use musli::{Encode, Decode};
//!
//! #[derive(Encode, Decode)]
//! struct Struct {
//!     field1: u32,
//!     field2: u32,
//!     #[musli(mode = Binary, pattern = 2..=4)]
//!     other: u32,
//! }
//! ```
//!
//! <br>
//!
//! #### `#[musli(packed)]`
//!
//! This specifies that encoding and decoding should happen through the
//! [`EncodePacked`] and [`DecodePacked`] traits, instead of the default
//! [`Encode`] and [`Decode`].
//!
//! These traits contained implementations which are biased towards encoding the
//! field as a compact, non-future compatible pack. In essense, the fields are
//! encoded "one after another" without any metadata separating them. So for
//! packed fields, the order, types and number of the fields are important.
//!
//! ```
//! use std::collections::VecDeque;
//!
//! use musli::{Decode, Encode};
//!
//! #[derive(Decode, Encode)]
//! struct Container {
//!     #[musli(packed)]
//!     tuple: (u32, u64),
//!     #[musli(packed)]
//!     array: [u32; 4],
//! }
//! ```
//!
//! <br>
//!
//! #### `#[musli(bytes)]`
//!
//! This specifies that encoding and decoding should happen through the
//! [`EncodeBytes`] and [`DecodeBytes`] traits, instead of the default
//! [`Encode`] and [`Decode`].
//!
//! These traits contained implementations which are biased towards encoding the
//! field as an array of bytes.
//!
//! ```
//! use std::collections::VecDeque;
//!
//! use musli::{Decode, Encode};
//!
//! #[derive(Decode, Encode)]
//! struct Container<'de> {
//!     #[musli(bytes)]
//!     vec: Vec<u8>,
//!     #[musli(bytes)]
//!     vec_deque: VecDeque<u8>,
//!     #[musli(bytes)]
//!     bytes: &'de [u8],
//! }
//! ```
//!
//! <br>
//!
//! #### `#[musli(with = <path>)]`
//!
//! This specifies the path to a module to use instead of the fields default
//! [`Encode`] or [`Decode`] implementations.
//!
//! It expects `encode` and `decode` and decodee function to be defined in the path being specified, like this:
//!
//! ```
//! # mod example {
//! use musli::{Decode, Encode};
//!
//! #[derive(Decode, Encode)]
//! struct Container {
//!     #[musli(with = self::module)]
//!     field: Field,
//! }
//!
//! struct Field {
//!     /* internal */
//! }
//!
//! mod module {
//!     use musli::{Decoder, Encoder};
//!
//!     use super::Field;
//!
//!     pub fn encode<E>(field: &Field, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
//!     where
//!         E: Encoder,
//! # { todo!() }
//!
//!     pub fn decode<'de, D>(cx: &D::Cx, decoder: D) -> Result<Field, D::Error>
//!     where
//!         D: Decoder<'de>,
//! # { todo!() }
//! }
//! # }
//! ```
//!
//! This can also be generic such as:
//!
//! ```
//! # mod example {
//! use musli::{Decode, Encode};
//!
//! #[derive(Decode, Encode)]
//! struct Container {
//!     #[musli(with = self::module)]
//!     field: Field<u32>,
//! }
//!
//! struct Field<T> {
//!     /* internal */
//! #   value: T,
//! }
//!
//! mod module {
//!     use musli::{Decoder, Encoder};
//!
//!     use super::Field;
//!
//!     pub fn encode<E, T>(field: &Field<T>, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
//!     where
//!         E: Encoder,
//! # { todo!() }
//!
//!     pub fn decode<'de, D, T>(cx: &D::Cx, decoder: D) -> Result<Field<T>, D::Error>
//!     where
//!         D: Decoder<'de>,
//! # { todo!() }
//! }
//! # }
//! ```
//!
//! More complete example:
//!
//! ```
//! # mod example {
//! use std::collections::HashSet;
//! use musli::{Encode, Decode};
//!
//! pub struct CustomUuid(u128);
//!
//! #[derive(Encode, Decode)]
//! struct Struct {
//!     #[musli(with = self::custom_uuid)]
//!     id: CustomUuid,
//!     #[musli(with = self::custom_set)]
//!     numbers: HashSet<u32>,
//! }
//!
//! mod custom_uuid {
//!     use musli::{Context, Decode, Decoder, Encode, Encoder};
//!
//!     use super::CustomUuid;
//!
//!     pub fn encode<E>(uuid: &CustomUuid, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
//!     where
//!         E: Encoder,
//!     {
//!         uuid.0.encode(cx, encoder)
//!     }
//!
//!     pub fn decode<'de, D>(cx: &D::Cx, decoder: D) -> Result<CustomUuid, D::Error>
//!     where
//!         D: Decoder<'de>,
//!     {
//!         Ok(CustomUuid(decoder.decode()?))
//!     }
//! }
//!
//! mod custom_set {
//!     use std::collections::HashSet;
//!     use std::hash::Hash;
//!
//!     use musli::{Context, Decode, Decoder, Encode, Encoder};
//!
//!     pub fn encode<E, T>(set: &HashSet<T>, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
//!     where
//!         E: Encoder,
//!         T: Encode<E::Mode> + Eq + Hash,
//!     {
//!         encoder.encode(set)
//!     }
//!
//!     pub fn decode<'de, D, T>(cx: &D::Cx, decoder: D) -> Result<HashSet<T>, D::Error>
//!     where
//!         D: Decoder<'de>,
//!         T: Decode<'de, D::Mode> + Eq + Hash,
//!     {
//!         decoder.decode()
//!     }
//! }
//! # }
//! ```
//!
//! <br>
//!
//! #### `#[musli(skip_encoding_if = <path>)]`
//!
//! This adds a condition to skip encoding a field entirely if the condition is
//! true. This is very commonly used to skip over encoding `Option<T>` fields.
//!
//! ```
//! use musli::{Encode, Decode};
//!
//! #[derive(Encode, Decode)]
//! struct Person {
//!     name: String,
//!     #[musli(skip_encoding_if = Option::is_none)]
//!     age: Option<u32>,
//! }
//! ```
//!
//! #### `#[musli(trace)]`
//!
//! This causes the field to use the [`DecodeTrace`] / [`EncodeTrace`] when
//! encoding the field. This is left optional for types where enabling tracing
//! for the field requires extra traits to be implemented, such as `HashMap<K,
//! V>` where we'd need `K` to implement `fmt::Display`.
//!
//! Without using the `trace` attribute below, the keys in the `values` field
//! would not be instrumented, so with a decoding error you'd see this:
//!
//! ```text
//! .values: not numeric (at bytes 15-16)
//! ```
//!
//! Instead of this (where `#[musli(trace)]` is enabled):
//!
//! ```text
//! .values[Hello]: not numeric (at bytes 15-16)
//! ```
//!
//! ```
//! use std::collections::HashMap;
//!
//! use musli::{Encode, Decode};
//!
//! #[derive(Encode, Decode)]
//! struct Collection {
//!     #[musli(trace)]
//!     values: HashMap<String, u32>,
//! }
//! ```
//!
//! <br>
//!
//! # Enum representations
//!
//! Müsli supports the following enum representations, which mimics the ones
//! supported by *serde*:
//!
//! * Externally tagged (*default*).
//! * Internally tagged when `#[musli(tag = ..)]` is specified on the enum.
//! * Adjacently tagged when both `#[musli(tag = ..)]` and `#[musli(content)]`
//!   are specified.
//!
//! <br>
//!
//! ## Externally tagged
//!
//! ```
//! # use musli::{Encode, Decode};
//! # #[derive(Encode, Decode)] struct Params;
//! # #[derive(Encode, Decode)] struct Value;
//! #[derive(Encode, Decode)]
//! enum Message {
//!     Request { id: String, method: String, params: Params },
//!     Response { id: String, result: Value },
//! }
//! ```
//!
//! When an enum is externally tagged it is represented by a single field
//! indicating the variant of the enum.
//!
//! ```json
//! {"Request": {"id": "...", "method": "...", "params": {...}}}
//! ```
//!
//! This is the most portable representation and is supported by most formats.
//! It has special support in the [`Encoder`] and [`Decoder`] traits through
//! [`Encoder::encode_variant`] and [`Decoder::decode_variant`].
//!
//! Conceptually this can be considered as a "pair", where the variant tag can
//! be extracted from the format before the variant is decoded.
//!
//! <br>
//!
//! ## Internally tagged
//!
//! ```
//! # use musli::{Encode, Decode};
//! # #[derive(Encode, Decode)] struct Params;
//! # #[derive(Encode, Decode)] struct Value;
//! #[derive(Encode, Decode)]
//! #[musli(name_all = "name", tag = "type")]
//! enum Message {
//!     Request { id: String, method: String, params: Params },
//!     Response { id: String, result: Value },
//! }
//! ```
//!
//! In JSON, the `Message::Request` would be represented as:
//!
//! ```json
//! {"type": "Request", "id": "...", "method": "...", "params": {...}}
//! ```
//!
//! This is only supported by formats which are *self descriptive*, which is a
//! requirement for the format to be buffered through [`Decoder::decode_buffer`].
//!
//! It is necessary to buffer the value, since we need to inspect the fields of
//! a map for the field corresponding to the `tag`, and then use this to
//! determine which decoder implementation to call.
//!
//! [`Binary`]: crate::mode::Binary
//! [`Text`]: crate::mode::Text
//! [`Decode`]: crate::Decode
//! [`DecodeBytes`]: crate::de::DecodeBytes
//! [`DecodePacked`]: crate::de::DecodePacked
//! [`Decoder::decode_buffer`]: crate::Decoder::decode_buffer
//! [`Decoder::decode_variant`]: crate::Decoder::decode_variant
//! [`Decoder`]: crate::Decoder
//! [`DecodeTrace`]: crate::de::DecodeTrace
//! [`Encode`]: crate::Encode
//! [`EncodeBytes`]: crate::en::EncodeBytes
//! [`EncodePacked`]: crate::en::EncodePacked
//! [`Encoder::encode_variant`]: crate::Encoder::encode_variant
//! [`Encoder`]: crate::Encoder
//! [`EncodeTrace`]: crate::en::EncodeTrace
//! [default mode]: crate::mode::Binary

// Parts of this documentation