structured-webauthn 0.1.0

Server-side Web Authentication (WebAuthn) Relying Party (RP) API in pure Rust: registration and authentication ceremonies, passkeys, no OpenSSL.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
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
extern crate alloc;
use super::{
    AllAcceptedCredentialsOptions, AuthTransports, AuthenticatorAttachment, AuthenticatorTransport,
    Challenge, CredentialId, CurrentUserDetailsOptions, Origin, SentChallenge,
};
use alloc::borrow::Cow;
use core::{
    fmt::{self, Formatter},
    marker::PhantomData,
};
use serde::{
    de::{Deserialize, Deserializer, Error, IgnoredAny, MapAccess, SeqAccess, Unexpected, Visitor},
    ser::{Serialize, SerializeSeq as _, SerializeStruct as _, Serializer},
};
/// [`"ble"`](https://www.w3.org/TR/webauthn-3/#dom-authenticatortransport-ble).
const BLE: &str = "ble";
/// [`"hybrid"`](https://www.w3.org/TR/webauthn-3/#dom-authenticatortransport-hybrid).
const HYBRID: &str = "hybrid";
/// [`"internal"`](https://www.w3.org/TR/webauthn-3/#dom-authenticatortransport-internal).
const INTERNAL: &str = "internal";
/// [`"nfc"`](https://www.w3.org/TR/webauthn-3/#dom-authenticatortransport-nfc).
const NFC: &str = "nfc";
/// [`"smart-card"`](https://www.w3.org/TR/webauthn-3/#dom-authenticatortransport-smart-card).
const SMART_CARD: &str = "smart-card";
/// [`"usb"`](https://www.w3.org/TR/webauthn-3/#dom-authenticatortransport-usb).
const USB: &str = "usb";
impl Serialize for AuthenticatorTransport {
    /// Serializes `self` as
    /// [`AuthenticatorTransport`](https://www.w3.org/TR/webauthn-3/#enumdef-authenticatortransport).
    ///
    /// # Examples
    ///
    /// ```
    /// # use structured_webauthn::response::AuthenticatorTransport;
    /// assert_eq!(
    ///     serde_json::to_string(&AuthenticatorTransport::Usb)?,
    ///     r#""usb""#
    /// );
    /// # Ok::<_, serde_json::Error>(())
    /// ```
    #[inline]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(match *self {
            Self::Ble => BLE,
            Self::Hybrid => HYBRID,
            Self::Internal => INTERNAL,
            Self::Nfc => NFC,
            Self::SmartCard => SMART_CARD,
            Self::Usb => USB,
        })
    }
}
impl<'de> Deserialize<'de> for AuthenticatorTransport {
    /// Deserializes [`prim@str`] based on
    /// [`AuthenticatorTransport`](https://www.w3.org/TR/webauthn-3/#enumdef-authenticatortransport).
    ///
    /// Note `"cable"` is also supported and will be interpreted as [`Self::Hybrid`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use structured_webauthn::response::AuthenticatorTransport;
    /// assert!(matches!(
    ///     serde_json::from_str::<AuthenticatorTransport>(r#""usb""#)?,
    ///     AuthenticatorTransport::Usb
    /// ));
    /// // Case matters.
    /// assert!(serde_json::from_str::<AuthenticatorTransport>(r#""Usb""#).is_err());
    /// # Ok::<_, serde_json::Error>(())
    /// ```
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        /// `Visitor` for `AuthenticatorTransport`.
        struct AuthenticatorTransportVisitor;
        impl Visitor<'_> for AuthenticatorTransportVisitor {
            type Value = AuthenticatorTransport;
            fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
                formatter.write_str("AuthenticatorTransport")
            }
            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
            where
                E: Error,
            {
                /// Legacy version of [`Self::Hybrid`].
                const CA_BLE: &str = "cable";
                match v {
                    BLE => Ok(AuthenticatorTransport::Ble),
                    CA_BLE | HYBRID => Ok(AuthenticatorTransport::Hybrid),
                    INTERNAL => Ok(AuthenticatorTransport::Internal),
                    NFC => Ok(AuthenticatorTransport::Nfc),
                    SMART_CARD => Ok(AuthenticatorTransport::SmartCard),
                    USB => Ok(AuthenticatorTransport::Usb),
                    _ => Err(E::invalid_value(
                        Unexpected::Str(v),
                        &format!(
                            "'{BLE}', '{CA_BLE}', '{HYBRID}', '{INTERNAL}', '{NFC}', '{SMART_CARD}', or '{USB}'"
                        )
                        .as_str(),
                    )),
                }
            }
        }
        deserializer.deserialize_str(AuthenticatorTransportVisitor)
    }
}
impl Serialize for AuthTransports {
    /// Serializes `self` based on
    /// [`transports`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialdescriptor-transports).
    ///
    /// # Examples
    ///
    /// ```
    /// # use structured_webauthn::response::{AuthTransports, AuthenticatorTransport};
    /// # #[cfg(feature = "custom")]
    /// assert_eq!(
    ///     serde_json::to_string(&AuthTransports::ALL)?,
    ///     r#"["ble","hybrid","internal","nfc","smart-card","usb"]"#
    /// );
    /// # Ok::<_, serde_json::Error>(())
    /// ```
    #[expect(clippy::unreachable, reason = "there is a bug, so we want to crash")]
    #[inline]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let count = usize::try_from(self.count())
            .unwrap_or_else(|_e| unreachable!("there is a bug in AuthenticatorTransports::count"));
        serializer.serialize_seq(Some(count)).and_then(|mut ser| {
            if self.contains(AuthenticatorTransport::Ble) {
                ser.serialize_element(&AuthenticatorTransport::Ble)
            } else {
                Ok(())
            }
            .and_then(|()| {
                if self.contains(AuthenticatorTransport::Hybrid) {
                    ser.serialize_element(&AuthenticatorTransport::Hybrid)
                } else {
                    Ok(())
                }
                .and_then(|()| {
                    if self.contains(AuthenticatorTransport::Internal) {
                        ser.serialize_element(&AuthenticatorTransport::Internal)
                    } else {
                        Ok(())
                    }
                    .and_then(|()| {
                        if self.contains(AuthenticatorTransport::Nfc) {
                            ser.serialize_element(&AuthenticatorTransport::Nfc)
                        } else {
                            Ok(())
                        }
                        .and_then(|()| {
                            if self.contains(AuthenticatorTransport::SmartCard) {
                                ser.serialize_element(&AuthenticatorTransport::SmartCard)
                            } else {
                                Ok(())
                            }
                            .and_then(|()| {
                                if self.contains(AuthenticatorTransport::Usb) {
                                    ser.serialize_element(&AuthenticatorTransport::Usb)
                                } else {
                                    Ok(())
                                }
                                .and_then(|()| ser.end())
                            })
                        })
                    })
                })
            })
        })
    }
}
impl<'de> Deserialize<'de> for AuthTransports {
    /// Deserializes a sequence based on
    /// [`transports`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialdescriptor-transports).
    ///
    /// # Examples
    ///
    /// ```
    /// # use structured_webauthn::response::{AuthTransports, AuthenticatorTransport};
    /// # #[cfg(feature = "custom")]
    /// assert_eq!(
    ///     serde_json::from_str::<AuthTransports>(
    ///         r#"["ble","hybrid","internal","nfc","smart-card","usb"]"#
    ///     )
    ///     ?.count(),
    ///     6
    /// );
    /// // Errors since `"foo"` is not valid.
    /// assert!(serde_json::from_str::<AuthTransports>(r#"["foo"]"#).is_err());
    /// # Ok::<_, serde_json::Error>(())
    /// ```
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        /// `Visitor` for `AuthTransports`.
        struct AuthTransportsVisitor;
        impl<'d> Visitor<'d> for AuthTransportsVisitor {
            type Value = AuthTransports;
            fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
                formatter.write_str("AuthTransports")
            }
            fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
            where
                A: SeqAccess<'d>,
            {
                let mut transports = AuthTransports::new();
                while let Some(val) = seq.next_element::<AuthenticatorTransport>()? {
                    transports = transports.add_transport(val);
                }
                Ok(transports)
            }
        }
        deserializer.deserialize_seq(AuthTransportsVisitor)
    }
}
impl<T: AsRef<[u8]>> Serialize for CredentialId<T> {
    /// Serializes `self` into a [`prim@str`] based on
    /// [`id`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialdescriptorjson-id).
    ///
    /// # Examples
    ///
    /// ```
    /// # use structured_webauthn::response::CredentialId;
    /// // `CredentialId::try_from` only exists when `custom` is enabled; and even then, it is
    /// // likely never needed since the `CredentialId` was originally sent from the client and is likely
    /// // stored in a database which would be fetched by `UserHandle` or `Authentication::raw_id`.
    /// # #[cfg(feature = "custom")]
    /// assert_eq!(
    ///     serde_json::to_string(&CredentialId::try_from(vec![0; 16].into_boxed_slice())?).unwrap(),
    ///     r#""AAAAAAAAAAAAAAAAAAAAAA""#
    /// );
    /// # Ok::<_, structured_webauthn::AggErr>(())
    ///```
    #[inline]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(base64url_nopad::encode(self.0.as_ref()).as_str())
    }
}
impl<'de> Deserialize<'de> for CredentialId<Box<[u8]>> {
    /// Deserializes [`prim@str`] based on
    /// [`id`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialdescriptorjson-id).
    ///
    /// # Examples
    ///
    /// ```
    /// # use structured_webauthn::response::CredentialId;
    /// # #[cfg(feature = "custom")]
    /// assert_eq!(
    ///     serde_json::from_str::<CredentialId<_>>(r#""AAAAAAAAAAAAAAAAAAAAAA""#).unwrap(),
    ///     CredentialId::try_from(vec![0; 16].into_boxed_slice())?
    /// );
    /// # Ok::<_, structured_webauthn::AggErr>(())
    ///```
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        /// `Visitor` for `CredentialId`.
        struct CredentialIdVisitor;
        impl Visitor<'_> for CredentialIdVisitor {
            type Value = CredentialId<Box<[u8]>>;
            fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
                formatter.write_str("CredentialId")
            }
            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
            where
                E: Error,
            {
                /// Minimum possible encoded length of a `CredentialId`.
                const MIN_LEN: usize = base64url_nopad::encode_len(super::CRED_ID_MIN_LEN);
                /// Maximum possible encoded length of a `CredentialId`.
                const MAX_LEN: usize = base64url_nopad::encode_len(super::CRED_ID_MAX_LEN);
                if (MIN_LEN..=MAX_LEN).contains(&v.len()) {
                    base64url_nopad::decode(v.as_bytes())
                        .map_err(E::custom)
                        .map(|c| CredentialId(c.into_boxed_slice()))
                } else {
                    Err(E::invalid_value(
                        Unexpected::Str(v),
                        &"16 to 1023 bytes encoded in base64url without padding",
                    ))
                }
            }
        }
        deserializer.deserialize_str(CredentialIdVisitor)
    }
}
impl<'de> Deserialize<'de> for AuthenticatorAttachment {
    /// Deserializes [`prim@str`] based on
    /// [`AuthenticatorAttachment`](https://www.w3.org/TR/webauthn-3/#enumdef-authenticatorattachment).
    ///
    /// # Examples
    ///
    /// ```
    /// # use structured_webauthn::response::AuthenticatorAttachment;
    /// assert!(matches!(
    ///     serde_json::from_str::<AuthenticatorAttachment>(r#""cross-platform""#)?,
    ///     AuthenticatorAttachment::CrossPlatform)
    /// );
    /// assert!(matches!(
    ///     serde_json::from_str::<AuthenticatorAttachment>(r#""platform""#)?,
    ///     AuthenticatorAttachment::Platform)
    /// );
    /// // Case matters.
    /// assert!(serde_json::from_str::<AuthenticatorAttachment>(r#""Platform""#).is_err());
    /// // `AuthenticatorAttachment::None` is not deserializable.
    /// assert!(serde_json::from_str::<AuthenticatorAttachment>(r#""""#).is_err());
    /// assert!(serde_json::from_str::<AuthenticatorAttachment>("null").is_err());
    /// assert!(serde_json::from_str::<AuthenticatorAttachment>(r#""none""#).is_err());
    /// assert!(serde_json::from_str::<AuthenticatorAttachment>(r#""None""#).is_err());
    /// # Ok::<_, serde_json::Error>(())
    ///```
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        /// `Visitor` for `AuthenticatorAttachment`.
        struct AuthenticatorAttachmentVisitor;
        impl Visitor<'_> for AuthenticatorAttachmentVisitor {
            type Value = AuthenticatorAttachment;
            fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
                formatter.write_str("AuthenticatorAttachment")
            }
            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
            where
                E: Error,
            {
                /// `"platform"`
                const PLATFORM: &str = "platform";
                /// `"cross-platform"`
                const CROSS_PLATFORM: &str = "cross-platform";
                match v {
                    PLATFORM => Ok(AuthenticatorAttachment::Platform),
                    CROSS_PLATFORM => Ok(AuthenticatorAttachment::CrossPlatform),
                    _ => Err(E::invalid_value(
                        Unexpected::Str(v),
                        &format!("'{PLATFORM}' or '{CROSS_PLATFORM}'").as_str(),
                    )),
                }
            }
        }
        deserializer.deserialize_str(AuthenticatorAttachmentVisitor)
    }
}
/// Container of data that was encoded in base64url.
pub(crate) struct Base64DecodedVal(pub Vec<u8>);
impl<'de> Deserialize<'de> for Base64DecodedVal {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        /// `Visitor` for `Base64DecodedVal`.
        struct Base64DecodedValVisitor;
        impl Visitor<'_> for Base64DecodedValVisitor {
            type Value = Base64DecodedVal;
            fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
                formatter.write_str("base64url-encoded data")
            }
            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
            where
                E: Error,
            {
                base64url_nopad::decode(v.as_bytes())
                    .map_err(E::custom)
                    .map(Base64DecodedVal)
            }
        }
        deserializer.deserialize_str(Base64DecodedValVisitor)
    }
}
impl<'de> Deserialize<'de> for SentChallenge {
    /// Deserializes `[u8]` or [`prim@str`] based on
    /// [`challenge`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-challenge).
    ///
    /// Specifically a `[u8]` or `str` is base64url-decoded and interpreted as a little-endian
    /// `u128`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use structured_webauthn::response::SentChallenge;
    /// assert_eq!(
    ///     serde_json::from_slice::<SentChallenge>(br#""AAAAAAAAAAAAAAAAAAAAAA""#)?,
    ///     SentChallenge(0)
    /// );
    /// # Ok::<_, serde_json::Error>(())
    ///```
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        /// `Visitor` for `SentChallenge`.
        struct ChallengeVisitor;
        impl Visitor<'_> for ChallengeVisitor {
            type Value = SentChallenge;
            fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
                formatter.write_str(
                    "base64 encoding of the 16-byte challenge in a URL safe way without padding",
                )
            }
            fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
            where
                E: Error,
            {
                if v.len() == Challenge::BASE64_LEN {
                    let mut data = [0; 16];
                    base64url_nopad::decode_buffer_exact(v, data.as_mut_slice())
                        .map_err(E::custom)
                        .map(|()| SentChallenge::from_array(data))
                } else {
                    Err(E::invalid_value(
                        Unexpected::Bytes(v),
                        &"22 bytes encoded in base64url without padding",
                    ))
                }
            }
            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
            where
                E: Error,
            {
                self.visit_bytes(v.as_bytes())
            }
        }
        deserializer.deserialize_bytes(ChallengeVisitor)
    }
}
impl<'de: 'a, 'a> Deserialize<'de> for Origin<'a> {
    /// Deserializes [`prim@str`] by borrowing the data when possible.
    ///
    /// # Examples
    ///
    /// ```
    /// # extern crate alloc;
    /// # use alloc::borrow::Cow;
    /// # use structured_webauthn::response::Origin;
    /// let origin_borrowed = "https://example.com";
    /// let origin_owned = "\\\\https://example.com";
    /// assert!(
    ///     matches!(serde_json::from_str::<Origin<'_>>(format!("\"{origin_borrowed}\"").as_str())?.0, Cow::Borrowed(val) if val == origin_borrowed)
    /// );
    /// assert!(
    ///     matches!(serde_json::from_str::<Origin<'_>>(format!("\"{origin_owned}\"").as_str())?.0, Cow::Owned(val) if *val.as_bytes() == origin_owned.as_bytes()[1..])
    /// );
    /// # Ok::<_, serde_json::Error>(())
    ///```
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        /// `Visitor` for `Origin`.
        struct OriginVisitor<'b>(PhantomData<fn() -> &'b ()>);
        impl<'d: 'b, 'b> Visitor<'d> for OriginVisitor<'b> {
            type Value = Origin<'b>;
            fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
                formatter.write_str("Origin")
            }
            fn visit_borrowed_str<E>(self, v: &'d str) -> Result<Self::Value, E>
            where
                E: Error,
            {
                Ok(Origin(Cow::Borrowed(v)))
            }
            fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
            where
                E: Error,
            {
                Ok(Origin(Cow::Owned(v)))
            }
            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
            where
                E: Error,
            {
                self.visit_string(v.to_owned())
            }
        }
        deserializer.deserialize_str(OriginVisitor(PhantomData))
    }
}
/// `trait` that returns an empty instance of `Self`.
pub(super) trait ClientExtensions: Sized {
    /// Returns an empty instance of `Self`.
    fn empty() -> Self;
}
/// Response for both registration and authentication ceremonies.
///
/// [`Self::raw_id`] is always `Some` when `!RELAXED` or `!REG`.
///
/// `RELAXED` and `REG` are used purely for deserialization purposes.
pub(super) struct PublicKeyCredential<const RELAXED: bool, const REG: bool, AuthResp, Ext> {
    /// [`rawId`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredential-rawid).
    pub id: Option<CredentialId<Box<[u8]>>>,
    /// [`response`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredential-response).
    pub response: AuthResp,
    /// [`authenticatorAttachment`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredential-authenticatorattachment).
    pub authenticator_attachment: AuthenticatorAttachment,
    /// [`getClientExtensionResults`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredential-getclientextensionresults).
    pub client_extension_results: Ext,
}
/// Deserializes the value for type.
pub(crate) struct Type;
impl<'e> Deserialize<'e> for Type {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'e>,
    {
        /// `Visitor` for `Type`.
        struct TypeVisitor;
        impl Visitor<'_> for TypeVisitor {
            type Value = Type;
            fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
                formatter.write_str(PUBLIC_KEY)
            }
            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
            where
                E: Error,
            {
                if v == PUBLIC_KEY {
                    Ok(Type)
                } else {
                    Err(E::invalid_value(Unexpected::Str(v), &PUBLIC_KEY))
                }
            }
        }
        deserializer.deserialize_str(TypeVisitor)
    }
}
/// `Visitor` for `PublicKeyCredential`.
///
/// When `!RELAXED`, `REG` is ignored and all fields must exist and unknown fields are not allowed.
/// When `RELAXED`, unknown fields are ignored.
/// When `RELAXED` and `REG`, only `response` is required.
/// When `RELAXED` and `!REG`, only `id` and `response` are required.
struct PublicKeyCredentialVisitor<const RELAXED: bool, const REG: bool, R, E>(
    pub PhantomData<fn() -> (R, E)>,
);
impl<'d, const REL: bool, const REGI: bool, R, E> Visitor<'d>
    for PublicKeyCredentialVisitor<REL, REGI, R, E>
where
    R: Deserialize<'d>,
    E: for<'a> Deserialize<'a> + ClientExtensions,
{
    type Value = PublicKeyCredential<REL, REGI, R, E>;
    fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
        formatter.write_str("PublicKeyCredential")
    }
    #[expect(
        clippy::too_many_lines,
        reason = "rather hide all the internal logic instead instead of moving into an outer scope"
    )]
    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
    where
        A: MapAccess<'d>,
    {
        /// `PublicKeyCredentialJSON` fields.
        enum Field<const IGNORE_UNKNOWN: bool> {
            /// `id`.
            Id,
            /// `type`.
            Type,
            /// `rawId`.
            RawId,
            /// `response`.
            Response,
            /// `authenticatorAttachment`.
            AuthenticatorAttachment,
            /// `clientExtensionResults`.
            ClientExtensionResults,
            /// Unknown field.
            Other,
        }
        impl<'e, const IGNORE: bool> Deserialize<'e> for Field<IGNORE> {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: Deserializer<'e>,
            {
                /// `Visitor` for `Field`.
                struct FieldVisitor<const IGNORE_UNKNOWN: bool>;
                impl<const IGN: bool> Visitor<'_> for FieldVisitor<IGN> {
                    type Value = Field<IGN>;
                    fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
                        write!(
                            formatter,
                            "'{ID}', '{TYPE}', '{RAW_ID}', '{RESPONSE}', '{AUTHENTICATOR_ATTACHMENT}', or '{CLIENT_EXTENSION_RESULTS}'"
                        )
                    }
                    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
                    where
                        E: Error,
                    {
                        match v {
                            ID => Ok(Field::Id),
                            TYPE => Ok(Field::Type),
                            RAW_ID => Ok(Field::RawId),
                            RESPONSE => Ok(Field::Response),
                            AUTHENTICATOR_ATTACHMENT => Ok(Field::AuthenticatorAttachment),
                            CLIENT_EXTENSION_RESULTS => Ok(Field::ClientExtensionResults),
                            _ => {
                                if IGN {
                                    Ok(Field::Other)
                                } else {
                                    Err(E::unknown_field(v, REG_FIELDS))
                                }
                            }
                        }
                    }
                }
                deserializer.deserialize_identifier(FieldVisitor::<IGNORE>)
            }
        }
        let mut opt_id = None;
        let mut typ = false;
        let mut raw = None;
        let mut resp = None;
        let mut attach = None;
        let mut ext = None;
        while let Some(key) = map.next_key::<Field<REL>>()? {
            match key {
                Field::Id => {
                    if opt_id.is_some() {
                        return Err(Error::duplicate_field(ID));
                    }
                    opt_id = map.next_value::<CredentialId<_>>().map(Some)?;
                }
                Field::Type => {
                    if typ {
                        return Err(Error::duplicate_field(TYPE));
                    }
                    typ = map.next_value::<Type>().map(|_| true)?;
                }
                Field::RawId => {
                    if raw.is_some() {
                        return Err(Error::duplicate_field(RAW_ID));
                    }
                    raw = map.next_value::<CredentialId<_>>().map(Some)?;
                }
                Field::Response => {
                    if resp.is_some() {
                        return Err(Error::duplicate_field(RESPONSE));
                    }
                    resp = map.next_value::<R>().map(Some)?;
                }
                Field::AuthenticatorAttachment => {
                    if attach.is_some() {
                        return Err(Error::duplicate_field(AUTHENTICATOR_ATTACHMENT));
                    }
                    attach = map.next_value().map(Some)?;
                }
                Field::ClientExtensionResults => {
                    if ext.is_some() {
                        return Err(Error::duplicate_field(CLIENT_EXTENSION_RESULTS));
                    }
                    ext = map.next_value::<Option<E>>().map(Some)?;
                }
                Field::Other => map.next_value::<IgnoredAny>().map(|_| ())?,
            }
        }
        resp.ok_or_else(|| Error::missing_field(RESPONSE))
            .and_then(|response| {
                opt_id.map_or_else(
                    || {
                        if REL && REGI {
                            Ok(None)
                        } else {
                            Err(Error::missing_field(ID))
                        }
                    },
                    |id| Ok(Some(id)),
                )
                .and_then(|id| {
                    raw.map_or_else(
                        || {
                            if REL {
                                Ok(())
                            } else {
                                Err(Error::missing_field(RAW_ID))
                            }
                        },
                        |raw_id| {
                            id.as_ref().map_or_else(
                                || Ok(()),
                                |i| {
                                    if raw_id == i {
                                        Ok(())
                                    } else {
                                        Err(Error::invalid_value(
                                            Unexpected::Bytes(raw_id.as_ref()),
                                            &format!("{ID} and {RAW_ID} to match: {i:?}").as_str(),
                                        ))
                                    }
                                },
                            )
                        }
                    )
                    .and_then(|()| {
                        ext.ok_or(false).and_then(|opt_ext| opt_ext.ok_or(true)).map_or_else(
                            |flag| {
                                if REL {
                                    Ok(E::empty())
                                } else if flag {
                                    Err(Error::invalid_type(Unexpected::Other("null"), &format!("{CLIENT_EXTENSION_RESULTS} to be a map of allowed client extensions").as_str()))
                                } else {
                                    Err(Error::missing_field(CLIENT_EXTENSION_RESULTS))
                                }
                            },
                            Ok
                        )
                        .and_then(|client_extension_results| {
                            if typ || REL {
                                Ok(PublicKeyCredential {
                                    id,
                                    response,
                                    authenticator_attachment: attach.flatten().unwrap_or(AuthenticatorAttachment::None),
                                    client_extension_results,
                                })
                            } else {
                                Err(Error::missing_field(TYPE))
                            }
                        })
                    })
                })
            })
    }
}
/// `"id"`.
const ID: &str = "id";
/// `"type"`.
const TYPE: &str = "type";
/// `"rawId"`.
const RAW_ID: &str = "rawId";
/// `"response"`.
const RESPONSE: &str = "response";
/// `"authenticatorAttachment"`.
const AUTHENTICATOR_ATTACHMENT: &str = "authenticatorAttachment";
/// `"clientExtensionResults"`.
const CLIENT_EXTENSION_RESULTS: &str = "clientExtensionResults";
/// `"public-key"`.
const PUBLIC_KEY: &str = "public-key";
/// Fields for `PublicKeyCredentialJSON`.
const REG_FIELDS: &[&str; 6] = &[
    ID,
    TYPE,
    RAW_ID,
    RESPONSE,
    AUTHENTICATOR_ATTACHMENT,
    CLIENT_EXTENSION_RESULTS,
];
impl<'de, const REL: bool, const REGI: bool, R, E> Deserialize<'de>
    for PublicKeyCredential<REL, REGI, R, E>
where
    R: Deserialize<'de>,
    E: for<'a> Deserialize<'a> + ClientExtensions,
{
    /// Deserializes a `struct` based on
    /// [`PublicKeyCredentialJSON`](https://www.w3.org/TR/webauthn-3/#typedefdef-publickeycredentialjson).
    ///
    /// `REL` iff unknown fields should be ignored and not cause an error.
    /// `REGI` iff `Self` is from a registration ceremony.
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_struct(
            "PublicKeyCredential",
            REG_FIELDS,
            PublicKeyCredentialVisitor::<REL, REGI, _, _>(PhantomData),
        )
    }
}
use super::UserHandle;
impl<const USER_LEN: usize> Serialize for AllAcceptedCredentialsOptions<'_, '_, USER_LEN>
where
    UserHandle<USER_LEN>: Serialize,
{
    /// Serializes `self` to conform with
    /// [`AllAcceptedCredentialsOptions`](https://www.w3.org/TR/webauthn-3/#dictdef-allacceptedcredentialsoptions).
    ///
    /// # Examples
    ///
    /// ```
    /// # use core::str::FromStr;
    /// # #[cfg(feature = "bin")]
    /// # use structured_webauthn::bin::Decode;
    /// # use structured_webauthn::{
    /// #     request::{register::{UserHandle, USER_HANDLE_MIN_LEN}, AsciiDomain, RpId},
    /// #     response::{error::CredentialIdErr, AllAcceptedCredentialsOptions, CredentialId},
    /// # };
    /// /// Retrieves the `CredentialId`s associated with `user_id` from the database.
    /// # #[cfg(all(feature = "bin", feature = "custom"))]
    /// fn get_credential_ids(user_id: UserHandle<USER_HANDLE_MIN_LEN>) -> Result<Vec<CredentialId<Box<[u8]>>>, CredentialIdErr> {
    ///     // â‹®
    /// #     CredentialId::decode(vec![0; 16].into_boxed_slice()).map(|cred_id| vec![cred_id])
    /// }
    /// /// Retrieves the `UserHandle` from a session cookie.
    /// # #[cfg(feature = "custom")]
    /// fn get_user_handle() -> UserHandle<USER_HANDLE_MIN_LEN> {
    ///     // â‹®
    /// #     [0].into()
    /// }
    /// # #[cfg(feature = "custom")]
    /// let user_id = get_user_handle();
    /// # #[cfg(all(feature = "bin", feature = "custom"))]
    /// assert_eq!(
    ///     serde_json::to_string(&AllAcceptedCredentialsOptions {
    ///         rp_id: &RpId::Domain(AsciiDomain::try_from("example.com".to_owned())?),
    ///         user_id: &user_id,
    ///         all_accepted_credential_ids: get_credential_ids(user_id)?,
    ///     })
    ///     .unwrap(),
    ///     r#"{"rpId":"example.com","userId":"AA","allAcceptedCredentialIds":["AAAAAAAAAAAAAAAAAAAAAA"]}"#
    /// );
    /// # Ok::<_, structured_webauthn::AggErr>(())
    /// ```
    #[inline]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer
            .serialize_struct("AllAcceptedCredentialsOptions", 3)
            .and_then(|mut ser| {
                ser.serialize_field("rpId", self.rp_id).and_then(|()| {
                    ser.serialize_field("userId", &self.user_id).and_then(|()| {
                        ser.serialize_field(
                            "allAcceptedCredentialIds",
                            &self.all_accepted_credential_ids,
                        )
                        .and_then(|()| ser.end())
                    })
                })
            })
    }
}
impl<const LEN: usize> Serialize for CurrentUserDetailsOptions<'_, '_, '_, '_, LEN>
where
    UserHandle<LEN>: Serialize,
{
    /// Serializes `self` to conform with
    /// [`CurrentUserDetailsOptions`](https://www.w3.org/TR/webauthn-3/#dictdef-currentuserdetailsoptions).
    ///
    /// # Examples
    ///
    /// ```
    /// # use core::str::FromStr;
    /// # #[cfg(feature = "bin")]
    /// # use structured_webauthn::bin::Decode;
    /// # use structured_webauthn::{
    /// #     request::{register::{PublicKeyCredentialUserEntity, UserHandle, USER_HANDLE_MIN_LEN}, AsciiDomain, RpId},
    /// #     response::CurrentUserDetailsOptions,
    /// #     AggErr,
    /// # };
    /// /// Retrieves the `PublicKeyCredentialUserEntity` info associated with `user_id` from the database.
    /// # #[cfg(feature = "bin")]
    /// fn get_user_info(user_id: UserHandle<USER_HANDLE_MIN_LEN>) -> Result<(String, String), AggErr> {
    ///     // â‹®
    /// #     Ok(("foo".to_owned(), "foo".to_owned()))
    /// }
    /// /// Retrieves the `UserHandle` from a session cookie.
    /// # #[cfg(feature = "custom")]
    /// fn get_user_handle() -> UserHandle<USER_HANDLE_MIN_LEN> {
    ///     // â‹®
    /// #     [0].into()
    /// }
    /// # #[cfg(feature = "custom")]
    /// let user_handle = get_user_handle();
    /// # #[cfg(all(feature = "bin", feature = "custom"))]
    /// let (name, display_name) = get_user_info(user_handle)?;
    /// # #[cfg(all(feature = "bin", feature = "custom"))]
    /// assert_eq!(
    ///     serde_json::to_string(&CurrentUserDetailsOptions {
    ///         rp_id: &RpId::Domain(AsciiDomain::try_from("example.com".to_owned())?),
    ///         user: PublicKeyCredentialUserEntity { name: &name, id: &user_handle, display_name: &display_name, },
    ///     })
    ///     .unwrap(),
    ///     r#"{"rpId":"example.com","userId":"AA","name":"foo","displayName":"foo"}"#
    /// );
    /// # Ok::<_, AggErr>(())
    /// ```
    #[inline]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer
            .serialize_struct("CurrentUserDetailsOptions", 4)
            .and_then(|mut ser| {
                ser.serialize_field("rpId", self.rp_id).and_then(|()| {
                    ser.serialize_field("userId", &self.user.id).and_then(|()| {
                        ser.serialize_field("name", &self.user.name).and_then(|()| {
                            ser.serialize_field("displayName", &self.user.display_name)
                                .and_then(|()| ser.end())
                        })
                    })
                })
            })
    }
}
/// JSON `null`.
pub(crate) struct Null;
impl<'de> Deserialize<'de> for Null {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        /// `Visitor` for `Null`.
        struct NullVisitor;
        impl Visitor<'_> for NullVisitor {
            type Value = Null;
            fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
                formatter.write_str("null")
            }
            fn visit_none<E>(self) -> Result<Self::Value, E>
            where
                E: Error,
            {
                Ok(Null)
            }
        }
        deserializer.deserialize_option(NullVisitor)
    }
}
/// [`AuthenticationExtensionsPRFValues`](https://www.w3.org/TR/webauthn-3/#dictdef-authenticationextensionsprfvalues).
pub(super) struct AuthenticationExtensionsPrfValues;
/// `Visitor` for `AuthenticationExtensionsPrfValues`.
///
/// Unknown fields are ignored iff `RELAXED`.`first` must always exist if `second` does.
/// `first` and `second` must be `null` if they exist. `first` must exist iff `!RELAXED`.
pub(super) struct AuthenticationExtensionsPrfValuesVisitor<const RELAXED: bool>;
impl<'d, const R: bool> Visitor<'d> for AuthenticationExtensionsPrfValuesVisitor<R> {
    type Value = AuthenticationExtensionsPrfValues;
    fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
        formatter.write_str("AuthenticationExtensionsPrfValues")
    }
    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
    where
        A: MapAccess<'d>,
    {
        /// Fields.
        enum Field<const IGNORE_UNKNOWN: bool> {
            /// `first` field.
            First,
            /// `second` field.
            Second,
            /// Unknown field.
            Other,
        }
        impl<'e, const I: bool> Deserialize<'e> for Field<I> {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: Deserializer<'e>,
            {
                /// `Visitor` for `Field`.
                ///
                /// Unknown fields are ignored iff `IGNORE_UNKNOWN`.
                struct FieldVisitor<const IGNORE_UNKNOWN: bool>;
                impl<const IG: bool> Visitor<'_> for FieldVisitor<IG> {
                    type Value = Field<IG>;
                    fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
                        write!(formatter, "'{FIRST}' or '{SECOND}'")
                    }
                    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
                    where
                        E: Error,
                    {
                        match v {
                            FIRST => Ok(Field::First),
                            SECOND => Ok(Field::Second),
                            _ => {
                                if IG {
                                    Ok(Field::Other)
                                } else {
                                    Err(E::unknown_field(v, PRF_VALUES_FIELDS))
                                }
                            }
                        }
                    }
                }
                deserializer.deserialize_identifier(FieldVisitor)
            }
        }
        let mut first = None;
        let mut second = None;
        while let Some(key) = map.next_key::<Field<R>>()? {
            match key {
                Field::First => {
                    if first.is_some() {
                        return Err(Error::duplicate_field(FIRST));
                    }
                    first = map.next_value::<Null>().map(Some)?;
                }
                Field::Second => {
                    if second.is_some() {
                        return Err(Error::duplicate_field(SECOND));
                    }
                    second = map.next_value::<Null>().map(Some)?;
                }
                Field::Other => map.next_value::<IgnoredAny>().map(|_| ())?,
            }
        }
        if first.is_some() || (R && second.is_none()) {
            Ok(AuthenticationExtensionsPrfValues)
        } else {
            Err(Error::missing_field(FIRST))
        }
    }
}
/// `"first"`
const FIRST: &str = "first";
/// `"second"`
const SECOND: &str = "second";
/// `AuthenticationExtensionsPrfValues` fields.
pub(super) const PRF_VALUES_FIELDS: &[&str; 2] = &[FIRST, SECOND];
impl<'de> Deserialize<'de> for AuthenticationExtensionsPrfValues {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_struct(
            "AuthenticationExtensionsPrfValues",
            PRF_VALUES_FIELDS,
            AuthenticationExtensionsPrfValuesVisitor::<false>,
        )
    }
}
/// [`AuthenticationExtensionsPRFOutputs`](https://www.w3.org/TR/webauthn-3/#dictdef-authenticationextensionsprfoutputs).
///
/// `RELAXED` iff unknown fields are ignored.
///
/// `REGISTRATION` iff
/// [`enabled`](https://www.w3.org/TR/webauthn-3/#dom-authenticationextensionsprfoutputs-enabled)
/// is required (and must not be `null`); otherwise it's forbidden.
///
/// The contained `Option` is `Some` iff `REGISTRATION`.
pub(super) struct AuthenticationExtensionsPrfOutputsHelper<
    const RELAXED: bool,
    const REGISTRATION: bool,
    Prf,
>(pub Option<bool>, pub PhantomData<fn() -> Prf>);
/// `Visitor` for `AuthenticationExtensionsPrfOutputs`.
///
/// Unknown fields are ignored iff `RELAXED`.`enabled` must exist and not be `null` iff `REGISTRATION`.
struct AuthenticationExtensionsPrfOutputsVisitor<const RELAXED: bool, const REGISTRATION: bool, Prf>(
    PhantomData<fn() -> Prf>,
);
impl<'d, const REL: bool, const REG: bool, Prf> Visitor<'d>
    for AuthenticationExtensionsPrfOutputsVisitor<REL, REG, Prf>
where
    Prf: for<'a> Deserialize<'a>,
{
    type Value = AuthenticationExtensionsPrfOutputsHelper<REL, REG, Prf>;
    fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
        formatter.write_str("AuthenticationExtensionsPrfOutputs")
    }
    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
    where
        A: MapAccess<'d>,
    {
        /// Fields.
        enum Field<const IGNORE_UNKNOWN: bool, const REGI: bool> {
            /// `enabled` field.
            Enabled,
            /// `results` field.
            Results,
            /// Unknown field.
            Other,
        }
        impl<'e, const I: bool, const R: bool> Deserialize<'e> for Field<I, R> {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: Deserializer<'e>,
            {
                /// `Visitor` for `Field`.
                ///
                /// Unknown fields are ignored iff `IGNORE_UNKNOWN`.
                /// `enabled` is allowed to exist iff `REGI`.
                struct FieldVisitor<const IGNORE_UNKNOWN: bool, const REGI: bool>;
                impl<const IG: bool, const RE: bool> Visitor<'_> for FieldVisitor<IG, RE> {
                    type Value = Field<IG, RE>;
                    fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
                        if RE {
                            write!(formatter, "'{ENABLED}' or '{RESULTS}'")
                        } else {
                            write!(formatter, "'{RESULTS}'")
                        }
                    }
                    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
                    where
                        E: Error,
                    {
                        match v {
                            ENABLED => {
                                if RE {
                                    Ok(Field::Enabled)
                                } else {
                                    Err(E::unknown_field(v, PRF_AUTH_OUTPUTS_FIELDS))
                                }
                            }
                            RESULTS => Ok(Field::Results),
                            _ => {
                                if IG {
                                    Ok(Field::Other)
                                } else if RE {
                                    Err(E::unknown_field(v, PRF_REG_OUTPUTS_FIELDS))
                                } else {
                                    Err(E::unknown_field(v, PRF_AUTH_OUTPUTS_FIELDS))
                                }
                            }
                        }
                    }
                }
                deserializer.deserialize_identifier(FieldVisitor)
            }
        }
        let mut enabled = None;
        let mut results = None;
        while let Some(key) = map.next_key::<Field<REL, REG>>()? {
            match key {
                Field::Enabled => {
                    if enabled.is_some() {
                        return Err(Error::duplicate_field(ENABLED));
                    }
                    enabled = map.next_value().map(Some)?;
                }
                Field::Results => {
                    if results.is_some() {
                        return Err(Error::duplicate_field(RESULTS));
                    }
                    results = map.next_value::<Option<Prf>>().map(Some)?;
                }
                Field::Other => map.next_value::<IgnoredAny>().map(|_| ())?,
            }
        }
        if REG {
            enabled.ok_or_else(|| Error::missing_field(ENABLED)).and_then(|e| {
                if e || results.is_none() {
                    Ok(())
                } else {
                    Err(Error::custom("prf must not have 'results', including a null 'results', if 'enabled' is false"))
                }
            })
        } else {
            Ok(())
        }.map(|()| AuthenticationExtensionsPrfOutputsHelper(enabled, PhantomData))
    }
}
/// `"enabled"`
const ENABLED: &str = "enabled";
/// `"results"`
const RESULTS: &str = "results";
/// `AuthenticationExtensionsPrfOutputs` field during registration.
const PRF_REG_OUTPUTS_FIELDS: &[&str; 2] = &[ENABLED, RESULTS];
/// `AuthenticationExtensionsPrfOutputs` field during authentication.
const PRF_AUTH_OUTPUTS_FIELDS: &[&str; 1] = &[RESULTS];
impl<'de, const REL: bool, const REG: bool, Prf> Deserialize<'de>
    for AuthenticationExtensionsPrfOutputsHelper<REL, REG, Prf>
where
    for<'a> Prf: Deserialize<'a>,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_struct(
            "AuthenticationExtensionsPrfOutputs",
            if REG {
                PRF_REG_OUTPUTS_FIELDS
            } else {
                PRF_AUTH_OUTPUTS_FIELDS
            },
            AuthenticationExtensionsPrfOutputsVisitor::<REL, REG, Prf>(PhantomData),
        )
    }
}