Skip to content

Commit e9b2ed6

Browse files
committed
Fix linting
1 parent 0f28a1c commit e9b2ed6

1 file changed

Lines changed: 85 additions & 91 deletions

File tree

thoth-export-server/src/csv/kbart_oclc.rs

Lines changed: 85 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -75,112 +75,106 @@ impl TryFrom<Work> for KbartOclcRow {
7575

7676
fn try_from(work: Work) -> ThothResult<Self> {
7777
// title_url is mandatory in KBART but optional in Thoth
78-
if work.landing_page.is_none() {
79-
Err(ThothError::IncompleteMetadataRecord(
78+
let landing_page = work.landing_page.ok_or_else(|| {
79+
ThothError::IncompleteMetadataRecord(
8080
KBART_ERROR.to_string(),
8181
"Missing Landing Page".to_string(),
82-
))
82+
)
83+
})?;
8384
// Don't output works with no publication date (mandatory in KBART)
84-
} else if work.publication_date.is_none() {
85-
Err(ThothError::IncompleteMetadataRecord(
85+
let publication_date = work.publication_date.ok_or_else(|| {
86+
ThothError::IncompleteMetadataRecord(
8687
KBART_ERROR.to_string(),
8788
"Missing Publication Date".to_string(),
88-
))
89+
)
90+
})?;
8991
// Don't output works with no license
90-
} else if work.license.is_none() {
91-
Err(ThothError::IncompleteMetadataRecord(
92+
work.license.ok_or_else(|| {
93+
ThothError::IncompleteMetadataRecord(
9294
KBART_ERROR.to_string(),
9395
"Missing License".to_string(),
94-
))
95-
} else {
96-
let mut print_identifier = None;
97-
let mut online_identifier = None;
98-
let mut print_edition_exists = false;
99-
for publication in work.publications {
100-
if publication.publication_type == PublicationType::PDF
101-
&& publication.isbn.is_some()
102-
{
103-
online_identifier = publication.isbn.as_ref().map(|i| i.to_string());
104-
}
105-
if publication.publication_type == PublicationType::PAPERBACK {
106-
print_edition_exists = true;
107-
if publication.isbn.is_some() {
108-
print_identifier = publication.isbn.as_ref().map(|i| i.to_string());
109-
}
110-
}
111-
if publication.publication_type == PublicationType::HARDBACK {
112-
print_edition_exists = true;
96+
)
97+
})?;
98+
99+
let mut print_identifier = None;
100+
let mut online_identifier = None;
101+
let mut print_edition_exists = false;
102+
for publication in work.publications {
103+
if publication.publication_type == PublicationType::PDF && publication.isbn.is_some() {
104+
online_identifier = publication.isbn.as_ref().map(|i| i.to_string());
105+
}
106+
if publication.publication_type == PublicationType::PAPERBACK {
107+
print_edition_exists = true;
108+
if publication.isbn.is_some() {
109+
print_identifier = publication.isbn.as_ref().map(|i| i.to_string());
113110
}
114111
}
115-
let mut first_author = None;
116-
let mut first_editor = None;
117-
let mut contributions = work.contributions;
118-
// The first author/editor will usually be the contributor with contribution_ordinal 1,
119-
// but this is not guaranteed, so we select the highest-ranked contributor of the
120-
// appropriate contribution type who is listed as a "main" contributor.
121-
// WorkQuery should already have retrieved these sorted by ordinal, but sort again for safety
122-
contributions.sort_by(|a, b| a.contribution_ordinal.cmp(&b.contribution_ordinal));
123-
for contribution in contributions {
124-
if contribution.main_contribution {
125-
if work.work_type == WorkType::EDITED_BOOK {
126-
if contribution.contribution_type == ContributionType::EDITOR {
127-
first_editor = Some(contribution.last_name);
128-
break;
129-
}
130-
} else if contribution.contribution_type == ContributionType::AUTHOR {
131-
first_author = Some(contribution.last_name);
112+
if publication.publication_type == PublicationType::HARDBACK {
113+
print_edition_exists = true;
114+
}
115+
}
116+
let mut first_author = None;
117+
let mut first_editor = None;
118+
let mut contributions = work.contributions;
119+
// The first author/editor will usually be the contributor with contribution_ordinal 1,
120+
// but this is not guaranteed, so we select the highest-ranked contributor of the
121+
// appropriate contribution type who is listed as a "main" contributor.
122+
// WorkQuery should already have retrieved these sorted by ordinal, but sort again for safety
123+
contributions.sort_by(|a, b| a.contribution_ordinal.cmp(&b.contribution_ordinal));
124+
for contribution in contributions {
125+
if contribution.main_contribution {
126+
if work.work_type == WorkType::EDITED_BOOK {
127+
if contribution.contribution_type == ContributionType::EDITOR {
128+
first_editor = Some(contribution.last_name);
132129
break;
133130
}
131+
} else if contribution.contribution_type == ContributionType::AUTHOR {
132+
first_author = Some(contribution.last_name);
133+
break;
134134
}
135135
}
136-
let date_monograph_published_online = work
137-
.publication_date
138-
.map(|date| chrono::Datelike::year(&date).into())
139-
.unwrap();
140-
let date_monograph_published_print = match print_edition_exists {
141-
true => Some(date_monograph_published_online),
142-
false => None,
143-
};
144-
Ok(KbartOclcRow {
145-
publication_title: work.titles[0].full_title.clone(),
146-
print_identifier,
147-
online_identifier,
148-
date_first_issue_online: None,
149-
num_first_vol_online: None,
150-
num_first_issue_online: None,
151-
date_last_issue_online: None,
152-
num_last_vol_online: None,
153-
num_last_issue_online: None,
154-
title_url: work.landing_page.unwrap(),
155-
first_author,
156-
title_id: work
157-
.doi
158-
.map(|d| d.to_string())
159-
.unwrap_or_else(|| work.work_id.to_string()),
160-
embargo_info: None,
161-
coverage_depth: "fulltext".to_string(),
162-
notes: None,
163-
publisher_name: Some(work.imprint.publisher.publisher_name),
164-
publication_type: match work.work_type {
165-
WorkType::BOOK_SET => "Serial".to_string(),
166-
_ => "Monograph".to_string(),
167-
},
168-
date_monograph_published_print,
169-
date_monograph_published_online,
170-
// Note that it is possible for a work to belong to more than one series.
171-
// Only one series can be listed in KBART, so we select the first one found (if any).
172-
monograph_volume: work.issues.first().map(|i| i.issue_ordinal),
173-
monograph_edition: work.edition,
174-
first_editor,
175-
// This should match the series' `title_id` if also provided in the KBART.
176-
parent_publication_title_id: work
177-
.issues
178-
.first()
179-
.and_then(|i| i.series.issn_digital.as_ref().map(|s| s.to_string())),
180-
preceding_publication_title_id: None,
181-
access_type: "F".to_string(),
182-
})
183136
}
137+
let date_monograph_published_online = chrono::Datelike::year(&publication_date).into();
138+
let date_monograph_published_print = match print_edition_exists {
139+
true => Some(date_monograph_published_online),
140+
false => None,
141+
};
142+
Ok(KbartOclcRow {
143+
publication_title: work.titles[0].full_title.clone(),
144+
print_identifier,
145+
online_identifier,
146+
date_first_issue_online: None,
147+
num_first_vol_online: None,
148+
num_first_issue_online: None,
149+
date_last_issue_online: None,
150+
num_last_vol_online: None,
151+
num_last_issue_online: None,
152+
title_url: landing_page,
153+
first_author,
154+
title_id: work.titles[0].title_id.to_string(),
155+
embargo_info: None,
156+
coverage_depth: "fulltext".to_string(),
157+
notes: None,
158+
publisher_name: Some(work.imprint.publisher.publisher_name),
159+
publication_type: match work.work_type {
160+
WorkType::BOOK_SET => "Serial".to_string(),
161+
_ => "Monograph".to_string(),
162+
},
163+
date_monograph_published_print,
164+
date_monograph_published_online,
165+
// Note that it is possible for a work to belong to more than one series.
166+
// Only one series can be listed in KBART, so we select the first one found (if any).
167+
monograph_volume: work.issues.first().map(|i| i.issue_ordinal),
168+
monograph_edition: work.edition,
169+
first_editor,
170+
// This should match the series' `title_id` if also provided in the KBART.
171+
parent_publication_title_id: work
172+
.issues
173+
.first()
174+
.and_then(|i| i.series.issn_digital.as_ref().map(|s| s.to_string())),
175+
preceding_publication_title_id: None,
176+
access_type: "F".to_string(),
177+
})
184178
}
185179
}
186180

0 commit comments

Comments
 (0)