citum-schema-data 0.53.3

Citum bibliographic data schema 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
/*
SPDX-License-Identifier: MIT OR Apache-2.0
SPDX-FileCopyrightText: © 2023-2026 Bruce D'Arcus
*/

//! Shared primitive types used across all reference categories.
//!
//! Includes multilingual string support, title representation, date types,
//! and reusable metadata structs (archive, eprint).

use crate::reference::contributor::Contributor;
#[cfg(feature = "schema")]
use schemars::JsonSchema;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[cfg(feature = "bindings")]
use specta::Type;
use std::borrow::Borrow;
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt;
use std::fmt::{Display, Formatter};
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::str::FromStr;
use url::Url;

/// Unique identifier for a reference item.
#[derive(Debug, Deserialize, Serialize, Clone, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[cfg_attr(feature = "bindings", derive(Type))]
#[serde(transparent)]
pub struct RefID(pub String);

impl RefID {
    /// Borrow the reference identifier as a string slice.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl Display for RefID {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

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

impl Borrow<str> for RefID {
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

impl Deref for RefID {
    type Target = str;

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

impl From<String> for RefID {
    fn from(value: String) -> Self {
        Self(value)
    }
}

impl From<&str> for RefID {
    fn from(value: &str) -> Self {
        Self(value.to_string())
    }
}

impl From<RefID> for String {
    fn from(value: RefID) -> Self {
        value.0
    }
}

impl FromStr for RefID {
    type Err = std::convert::Infallible;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        Ok(Self::from(value))
    }
}

impl PartialEq<&str> for RefID {
    fn eq(&self, other: &&str) -> bool {
        self.as_str() == *other
    }
}

impl PartialEq<str> for RefID {
    fn eq(&self, other: &str) -> bool {
        self.as_str() == other
    }
}

impl PartialEq<String> for RefID {
    fn eq(&self, other: &String) -> bool {
        self.as_str() == other
    }
}

impl PartialEq<RefID> for &str {
    fn eq(&self, other: &RefID) -> bool {
        *self == other.as_str()
    }
}

impl PartialEq<RefID> for String {
    fn eq(&self, other: &RefID) -> bool {
        self.as_str() == other.as_str()
    }
}

/// Geographic place name used for publication and archive locations.
#[derive(Debug, Deserialize, Serialize, Clone, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[cfg_attr(feature = "bindings", derive(Type))]
#[serde(transparent)]
pub struct Place(
    /// The rendered place string, such as a city or city-country label.
    pub String,
);

impl Place {
    /// Borrow the place name as a string slice.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl Display for Place {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

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

impl Borrow<str> for Place {
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

impl Deref for Place {
    type Target = str;

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

impl From<String> for Place {
    fn from(value: String) -> Self {
        Self(value)
    }
}

impl From<&str> for Place {
    fn from(value: &str) -> Self {
        Self(value.to_string())
    }
}

impl From<Place> for String {
    fn from(value: Place) -> Self {
        value.0
    }
}

impl FromStr for Place {
    type Err = std::convert::Infallible;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        Ok(Self::from(value))
    }
}

impl PartialEq<&str> for Place {
    fn eq(&self, other: &&str) -> bool {
        self.as_str() == *other
    }
}

impl PartialEq<str> for Place {
    fn eq(&self, other: &str) -> bool {
        self.as_str() == other
    }
}

impl PartialEq<String> for Place {
    fn eq(&self, other: &String) -> bool {
        self.as_str() == other
    }
}

impl PartialEq<Place> for &str {
    fn eq(&self, other: &Place) -> bool {
        *self == other.as_str()
    }
}

impl PartialEq<Place> for String {
    fn eq(&self, other: &Place) -> bool {
        self.as_str() == other.as_str()
    }
}

/// Inline markup for freeform text fields (note, abstract).
///
/// Serializes from a plain string or a `{ djot: "..." }` object.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[cfg_attr(feature = "bindings", derive(Type))]
#[serde(untagged)]
pub enum RichText {
    /// Plain text with no inline markup.
    Plain(String),
    /// Djot inline markup.
    Djot {
        /// Djot inline markup source.
        djot: String,
    },
}

impl Default for RichText {
    fn default() -> Self {
        RichText::Plain(String::new())
    }
}

impl RichText {
    /// Return the raw source string regardless of format.
    pub fn raw(&self) -> &str {
        match self {
            RichText::Plain(s) | RichText::Djot { djot: s } => s,
        }
    }

    /// Return true when the content is empty.
    pub fn is_empty(&self) -> bool {
        self.raw().is_empty()
    }
}

/// A numbering identifier for a work (e.g., volume, issue, number).
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[cfg_attr(feature = "bindings", derive(Type))]
pub struct Numbering {
    /// The type of the numbering.
    pub r#type: NumberingType,
    /// The value of the numbering (e.g., "4", "B").
    pub value: String,
}

/// Controlled vocabulary for numbering types.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "bindings", derive(Type))]
#[non_exhaustive]
pub enum NumberingType {
    /// A volume number.
    Volume,
    /// An issue number.
    Issue,
    /// A generic document number.
    Number,
    /// A report identifier.
    Report,
    /// A part number.
    Part,
    /// A supplement number.
    Supplement,
    /// A printing run identifier.
    Printing,
    /// A chapter number.
    Chapter,
    /// A section identifier.
    Section,
    /// An edition identifier.
    Edition,
    /// A season number.
    Season,
    /// An episode number.
    Episode,
    /// A custom numbering identifier for domain-specific kinds.
    Custom(String),
}

impl NumberingType {
    /// Return the canonical kebab-case key for this numbering kind.
    #[must_use]
    pub fn as_key(&self) -> Cow<'_, str> {
        match self {
            Self::Volume => Cow::Borrowed("volume"),
            Self::Issue => Cow::Borrowed("issue"),
            Self::Number => Cow::Borrowed("number"),
            Self::Report => Cow::Borrowed("report"),
            Self::Part => Cow::Borrowed("part"),
            Self::Supplement => Cow::Borrowed("supplement"),
            Self::Printing => Cow::Borrowed("printing"),
            Self::Chapter => Cow::Borrowed("chapter"),
            Self::Section => Cow::Borrowed("section"),
            Self::Edition => Cow::Borrowed("edition"),
            Self::Season => Cow::Borrowed("season"),
            Self::Episode => Cow::Borrowed("episode"),
            Self::Custom(value) => normalize_kind_key(value)
                .map(Cow::Owned)
                .unwrap_or_else(|| Cow::Borrowed(value.as_str())),
        }
    }

    /// Parse a numbering kind from a known keyword or custom identifier.
    ///
    /// # Errors
    ///
    /// Returns an error when the input is empty or normalizes to an empty key.
    pub fn from_key(value: &str) -> Result<Self, String> {
        let canonical = normalize_kind_key(value)
            .ok_or_else(|| "numbering kind must not be empty".to_string())?;
        Ok(match canonical.as_str() {
            "volume" => Self::Volume,
            "issue" => Self::Issue,
            "number" => Self::Number,
            "report" => Self::Report,
            "part" => Self::Part,
            "supplement" => Self::Supplement,
            "printing" => Self::Printing,
            "chapter" => Self::Chapter,
            "section" => Self::Section,
            "edition" => Self::Edition,
            "season" => Self::Season,
            "episode" => Self::Episode,
            _ => Self::Custom(canonical),
        })
    }
}

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

impl Eq for NumberingType {}

impl Hash for NumberingType {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.as_key().as_ref().hash(state);
    }
}

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

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

#[cfg(feature = "schema")]
impl JsonSchema for NumberingType {
    fn schema_name() -> std::borrow::Cow<'static, str> {
        "NumberingType".into()
    }

    fn json_schema(_gen: &mut schemars::SchemaGenerator) -> schemars::Schema {
        schemars::json_schema!({
            "type": "string",
            "description": "Known numbering kind keyword or custom kebab-case identifier."
        })
    }
}

pub(crate) trait HasNumbering {
    fn numbering(&self) -> &[Numbering];

    fn find_numbering(&self, numbering_type: NumberingType) -> Option<String> {
        self.numbering()
            .iter()
            .find(|numbering| numbering.r#type == numbering_type)
            .map(|numbering| numbering.value.clone())
    }
}

pub(crate) trait NormalizeNumbering {
    fn numbering_mut(&mut self) -> &mut Vec<Numbering>;
    fn volume_mut(&mut self) -> &mut Option<String>;
    fn issue_mut(&mut self) -> &mut Option<String>;
    fn edition_mut(&mut self) -> &mut Option<String>;
    fn number_mut(&mut self) -> &mut Option<String>;
    fn part_number_mut(&mut self) -> &mut Option<String>;
    fn supplement_number_mut(&mut self) -> &mut Option<String>;
    fn printing_number_mut(&mut self) -> &mut Option<String>;

    fn normalize_numbering(&mut self) {
        let volume = self.volume_mut().take();
        let issue = self.issue_mut().take();
        let edition = self.edition_mut().take();
        let number = self.number_mut().take();
        let part_number = self.part_number_mut().take();
        let supplement_number = self.supplement_number_mut().take();
        let printing_number = self.printing_number_mut().take();
        let has_volume = volume.is_some();
        let has_issue = issue.is_some();
        let has_edition = edition.is_some();
        let has_number = number.is_some();
        let has_part = part_number.is_some();
        let has_supplement = supplement_number.is_some();
        let has_printing = printing_number.is_some();
        let existing = std::mem::take(self.numbering_mut());

        let mut normalized = Vec::with_capacity(existing.len() + 7);
        let mut push_shorthand = |r#type, value: Option<String>| {
            if let Some(value) = value {
                normalized.push(Numbering { r#type, value });
            }
        };

        push_shorthand(NumberingType::Volume, volume);
        push_shorthand(NumberingType::Issue, issue);
        push_shorthand(NumberingType::Edition, edition);
        push_shorthand(NumberingType::Number, number);
        push_shorthand(NumberingType::Part, part_number);
        push_shorthand(NumberingType::Supplement, supplement_number);
        push_shorthand(NumberingType::Printing, printing_number);

        normalized.extend(existing.into_iter().filter(|entry| match entry.r#type {
            NumberingType::Volume => !has_volume,
            NumberingType::Issue => !has_issue,
            NumberingType::Edition => !has_edition,
            NumberingType::Number => !has_number,
            NumberingType::Part => !has_part,
            NumberingType::Supplement => !has_supplement,
            NumberingType::Printing => !has_printing,
            _ => true,
        }));

        *self.numbering_mut() = normalized;
    }
}

fn normalize_kind_key(value: &str) -> Option<String> {
    let mut normalized = String::new();
    let mut pending_dash = false;

    for ch in value.trim().chars() {
        if ch.is_ascii_alphanumeric() {
            if pending_dash && !normalized.is_empty() {
                normalized.push('-');
            }
            normalized.push(ch.to_ascii_lowercase());
            pending_dash = false;
        } else if !normalized.is_empty() {
            pending_dash = true;
        }
    }

    if normalized.is_empty() {
        None
    } else {
        Some(normalized)
    }
}

/// BCP 47 language tag (e.g., `"en"`, `"de"`, `"ja"`).
#[derive(Debug, Deserialize, Serialize, Clone, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[cfg_attr(feature = "bindings", derive(Type))]
#[serde(transparent)]
pub struct LangID(pub String);

impl LangID {
    /// Borrow the language identifier as a string slice.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl Display for LangID {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

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

impl Borrow<str> for LangID {
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

impl Deref for LangID {
    type Target = str;

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

impl From<String> for LangID {
    fn from(value: String) -> Self {
        Self(value)
    }
}

impl From<&str> for LangID {
    fn from(value: &str) -> Self {
        Self(value.to_string())
    }
}

impl From<LangID> for String {
    fn from(value: LangID) -> Self {
        value.0
    }
}

impl FromStr for LangID {
    type Err = std::convert::Infallible;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        Ok(Self::from(value))
    }
}

impl PartialEq<&str> for LangID {
    fn eq(&self, other: &&str) -> bool {
        self.as_str() == *other
    }
}

impl PartialEq<str> for LangID {
    fn eq(&self, other: &str) -> bool {
        self.as_str() == other
    }
}

impl PartialEq<String> for LangID {
    fn eq(&self, other: &String) -> bool {
        self.as_str() == other
    }
}

impl PartialEq<LangID> for &str {
    fn eq(&self, other: &LangID) -> bool {
        *self == other.as_str()
    }
}

impl PartialEq<LangID> for String {
    fn eq(&self, other: &LangID) -> bool {
        self.as_str() == other.as_str()
    }
}
/// Maps field names to their language tags for multilingual references.
pub type FieldLanguageMap = HashMap<String, LangID>;

/// A value that could be either a number or a string.
///
/// Used for fields that may contain numeric or string values, such as issue numbers,
/// volume numbers, or similar identifiers that can be formatted as either type.
/// The `Display` implementation shows the value in its appropriate form.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[cfg_attr(feature = "bindings", derive(Type))]
#[serde(untagged)]
pub enum NumOrStr {
    /// A numeric value.
    Number(i64),
    /// A string value.
    Str(String),
}

impl Display for NumOrStr {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
        match self {
            Self::Number(i) => write!(f, "{}", i),
            Self::Str(s) => write!(f, "{}", s),
        }
    }
}

/// A string that can be represented in multiple languages and scripts.
///
/// This is an enum that supports both simple strings and complex multilingual representations.
/// Use `Simple` for basic strings and `Complex` when you need to track original language,
/// transliterations, and translations.
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[cfg_attr(feature = "bindings", derive(Type))]
#[serde(untagged)]
pub enum MultilingualString {
    /// A simple string in a single language.
    Simple(String),
    /// A complex multilingual string with original, transliterations, and translations.
    Complex(MultilingualComplex),
}

/// Complex multilingual representation with original, transliterations, and translations.
///
/// Allows capturing the original text in its native script, along with transliterations
/// (phonetic representations in different scripts) and translations (semantic equivalents
/// in different languages). This is essential for accurately rendering bibliographies in
/// multilingual and non-Latin-script contexts.
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[cfg_attr(feature = "bindings", derive(Type))]
#[serde(rename_all = "kebab-case")]
pub struct MultilingualComplex {
    /// The text in its original script and language.
    pub original: String,
    /// ISO 639/BCP 47 language code for the original text.
    pub lang: Option<LangID>,
    /// Transliterations/Transcriptions of the original text into other scripts.
    ///
    /// Keys are typically script codes (e.g., "Latn" for Latin) or full BCP 47 tags.
    /// Values are the transliterated or transcribed text.
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub transliterations: HashMap<String, String>,
    /// Translations of the text into other languages.
    ///
    /// Keys are ISO 639/BCP 47 language codes (e.g., "en" for English, "fr" for French).
    /// Values are the translated text.
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub translations: HashMap<LangID, String>,
}

impl From<String> for MultilingualString {
    fn from(s: String) -> Self {
        Self::Simple(s)
    }
}

impl From<&str> for MultilingualString {
    fn from(s: &str) -> Self {
        Self::Simple(s.to_string())
    }
}

impl Display for MultilingualString {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
        match self {
            Self::Simple(s) => write!(f, "{}", s),
            Self::Complex(c) => write!(f, "{}", c.original),
        }
    }
}

impl Default for MultilingualString {
    fn default() -> Self {
        Self::Simple(String::new())
    }
}

/// Structured archival location metadata for unpublished and archival material.
///
/// Models the hierarchical location of an item within an archive, following
/// common archival description standards (DACS, ISAD(G), EAD).
#[derive(Debug, Default, Deserialize, Serialize, Clone, PartialEq)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[cfg_attr(feature = "bindings", derive(Type))]
#[serde(rename_all = "kebab-case")]
pub struct ArchiveInfo {
    /// Name of the archive or repository holding the material.
    ///
    /// Uses `MultilingualString` to support international institution names
    /// (e.g., 国立国会図書館 / National Diet Library).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<MultilingualString>,
    /// Geographic location (city/country) of the archive.
    ///
    /// Parallel to `SimpleName.location`; both carry geographic-place semantics.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub place: Option<Place>,
    /// Name of the collection within the archive.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub collection: Option<String>,
    /// Identifier for the collection (call number, accession number, etc.).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub collection_id: Option<String>,
    /// Name of the series within the collection.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub series: Option<String>,
    /// Box number within the collection.
    ///
    /// Uses raw identifier syntax `r#box`, which serde serializes as `"box"` transparently.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub r#box: Option<String>,
    /// Folder number within the box.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub folder: Option<String>,
    /// Item identifier within the folder.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub item: Option<String>,
    /// Display override for the archival location (shelfmark, call number, or complex location string).
    ///
    /// When present, overrides the structured fields for display. Acts as the legacy
    /// `archive_location` fallback for complex shelfmarks that don't fit the structured model.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub location: Option<String>,
    /// URL for the archival item (e.g. digitized finding aid or item page).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub url: Option<Url>,
}

/// Preprint server identifier following the biblatex eprint model.
///
/// Used on all three reference classes (Monograph, CollectionComponent, SerialComponent)
/// because journal articles routinely carry arXiv or similar identifiers.
#[derive(Debug, Default, Deserialize, Serialize, Clone, PartialEq)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[cfg_attr(feature = "bindings", derive(Type))]
#[serde(rename_all = "kebab-case")]
pub struct EprintInfo {
    /// The identifier on the preprint server (e.g. "2301.00001").
    pub id: String,
    /// The preprint server name in canonical lowercase form.
    ///
    /// Producers may supply mixed-case values such as "arXiv" or "SSRN";
    /// implementations should treat the field case-insensitively and compare
    /// or normalize on the lowercase form.
    pub server: String,
    /// Subject class or category used by the server (e.g. "cs.CL").
    #[serde(skip_serializing_if = "Option::is_none")]
    pub class: Option<String>,
}

/// A publisher or production company.
///
/// Publisher is always a corporate entity — a person is never a publisher
/// in the bibliographic sense.
#[derive(Debug, Default, Deserialize, Serialize, Clone, PartialEq)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[cfg_attr(feature = "bindings", derive(Type))]
#[serde(rename_all = "kebab-case")]
#[serde(from = "PublisherCompat")]
pub struct Publisher {
    /// Publisher name.
    pub name: MultilingualString,
    /// Geographic place of publication (city, country).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub place: Option<Place>,
}

#[derive(Debug, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[cfg_attr(feature = "bindings", derive(Type))]
#[serde(untagged)]
enum PublisherCompat {
    PublisherObject {
        name: MultilingualString,
        place: Option<Place>,
    },
    Contributor(Box<Contributor>),
    Name(MultilingualString),
    String(String),
}

impl From<PublisherCompat> for Publisher {
    fn from(value: PublisherCompat) -> Self {
        match value {
            PublisherCompat::PublisherObject { name, place } => Self { name, place },
            PublisherCompat::Contributor(contributor) => match *contributor {
                Contributor::SimpleName(name) => Self {
                    name: name.name,
                    place: name.location,
                },
                Contributor::StructuredName(name) => Self {
                    name: format!("{} {}", name.given, name.family).into(),
                    place: None,
                },
                Contributor::Multilingual(name) => Self {
                    name: format!("{} {}", name.original.given, name.original.family).into(),
                    place: None,
                },
                Contributor::ContributorList(list) => Self {
                    name: list.to_string().into(),
                    place: None,
                },
            },
            PublisherCompat::Name(name) => Self { name, place: None },
            PublisherCompat::String(name) => Self {
                name: name.into(),
                place: None,
            },
        }
    }
}

/// A title can be a single string, a structured title, or a multilingual title.
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[cfg_attr(feature = "bindings", derive(Type))]
#[serde(untagged)]
pub enum Title {
    /// A title in a single language.
    Single(String),
    /// A structured title.
    Structured(StructuredTitle),
    /// A complex multilingual title.
    Multilingual(MultilingualComplex),
    /// A title in multiple languages.
    Multi(Vec<(LangID, String)>),
    /// A structured title in multiple languages.
    MultiStructured(Vec<(LangID, StructuredTitle)>),
    /// An abbreviated title (shorthand, full).
    Shorthand(String, String),
}

/// Where title parts are meaningful, use this struct; Citum processors will not parse title strings.
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[cfg_attr(feature = "bindings", derive(Type))]
#[serde(rename_all = "kebab-case")]
pub struct StructuredTitle {
    /// Full rendered title string (optional override).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub full: Option<String>,
    /// Main title component.
    pub main: String,
    /// Subtitle component.
    pub sub: Subtitle,
}

/// The subtitle can either be a string, as is the common case, or a vector of strings.
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[cfg_attr(feature = "bindings", derive(Type))]
#[serde(untagged)]
pub enum Subtitle {
    /// A single subtitle string.
    String(String),
    /// Multiple subtitle strings.
    Vector(Vec<String>),
}

impl fmt::Display for Title {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Title::Single(s) => write!(f, "{}", s),
            Title::Multi(_m) => write!(f, "[multilingual title]"),
            Title::Multilingual(m) => write!(f, "{}", m.original),
            Title::Structured(s) => {
                let subtitle = match &s.sub {
                    Subtitle::String(s) => s.clone(),
                    Subtitle::Vector(v) => v.join(", "),
                };
                write!(f, "{}: {}", s.main, subtitle)
            }
            Title::MultiStructured(_m) => write!(f, "[multilingual structured title]"),
            Title::Shorthand(s, t) => write!(f, "{} ({})", s, t),
        }
    }
}

/// Date type.
#[derive(Debug, Clone, PartialEq)]
pub enum RefDate {
    /// A parsed EDTF date.
    Edtf(citum_edtf::Edtf),
    /// A literal date string that could not be parsed as EDTF.
    Literal(String),
}

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::indexing_slicing,
    clippy::todo,
    clippy::unimplemented,
    clippy::unreachable,
    clippy::get_unwrap,
    reason = "Panicking is acceptable and often desired in tests."
)]
mod tests {
    use super::{LangID, RefID};
    #[cfg(feature = "schema")]
    use schemars::schema_for;
    use std::collections::HashMap;

    #[test]
    fn ref_id_round_trips_as_a_scalar_string() {
        let id = RefID::from("kuhn1962");
        let serialized = serde_json::to_string(&id).expect("ref id should serialize");
        let deserialized: RefID =
            serde_json::from_str(&serialized).expect("ref id should deserialize");

        assert_eq!(serialized, "\"kuhn1962\"");
        assert_eq!(deserialized, "kuhn1962");
        assert_eq!(deserialized.as_ref(), "kuhn1962");
        assert_eq!(String::from(deserialized), "kuhn1962");
    }

    #[test]
    fn lang_id_supports_string_lookup_and_parsing() {
        let lang = "en-US".parse::<LangID>().expect("lang id should parse");
        let mut translations = HashMap::new();
        translations.insert(lang.clone(), "English".to_string());

        assert_eq!(lang, "en-US");
        assert_eq!(
            translations.get("en-US").map(String::as_str),
            Some("English")
        );
    }

    #[cfg(feature = "schema")]
    #[test]
    fn newtypes_stay_string_shaped_in_json_schema() {
        let ref_schema = schema_for!(RefID);
        let lang_schema = schema_for!(LangID);
        let place_schema = schema_for!(super::Place);

        assert_eq!(ref_schema.to_value()["type"], "string");
        assert_eq!(lang_schema.to_value()["type"], "string");
        assert_eq!(place_schema.to_value()["type"], "string");
    }
}