-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasettype.rs
289 lines (258 loc) · 8.09 KB
/
datasettype.rs
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
use crate::{metadata::metadatatype, prelude::*, utils::string_to_datetime};
use chrono::{DateTime, Utc};
use metadatatype::MetaDataType;
use serde_derive::{Deserialize, Serialize};
/// Striped down version of Metadata
#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)]
pub struct Dataset {
pub pangaea_id: Option<String>,
pub title: String,
pub text_abstract: Option<String>,
pub authors: Vec<Author>,
pub publication_date: Option<DateTime<Utc>>,
pub uri: Option<String>,
pub extent: Option<Extent>,
pub keywords: Vec<String>,
pub license: Option<License>,
pub projects: Vec<Project>,
}
fn get_pangaea_id(md: &MetaDataType) -> Option<String> {
match &md.citation.citation_type.id_attributes.id {
None => None,
Some(id) => id.strip_prefix("dataset").map(|s| s.to_owned()),
}
}
fn get_title(md: &MetaDataType) -> String {
md.citation.citation_type.title.to_owned()
}
fn get_abstract(md: &MetaDataType) -> Option<String> {
md.text_abstract.to_owned()
}
fn get_authors(md: &MetaDataType) -> Vec<Author> {
md.citation
.citation_type
.authors
.clone()
.into_iter()
.map(|a| a.into())
.collect()
}
fn get_publication_date(md: &MetaDataType) -> Option<DateTime<Utc>> {
match &md.citation.date_time.clone().map(string_to_datetime) {
None => None,
Some(v) => v.to_owned(),
}
}
fn get_uri(md: &MetaDataType) -> Option<String> {
md.citation.citation_type.uri.to_owned()
}
fn get_extent(md: &MetaDataType) -> Option<Extent> {
md.extent.clone().map(|ext| ext.into())
}
fn get_keywords(md: &MetaDataType) -> Vec<String> {
md.keywords
.clone()
.map(|k| k.keywords)
.unwrap_or_default()
.into_iter()
.map(|kt| kt.text)
.collect()
}
fn get_license(md: &MetaDataType) -> Option<License> {
match md.license.clone() {
None => None,
Some(license) => License::try_from(license).ok(),
}
}
fn get_projects(md: &MetaDataType) -> Vec<Project> {
md.projects
.clone()
.into_iter()
.filter_map(|p| Project::try_from(p).ok())
.collect()
}
impl From<metadatatype::MetaDataType> for Dataset {
fn from(md: metadatatype::MetaDataType) -> Self {
Dataset {
pangaea_id: get_pangaea_id(&md),
title: get_title(&md),
text_abstract: get_abstract(&md),
authors: get_authors(&md),
publication_date: get_publication_date(&md),
uri: get_uri(&md),
extent: get_extent(&md),
keywords: get_keywords(&md),
license: get_license(&md),
projects: get_projects(&md),
}
}
}
#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)]
pub struct Author {
pub last_name: String,
pub first_name: Option<String>,
pub e_mail: Option<String>,
pub uri: Option<String>,
pub orcid: Option<String>,
pub affiliations: Vec<Institution>,
}
impl From<metadatatype::ResponsiblePartyType> for Author {
fn from(party: metadatatype::ResponsiblePartyType) -> Self {
Self {
last_name: party.last_name,
first_name: party.first_name,
e_mail: party.e_mail,
uri: party.uri,
orcid: party.orcid,
affiliations: party
.affiliations
.into_iter()
.map(|aff| aff.into())
.collect(),
}
}
}
#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)]
pub struct Institution {
pub name: String,
pub uri: Option<String>,
pub ror: Option<String>,
pub crossref_funder_id: Option<String>,
}
impl From<metadatatype::InstitutionType> for Institution {
fn from(inst: metadatatype::InstitutionType) -> Self {
Self {
name: inst.linked_name_type.name,
uri: inst.linked_name_type.uri,
ror: inst.ror,
crossref_funder_id: inst.crossref_funder_id,
}
}
}
#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)]
pub struct Extent {
pub geographic: Option<Geographic>,
pub temporal: Option<Temporal>,
pub elevation: Option<Elevation>,
}
impl From<metadatatype::ExtentType> for Extent {
fn from(extent: metadatatype::ExtentType) -> Self {
Self {
geographic: extent.geographic.map(|g| g.into()),
temporal: extent.temporal.map(|t| t.into()),
elevation: extent.elevation.map(|e| e.into()),
}
}
}
#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)]
pub struct Geographic {
pub west_bound_longitude: f64,
pub east_bound_longitude: f64,
pub south_bound_latitude: f64,
pub north_bound_latitude: f64,
pub mean_longitude: f64,
pub mean_latitude: f64,
}
impl From<metadatatype::Geographic> for Geographic {
fn from(geo: metadatatype::Geographic) -> Self {
Self {
west_bound_longitude: geo.west_bound_longitude,
east_bound_longitude: geo.east_bound_longitude,
south_bound_latitude: geo.south_bound_latitude,
north_bound_latitude: geo.north_bound_latitude,
mean_longitude: geo.mean_longitude,
mean_latitude: geo.mean_latitude,
}
}
}
#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)]
pub struct Temporal {
pub min_date_time: Option<DateTime<Utc>>,
pub max_date_time: Option<DateTime<Utc>>,
}
impl From<metadatatype::Temporal> for Temporal {
fn from(temp: metadatatype::Temporal) -> Self {
let min_date_time: Option<DateTime<Utc>> = string_to_datetime(temp.min_date_time);
let max_date_time: Option<DateTime<Utc>> = string_to_datetime(temp.max_date_time);
Self {
min_date_time,
max_date_time,
}
}
}
#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)]
pub struct Elevation {
pub name: String,
pub unit: Option<String>,
pub min: f64,
pub max: f64,
}
impl From<metadatatype::Elevation> for Elevation {
fn from(el: metadatatype::Elevation) -> Self {
Self {
name: el.name,
unit: el.unit,
min: el.min,
max: el.max,
}
}
}
#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)]
pub struct License {
pub label: String,
pub name: String,
pub uri: String,
}
impl TryFrom<metadatatype::LinkedLabelNameType> for License {
type Error = Error;
fn try_from(md_license: metadatatype::LinkedLabelNameType) -> Result<Self> {
let label = md_license
.label
.ok_or(Error::Generic("License label is None".to_string()))?;
let name = md_license
.name
.ok_or(Error::Generic("License name is None".to_string()))?;
let uri = md_license
.uri
.ok_or(Error::Generic("License uri is None".to_string()))?;
Ok(Self { label, name, uri })
}
}
#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)]
pub struct Project {
pub project_type: String,
pub institution: Institution,
pub label: String,
pub name: String,
pub uri: String,
}
impl TryFrom<metadatatype::ProjectType> for Project {
type Error = Error;
fn try_from(project: metadatatype::ProjectType) -> Result<Self> {
let project_type = project
.project_type
.ok_or(Error::Generic("Project project_type is None".to_string()))?;
let institution = project
.institution
.ok_or(Error::Generic("Project institution is None".to_string()))?;
let label = project
.linked_label_name_type
.label
.ok_or(Error::Generic("Project label is None".to_string()))?;
let name = project
.linked_label_name_type
.name
.ok_or(Error::Generic("Project name is None".to_string()))?;
let uri = project
.linked_label_name_type
.uri
.ok_or(Error::Generic("Project uri is None".to_string()))?;
Ok(Self {
project_type,
institution: institution.into(),
label,
name,
uri,
})
}
}