1use crate::response::{FacetMap, QueryResponse};
4use chrono::NaiveDate;
5use serde::{Deserialize, Serialize};
6use serde_json::Value;
7
8pub type Relations = std::collections::HashMap<String, Value>;
13
14#[derive(Debug, Clone, Deserialize, Serialize)]
15#[serde(rename_all = "kebab-case")]
16#[allow(missing_docs)]
17pub struct WorkList {
18 pub facets: FacetMap,
19 pub total_results: usize,
21 pub items_per_page: Option<usize>,
23 pub query: Option<QueryResponse>,
25 pub items: Vec<Work>,
27 pub next_cursor: Option<String>,
29}
30
31#[derive(Debug, Clone, Deserialize, Serialize)]
36#[serde(rename_all = "kebab-case")]
37#[allow(missing_docs)]
38pub struct Work {
39 pub publisher: String,
41 pub title: Vec<String>,
43 pub original_title: Option<Vec<String>>,
45 pub language: Option<String>,
47 pub short_title: Option<Vec<String>>,
49 #[serde(rename = "abstract")]
51 pub abstract_: Option<String>,
52 pub references_count: i32,
54 pub is_referenced_by_count: i32,
56 pub source: String,
58 pub journal_issue: Option<Issue>,
59 pub prefix: String,
61 #[serde(rename = "DOI")]
63 pub doi: String,
64 #[serde(rename = "URL")]
66 pub url: String,
67 pub member: String,
69 #[serde(rename = "type")]
71 pub type_: String,
72 pub created: Option<Date>,
74 pub date: Option<Date>,
76 pub deposited: Option<Date>,
78 pub score: Option<f32>,
81 pub indexed: Date,
84 pub issued: PartialDate,
86 pub posted: Option<PartialDate>,
88 pub accepted: Option<PartialDate>,
90 pub subtitle: Option<Vec<String>>,
92 pub container_title: Option<Vec<String>>,
94 pub short_container_title: Option<Vec<String>>,
96 pub group_title: Option<String>,
98 pub issue: Option<String>,
100 pub volume: Option<String>,
102 pub page: Option<String>,
104 pub article_number: Option<String>,
106 pub published_print: Option<PartialDate>,
108 pub published_online: Option<PartialDate>,
110 pub subject: Option<Vec<String>>,
113 #[serde(rename = "ISSN")]
114 pub issn: Option<Vec<String>>,
115 pub issn_type: Option<Vec<ISSN>>,
117 #[serde(rename = "ISBN")]
118 pub isbn: Option<Vec<String>>,
119 pub archive: Option<Vec<String>>,
120 pub license: Option<Vec<License>>,
121 pub funder: Option<Vec<FundingBody>>,
122 pub assertion: Option<Vec<Assertion>>,
123 pub author: Option<Vec<Contributor>>,
124 pub editor: Option<Vec<Contributor>>,
125 pub chair: Option<Vec<Contributor>>,
126 pub translator: Option<Vec<Contributor>>,
127 pub update_to: Option<Vec<Update>>,
128 pub update_policy: Option<String>,
130 pub link: Option<Vec<ResourceLink>>,
132 pub clinical_trial_number: Option<Vec<ClinicalTrialNumber>>,
133 pub alternative_id: Option<Vec<String>>,
135 pub reference: Option<Vec<Reference>>,
137 pub content_domain: Option<ContentDomain>,
139 pub relation: Option<Relations>,
141 pub review: Option<Relations>,
143}
144
145#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
147pub struct DateParts(pub Vec<Vec<Option<u32>>>);
148
149impl DateParts {
150 pub fn as_date(&self) -> Option<DateField> {
154 fn naive(v: &[Option<u32>]) -> Option<NaiveDate> {
156 match v.len() {
157 0 => None,
158 1 => Some(NaiveDate::from_ymd_opt(v[0]? as i32, 1, 1)?),
159 2 => Some(NaiveDate::from_ymd_opt(v[0]? as i32, v[1]?, 1)?),
160 3 => Some(NaiveDate::from_ymd_opt(v[0]? as i32, v[1]?, v[2]?)?),
161 _ => None,
162 }
163 }
164
165 match self.0.len() {
166 0 => None,
167 1 => Some(DateField::Single(naive(&self.0[0])?)),
168 2 => Some(DateField::Range {
169 from: naive(&self.0[0])?,
170 to: naive(&self.0[1])?,
171 }),
172 _ => Some(DateField::Multi(
173 self.0
174 .iter()
175 .map(|x| naive(x))
176 .collect::<Option<Vec<_>>>()?,
177 )),
178 }
179 }
180}
181
182#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
183#[allow(missing_docs)]
184pub struct FundingBody {
185 pub name: String,
187 #[serde(rename = "DOI")]
189 pub doi: Option<String>,
190 pub award: Option<Vec<String>>,
192 #[serde(rename = "doi-asserted-by")]
194 pub doi_asserted_by: Option<String>,
195}
196
197#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
198#[allow(missing_docs)]
199pub struct ClinicalTrialNumber {
200 #[serde(rename = "clinical-trial-number")]
202 pub clinical_trial_number: String,
203 pub registry: String,
205 #[serde(rename = "type")]
207 pub type_: Option<String>,
208}
209
210#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
211#[allow(missing_docs)]
212pub struct Contributor {
213 pub family: String,
214 pub given: Option<String>,
215 #[serde(rename = "ORCID")]
217 pub orcid: Option<String>,
218 #[serde(rename = "authenticated-orcid")]
220 pub authenticated_orcid: Option<bool>,
221 pub affiliation: Option<Vec<Affiliation>>,
222}
223
224#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
225#[allow(missing_docs)]
226pub struct Affiliation {
227 pub name: String,
229}
230
231#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
233#[serde(rename_all = "kebab-case")]
234pub struct Date {
235 pub date_parts: DateParts,
239 pub timestamp: usize,
241 pub date_time: String,
243}
244
245impl Date {
246 pub fn as_date_field(&self) -> Option<DateField> {
248 self.date_parts.as_date()
249 }
250}
251
252#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
254pub struct PartialDate {
255 #[serde(rename = "date-parts")]
259 pub date_parts: DateParts,
260}
261
262impl PartialDate {
263 pub fn as_date_field(&self) -> Option<DateField> {
265 self.date_parts.as_date()
266 }
267}
268
269#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
271pub enum DateField {
272 Single(NaiveDate),
274 Range {
276 from: NaiveDate,
278 to: NaiveDate,
280 },
281 Multi(Vec<NaiveDate>),
283}
284
285#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
287pub struct Update {
288 pub updated: PartialDate,
290 #[serde(rename = "DOI")]
292 pub doi: String,
293 #[serde(rename = "type")]
295 pub type_: String,
296 pub label: Option<String>,
298}
299
300#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
301#[allow(missing_docs)]
302pub struct Assertion {
303 pub name: String,
304 pub value: String,
305 #[serde(rename = "URL")]
306 pub url: Option<String>,
307 pub explanation: Option<String>,
308 pub label: Option<String>,
309 pub order: Option<i32>,
310 pub group: Option<AssertionGroup>,
311}
312
313#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
314#[serde(rename_all = "kebab-case")]
315#[allow(missing_docs)]
316pub struct Issue {
317 pub published_print: Option<PartialDate>,
319 pub published_online: Option<PartialDate>,
321 pub issue: Option<String>,
323}
324
325#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
326#[allow(missing_docs)]
327pub struct AssertionGroup {
328 pub name: String,
329 pub label: Option<String>,
330}
331
332#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
333#[allow(missing_docs)]
334pub struct Agency {
335 pub id: String,
336 pub label: Option<String>,
337}
338
339#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
341#[serde(rename_all = "kebab-case")]
342pub struct License {
343 pub content_version: String,
345 pub delay_in_days: i32,
347 pub start: PartialDate,
349 #[serde(rename = "URL")]
351 pub url: String,
352}
353
354#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
356#[serde(rename_all = "kebab-case")]
357pub struct ResourceLink {
358 pub intended_application: String,
360 pub content_version: String,
362 #[serde(rename = "URL")]
364 pub url: String,
365 pub content_type: Option<String>,
367}
368
369#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
370#[serde(rename_all = "kebab-case")]
371#[allow(missing_docs)]
372pub struct Reference {
373 pub key: String,
374 #[serde(rename = "DOI")]
375 pub doi: Option<String>,
376 pub doi_asserted_by: Option<String>,
378 pub issue: Option<String>,
379 pub first_page: Option<String>,
380 pub volume: Option<String>,
381 pub edition: Option<String>,
382 pub component: Option<String>,
383 pub standard_designator: Option<String>,
384 pub standards_body: Option<String>,
385 pub author: Option<String>,
386 pub year: Option<String>,
387 pub unstructured: Option<String>,
388 pub journal_title: Option<String>,
389 pub article_title: Option<String>,
390 pub series_title: Option<String>,
391 pub volume_title: Option<String>,
392 #[serde(rename = "ISSN")]
393 pub issn: Option<String>,
394 pub issn_type: Option<String>,
396 #[serde(rename = "ISBN")]
397 pub isbn: Option<String>,
398 pub isbn_type: Option<String>,
399}
400
401#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
403#[serde(rename_all = "kebab-case")]
404pub struct ISSN {
405 pub value: String,
407 #[serde(rename = "type")]
409 pub type_: String,
410}
411
412#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
413#[serde(rename_all = "kebab-case")]
414#[allow(missing_docs)]
415pub struct ContentDomain {
416 pub domain: Vec<String>,
417 pub crossmark_restriction: bool,
418}
419
420#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
421#[serde(rename_all = "kebab-case")]
422#[allow(missing_docs)]
423pub struct Relation {
424 pub id_type: Option<String>,
425 pub id: Option<String>,
426 pub asserted_by: Option<String>,
427}
428
429#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
430#[serde(rename_all = "kebab-case")]
431#[allow(missing_docs)]
432pub struct Review {
433 pub running_number: Option<String>,
434 pub revision_round: Option<String>,
435 pub stage: Option<String>,
437 pub recommendation: Option<String>,
439 #[serde(rename = "type")]
441 pub type_: String,
442 pub competing_interest_statement: Option<String>,
443 pub language: Option<String>,
444}
445
446#[cfg(test)]
447mod tests {
448 use super::*;
449 use chrono::Datelike;
450 use serde_json::*;
451 #[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
452 struct Demo {
453 pub date_parts: DateParts,
454 }
455 #[test]
456 fn date_parts_serde() {
457 let demo = Demo {
458 date_parts: DateParts(vec![vec![Some(2017), Some(10), Some(11)]]),
459 };
460 let expected = r##"{"date_parts":[[2017,10,11]]}"##;
461 assert_eq!(expected, &to_string(&demo).unwrap());
462 assert_eq!(demo, from_str::<Demo>(expected).unwrap());
463 }
464
465 #[test]
466 fn date_parts_year_only() {
467 let parts = DateParts(vec![vec![Some(2004)]]);
469 let date = parts.as_date();
470 assert!(
471 date.is_some(),
472 "Year-only DateParts should produce a valid DateField"
473 );
474 if let Some(DateField::Single(d)) = date {
475 assert_eq!(d.year(), 2004);
476 } else {
477 panic!("Expected DateField::Single");
478 }
479 }
480
481 #[test]
482 fn date_parts_year_month_only() {
483 let parts = DateParts(vec![vec![Some(2004), Some(6)]]);
485 let date = parts.as_date();
486 assert!(
487 date.is_some(),
488 "Year+month DateParts should produce a valid DateField"
489 );
490 if let Some(DateField::Single(d)) = date {
491 assert_eq!(d.year(), 2004);
492 assert_eq!(d.month(), 6);
493 } else {
494 panic!("Expected DateField::Single");
495 }
496 }
497
498 #[test]
499 fn serialize_work() {
500 let work_str = r##"{
501 "indexed": {
502 "date-parts": [
503 [
504 2019,
505 2,
506 26
507 ]
508 ],
509 "date-time": "2019-02-26T10:43:14Z",
510 "timestamp": 1551177794515
511 },
512 "reference-count": 105,
513 "publisher": "American Psychological Association (APA)",
514 "issue": "1",
515 "content-domain": {
516 "domain": [],
517 "crossmark-restriction": false
518 },
519 "short-container-title": [
520 "American Psychologist"
521 ],
522 "DOI": "10.1037/0003-066x.59.1.29",
523 "type": "journal-article",
524 "created": {
525 "date-parts": [
526 [
527 2004,
528 1,
529 21
530 ]
531 ],
532 "date-time": "2004-01-21T14:31:19Z",
533 "timestamp": 1074695479000
534 },
535 "page": "29-40",
536 "source": "Crossref",
537 "is-referenced-by-count": 84,
538 "title": [
539 "How the Mind Hurts and Heals the Body."
540 ],
541 "prefix": "10.1037",
542 "volume": "59",
543 "author": [
544 {
545 "given": "Oakley",
546 "family": "Ray",
547 "sequence": "first",
548 "affiliation": []
549 }
550 ],
551 "member": "15",
552 "published-online": {
553 "date-parts": [
554 [
555 2004
556 ]
557 ]
558 },
559 "container-title": [
560 "American Psychologist"
561 ],
562 "original-title": [],
563 "language": "en",
564 "link": [
565 {
566 "URL": "http://psycnet.apa.org/journals/amp/59/1/29.pdf",
567 "content-type": "unspecified",
568 "content-version": "vor",
569 "intended-application": "similarity-checking"
570 }
571 ],
572 "deposited": {
573 "date-parts": [
574 [
575 2018,
576 4,
577 8
578 ]
579 ],
580 "date-time": "2018-04-08T18:56:17Z",
581 "timestamp": 1523213777000
582 },
583 "score": 1,
584 "subtitle": [],
585 "short-title": [],
586 "issued": {
587 "date-parts": [
588 [
589 null
590 ]
591 ]
592 },
593 "references-count": 105,
594 "journal-issue": {
595 "published-online": {
596 "date-parts": [
597 [
598 2004
599 ]
600 ]
601 },
602 "issue": "1"
603 },
604 "alternative-id": [
605 "2004-10043-004",
606 "14736318"
607 ],
608 "URL": "http://dx.doi.org/10.1037/0003-066x.59.1.29",
609 "relation": {},
610 "ISSN": [
611 "1935-990X",
612 "0003-066X"
613 ],
614 "issn-type": [
615 {
616 "value": "0003-066X",
617 "type": "print"
618 },
619 {
620 "value": "1935-990X",
621 "type": "electronic"
622 }
623 ]
624 }
625"##;
626
627 let _work: Work = from_str(work_str).unwrap();
628 }
629}