credential-exchange-format 0.4.0

Credential Exchange Format (CXF) rust types
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
use std::{borrow::Cow, fmt, str};

use chrono::{Month, NaiveDate};
use serde::{
    de::{
        value::{StrDeserializer, StringDeserializer},
        DeserializeOwned, Visitor,
    },
    ser::SerializeStruct,
    Deserialize, Serialize,
};

use crate::{B64Url, Extension};

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct EditableField<T, E = ()> {
    /// A unique identifier for the [EditableField] which is machine generated and an opaque byte
    /// sequence with a maximum size of 64 bytes. It SHOULD NOT be displayed to the user.
    pub id: Option<B64Url>,
    /// This member contains the fieldType defined by the user.
    pub value: Expected<T>,
    /// This member contains a user facing value describing the value stored. This value MAY be
    /// user defined.
    pub label: Option<String>,
    /// This member permits the exporting provider to add additional information associated to this
    /// [EditableField]. This MAY be used to provide an exchange where a minimal amount of
    /// information is lost.
    pub extensions: Option<Vec<Extension<E>>>,
}

/// A field of the incorrect type that was passed instead of an expected field type.
///
/// For example if the spec requires a `string` type, but the user passes in
/// `concealed-string` instead.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum UnexpectedField {
    String(EditableFieldString),
    ConcealedString(EditableFieldConcealedString),
    Boolean(EditableFieldBoolean),
    Date(EditableFieldDate),
    YearMonth(EditableFieldYearMonth),
    SubdivisionCode(EditableFieldSubdivisionCode),
    CountryCode(EditableFieldCountryCode),
    WifiNetworkSecurityType(EditableFieldWifiNetworkSecurityType),
    Email(EditableFieldEmail),
    Number(EditableFieldNumber),
    Unknown {
        /// The unexpected field type.
        field_type: FieldType,
        /// The Unknown string passed as a value of type `field_type`.
        ///
        /// Example: a `date` that doesn't conform to `yyyy-mm-dd`.
        value: String,
    },
}

impl UnexpectedField {
    /// Returns the [`FieldType`] for this value.
    #[inline]
    pub fn field_type(&self) -> FieldType {
        match self {
            Self::String(_) => EditableFieldString::field_type(),
            Self::ConcealedString(_) => EditableFieldConcealedString::field_type(),
            Self::Boolean(_) => EditableFieldBoolean::field_type(),
            Self::Date(_) => EditableFieldDate::field_type(),
            Self::YearMonth(_) => EditableFieldYearMonth::field_type(),
            Self::SubdivisionCode(_) => EditableFieldSubdivisionCode::field_type(),
            Self::CountryCode(_) => EditableFieldCountryCode::field_type(),
            Self::WifiNetworkSecurityType(_) => EditableFieldWifiNetworkSecurityType::field_type(),
            Self::Email(_) => EditableFieldEmail::field_type(),
            Self::Number(_) => EditableFieldNumber::field_type(),
            Self::Unknown { field_type, .. } => field_type.clone(),
        }
    }
}

impl From<UnexpectedField> for String {
    #[inline]
    fn from(value: UnexpectedField) -> Self {
        match value {
            UnexpectedField::String(v) => v.0,
            UnexpectedField::ConcealedString(v) => v.0,
            UnexpectedField::WifiNetworkSecurityType(v) => v.into(),
            UnexpectedField::SubdivisionCode(v) => v.0,
            UnexpectedField::CountryCode(v) => v.0,
            UnexpectedField::Unknown { value: v, .. } => v,
            UnexpectedField::Email(v) => v.0,
            UnexpectedField::Number(v) => v.0,
            UnexpectedField::Boolean(v) => v.into(),
            UnexpectedField::Date(v) => v.into(),
            UnexpectedField::YearMonth(v) => v.into(),
        }
    }
}

/// Holds onto an editable field, and records whether it was an expected or unexpected field.
#[derive(Clone, Debug, PartialEq, Eq)]
enum ExpectedInner<T> {
    /// The field we found had the same field type we expected.
    Expected(T),
    /// The field we found had a different field type than what we expected.
    Unexpected(UnexpectedField),
}

/// Holds onto an editable field, and records whether it was an expected or unexpected field.
///
/// This is used as a type-safe wrapper around an editable field to encode expectations around
/// the type of field we are expecting in a credential.
///
/// This can only be instantiated via the exposed `From` implementation so that newly
/// constructed credentials remain spec-compliant with regards to their fields.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Expected<T>(ExpectedInner<T>);

impl<T> Expected<T>
where
    T: EditableFieldType,
{
    /// Returns the real [`FieldType`] of this field.
    #[inline]
    pub fn field_type(&self) -> FieldType {
        match &self.0 {
            ExpectedInner::Expected(_) => T::field_type(),
            ExpectedInner::Unexpected(f) => f.field_type(),
        }
    }

    /// Tries to return the field type we were expecting.
    #[inline]
    pub fn as_expected(&self) -> Result<&T, &UnexpectedField> {
        match &self.0 {
            ExpectedInner::Expected(t) => Ok(t),
            ExpectedInner::Unexpected(f) => Err(f),
        }
    }

    /// Tries to return the field type we were expecting as an owned type.
    #[inline]
    pub fn into_expected(self) -> Result<T, UnexpectedField> {
        match self.0 {
            ExpectedInner::Expected(t) => Ok(t),
            ExpectedInner::Unexpected(f) => Err(f),
        }
    }
}

impl<T> From<T> for Expected<T> {
    #[inline]
    fn from(t: T) -> Self {
        Self(ExpectedInner::Expected(t))
    }
}

impl<T> From<Expected<T>> for String
where
    T: Into<String>,
{
    #[inline]
    fn from(value: Expected<T>) -> Self {
        match value.0 {
            ExpectedInner::Expected(t) => t.into(),
            ExpectedInner::Unexpected(f) => f.into(),
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub enum FieldType {
    /// A UTF-8 encoded string value which is unconcealed and does not have a specified format.
    String,
    /// A UTF-8 encoded string value which should be considered secret and not displayed unless the
    /// user explicitly requests it.
    ConcealedString,
    /// A UTF-8 encoded string value which follows the format specified in
    /// [RFC5322](https://www.rfc-editor.org/rfc/rfc5322#section-3.4). This field SHOULD be
    /// unconcealed.
    Email,
    /// A stringified numeric value which is unconcealed.
    Number,
    /// A boolean value which is unconcealed. It MUST be of the values "true" or "false".
    Boolean,
    /// A string value representing a calendar date which follows the format specified in
    /// [RFC3339](https://www.rfc-editor.org/rfc/rfc3339).
    Date,
    /// A string value representing a calendar date which follows the date-fullyear "-" date-month
    /// pattern as established in [RFC3339](https://www.rfc-editor.org/rfc/rfc3339) Appendix A.
    /// This is equivalent to the `YYYY-MM` format specified in ISO8601.
    YearMonth,
    /// A string value representing a value that SHOULD be a member of WIFINetworkSecurityType.
    WifiNetworkSecurityType,
    /// A string value which MUST follow the ISO3166-1 alpha-2 format.
    SubdivisionCode,
    /// A string which MUST follow the ISO3166-2 format.
    CountryCode,
    #[serde(untagged)]
    Unknown(String),
}

/// A trait to associate the field structs with their `field_type` tag.
pub trait EditableFieldType {
    /// The `field_type` value associated with the type
    fn field_type() -> FieldType;
}

impl<T, E> Serialize for EditableField<T, E>
where
    T: EditableFieldType + Serialize,
    E: Serialize,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let len = 2
            + self.id.is_some() as usize
            + self.label.is_some() as usize
            + self.extensions.is_some() as usize;
        let mut state = serializer.serialize_struct("editable_field", len)?;

        if let Some(ref id) = self.id {
            state.serialize_field("id", id)?;
        } else {
            state.skip_field("id")?;
        }

        state.serialize_field("fieldType", &self.value.field_type())?;
        match &self.value.0 {
            ExpectedInner::Expected(t) => {
                state.serialize_field("value", &t)?;
            }
            ExpectedInner::Unexpected(t) => match t {
                UnexpectedField::String(v) => state.serialize_field("value", &v)?,
                UnexpectedField::ConcealedString(v) => state.serialize_field("value", &v)?,
                UnexpectedField::WifiNetworkSecurityType(v) => {
                    state.serialize_field("value", &v)?
                }
                UnexpectedField::SubdivisionCode(v) => state.serialize_field("value", &v)?,
                UnexpectedField::CountryCode(v) => state.serialize_field("value", &v)?,
                UnexpectedField::Unknown { value: v, .. } => state.serialize_field("value", &v)?,
                UnexpectedField::Boolean(b) => {
                    let v = if b.0 { "true" } else { "false" };
                    state.serialize_field("value", v)?
                }
                UnexpectedField::Date(date) => state.serialize_field("value", &date)?,
                UnexpectedField::YearMonth(v) => state.serialize_field("value", &v)?,
                UnexpectedField::Email(v) => state.serialize_field("value", &v)?,
                UnexpectedField::Number(v) => state.serialize_field("value", &v)?,
            },
        }

        if let Some(ref label) = self.label {
            state.serialize_field("label", label)?;
        } else {
            state.skip_field("label")?;
        }

        if let Some(ref ext) = self.extensions {
            if ext.is_empty() {
                state.skip_field("extensions")?;
            } else {
                state.serialize_field("extensions", ext)?;
            }
        } else {
            state.skip_field("extensions")?;
        }

        state.end()
    }
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct EditableFieldHelper<E> {
    #[serde(default)]
    id: Option<B64Url>,
    value: String,
    field_type: FieldType,
    #[serde(default)]
    label: Option<String>,
    #[serde(default = "none::<E>")]
    extensions: Option<Vec<Extension<E>>>,
}

// Need to use this instead of the normal default,
// otherwise the derive creates a `E: Default` constraint.
fn none<E>() -> Option<Vec<Extension<E>>> {
    None
}

impl<'de, T, E> Deserialize<'de> for EditableField<T, E>
where
    T: EditableFieldType + DeserializeOwned,
    E: Deserialize<'de>,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let helper: EditableFieldHelper<E> = EditableFieldHelper::deserialize(deserializer)?;

        let value = if T::field_type() == helper.field_type {
            match T::deserialize(StrDeserializer::<serde::de::value::Error>::new(
                helper.value.as_str(),
            )) {
                Ok(t) => ExpectedInner::Expected(t),
                Err(_) => ExpectedInner::Unexpected(UnexpectedField::Unknown {
                    field_type: helper.field_type,
                    value: helper.value,
                }),
            }
        } else {
            macro_rules! deserialize_from_str {
                ($t:tt => $variant:ident) => {
                    match $t::deserialize(StrDeserializer::<D::Error>::new(&helper.value)) {
                        Ok(v) => UnexpectedField::$variant(v),
                        _ => UnexpectedField::Unknown {
                            field_type: helper.field_type,
                            value: helper.value,
                        },
                    }
                };
            }

            let value = match helper.field_type {
                FieldType::String => UnexpectedField::String(EditableFieldString(helper.value)),
                FieldType::ConcealedString => {
                    UnexpectedField::ConcealedString(EditableFieldConcealedString(helper.value))
                }
                FieldType::Boolean => deserialize_from_str!(EditableFieldBoolean => Boolean),
                FieldType::Date => deserialize_from_str!(EditableFieldDate => Date),
                FieldType::YearMonth => deserialize_from_str!(EditableFieldYearMonth => YearMonth),
                FieldType::WifiNetworkSecurityType => {
                    deserialize_from_str!(EditableFieldWifiNetworkSecurityType => WifiNetworkSecurityType)
                }
                FieldType::SubdivisionCode => {
                    UnexpectedField::SubdivisionCode(EditableFieldSubdivisionCode(helper.value))
                }
                FieldType::CountryCode => {
                    UnexpectedField::CountryCode(EditableFieldCountryCode(helper.value))
                }
                FieldType::Email => UnexpectedField::Email(EditableFieldEmail(helper.value)),
                FieldType::Number => UnexpectedField::Number(EditableFieldNumber(helper.value)),
                FieldType::Unknown(_) => UnexpectedField::Unknown {
                    field_type: helper.field_type,
                    value: helper.value,
                },
            };

            ExpectedInner::Unexpected(value)
        };

        Ok(Self {
            id: helper.id,
            value: Expected(value),
            label: helper.label,
            extensions: helper.extensions,
        })
    }
}

// Helper for converting inner types into EditableField
impl<T, E> From<T> for EditableField<T, E> {
    fn from(s: T) -> Self {
        EditableField {
            id: None,
            value: s.into(),
            label: None,
            extensions: None,
        }
    }
}

/// Macro to define a string-backed `EditableField` type with its `EditableFieldType` impl
/// and `From` conversions for `String`.
macro_rules! editable_field_string_type {
    ($name:ident, $variant:ident) => {
        #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
        #[serde(transparent)]
        pub struct $name(pub String);

        impl EditableFieldType for $name {
            fn field_type() -> FieldType {
                FieldType::$variant
            }
        }

        impl From<String> for $name {
            #[inline]
            fn from(s: String) -> Self {
                $name(s)
            }
        }

        impl<E> From<String> for EditableField<$name, E> {
            #[inline]
            fn from(s: String) -> Self {
                $name(s).into()
            }
        }

        impl<E> From<EditableField<$name, E>> for String {
            fn from(s: EditableField<$name, E>) -> Self {
                match s.value.0 {
                    ExpectedInner::Expected(f) => f.0.into(),
                    ExpectedInner::Unexpected(f) => f.into(),
                }
            }
        }

        impl From<$name> for String {
            #[inline]
            fn from(v: $name) -> Self {
                v.0
            }
        }

        impl AsRef<str> for $name {
            #[inline]
            fn as_ref(&self) -> &str {
                &self.0
            }
        }
    };
}

editable_field_string_type!(EditableFieldString, String);
editable_field_string_type!(EditableFieldConcealedString, ConcealedString);
editable_field_string_type!(EditableFieldEmail, Email);
editable_field_string_type!(EditableFieldNumber, Number);
editable_field_string_type!(EditableFieldSubdivisionCode, SubdivisionCode);
editable_field_string_type!(EditableFieldCountryCode, CountryCode);

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(transparent)]
pub struct EditableFieldBoolean(#[serde(with = "serde_bool")] pub bool);
impl EditableFieldType for EditableFieldBoolean {
    fn field_type() -> FieldType {
        FieldType::Boolean
    }
}

impl<E> From<bool> for EditableField<EditableFieldBoolean, E> {
    fn from(b: bool) -> Self {
        EditableFieldBoolean(b).into()
    }
}

impl From<EditableFieldBoolean> for String {
    #[inline]
    fn from(value: EditableFieldBoolean) -> Self {
        if value.0 { "true" } else { "false" }.into()
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(transparent)]
pub struct EditableFieldDate(pub NaiveDate);
impl EditableFieldType for EditableFieldDate {
    fn field_type() -> FieldType {
        FieldType::Date
    }
}

impl From<EditableFieldDate> for String {
    #[inline]
    fn from(value: EditableFieldDate) -> Self {
        value.0.format("%Y-%m-%d").to_string()
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(into = "String")]
pub struct EditableFieldYearMonth {
    /// The year in the format `YYYY`
    pub year: u16,
    /// The month in the format `MM`
    pub month: Month,
}

impl From<EditableFieldYearMonth> for String {
    #[inline]
    fn from(value: EditableFieldYearMonth) -> String {
        format!("{:04}-{:02}", value.year, value.month.number_from_month())
    }
}

impl EditableFieldType for EditableFieldYearMonth {
    fn field_type() -> FieldType {
        FieldType::YearMonth
    }
}

impl<'de> Deserialize<'de> for EditableFieldYearMonth {
    fn deserialize<D>(deserializer: D) -> Result<EditableFieldYearMonth, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = deserializer.deserialize_str(CowVisitor)?;
        let (year_str, month_str) = s
            .split_once('-')
            .ok_or_else(|| serde::de::Error::custom("Invalid format"))?;

        Ok(EditableFieldYearMonth {
            year: year_str
                .parse::<u16>()
                .map_err(|_| serde::de::Error::custom("Invalid year"))?,
            month: month_str
                .parse::<u8>()
                .map_err(|_| serde::de::Error::custom("Invalid month"))?
                .try_into()
                .map_err(|_| serde::de::Error::custom("Invalid month"))?,
        })
    }
}

/// Deserialize strings into `Cow` to avoid unnecessary allocations
struct CowVisitor;
impl<'de> Visitor<'de> for CowVisitor {
    type Value = Cow<'de, str>;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("a string")
    }

    fn visit_borrowed_str<E>(self, value: &'de str) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(Cow::Borrowed(value))
    }

    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(Cow::Owned(value.to_owned()))
    }

    fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(Cow::Owned(value))
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub enum EditableFieldWifiNetworkSecurityType {
    Unsecured,
    WpaPersonal,
    Wpa2Personal,
    Wpa3Personal,
    Wep,

    #[serde(untagged)]
    Other(String),
}
impl EditableFieldType for EditableFieldWifiNetworkSecurityType {
    fn field_type() -> FieldType {
        FieldType::WifiNetworkSecurityType
    }
}

impl From<EditableFieldWifiNetworkSecurityType> for String {
    #[inline]
    fn from(value: EditableFieldWifiNetworkSecurityType) -> Self {
        match value {
            EditableFieldWifiNetworkSecurityType::Unsecured => "unsecured".into(),
            EditableFieldWifiNetworkSecurityType::WpaPersonal => "wpa-personal".into(),
            EditableFieldWifiNetworkSecurityType::Wpa2Personal => "wpa2-personal".into(),
            EditableFieldWifiNetworkSecurityType::Wpa3Personal => "wpa3-personal".into(),
            EditableFieldWifiNetworkSecurityType::Wep => "wep".into(),
            EditableFieldWifiNetworkSecurityType::Other(o) => o,
        }
    }
}

/// Helper wrapper for `CustomFieldsCredential`.
#[derive(Clone, Debug, Serialize)]
#[serde(untagged, bound(deserialize = "E: Deserialize<'de>"))]
#[non_exhaustive]
pub enum EditableFieldValue<E = ()> {
    String(EditableField<EditableFieldString, E>),
    ConcealedString(EditableField<EditableFieldConcealedString, E>),
    Email(EditableField<EditableFieldEmail, E>),
    Number(EditableField<EditableFieldNumber, E>),
    Boolean(EditableField<EditableFieldBoolean, E>),
    Date(EditableField<EditableFieldDate, E>),
    YearMonth(EditableField<EditableFieldYearMonth, E>),
    SubdivisionCode(EditableField<EditableFieldSubdivisionCode, E>),
    CountryCode(EditableField<EditableFieldCountryCode, E>),
    WifiNetworkSecurityType(EditableField<EditableFieldWifiNetworkSecurityType, E>),
}

impl<'de, E> Deserialize<'de> for EditableFieldValue<E>
where
    E: Deserialize<'de>,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let helper: EditableFieldHelper<E> = EditableFieldHelper::deserialize(deserializer)?;

        macro_rules! deserialize_field {
            ($t: tt) => {{
                let v = $t::deserialize(StringDeserializer::new(helper.value))?;

                EditableField {
                    id: helper.id,
                    value: Expected(ExpectedInner::Expected(v)),
                    label: helper.label,
                    extensions: helper.extensions,
                }
            }};
        }

        let res = match helper.field_type {
            FieldType::String => Self::String(deserialize_field!(EditableFieldString)),
            FieldType::ConcealedString => {
                Self::ConcealedString(deserialize_field!(EditableFieldConcealedString))
            }
            FieldType::Boolean => Self::Boolean(deserialize_field!(EditableFieldBoolean)),
            FieldType::Date => Self::Date(deserialize_field!(EditableFieldDate)),
            FieldType::YearMonth => Self::YearMonth(deserialize_field!(EditableFieldYearMonth)),
            FieldType::WifiNetworkSecurityType => Self::WifiNetworkSecurityType(
                deserialize_field!(EditableFieldWifiNetworkSecurityType),
            ),
            FieldType::SubdivisionCode => {
                Self::SubdivisionCode(deserialize_field!(EditableFieldSubdivisionCode))
            }
            FieldType::CountryCode => {
                Self::CountryCode(deserialize_field!(EditableFieldCountryCode))
            }
            FieldType::Email => Self::Email(deserialize_field!(EditableFieldEmail)),
            FieldType::Number => Self::Number(deserialize_field!(EditableFieldNumber)),
            FieldType::Unknown(_) => {
                return Err(serde::de::Error::custom("Unknown custom field type"))
            }
        };

        Ok(res)
    }
}

mod serde_bool {
    pub fn serialize<S>(value: &bool, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&value.to_string())
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<bool, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = deserializer.deserialize_str(super::CowVisitor)?;

        s.trim()
            .to_lowercase()
            .parse()
            .map_err(serde::de::Error::custom)
    }
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;

    #[test]
    fn test_serialize_editable_field_string() {
        let field: EditableField<EditableFieldString> = EditableField {
            id: None,
            value: EditableFieldString("value".to_string()).into(),
            label: Some("label".to_string()),
            extensions: None,
        };
        let json = json!({
            "value": "value",
            "fieldType": "string",
            "label": "label",
        });
        assert_eq!(serde_json::to_value(&field).unwrap(), json);
    }

    #[test]
    fn test_deserialize_field_string() {
        let json = json!({
            "value": "value",
            "fieldType": "string",
            "label": "label",
        });
        let field: EditableField<EditableFieldString> = serde_json::from_value(json).unwrap();

        assert_eq!(
            field,
            EditableField {
                id: None,
                value: EditableFieldString("value".to_string()).into(),
                label: Some("label".to_string()),
                extensions: None,
            }
        );
    }

    #[test]
    fn test_serialize_field_concealed_string() {
        let field: EditableField<EditableFieldConcealedString> = EditableField {
            id: None,
            value: EditableFieldConcealedString("value".to_string()).into(),
            label: Some("label".to_string()),
            extensions: None,
        };
        let json = json!({
            "fieldType": "concealed-string",
            "value": "value",
            "label": "label",
        });
        assert_eq!(serde_json::to_value(&field).unwrap(), json);
    }

    #[test]
    fn test_deserialize_field_wrong_type() {
        let json = json!({
            "value": "value",
            "fieldType": "string",
            "label": "label",
        });
        let field: Result<EditableField<EditableFieldConcealedString>, _> =
            serde_json::from_value(json);

        assert!(field.unwrap().value.as_expected().is_err());
    }

    #[test]
    fn test_deserialize_field_bad_value_string() {
        let json = json!({
            "value": 5,
            "fieldType": "string",
            "label": "label",
        });
        let field: Result<EditableField<EditableFieldString>, _> = serde_json::from_value(json);

        assert!(field.is_err());
    }

    #[test]
    fn test_deserialize_field_bad_value_bool() {
        let json = json!({
            "value": "bad",
            "fieldType": "bool",
            "label": "label",
        });
        let field: Result<EditableField<EditableFieldBoolean>, _> = serde_json::from_value(json);

        assert!(field.unwrap().value.as_expected().is_err());
    }

    #[test]
    fn test_deserialize_field_missing_type() {
        let json = json!({
            "value": "value",
            "label": "label",
        });
        let field: Result<EditableField<EditableFieldConcealedString>, _> =
            serde_json::from_value(json);

        assert!(field.is_err());
    }

    #[test]
    fn test_deserialize_field_concealed_string() {
        let json = json!({
            "value": "value",
            "fieldType": "concealed-string",
            "label": "label",
        });
        let field: EditableField<EditableFieldConcealedString> =
            serde_json::from_value(json).unwrap();

        assert_eq!(
            field,
            EditableField {
                id: None,
                value: EditableFieldConcealedString("value".to_string()).into(),
                label: Some("label".to_string()),
                extensions: None,
            }
        );
    }

    #[test]
    fn test_serialize_field_boolean() {
        let field: EditableField<EditableFieldBoolean> = EditableField {
            id: None,
            value: EditableFieldBoolean(true).into(),
            label: Some("label".to_string()),
            extensions: None,
        };
        let json = json!({
            "fieldType": "boolean",
            "value": "true",
            "label": "label",
        });
        assert_eq!(serde_json::to_value(&field).unwrap(), json);
    }

    #[test]
    fn test_serialize_field_date() {
        let field: EditableField<EditableFieldDate> = EditableField {
            id: None,
            value: EditableFieldDate(NaiveDate::from_ymd_opt(2025, 2, 24).unwrap()).into(),
            label: None,
            extensions: None,
        };
        let json = json!({
            "fieldType": "date",
            "value": "2025-02-24",
        });
        assert_eq!(serde_json::to_value(&field).unwrap(), json);
    }

    #[test]
    fn test_serialize_editable_field_year_month() {
        let field: EditableField<EditableFieldYearMonth> = EditableField {
            id: None,
            value: EditableFieldYearMonth {
                year: 2025,
                month: Month::February,
            }
            .into(),
            label: None,
            extensions: None,
        };
        let json = json!({
            "fieldType": "year-month",
            "value": "2025-02",
        });
        assert_eq!(serde_json::to_value(&field).unwrap(), json);
    }

    #[test]
    fn test_deserialize_editable_field_year_month() {
        let json = json!({
            "fieldType": "year-month",
            "value": "2025-02",
        });
        let field: EditableField<EditableFieldYearMonth> = serde_json::from_value(json).unwrap();

        assert_eq!(
            field,
            EditableField {
                id: None,
                value: EditableFieldYearMonth {
                    year: 2025,
                    month: Month::February,
                }
                .into(),
                label: None,
                extensions: None,
            }
        );
    }

    #[test]
    fn test_deserialize_editable_field_year_month_invalid_format() {
        let json = json!({
            "fieldType": "year-month",
            "value": "2025/02",
        });
        let field: EditableField<EditableFieldYearMonth> = serde_json::from_value(json).unwrap();
        assert_eq!(
            field.value.as_expected(),
            Err(&UnexpectedField::Unknown {
                field_type: FieldType::YearMonth,
                value: "2025/02".into(),
            })
        );
    }

    #[test]
    fn test_extension_round_trip() {
        let json = json!({
            "fieldType": "string",
            "value": "hello",
            "extensions": [
                {
                    "name": "test",
                    "contents": "world"
                }
            ]
        });
        #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
        #[serde(tag = "name", rename_all = "camelCase")]
        enum CustomExtension {
            Test { contents: String },
        }

        let field: EditableField<EditableFieldString, CustomExtension> =
            serde_json::from_value(json.clone()).expect("Could not deserialize custom extensions");

        assert_eq!(
            field,
            EditableField {
                id: None,
                value: EditableFieldString("hello".to_string()).into(),
                label: None,
                extensions: Some(vec![Extension::External(CustomExtension::Test {
                    contents: "world".into()
                })])
            }
        );

        let returned = serde_json::to_value(field).expect("Could not serialize custom extensions");

        assert_eq!(returned, json);
    }

    #[test]
    fn editable_string_deserialized_from_others() {
        for (field_type, expected) in [
            (
                "concealed-string",
                UnexpectedField::ConcealedString("hello".to_owned().into()),
            ),
            (
                "unknown",
                UnexpectedField::Unknown {
                    field_type: FieldType::Unknown("unknown".into()),
                    value: "hello".into(),
                },
            ),
            (
                "date",
                UnexpectedField::Unknown {
                    field_type: FieldType::Date,
                    value: "hello".into(),
                },
            ),
        ] {
            let json = json!({
                "fieldType": field_type,
                "value": "hello",
            });

            let field: EditableField<EditableFieldString> =
                serde_json::from_value(json.clone()).expect("Could not deserialize field");

            assert_eq!(field.value.into_expected(), Err(expected));
        }
    }
}