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
//! Lexicon [string formats].
//!
//! [string formats]: https://atproto.com/specs/lexicon#string-formats

use std::{cell::OnceCell, cmp, ops::Deref, str::FromStr};

use chrono::DurationRound;
use langtag::{LanguageTag, LanguageTagBuf};
use regex::Regex;
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};

/// Common trait implementations for Lexicon string formats that are newtype wrappers
/// around `String`.
macro_rules! string_newtype {
    ($name:ident) => {
        impl FromStr for $name {
            type Err = &'static str;

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                Self::new(s.into())
            }
        }

        impl<'de> Deserialize<'de> for $name {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: Deserializer<'de>,
            {
                let value = Deserialize::deserialize(deserializer)?;
                Self::new(value).map_err(D::Error::custom)
            }
        }

        impl Into<String> for $name {
            fn into(self) -> String {
                self.0
            }
        }

        impl AsRef<str> for $name {
            fn as_ref(&self) -> &str {
                self.as_str()
            }
        }

        impl Deref for $name {
            type Target = str;

            fn deref(&self) -> &Self::Target {
                self.as_str()
            }
        }
    };
}

/// An AT Protocol identifier.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum AtIdentifier {
    Did(Did),
    Handle(Handle),
}

impl From<Did> for AtIdentifier {
    fn from(did: Did) -> Self {
        AtIdentifier::Did(did)
    }
}

impl From<Handle> for AtIdentifier {
    fn from(handle: Handle) -> Self {
        AtIdentifier::Handle(handle)
    }
}

impl FromStr for AtIdentifier {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if let Ok(did) = s.parse() {
            Ok(AtIdentifier::Did(did))
        } else {
            s.parse().map(AtIdentifier::Handle)
        }
    }
}

impl Into<String> for AtIdentifier {
    fn into(self) -> String {
        match self {
            AtIdentifier::Did(did) => did.into(),
            AtIdentifier::Handle(handle) => handle.into(),
        }
    }
}

impl AsRef<str> for AtIdentifier {
    fn as_ref(&self) -> &str {
        match self {
            AtIdentifier::Did(did) => did.as_ref(),
            AtIdentifier::Handle(handle) => handle.as_ref(),
        }
    }
}

/// A [CID in string format].
///
/// [CID in string format]: https://atproto.com/specs/data-model#link-and-cid-formats
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Cid(cid::Cid);

impl Cid {
    /// Prepares a CID for use as a Lexicon string.
    pub fn new(cid: cid::Cid) -> Self {
        Self(cid)
    }
}

impl FromStr for Cid {
    type Err = cid::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        s.parse().map(Self)
    }
}

impl<'de> Deserialize<'de> for Cid {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value: String = Deserialize::deserialize(deserializer)?;
        Self::from_str(&value).map_err(D::Error::custom)
    }
}

impl Serialize for Cid {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&self.0.to_string())
    }
}

impl AsRef<cid::Cid> for Cid {
    fn as_ref(&self) -> &cid::Cid {
        &self.0
    }
}

/// A Lexicon timestamp.
#[derive(Clone, Debug, Eq)]
pub struct Datetime {
    /// Serialized form. Preserved during parsing to ensure round-trip re-serialization.
    serialized: String,
    /// Parsed form.
    dt: chrono::DateTime<chrono::FixedOffset>,
}

impl PartialEq for Datetime {
    fn eq(&self, other: &Self) -> bool {
        self.dt == other.dt
    }
}

impl Ord for Datetime {
    fn cmp(&self, other: &Self) -> cmp::Ordering {
        self.dt.cmp(&other.dt)
    }
}

impl PartialOrd for Datetime {
    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Datetime {
    /// Returns a `Datetime` which corresponds to the current date and time in UTC.
    ///
    /// The timestamp uses microsecond precision.
    pub fn now() -> Self {
        Self::new(chrono::Utc::now().fixed_offset())
    }

    /// Constructs a new Lexicon timestamp.
    ///
    /// The timestamp is rounded to microsecond precision.
    pub fn new(dt: chrono::DateTime<chrono::FixedOffset>) -> Self {
        let dt = dt
            .duration_round(chrono::Duration::microseconds(1))
            .expect("delta does not exceed limits");
        // This serialization format is compatible with ISO 8601.
        let serialized = dt.to_rfc3339_opts(chrono::SecondsFormat::Micros, true);
        Self { serialized, dt }
    }
}

impl FromStr for Datetime {
    type Err = chrono::ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        // The `chrono` crate only supports RFC 3339 parsing, but Lexicon restricts
        // datetimes to the subset that is also valid under ISO 8601. Apply a regex that
        // validates enough of the relevant ISO 8601 format that the RFC 3339 parser can
        // do the rest.
        const RE_ISO_8601: OnceCell<Regex> = OnceCell::new();
        if RE_ISO_8601
            .get_or_init(|| Regex::new(r"^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]+)?(Z|(\+[0-9]{2}|\-[0-9][1-9]):[0-9]{2})$").unwrap())
            .is_match(&s)
        {
            let dt = chrono::DateTime::parse_from_rfc3339(s)?;
            Ok(Self {
                serialized: s.into(),
                dt,
            })
        } else {
            // Simulate an invalid `ParseError`.
            Err(chrono::DateTime::parse_from_rfc3339("invalid").expect_err("invalid"))
        }
    }
}

impl<'de> Deserialize<'de> for Datetime {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value: String = Deserialize::deserialize(deserializer)?;
        Self::from_str(&value).map_err(D::Error::custom)
    }
}

impl Serialize for Datetime {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&self.serialized)
    }
}

impl AsRef<chrono::DateTime<chrono::FixedOffset>> for Datetime {
    fn as_ref(&self) -> &chrono::DateTime<chrono::FixedOffset> {
        &self.dt
    }
}

/// A generic [DID Identifier].
///
/// [DID Identifier]: https://atproto.com/specs/did
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(transparent)]
pub struct Did(String);
string_newtype!(Did);

impl Did {
    /// Parses a `Did` from the given string.
    pub fn new(did: String) -> Result<Self, &'static str> {
        const RE_DID: OnceCell<Regex> = OnceCell::new();

        // https://atproto.com/specs/did#at-protocol-did-identifier-syntax
        if did.len() > 2048 {
            Err("DID too long")
        } else if !RE_DID
            .get_or_init(|| Regex::new(r"^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$").unwrap())
            .is_match(&did)
        {
            Err("Invalid DID")
        } else {
            Ok(Self(did))
        }
    }

    /// Returns the DID method.
    pub fn method(&self) -> &str {
        &self.0[..4 + self.0[4..].find(':').unwrap()]
    }

    /// Returns the DID as a string slice.
    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }
}

/// A [Handle Identifier].
///
/// [Handle Identifier]: https://atproto.com/specs/handle
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(transparent)]
pub struct Handle(String);
string_newtype!(Handle);

impl Handle {
    /// Parses a `Handle` from the given string.
    pub fn new(handle: String) -> Result<Self, &'static str> {
        const RE_HANDLE: OnceCell<Regex> = OnceCell::new();

        // https://atproto.com/specs/handle#handle-identifier-syntax
        if handle.len() > 253 {
            Err("Handle too long")
        } else if !RE_HANDLE
            .get_or_init(|| Regex::new(r"^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$").unwrap())
            .is_match(&handle)
        {
            Err("Invalid handle")
        } else {
            Ok(Self(handle))
        }
    }

    /// Returns the handle as a string slice.
    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }
}

/// A [Namespaced Identifier].
///
/// [Namespaced Identifier]: https://atproto.com/specs/nsid
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(transparent)]
pub struct Nsid(String);
string_newtype!(Nsid);

impl Nsid {
    /// Parses an NSID from the given string.
    pub fn new(nsid: String) -> Result<Self, &'static str> {
        const RE_NSID: OnceCell<Regex> = OnceCell::new();

        // https://atproto.com/specs/handle#handle-identifier-syntax
        if nsid.len() > 317 {
            Err("NSID too long")
        } else if !RE_NSID
            .get_or_init(|| Regex::new(r"^[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(\.[a-zA-Z]([a-zA-Z]{0,61}[a-zA-Z])?)$").unwrap())
            .is_match(&nsid)
        {
            Err("Invalid NSID")
        } else {
            Ok(Self(nsid))
        }
    }

    /// Returns the domain authority part of the NSID.
    pub fn domain_authority(&self) -> &str {
        let split = self.0.rfind('.').expect("enforced by constructor");
        &self.0[..split]
    }

    /// Returns the name segment of the NSID.
    pub fn name(&self) -> &str {
        let split = self.0.rfind('.').expect("enforced by constructor");
        &self.0[split + 1..]
    }

    /// Returns the NSID as a string slice.
    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }
}

/// An [IETF Language Tag] string.
///
/// [IETF Language Tag]: https://en.wikipedia.org/wiki/IETF_language_tag
#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
#[serde(transparent)]
pub struct Language(LanguageTagBuf);

impl Language {
    /// Creates a new language tag by parsing the given string.
    pub fn new(s: String) -> Result<Self, langtag::Error> {
        LanguageTagBuf::new(s.into()).map(Self).map_err(|(e, _)| e)
    }

    /// Returns a [`LanguageTag`] referencing this tag.
    #[inline]
    pub fn as_ref(&self) -> LanguageTag {
        self.0.as_ref()
    }
}

impl FromStr for Language {
    type Err = langtag::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::new(s.into())
    }
}

impl Serialize for Language {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.0.as_str())
    }
}

#[cfg(test)]
mod tests {
    use serde_json::{from_str, to_string};

    use super::*;

    #[test]
    fn valid_datetime() {
        // From https://atproto.com/specs/lexicon#datetime
        for valid in &[
            // preferred
            "1985-04-12T23:20:50.123Z",
            "1985-04-12T23:20:50.123456Z",
            "1985-04-12T23:20:50.120Z",
            "1985-04-12T23:20:50.120000Z",
            // supported
            "1985-04-12T23:20:50.12345678912345Z",
            "1985-04-12T23:20:50Z",
            "1985-04-12T23:20:50.0Z",
            "1985-04-12T23:20:50.123+00:00",
            "1985-04-12T23:20:50.123-07:00",
        ] {
            let json_valid = format!("\"{}\"", valid);
            let res = from_str::<Datetime>(&json_valid);
            assert!(res.is_ok(), "valid Datetime `{}` parsed as invalid", valid);
            let dt = res.unwrap();
            assert_eq!(to_string(&dt).unwrap(), json_valid);
        }
    }

    #[test]
    fn invalid_datetime() {
        // From https://atproto.com/specs/lexicon#datetime
        for invalid in &[
            "1985-04-12",
            "1985-04-12T23:20Z",
            "1985-04-12T23:20:5Z",
            "1985-04-12T23:20:50.123",
            "+001985-04-12T23:20:50.123Z",
            "23:20:50.123Z",
            "-1985-04-12T23:20:50.123Z",
            "1985-4-12T23:20:50.123Z",
            "01985-04-12T23:20:50.123Z",
            "1985-04-12T23:20:50.123+00",
            "1985-04-12T23:20:50.123+0000",
            // ISO-8601 strict capitalization
            "1985-04-12t23:20:50.123Z",
            "1985-04-12T23:20:50.123z",
            // RFC-3339, but not ISO-8601
            "1985-04-12T23:20:50.123-00:00",
            "1985-04-12 23:20:50.123Z",
            // timezone is required
            "1985-04-12T23:20:50.123",
            // syntax looks ok, but datetime is not valid
            "1985-04-12T23:99:50.123Z",
            "1985-00-12T23:20:50.123Z",
        ] {
            assert!(
                from_str::<Datetime>(&format!("\"{}\"", invalid)).is_err(),
                "invalid Datetime `{}` parsed as valid",
                invalid,
            );
        }
    }

    #[test]
    fn datetime_round_trip() {
        let dt = Datetime::now();
        let encoded = to_string(&dt).unwrap();
        assert_eq!(from_str::<Datetime>(&encoded).unwrap(), dt);
    }

    #[test]
    fn valid_did() {
        // From https://atproto.com/specs/did#examples
        for valid in &[
            "did:plc:z72i7hdynmk6r22z27h6tvur",
            "did:web:blueskyweb.xyz",
            "did:method:val:two",
            "did:m:v",
            "did:method::::val",
            "did:method:-:_:.",
            "did:key:zQ3shZc2QzApp2oymGvQbzP8eKheVshBHbU4ZYjeXqwSKEn6N",
        ] {
            assert!(
                from_str::<Did>(&format!("\"{}\"", valid)).is_ok(),
                "valid DID `{}` parsed as invalid",
                valid,
            );
        }
    }

    #[test]
    fn invalid_did() {
        // From https://atproto.com/specs/did#examples
        for invalid in &[
            "did:METHOD:val",
            "did:m123:val",
            "DID:method:val",
            "did:method:",
            "did:method:val/two",
            "did:method:val?two",
            "did:method:val#two",
        ] {
            assert!(
                from_str::<Did>(&format!("\"{}\"", invalid)).is_err(),
                "invalid DID `{}` parsed as valid",
                invalid,
            );
        }
    }

    #[test]
    fn did_method() {
        // From https://atproto.com/specs/did#examples
        for (method, did) in &[
            ("did:plc", "did:plc:z72i7hdynmk6r22z27h6tvur"),
            ("did:web", "did:web:blueskyweb.xyz"),
            ("did:method", "did:method:val:two"),
            ("did:m", "did:m:v"),
            ("did:method", "did:method::::val"),
            ("did:method", "did:method:-:_:."),
            (
                "did:key",
                "did:key:zQ3shZc2QzApp2oymGvQbzP8eKheVshBHbU4ZYjeXqwSKEn6N",
            ),
        ] {
            assert_eq!(Did::new(did.to_string()).unwrap().method(), *method);
        }
    }

    #[test]
    fn valid_handle() {
        // From https://atproto.com/specs/handle#identifier-examples
        for valid in &[
            "jay.bsky.social",
            "8.cn",
            "name.t--t", // not a real TLD, but syntax ok
            "XX.LCS.MIT.EDU",
            "a.co",
            "xn--notarealidn.com",
            "xn--fiqa61au8b7zsevnm8ak20mc4a87e.xn--fiqs8s",
            "xn--ls8h.test",
            "example.t", // not a real TLD, but syntax ok
            // Valid syntax, but must always fail resolution due to other restrictions:
            "2gzyxa5ihm7nsggfxnu52rck2vv4rvmdlkiu3zzui5du4xyclen53wid.onion",
            "laptop.local",
            "blah.arpa",
        ] {
            assert!(
                from_str::<Handle>(&format!("\"{}\"", valid)).is_ok(),
                "valid handle `{}` parsed as invalid",
                valid,
            );
        }
    }

    #[test]
    fn invalid_handle() {
        // From https://atproto.com/specs/handle#identifier-examples
        for invalid in &[
            "jo@hn.test",
            "💩.test",
            "john..test",
            "xn--bcher-.tld",
            "john.0",
            "cn.8",
            "www.masełkowski.pl.com",
            "org",
            "name.org.",
        ] {
            assert!(
                from_str::<Handle>(&format!("\"{}\"", invalid)).is_err(),
                "invalid handle `{}` parsed as valid",
                invalid,
            );
        }
    }

    #[test]
    fn valid_nsid() {
        // From https://atproto.com/specs/nsid#examples
        for valid in &[
            "com.example.fooBar",
            "net.users.bob.ping",
            "a-0.b-1.c",
            "a.b.c",
            "cn.8.lex.stuff",
        ] {
            assert!(
                from_str::<Nsid>(&format!("\"{}\"", valid)).is_ok(),
                "valid NSID `{}` parsed as invalid",
                valid,
            );
        }
    }

    #[test]
    fn invalid_nsid() {
        // From https://atproto.com/specs/nsid#examples
        for invalid in &["com.exa💩ple.thing", "com.example"] {
            assert!(
                from_str::<Nsid>(&format!("\"{}\"", invalid)).is_err(),
                "invalid NSID `{}` parsed as valid",
                invalid,
            );
        }
    }

    #[test]
    fn nsid_parts() {
        // From https://atproto.com/specs/nsid#examples
        for (nsid, domain_authority, name) in &[
            ("com.example.fooBar", "com.example", "fooBar"),
            ("net.users.bob.ping", "net.users.bob", "ping"),
            ("a-0.b-1.c", "a-0.b-1", "c"),
            ("a.b.c", "a.b", "c"),
            ("cn.8.lex.stuff", "cn.8.lex", "stuff"),
        ] {
            let nsid = Nsid::new(nsid.to_string()).unwrap();
            assert_eq!(nsid.domain_authority(), *domain_authority);
            assert_eq!(nsid.name(), *name);
        }
    }

    #[test]
    fn valid_language() {
        // From https://www.rfc-editor.org/rfc/rfc5646.html#appendix-A
        for valid in &[
            // Simple language subtag:
            "de",         // German
            "fr",         // French
            "ja",         // Japanese
            "i-enochian", // example of a grandfathered tag
            // Language subtag plus Script subtag:
            "zh-Hant", // Chinese written using the Traditional Chinese script
            "zh-Hans", // Chinese written using the Simplified Chinese script
            "sr-Cyrl", // Serbian written using the Cyrillic script
            "sr-Latn", // Serbian written using the Latin script
            // Extended language subtags and their primary language subtag counterparts:
            "zh-cmn-Hans-CN", // Chinese, Mandarin, Simplified script, as used in China
            "cmn-Hans-CN",    // Mandarin Chinese, Simplified script, as used in China
            "zh-yue-HK",      // Chinese, Cantonese, as used in Hong Kong SAR
            "yue-HK",         // Cantonese Chinese, as used in Hong Kong SAR
            // Language-Script-Region:
            "zh-Hans-CN", // Chinese written using the Simplified script as used in mainland China
            "sr-Latn-RS", // Serbian written using the Latin script as used in Serbia
            // Language-Variant:
            "sl-rozaj",       // Resian dialect of Slovenian
            "sl-rozaj-biske", // San Giorgio dialect of Resian dialect of Slovenian
            "sl-nedis",       // Nadiza dialect of Slovenian
            // Language-Region-Variant:
            "de-CH-1901", // German as used in Switzerland using the 1901 variant orthography
            "sl-IT-nedis", // Slovenian as used in Italy, Nadiza dialect
            // Language-Script-Region-Variant:
            "hy-Latn-IT-arevela", // Eastern Armenian written in Latin script, as used in Italy
            // Language-Region:
            "de-DE",  // German for Germany
            "en-US",  // English as used in the United States
            "es-419", // Spanish appropriate for the Latin America and Caribbean region using the UN region code
            // Private use subtags:
            "de-CH-x-phonebk",
            "az-Arab-x-AZE-derbend",
            // Private use registry values:
            "x-whatever",             // private use using the singleton 'x'
            "qaa-Qaaa-QM-x-southern", // all private tags
            "de-Qaaa",                // German, with a private script
            "sr-Latn-QM",             // Serbian, Latin script, private region
            "sr-Qaaa-RS",             // Serbian, private script, for Serbia
            // Tags that use extensions (examples ONLY -- extensions MUST be defined by RFC):
            "en-US-u-islamcal",
            "zh-CN-a-myext-x-private",
            "en-a-myext-b-another",
            // Invalid tags that are well-formed:
            "ar-a-aaa-b-bbb-a-ccc", // two extensions with same single-letter prefix
        ] {
            let json_valid = format!("\"{}\"", valid);
            let res = from_str::<Language>(&json_valid);
            assert!(res.is_ok(), "valid language `{}` parsed as invalid", valid);
            let dt = res.unwrap();
            assert_eq!(to_string(&dt).unwrap(), json_valid);
        }
    }

    #[test]
    fn invalid_language() {
        // From https://www.rfc-editor.org/rfc/rfc5646.html#appendix-A
        for invalid in &[
            "de-419-DE", // two region tags
            // use of a single-character subtag in primary position; note that there are a
            // few grandfathered tags that start with "i-" that are valid
            "a-DE",
        ] {
            assert!(
                from_str::<Language>(&format!("\"{}\"", invalid)).is_err(),
                "invalid language `{}` parsed as valid",
                invalid,
            );
        }
    }
}