shrike 0.1.1

AT Protocol library for Rust
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
// Code generated by lexgen. DO NOT EDIT.

/// LabelDefsLabel — Metadata tag on an atproto resource (eg, repo or record).
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LabelDefsLabel {
    /// Optionally, CID specifying the specific version of 'uri' resource this label applies to.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cid: Option<String>,
    /// Timestamp when this label was created.
    pub cts: crate::syntax::Datetime,
    /// Timestamp at which this label expires (no longer applies).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub exp: Option<crate::syntax::Datetime>,
    /// If true, this is a negation label, overwriting a previous label.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub neg: Option<bool>,
    /// Signature of dag-cbor encoded label.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sig: Option<String>,
    /// DID of the actor who created this label.
    pub src: crate::syntax::Did,
    /// AT URI of the record, repository (account), or other resource that this label applies to.
    pub uri: String,
    /// The short string name of the value or type of this label.
    pub val: String,
    /// The AT Protocol version of the label object.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ver: Option<i64>,
    /// Extra fields not defined in the schema (JSON).
    #[serde(flatten)]
    pub extra: std::collections::HashMap<String, serde_json::Value>,
    /// Extra fields not defined in the schema (CBOR).
    #[serde(skip)]
    pub extra_cbor: Vec<(String, Vec<u8>)>,
}

impl LabelDefsLabel {
    pub fn to_cbor(&self) -> Result<Vec<u8>, crate::cbor::CborError> {
        let mut buf = Vec::new();
        self.encode_cbor(&mut buf)?;
        Ok(buf)
    }

    pub fn encode_cbor(&self, buf: &mut Vec<u8>) -> Result<(), crate::cbor::CborError> {
        if self.extra_cbor.is_empty() {
            // Fast path: no extra fields to merge.
            let mut count = 4u64;
            if self.cid.is_some() {
                count += 1;
            }
            if self.exp.is_some() {
                count += 1;
            }
            if self.neg.is_some() {
                count += 1;
            }
            if self.sig.is_some() {
                count += 1;
            }
            if self.ver.is_some() {
                count += 1;
            }
            crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
            if self.cid.is_some() {
                crate::cbor::Encoder::new(&mut *buf).encode_text("cid")?;
                if let Some(ref val) = self.cid {
                    crate::cbor::Encoder::new(&mut *buf).encode_text(val)?;
                }
            }
            crate::cbor::Encoder::new(&mut *buf).encode_text("cts")?;
            crate::cbor::Encoder::new(&mut *buf).encode_text(self.cts.as_str())?;
            if self.exp.is_some() {
                crate::cbor::Encoder::new(&mut *buf).encode_text("exp")?;
                if let Some(ref val) = self.exp {
                    crate::cbor::Encoder::new(&mut *buf).encode_text(val.as_str())?;
                }
            }
            if self.neg.is_some() {
                crate::cbor::Encoder::new(&mut *buf).encode_text("neg")?;
                if let Some(ref val) = self.neg {
                    crate::cbor::Encoder::new(&mut *buf).encode_bool(*val)?;
                }
            }
            if self.sig.is_some() {
                crate::cbor::Encoder::new(&mut *buf).encode_text("sig")?;
                if let Some(ref val) = self.sig {
                    crate::cbor::Encoder::new(&mut *buf).encode_text(val)?;
                }
            }
            crate::cbor::Encoder::new(&mut *buf).encode_text("src")?;
            crate::cbor::Encoder::new(&mut *buf).encode_text(self.src.as_str())?;
            crate::cbor::Encoder::new(&mut *buf).encode_text("uri")?;
            crate::cbor::Encoder::new(&mut *buf).encode_text(&self.uri)?;
            crate::cbor::Encoder::new(&mut *buf).encode_text("val")?;
            crate::cbor::Encoder::new(&mut *buf).encode_text(&self.val)?;
            if self.ver.is_some() {
                crate::cbor::Encoder::new(&mut *buf).encode_text("ver")?;
                if let Some(ref val) = self.ver {
                    crate::cbor::Encoder::new(&mut *buf).encode_i64(*val)?;
                }
            }
        } else {
            // Slow path: merge known fields with extra_cbor, sort, encode.
            let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
            if self.cid.is_some() {
                let mut vbuf = Vec::new();
                if let Some(ref val) = self.cid {
                    crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
                }
                pairs.push(("cid", vbuf));
            }
            {
                let mut vbuf = Vec::new();
                crate::cbor::Encoder::new(&mut vbuf).encode_text(self.cts.as_str())?;
                pairs.push(("cts", vbuf));
            }
            if self.exp.is_some() {
                let mut vbuf = Vec::new();
                if let Some(ref val) = self.exp {
                    crate::cbor::Encoder::new(&mut vbuf).encode_text(val.as_str())?;
                }
                pairs.push(("exp", vbuf));
            }
            if self.neg.is_some() {
                let mut vbuf = Vec::new();
                if let Some(ref val) = self.neg {
                    crate::cbor::Encoder::new(&mut vbuf).encode_bool(*val)?;
                }
                pairs.push(("neg", vbuf));
            }
            if self.sig.is_some() {
                let mut vbuf = Vec::new();
                if let Some(ref val) = self.sig {
                    crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
                }
                pairs.push(("sig", vbuf));
            }
            {
                let mut vbuf = Vec::new();
                crate::cbor::Encoder::new(&mut vbuf).encode_text(self.src.as_str())?;
                pairs.push(("src", vbuf));
            }
            {
                let mut vbuf = Vec::new();
                crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.uri)?;
                pairs.push(("uri", vbuf));
            }
            {
                let mut vbuf = Vec::new();
                crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.val)?;
                pairs.push(("val", vbuf));
            }
            if self.ver.is_some() {
                let mut vbuf = Vec::new();
                if let Some(ref val) = self.ver {
                    crate::cbor::Encoder::new(&mut vbuf).encode_i64(*val)?;
                }
                pairs.push(("ver", vbuf));
            }
            for (k, v) in &self.extra_cbor {
                pairs.push((k.as_str(), v.clone()));
            }
            pairs.sort_by(|a, b| crate::cbor::cbor_key_cmp(a.0, b.0));
            crate::cbor::Encoder::new(&mut *buf).encode_map_header(pairs.len() as u64)?;
            for (k, v) in &pairs {
                crate::cbor::Encoder::new(&mut *buf).encode_text(k)?;
                buf.extend_from_slice(v);
            }
        }
        Ok(())
    }

    pub fn from_cbor(data: &[u8]) -> Result<Self, crate::cbor::CborError> {
        let mut decoder = crate::cbor::Decoder::new(data);
        let result = Self::decode_cbor(&mut decoder)?;
        if !decoder.is_empty() {
            return Err(crate::cbor::CborError::InvalidCbor("trailing data".into()));
        }
        Ok(result)
    }

    pub fn decode_cbor(decoder: &mut crate::cbor::Decoder) -> Result<Self, crate::cbor::CborError> {
        let val = decoder.decode()?;
        let entries = match val {
            crate::cbor::Value::Map(entries) => entries,
            _ => return Err(crate::cbor::CborError::InvalidCbor("expected map".into())),
        };

        let mut field_cid: Option<String> = None;
        let mut field_cts: Option<crate::syntax::Datetime> = None;
        let mut field_exp: Option<crate::syntax::Datetime> = None;
        let mut field_neg: Option<bool> = None;
        let mut field_sig: Option<String> = None;
        let mut field_src: Option<crate::syntax::Did> = None;
        let mut field_uri: Option<String> = None;
        let mut field_val: Option<String> = None;
        let mut field_ver: Option<i64> = None;
        let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();

        for (key, value) in entries {
            match key {
                "cid" => {
                    if let crate::cbor::Value::Text(s) = value {
                        field_cid = Some(s.to_string());
                    } else {
                        return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
                    }
                }
                "cts" => {
                    if let crate::cbor::Value::Text(s) = value {
                        field_cts = Some(
                            crate::syntax::Datetime::try_from(s)
                                .map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
                        );
                    } else {
                        return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
                    }
                }
                "exp" => {
                    if let crate::cbor::Value::Text(s) = value {
                        field_exp = Some(
                            crate::syntax::Datetime::try_from(s)
                                .map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
                        );
                    } else {
                        return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
                    }
                }
                "neg" => {
                    if let crate::cbor::Value::Bool(b) = value {
                        field_neg = Some(b);
                    } else {
                        return Err(crate::cbor::CborError::InvalidCbor("expected bool".into()));
                    }
                }
                "sig" => {
                    if let crate::cbor::Value::Text(s) = value {
                        field_sig = Some(s.to_string());
                    } else {
                        return Err(crate::cbor::CborError::InvalidCbor(
                            "expected text for bytes field".into(),
                        ));
                    }
                }
                "src" => {
                    if let crate::cbor::Value::Text(s) = value {
                        field_src = Some(
                            crate::syntax::Did::try_from(s)
                                .map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
                        );
                    } else {
                        return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
                    }
                }
                "uri" => {
                    if let crate::cbor::Value::Text(s) = value {
                        field_uri = Some(s.to_string());
                    } else {
                        return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
                    }
                }
                "val" => {
                    if let crate::cbor::Value::Text(s) = value {
                        field_val = Some(s.to_string());
                    } else {
                        return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
                    }
                }
                "ver" => match value {
                    crate::cbor::Value::Unsigned(n) => {
                        field_ver = Some(n as i64);
                    }
                    crate::cbor::Value::Signed(n) => {
                        field_ver = Some(n);
                    }
                    _ => {
                        return Err(crate::cbor::CborError::InvalidCbor(
                            "expected integer".into(),
                        ));
                    }
                },
                _ => {
                    let raw = crate::cbor::encode_value(&value)?;
                    extra_cbor.push((key.to_string(), raw));
                }
            }
        }

        Ok(LabelDefsLabel {
            cid: field_cid,
            cts: field_cts.ok_or_else(|| {
                crate::cbor::CborError::InvalidCbor("missing required field 'cts'".into())
            })?,
            exp: field_exp,
            neg: field_neg,
            sig: field_sig,
            src: field_src.ok_or_else(|| {
                crate::cbor::CborError::InvalidCbor("missing required field 'src'".into())
            })?,
            uri: field_uri.ok_or_else(|| {
                crate::cbor::CborError::InvalidCbor("missing required field 'uri'".into())
            })?,
            val: field_val.ok_or_else(|| {
                crate::cbor::CborError::InvalidCbor("missing required field 'val'".into())
            })?,
            ver: field_ver,
            extra: std::collections::HashMap::new(),
            extra_cbor,
        })
    }
}

/// LabelDefsLabelValue is a string type from com.atproto.label.defs.
pub type LabelDefsLabelValue = String;

/// LabelDefsLabelValueDefinition — Declares a label value and its expected interpretations and behaviors.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LabelDefsLabelValueDefinition {
    /// Does the user need to have adult content enabled in order to configure this label?
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub adult_only: Option<bool>,
    /// What should this label hide in the UI, if applied? 'content' hides all of the target; 'media' hides the images/video/audio; 'none' hides nothing.
    pub blurs: String,
    /// The default setting for this label.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default_setting: Option<String>,
    /// The value of the label being defined. Must only include lowercase ascii and the '-' character ([a-z-]+).
    pub identifier: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub locales: Vec<LabelDefsLabelValueDefinitionStrings>,
    /// How should a client visually convey this label? 'inform' means neutral and informational; 'alert' means negative and warning; 'none' means show nothing.
    pub severity: String,
    /// Extra fields not defined in the schema (JSON).
    #[serde(flatten)]
    pub extra: std::collections::HashMap<String, serde_json::Value>,
    /// Extra fields not defined in the schema (CBOR).
    #[serde(skip)]
    pub extra_cbor: Vec<(String, Vec<u8>)>,
}

impl LabelDefsLabelValueDefinition {
    pub fn to_cbor(&self) -> Result<Vec<u8>, crate::cbor::CborError> {
        let mut buf = Vec::new();
        self.encode_cbor(&mut buf)?;
        Ok(buf)
    }

    pub fn encode_cbor(&self, buf: &mut Vec<u8>) -> Result<(), crate::cbor::CborError> {
        if self.extra_cbor.is_empty() {
            // Fast path: no extra fields to merge.
            let mut count = 4u64;
            if self.adult_only.is_some() {
                count += 1;
            }
            if self.default_setting.is_some() {
                count += 1;
            }
            crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
            crate::cbor::Encoder::new(&mut *buf).encode_text("blurs")?;
            crate::cbor::Encoder::new(&mut *buf).encode_text(&self.blurs)?;
            crate::cbor::Encoder::new(&mut *buf).encode_text("locales")?;
            crate::cbor::Encoder::new(&mut *buf).encode_array_header(self.locales.len() as u64)?;
            for item in &self.locales {
                item.encode_cbor(buf)?;
            }
            crate::cbor::Encoder::new(&mut *buf).encode_text("severity")?;
            crate::cbor::Encoder::new(&mut *buf).encode_text(&self.severity)?;
            if self.adult_only.is_some() {
                crate::cbor::Encoder::new(&mut *buf).encode_text("adultOnly")?;
                if let Some(ref val) = self.adult_only {
                    crate::cbor::Encoder::new(&mut *buf).encode_bool(*val)?;
                }
            }
            crate::cbor::Encoder::new(&mut *buf).encode_text("identifier")?;
            crate::cbor::Encoder::new(&mut *buf).encode_text(&self.identifier)?;
            if self.default_setting.is_some() {
                crate::cbor::Encoder::new(&mut *buf).encode_text("defaultSetting")?;
                if let Some(ref val) = self.default_setting {
                    crate::cbor::Encoder::new(&mut *buf).encode_text(val)?;
                }
            }
        } else {
            // Slow path: merge known fields with extra_cbor, sort, encode.
            let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
            {
                let mut vbuf = Vec::new();
                crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.blurs)?;
                pairs.push(("blurs", vbuf));
            }
            {
                let mut vbuf = Vec::new();
                crate::cbor::Encoder::new(&mut vbuf)
                    .encode_array_header(self.locales.len() as u64)?;
                for item in &self.locales {
                    item.encode_cbor(&mut vbuf)?;
                }
                pairs.push(("locales", vbuf));
            }
            {
                let mut vbuf = Vec::new();
                crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.severity)?;
                pairs.push(("severity", vbuf));
            }
            if self.adult_only.is_some() {
                let mut vbuf = Vec::new();
                if let Some(ref val) = self.adult_only {
                    crate::cbor::Encoder::new(&mut vbuf).encode_bool(*val)?;
                }
                pairs.push(("adultOnly", vbuf));
            }
            {
                let mut vbuf = Vec::new();
                crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.identifier)?;
                pairs.push(("identifier", vbuf));
            }
            if self.default_setting.is_some() {
                let mut vbuf = Vec::new();
                if let Some(ref val) = self.default_setting {
                    crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
                }
                pairs.push(("defaultSetting", vbuf));
            }
            for (k, v) in &self.extra_cbor {
                pairs.push((k.as_str(), v.clone()));
            }
            pairs.sort_by(|a, b| crate::cbor::cbor_key_cmp(a.0, b.0));
            crate::cbor::Encoder::new(&mut *buf).encode_map_header(pairs.len() as u64)?;
            for (k, v) in &pairs {
                crate::cbor::Encoder::new(&mut *buf).encode_text(k)?;
                buf.extend_from_slice(v);
            }
        }
        Ok(())
    }

    pub fn from_cbor(data: &[u8]) -> Result<Self, crate::cbor::CborError> {
        let mut decoder = crate::cbor::Decoder::new(data);
        let result = Self::decode_cbor(&mut decoder)?;
        if !decoder.is_empty() {
            return Err(crate::cbor::CborError::InvalidCbor("trailing data".into()));
        }
        Ok(result)
    }

    pub fn decode_cbor(decoder: &mut crate::cbor::Decoder) -> Result<Self, crate::cbor::CborError> {
        let val = decoder.decode()?;
        let entries = match val {
            crate::cbor::Value::Map(entries) => entries,
            _ => return Err(crate::cbor::CborError::InvalidCbor("expected map".into())),
        };

        let mut field_blurs: Option<String> = None;
        let mut field_locales: Vec<LabelDefsLabelValueDefinitionStrings> = Vec::new();
        let mut field_severity: Option<String> = None;
        let mut field_adult_only: Option<bool> = None;
        let mut field_identifier: Option<String> = None;
        let mut field_default_setting: Option<String> = None;
        let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();

        for (key, value) in entries {
            match key {
                "blurs" => {
                    if let crate::cbor::Value::Text(s) = value {
                        field_blurs = Some(s.to_string());
                    } else {
                        return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
                    }
                }
                "locales" => {
                    if let crate::cbor::Value::Array(items) = value {
                        for item in items {
                            let raw = crate::cbor::encode_value(&item)?;
                            let mut dec = crate::cbor::Decoder::new(&raw);
                            field_locales
                                .push(LabelDefsLabelValueDefinitionStrings::decode_cbor(&mut dec)?);
                        }
                    } else {
                        return Err(crate::cbor::CborError::InvalidCbor("expected array".into()));
                    }
                }
                "severity" => {
                    if let crate::cbor::Value::Text(s) = value {
                        field_severity = Some(s.to_string());
                    } else {
                        return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
                    }
                }
                "adultOnly" => {
                    if let crate::cbor::Value::Bool(b) = value {
                        field_adult_only = Some(b);
                    } else {
                        return Err(crate::cbor::CborError::InvalidCbor("expected bool".into()));
                    }
                }
                "identifier" => {
                    if let crate::cbor::Value::Text(s) = value {
                        field_identifier = Some(s.to_string());
                    } else {
                        return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
                    }
                }
                "defaultSetting" => {
                    if let crate::cbor::Value::Text(s) = value {
                        field_default_setting = Some(s.to_string());
                    } else {
                        return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
                    }
                }
                _ => {
                    let raw = crate::cbor::encode_value(&value)?;
                    extra_cbor.push((key.to_string(), raw));
                }
            }
        }

        Ok(LabelDefsLabelValueDefinition {
            blurs: field_blurs.ok_or_else(|| {
                crate::cbor::CborError::InvalidCbor("missing required field 'blurs'".into())
            })?,
            locales: field_locales,
            severity: field_severity.ok_or_else(|| {
                crate::cbor::CborError::InvalidCbor("missing required field 'severity'".into())
            })?,
            adult_only: field_adult_only,
            identifier: field_identifier.ok_or_else(|| {
                crate::cbor::CborError::InvalidCbor("missing required field 'identifier'".into())
            })?,
            default_setting: field_default_setting,
            extra: std::collections::HashMap::new(),
            extra_cbor,
        })
    }
}

/// LabelDefsLabelValueDefinitionStrings — Strings which describe the label in the UI, localized into a specific language.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LabelDefsLabelValueDefinitionStrings {
    /// A longer description of what the label means and why it might be applied.
    pub description: String,
    /// The code of the language these strings are written in.
    pub lang: crate::syntax::Language,
    /// A short human-readable name for the label.
    pub name: String,
    /// Extra fields not defined in the schema (JSON).
    #[serde(flatten)]
    pub extra: std::collections::HashMap<String, serde_json::Value>,
    /// Extra fields not defined in the schema (CBOR).
    #[serde(skip)]
    pub extra_cbor: Vec<(String, Vec<u8>)>,
}

impl LabelDefsLabelValueDefinitionStrings {
    pub fn to_cbor(&self) -> Result<Vec<u8>, crate::cbor::CborError> {
        let mut buf = Vec::new();
        self.encode_cbor(&mut buf)?;
        Ok(buf)
    }

    pub fn encode_cbor(&self, buf: &mut Vec<u8>) -> Result<(), crate::cbor::CborError> {
        if self.extra_cbor.is_empty() {
            // Fast path: no extra fields to merge.
            let count = 3u64;
            crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
            crate::cbor::Encoder::new(&mut *buf).encode_text("lang")?;
            crate::cbor::Encoder::new(&mut *buf).encode_text(self.lang.as_str())?;
            crate::cbor::Encoder::new(&mut *buf).encode_text("name")?;
            crate::cbor::Encoder::new(&mut *buf).encode_text(&self.name)?;
            crate::cbor::Encoder::new(&mut *buf).encode_text("description")?;
            crate::cbor::Encoder::new(&mut *buf).encode_text(&self.description)?;
        } else {
            // Slow path: merge known fields with extra_cbor, sort, encode.
            let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
            {
                let mut vbuf = Vec::new();
                crate::cbor::Encoder::new(&mut vbuf).encode_text(self.lang.as_str())?;
                pairs.push(("lang", vbuf));
            }
            {
                let mut vbuf = Vec::new();
                crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.name)?;
                pairs.push(("name", vbuf));
            }
            {
                let mut vbuf = Vec::new();
                crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.description)?;
                pairs.push(("description", vbuf));
            }
            for (k, v) in &self.extra_cbor {
                pairs.push((k.as_str(), v.clone()));
            }
            pairs.sort_by(|a, b| crate::cbor::cbor_key_cmp(a.0, b.0));
            crate::cbor::Encoder::new(&mut *buf).encode_map_header(pairs.len() as u64)?;
            for (k, v) in &pairs {
                crate::cbor::Encoder::new(&mut *buf).encode_text(k)?;
                buf.extend_from_slice(v);
            }
        }
        Ok(())
    }

    pub fn from_cbor(data: &[u8]) -> Result<Self, crate::cbor::CborError> {
        let mut decoder = crate::cbor::Decoder::new(data);
        let result = Self::decode_cbor(&mut decoder)?;
        if !decoder.is_empty() {
            return Err(crate::cbor::CborError::InvalidCbor("trailing data".into()));
        }
        Ok(result)
    }

    pub fn decode_cbor(decoder: &mut crate::cbor::Decoder) -> Result<Self, crate::cbor::CborError> {
        let val = decoder.decode()?;
        let entries = match val {
            crate::cbor::Value::Map(entries) => entries,
            _ => return Err(crate::cbor::CborError::InvalidCbor("expected map".into())),
        };

        let mut field_lang: Option<crate::syntax::Language> = None;
        let mut field_name: Option<String> = None;
        let mut field_description: Option<String> = None;
        let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();

        for (key, value) in entries {
            match key {
                "lang" => {
                    if let crate::cbor::Value::Text(s) = value {
                        field_lang = Some(
                            crate::syntax::Language::try_from(s)
                                .map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
                        );
                    } else {
                        return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
                    }
                }
                "name" => {
                    if let crate::cbor::Value::Text(s) = value {
                        field_name = Some(s.to_string());
                    } else {
                        return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
                    }
                }
                "description" => {
                    if let crate::cbor::Value::Text(s) = value {
                        field_description = Some(s.to_string());
                    } else {
                        return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
                    }
                }
                _ => {
                    let raw = crate::cbor::encode_value(&value)?;
                    extra_cbor.push((key.to_string(), raw));
                }
            }
        }

        Ok(LabelDefsLabelValueDefinitionStrings {
            lang: field_lang.ok_or_else(|| {
                crate::cbor::CborError::InvalidCbor("missing required field 'lang'".into())
            })?,
            name: field_name.ok_or_else(|| {
                crate::cbor::CborError::InvalidCbor("missing required field 'name'".into())
            })?,
            description: field_description.ok_or_else(|| {
                crate::cbor::CborError::InvalidCbor("missing required field 'description'".into())
            })?,
            extra: std::collections::HashMap::new(),
            extra_cbor,
        })
    }
}

/// LabelDefsSelfLabel — Metadata tag on an atproto record, published by the author within the record. Note that schemas should use #selfLabels, not #selfLabel.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LabelDefsSelfLabel {
    /// The short string name of the value or type of this label.
    pub val: String,
    /// Extra fields not defined in the schema (JSON).
    #[serde(flatten)]
    pub extra: std::collections::HashMap<String, serde_json::Value>,
    /// Extra fields not defined in the schema (CBOR).
    #[serde(skip)]
    pub extra_cbor: Vec<(String, Vec<u8>)>,
}

impl LabelDefsSelfLabel {
    pub fn to_cbor(&self) -> Result<Vec<u8>, crate::cbor::CborError> {
        let mut buf = Vec::new();
        self.encode_cbor(&mut buf)?;
        Ok(buf)
    }

    pub fn encode_cbor(&self, buf: &mut Vec<u8>) -> Result<(), crate::cbor::CborError> {
        if self.extra_cbor.is_empty() {
            // Fast path: no extra fields to merge.
            let count = 1u64;
            crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
            crate::cbor::Encoder::new(&mut *buf).encode_text("val")?;
            crate::cbor::Encoder::new(&mut *buf).encode_text(&self.val)?;
        } else {
            // Slow path: merge known fields with extra_cbor, sort, encode.
            let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
            {
                let mut vbuf = Vec::new();
                crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.val)?;
                pairs.push(("val", vbuf));
            }
            for (k, v) in &self.extra_cbor {
                pairs.push((k.as_str(), v.clone()));
            }
            pairs.sort_by(|a, b| crate::cbor::cbor_key_cmp(a.0, b.0));
            crate::cbor::Encoder::new(&mut *buf).encode_map_header(pairs.len() as u64)?;
            for (k, v) in &pairs {
                crate::cbor::Encoder::new(&mut *buf).encode_text(k)?;
                buf.extend_from_slice(v);
            }
        }
        Ok(())
    }

    pub fn from_cbor(data: &[u8]) -> Result<Self, crate::cbor::CborError> {
        let mut decoder = crate::cbor::Decoder::new(data);
        let result = Self::decode_cbor(&mut decoder)?;
        if !decoder.is_empty() {
            return Err(crate::cbor::CborError::InvalidCbor("trailing data".into()));
        }
        Ok(result)
    }

    pub fn decode_cbor(decoder: &mut crate::cbor::Decoder) -> Result<Self, crate::cbor::CborError> {
        let val = decoder.decode()?;
        let entries = match val {
            crate::cbor::Value::Map(entries) => entries,
            _ => return Err(crate::cbor::CborError::InvalidCbor("expected map".into())),
        };

        let mut field_val: Option<String> = None;
        let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();

        for (key, value) in entries {
            match key {
                "val" => {
                    if let crate::cbor::Value::Text(s) = value {
                        field_val = Some(s.to_string());
                    } else {
                        return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
                    }
                }
                _ => {
                    let raw = crate::cbor::encode_value(&value)?;
                    extra_cbor.push((key.to_string(), raw));
                }
            }
        }

        Ok(LabelDefsSelfLabel {
            val: field_val.ok_or_else(|| {
                crate::cbor::CborError::InvalidCbor("missing required field 'val'".into())
            })?,
            extra: std::collections::HashMap::new(),
            extra_cbor,
        })
    }
}

/// LabelDefsSelfLabels — Metadata tags on an atproto record, published by the author within the record.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LabelDefsSelfLabels {
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub values: Vec<LabelDefsSelfLabel>,
    /// Extra fields not defined in the schema (JSON).
    #[serde(flatten)]
    pub extra: std::collections::HashMap<String, serde_json::Value>,
    /// Extra fields not defined in the schema (CBOR).
    #[serde(skip)]
    pub extra_cbor: Vec<(String, Vec<u8>)>,
}

impl LabelDefsSelfLabels {
    pub fn to_cbor(&self) -> Result<Vec<u8>, crate::cbor::CborError> {
        let mut buf = Vec::new();
        self.encode_cbor(&mut buf)?;
        Ok(buf)
    }

    pub fn encode_cbor(&self, buf: &mut Vec<u8>) -> Result<(), crate::cbor::CborError> {
        if self.extra_cbor.is_empty() {
            // Fast path: no extra fields to merge.
            let count = 1u64;
            crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
            crate::cbor::Encoder::new(&mut *buf).encode_text("values")?;
            crate::cbor::Encoder::new(&mut *buf).encode_array_header(self.values.len() as u64)?;
            for item in &self.values {
                item.encode_cbor(buf)?;
            }
        } else {
            // Slow path: merge known fields with extra_cbor, sort, encode.
            let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
            {
                let mut vbuf = Vec::new();
                crate::cbor::Encoder::new(&mut vbuf)
                    .encode_array_header(self.values.len() as u64)?;
                for item in &self.values {
                    item.encode_cbor(&mut vbuf)?;
                }
                pairs.push(("values", vbuf));
            }
            for (k, v) in &self.extra_cbor {
                pairs.push((k.as_str(), v.clone()));
            }
            pairs.sort_by(|a, b| crate::cbor::cbor_key_cmp(a.0, b.0));
            crate::cbor::Encoder::new(&mut *buf).encode_map_header(pairs.len() as u64)?;
            for (k, v) in &pairs {
                crate::cbor::Encoder::new(&mut *buf).encode_text(k)?;
                buf.extend_from_slice(v);
            }
        }
        Ok(())
    }

    pub fn from_cbor(data: &[u8]) -> Result<Self, crate::cbor::CborError> {
        let mut decoder = crate::cbor::Decoder::new(data);
        let result = Self::decode_cbor(&mut decoder)?;
        if !decoder.is_empty() {
            return Err(crate::cbor::CborError::InvalidCbor("trailing data".into()));
        }
        Ok(result)
    }

    pub fn decode_cbor(decoder: &mut crate::cbor::Decoder) -> Result<Self, crate::cbor::CborError> {
        let val = decoder.decode()?;
        let entries = match val {
            crate::cbor::Value::Map(entries) => entries,
            _ => return Err(crate::cbor::CborError::InvalidCbor("expected map".into())),
        };

        let mut field_values: Vec<LabelDefsSelfLabel> = Vec::new();
        let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();

        for (key, value) in entries {
            match key {
                "values" => {
                    if let crate::cbor::Value::Array(items) = value {
                        for item in items {
                            let raw = crate::cbor::encode_value(&item)?;
                            let mut dec = crate::cbor::Decoder::new(&raw);
                            field_values.push(LabelDefsSelfLabel::decode_cbor(&mut dec)?);
                        }
                    } else {
                        return Err(crate::cbor::CborError::InvalidCbor("expected array".into()));
                    }
                }
                _ => {
                    let raw = crate::cbor::encode_value(&value)?;
                    extra_cbor.push((key.to_string(), raw));
                }
            }
        }

        Ok(LabelDefsSelfLabels {
            values: field_values,
            extra: std::collections::HashMap::new(),
            extra_cbor,
        })
    }
}