oxigdal-metadata 0.1.0

Metadata standards support for OxiGDAL - ISO 19115, FGDC, INSPIRE, DataCite, DCAT
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
//! DataCite metadata for research data DOIs.
//!
//! This module implements the DataCite Metadata Schema for publication
//! and citation of research data and other resources.
//!
//! # Overview
//!
//! DataCite provides a standard for describing research data with
//! Digital Object Identifiers (DOIs). The schema includes:
//! - Resource identification
//! - Creators and contributors
//! - Publication information
//! - Subject and classification
//! - Rights and licensing
//!
//! # Examples
//!
//! ```no_run
//! use oxigdal_metadata::datacite::*;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let metadata = DataCiteMetadata::builder()
//!     .identifier("10.5281/zenodo.123456", IdentifierType::Doi)
//!     .title("Research Dataset")
//!     .creator(Creator::new("Smith, John"))
//!     .publisher("Zenodo")
//!     .publication_year(2024)
//!     .resource_type(ResourceTypeGeneral::Dataset)
//!     .build()?;
//! # Ok(())
//! # }
//! ```

use crate::error::{MetadataError, Result};
use serde::{Deserialize, Serialize};

/// DataCite metadata record (Schema 4.4).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataCiteMetadata {
    /// Identifier (mandatory)
    pub identifier: Identifier,
    /// Creators (mandatory)
    pub creators: Vec<Creator>,
    /// Titles (mandatory)
    pub titles: Vec<Title>,
    /// Publisher (mandatory)
    pub publisher: String,
    /// Publication year (mandatory)
    pub publication_year: u16,
    /// Resource type (mandatory)
    pub resource_type: ResourceType,
    /// Subjects (recommended)
    pub subjects: Vec<Subject>,
    /// Contributors (optional)
    pub contributors: Vec<Contributor>,
    /// Dates (recommended)
    pub dates: Vec<Date>,
    /// Language (optional)
    pub language: Option<String>,
    /// Alternate identifiers (optional)
    pub alternate_identifiers: Vec<AlternateIdentifier>,
    /// Related identifiers (recommended)
    pub related_identifiers: Vec<RelatedIdentifier>,
    /// Sizes (optional)
    pub sizes: Vec<String>,
    /// Formats (optional)
    pub formats: Vec<String>,
    /// Version (optional)
    pub version: Option<String>,
    /// Rights (optional)
    pub rights_list: Vec<Rights>,
    /// Descriptions (recommended)
    pub descriptions: Vec<Description>,
    /// Geo locations (recommended)
    pub geo_locations: Vec<GeoLocation>,
    /// Funding references (optional)
    pub funding_references: Vec<FundingReference>,
}

/// Builder for DataCite metadata.
pub struct DataCiteBuilder {
    identifier: Option<Identifier>,
    creators: Vec<Creator>,
    titles: Vec<Title>,
    publisher: Option<String>,
    publication_year: Option<u16>,
    resource_type: Option<ResourceType>,
    subjects: Vec<Subject>,
    contributors: Vec<Contributor>,
    dates: Vec<Date>,
    language: Option<String>,
    alternate_identifiers: Vec<AlternateIdentifier>,
    related_identifiers: Vec<RelatedIdentifier>,
    sizes: Vec<String>,
    formats: Vec<String>,
    version: Option<String>,
    rights_list: Vec<Rights>,
    descriptions: Vec<Description>,
    geo_locations: Vec<GeoLocation>,
    funding_references: Vec<FundingReference>,
}

impl DataCiteMetadata {
    /// Create a new builder.
    pub fn builder() -> DataCiteBuilder {
        DataCiteBuilder {
            identifier: None,
            creators: Vec::new(),
            titles: Vec::new(),
            publisher: None,
            publication_year: None,
            resource_type: None,
            subjects: Vec::new(),
            contributors: Vec::new(),
            dates: Vec::new(),
            language: None,
            alternate_identifiers: Vec::new(),
            related_identifiers: Vec::new(),
            sizes: Vec::new(),
            formats: Vec::new(),
            version: None,
            rights_list: Vec::new(),
            descriptions: Vec::new(),
            geo_locations: Vec::new(),
            funding_references: Vec::new(),
        }
    }
}

impl DataCiteBuilder {
    /// Set the identifier.
    pub fn identifier(mut self, identifier: impl Into<String>, id_type: IdentifierType) -> Self {
        self.identifier = Some(Identifier {
            identifier: identifier.into(),
            identifier_type: id_type,
        });
        self
    }

    /// Add a creator.
    pub fn creator(mut self, creator: Creator) -> Self {
        self.creators.push(creator);
        self
    }

    /// Add a title.
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.titles.push(Title {
            title: title.into(),
            title_type: None,
            lang: None,
        });
        self
    }

    /// Set the publisher.
    pub fn publisher(mut self, publisher: impl Into<String>) -> Self {
        self.publisher = Some(publisher.into());
        self
    }

    /// Set the publication year.
    pub fn publication_year(mut self, year: u16) -> Self {
        self.publication_year = Some(year);
        self
    }

    /// Set the resource type.
    pub fn resource_type(mut self, resource_type: ResourceTypeGeneral) -> Self {
        self.resource_type = Some(ResourceType {
            resource_type_general: resource_type,
            resource_type: None,
        });
        self
    }

    /// Add a subject.
    pub fn subject(mut self, subject: Subject) -> Self {
        self.subjects.push(subject);
        self
    }

    /// Add a description.
    pub fn description(mut self, description: Description) -> Self {
        self.descriptions.push(description);
        self
    }

    /// Add rights information.
    pub fn rights(mut self, rights: Rights) -> Self {
        self.rights_list.push(rights);
        self
    }

    /// Build the metadata.
    pub fn build(self) -> Result<DataCiteMetadata> {
        let identifier = self
            .identifier
            .ok_or_else(|| MetadataError::MissingField("identifier".to_string()))?;

        if self.creators.is_empty() {
            return Err(MetadataError::MissingField("creators".to_string()));
        }

        if self.titles.is_empty() {
            return Err(MetadataError::MissingField("titles".to_string()));
        }

        let publisher = self
            .publisher
            .ok_or_else(|| MetadataError::MissingField("publisher".to_string()))?;

        let publication_year = self
            .publication_year
            .ok_or_else(|| MetadataError::MissingField("publication_year".to_string()))?;

        let resource_type = self
            .resource_type
            .ok_or_else(|| MetadataError::MissingField("resource_type".to_string()))?;

        Ok(DataCiteMetadata {
            identifier,
            creators: self.creators,
            titles: self.titles,
            publisher,
            publication_year,
            resource_type,
            subjects: self.subjects,
            contributors: self.contributors,
            dates: self.dates,
            language: self.language,
            alternate_identifiers: self.alternate_identifiers,
            related_identifiers: self.related_identifiers,
            sizes: self.sizes,
            formats: self.formats,
            version: self.version,
            rights_list: self.rights_list,
            descriptions: self.descriptions,
            geo_locations: self.geo_locations,
            funding_references: self.funding_references,
        })
    }
}

/// Identifier.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Identifier {
    /// Identifier value
    pub identifier: String,
    /// Identifier type
    pub identifier_type: IdentifierType,
}

/// Identifier type.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum IdentifierType {
    /// ARK
    ARK,
    /// arXiv
    ArXiv,
    /// Bibcode
    Bibcode,
    /// DOI
    #[serde(rename = "DOI")]
    Doi,
    /// EAN13
    EAN13,
    /// EISSN
    EISSN,
    /// Handle
    Handle,
    /// IGSN
    IGSN,
    /// ISBN
    ISBN,
    /// ISSN
    ISSN,
    /// ISTC
    ISTC,
    /// LISSN
    LISSN,
    /// LSID
    LSID,
    /// PMID
    PMID,
    /// PURL
    PURL,
    /// UPC
    UPC,
    /// URL
    URL,
    /// URN
    URN,
    /// Other
    Other,
}

/// Creator.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Creator {
    /// Creator name (mandatory)
    pub name: String,
    /// Name type
    pub name_type: Option<NameType>,
    /// Given name
    pub given_name: Option<String>,
    /// Family name
    pub family_name: Option<String>,
    /// Name identifiers
    pub name_identifiers: Vec<NameIdentifier>,
    /// Affiliations
    pub affiliations: Vec<String>,
}

impl Creator {
    /// Create a new creator.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            name_type: Some(NameType::Personal),
            given_name: None,
            family_name: None,
            name_identifiers: Vec::new(),
            affiliations: Vec::new(),
        }
    }

    /// Create an organizational creator.
    pub fn organization(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            name_type: Some(NameType::Organizational),
            given_name: None,
            family_name: None,
            name_identifiers: Vec::new(),
            affiliations: Vec::new(),
        }
    }
}

/// Name type.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum NameType {
    /// Organizational
    Organizational,
    /// Personal
    Personal,
}

/// Name identifier.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NameIdentifier {
    /// Name identifier
    pub name_identifier: String,
    /// Name identifier scheme
    pub name_identifier_scheme: String,
    /// Scheme URI
    pub scheme_uri: Option<String>,
}

/// Title.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Title {
    /// Title text
    pub title: String,
    /// Title type
    pub title_type: Option<TitleType>,
    /// Language
    pub lang: Option<String>,
}

/// Title type.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum TitleType {
    /// Alternative title
    AlternativeTitle,
    /// Subtitle
    Subtitle,
    /// Translated title
    TranslatedTitle,
    /// Other
    Other,
}

/// Resource type.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceType {
    /// Resource type general (mandatory)
    pub resource_type_general: ResourceTypeGeneral,
    /// Free text resource type
    pub resource_type: Option<String>,
}

/// Resource type general.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum ResourceTypeGeneral {
    /// Audiovisual
    Audiovisual,
    /// Book
    Book,
    /// Book chapter
    BookChapter,
    /// Collection
    Collection,
    /// Computational notebook
    ComputationalNotebook,
    /// Conference paper
    ConferencePaper,
    /// Conference proceeding
    ConferenceProceeding,
    /// Data paper
    DataPaper,
    /// Dataset
    Dataset,
    /// Dissertation
    Dissertation,
    /// Event
    Event,
    /// Image
    Image,
    /// Interactive resource
    InteractiveResource,
    /// Journal
    Journal,
    /// Journal article
    JournalArticle,
    /// Model
    Model,
    /// Output management plan
    OutputManagementPlan,
    /// Peer review
    PeerReview,
    /// Physical object
    PhysicalObject,
    /// Preprint
    Preprint,
    /// Report
    Report,
    /// Service
    Service,
    /// Software
    Software,
    /// Sound
    Sound,
    /// Standard
    Standard,
    /// Text
    Text,
    /// Workflow
    Workflow,
    /// Other
    Other,
}

/// Subject.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Subject {
    /// Subject text
    pub subject: String,
    /// Subject scheme
    pub subject_scheme: Option<String>,
    /// Scheme URI
    pub scheme_uri: Option<String>,
    /// Value URI
    pub value_uri: Option<String>,
    /// Classification code
    pub classification_code: Option<String>,
}

impl Subject {
    /// Create a new subject.
    pub fn new(subject: impl Into<String>) -> Self {
        Self {
            subject: subject.into(),
            subject_scheme: None,
            scheme_uri: None,
            value_uri: None,
            classification_code: None,
        }
    }
}

/// Contributor.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Contributor {
    /// Contributor name
    pub name: String,
    /// Contributor type
    pub contributor_type: ContributorType,
    /// Name type
    pub name_type: Option<NameType>,
    /// Given name
    pub given_name: Option<String>,
    /// Family name
    pub family_name: Option<String>,
    /// Name identifiers
    pub name_identifiers: Vec<NameIdentifier>,
    /// Affiliations
    pub affiliations: Vec<String>,
}

/// Contributor type.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum ContributorType {
    /// Contact person
    ContactPerson,
    /// Data collector
    DataCollector,
    /// Data curator
    DataCurator,
    /// Data manager
    DataManager,
    /// Distributor
    Distributor,
    /// Editor
    Editor,
    /// Hosting institution
    HostingInstitution,
    /// Producer
    Producer,
    /// Project leader
    ProjectLeader,
    /// Project manager
    ProjectManager,
    /// Project member
    ProjectMember,
    /// Registration agency
    RegistrationAgency,
    /// Registration authority
    RegistrationAuthority,
    /// Related person
    RelatedPerson,
    /// Researcher
    Researcher,
    /// Research group
    ResearchGroup,
    /// Rights holder
    RightsHolder,
    /// Sponsor
    Sponsor,
    /// Supervisor
    Supervisor,
    /// Work package leader
    WorkPackageLeader,
    /// Other
    Other,
}

/// Date.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Date {
    /// Date value
    pub date: String,
    /// Date type
    pub date_type: DateType,
    /// Date information
    pub date_information: Option<String>,
}

/// Date type.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum DateType {
    /// Accepted
    Accepted,
    /// Available
    Available,
    /// Collected
    Collected,
    /// Copyrighted
    Copyrighted,
    /// Created
    Created,
    /// Issued
    Issued,
    /// Submitted
    Submitted,
    /// Updated
    Updated,
    /// Valid
    Valid,
    /// Withdrawn
    Withdrawn,
    /// Other
    Other,
}

/// Alternate identifier.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlternateIdentifier {
    /// Alternate identifier
    pub alternate_identifier: String,
    /// Alternate identifier type
    pub alternate_identifier_type: String,
}

/// Related identifier.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RelatedIdentifier {
    /// Related identifier
    pub related_identifier: String,
    /// Related identifier type
    pub related_identifier_type: IdentifierType,
    /// Relation type
    pub relation_type: RelationType,
    /// Related metadata scheme
    pub related_metadata_scheme: Option<String>,
    /// Scheme URI
    pub scheme_uri: Option<String>,
    /// Scheme type
    pub scheme_type: Option<String>,
    /// Resource type general
    pub resource_type_general: Option<ResourceTypeGeneral>,
}

/// Relation type.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum RelationType {
    /// Is cited by
    IsCitedBy,
    /// Cites
    Cites,
    /// Is supplement to
    IsSupplementTo,
    /// Is supplemented by
    IsSupplementedBy,
    /// Is continued by
    IsContinuedBy,
    /// Continues
    Continues,
    /// Describes
    Describes,
    /// Is described by
    IsDescribedBy,
    /// Has metadata
    HasMetadata,
    /// Is metadata for
    IsMetadataFor,
    /// Has version
    HasVersion,
    /// Is version of
    IsVersionOf,
    /// Is new version of
    IsNewVersionOf,
    /// Is previous version of
    IsPreviousVersionOf,
    /// Is part of
    IsPartOf,
    /// Has part
    HasPart,
    /// Is referenced by
    IsReferencedBy,
    /// References
    References,
    /// Is documented by
    IsDocumentedBy,
    /// Documents
    Documents,
    /// Is compiled by
    IsCompiledBy,
    /// Compiles
    Compiles,
    /// Is variant form of
    IsVariantFormOf,
    /// Is original form of
    IsOriginalFormOf,
    /// Is identical to
    IsIdenticalTo,
    /// Is reviewed by
    IsReviewedBy,
    /// Reviews
    Reviews,
    /// Is derived from
    IsDerivedFrom,
    /// Is source of
    IsSourceOf,
    /// Is required by
    IsRequiredBy,
    /// Requires
    Requires,
    /// Obsoletes
    Obsoletes,
    /// Is obsoleted by
    IsObsoletedBy,
}

/// Rights.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Rights {
    /// Rights text
    pub rights: Option<String>,
    /// Rights URI
    pub rights_uri: Option<String>,
    /// Rights identifier
    pub rights_identifier: Option<String>,
    /// Rights identifier scheme
    pub rights_identifier_scheme: Option<String>,
    /// Scheme URI
    pub scheme_uri: Option<String>,
}

/// Description.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Description {
    /// Description text
    pub description: String,
    /// Description type
    pub description_type: DescriptionType,
    /// Language
    pub lang: Option<String>,
}

/// Description type.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum DescriptionType {
    /// Abstract
    Abstract,
    /// Methods
    Methods,
    /// Series information
    SeriesInformation,
    /// Table of contents
    TableOfContents,
    /// Technical info
    TechnicalInfo,
    /// Other
    Other,
}

/// Geo location.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GeoLocation {
    /// Geo location place
    pub geo_location_place: Option<String>,
    /// Geo location point
    pub geo_location_point: Option<GeoLocationPoint>,
    /// Geo location box
    pub geo_location_box: Option<GeoLocationBox>,
    /// Geo location polygons
    pub geo_location_polygons: Vec<GeoLocationPolygon>,
}

/// Geo location point.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct GeoLocationPoint {
    /// Point longitude
    pub point_longitude: f64,
    /// Point latitude
    pub point_latitude: f64,
}

/// Geo location box.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct GeoLocationBox {
    /// West bound longitude
    pub west_bound_longitude: f64,
    /// East bound longitude
    pub east_bound_longitude: f64,
    /// South bound latitude
    pub south_bound_latitude: f64,
    /// North bound latitude
    pub north_bound_latitude: f64,
}

/// Geo location polygon.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GeoLocationPolygon {
    /// Polygon points
    pub polygon_points: Vec<GeoLocationPoint>,
    /// Inpolygon point
    pub in_polygon_point: Option<GeoLocationPoint>,
}

/// Funding reference.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FundingReference {
    /// Funder name
    pub funder_name: String,
    /// Funder identifier
    pub funder_identifier: Option<String>,
    /// Funder identifier type
    pub funder_identifier_type: Option<String>,
    /// Award number
    pub award_number: Option<String>,
    /// Award URI
    pub award_uri: Option<String>,
    /// Award title
    pub award_title: Option<String>,
}

#[cfg(feature = "xml")]
impl DataCiteMetadata {
    /// Export to DataCite XML format.
    pub fn to_xml(&self) -> Result<String> {
        use quick_xml::se::to_string;
        to_string(&self).map_err(|e| MetadataError::XmlError(e.to_string()))
    }

    /// Import from DataCite XML format.
    pub fn from_xml(xml: &str) -> Result<Self> {
        use quick_xml::de::from_str;
        from_str(xml).map_err(|e| MetadataError::XmlError(e.to_string()))
    }
}

impl DataCiteMetadata {
    /// Export to JSON format.
    pub fn to_json(&self) -> Result<String> {
        serde_json::to_string_pretty(&self).map_err(|e| MetadataError::JsonError(e.to_string()))
    }

    /// Import from JSON format.
    pub fn from_json(json: &str) -> Result<Self> {
        serde_json::from_str(json).map_err(|e| MetadataError::JsonError(e.to_string()))
    }
}