|
| 1 | +use marctk::{Field, Record}; |
| 2 | + |
| 3 | +use crate::{marc::trim_punctuation, solr::AuthorRoles}; |
| 4 | + |
| 5 | +impl From<&Record> for AuthorRoles { |
| 6 | + fn from(record: &Record) -> Self { |
| 7 | + let primary_author = record |
| 8 | + .extract_values("100a") |
| 9 | + .first() |
| 10 | + .map(|name| trim_punctuation(name)); |
| 11 | + let mut secondary_authors: Vec<String> = Default::default(); |
| 12 | + let mut compilers: Vec<String> = Default::default(); |
| 13 | + let mut editors: Vec<String> = Default::default(); |
| 14 | + let mut translators: Vec<String> = Default::default(); |
| 15 | + let other_author_fields = record.fields().iter().filter(|field| { |
| 16 | + ["110", "111", "700", "710", "711"].contains(&field.tag()) && field.has_subfield("a") |
| 17 | + }); |
| 18 | + for field in other_author_fields { |
| 19 | + match (ContributorType::from(field), field.first_subfield("a")) { |
| 20 | + (ContributorType::Compiler, Some(contributor_subfield)) => { |
| 21 | + compilers.push(trim_punctuation(contributor_subfield.content())); |
| 22 | + } |
| 23 | + (ContributorType::Editor, Some(contributor_subfield)) => { |
| 24 | + editors.push(trim_punctuation(contributor_subfield.content())); |
| 25 | + } |
| 26 | + (ContributorType::Translator, Some(contributor_subfield)) => { |
| 27 | + translators.push(trim_punctuation(contributor_subfield.content())); |
| 28 | + } |
| 29 | + (_, Some(contributor_subfield)) => { |
| 30 | + secondary_authors.push(trim_punctuation(contributor_subfield.content())); |
| 31 | + } |
| 32 | + _ => {} |
| 33 | + } |
| 34 | + } |
| 35 | + Self { |
| 36 | + primary_author, |
| 37 | + secondary_authors, |
| 38 | + compilers, |
| 39 | + editors, |
| 40 | + translators, |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +enum ContributorType { |
| 46 | + Compiler, |
| 47 | + Editor, |
| 48 | + Translator, |
| 49 | + Other, |
| 50 | +} |
| 51 | + |
| 52 | +impl From<&Field> for ContributorType { |
| 53 | + fn from(field: &Field) -> Self { |
| 54 | + let relator = find_potential_relator(field, "4").or(find_potential_relator(field, "e")); |
| 55 | + match relator.as_deref() { |
| 56 | + Some("COM") => Self::Compiler, |
| 57 | + Some("COMPILER") => Self::Compiler, |
| 58 | + Some("EDT") => Self::Editor, |
| 59 | + Some("EDITOR") => Self::Editor, |
| 60 | + Some("TRL") => Self::Translator, |
| 61 | + Some("TRANSLATOR") => Self::Translator, |
| 62 | + _ => Self::Other, |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +fn find_potential_relator(field: &Field, subfield: &str) -> Option<String> { |
| 68 | + field |
| 69 | + .first_subfield(subfield) |
| 70 | + .map(|subfield| clean_potential_relator(subfield.content())) |
| 71 | +} |
| 72 | + |
| 73 | +fn clean_potential_relator(raw: &str) -> String { |
| 74 | + raw.chars() |
| 75 | + .filter_map(|c| { |
| 76 | + if c.is_ascii_alphabetic() { |
| 77 | + Some(c.to_ascii_uppercase()) |
| 78 | + } else { |
| 79 | + None |
| 80 | + } |
| 81 | + }) |
| 82 | + .collect() |
| 83 | +} |
| 84 | + |
| 85 | +#[cfg(test)] |
| 86 | +mod tests { |
| 87 | + use super::*; |
| 88 | + |
| 89 | + #[test] |
| 90 | + fn it_can_find_primary_author_from_marc_record() { |
| 91 | + let record = Record::from_breaker("=100 \\$aPrimary").unwrap(); |
| 92 | + assert_eq!( |
| 93 | + AuthorRoles::from(&record), |
| 94 | + AuthorRoles { |
| 95 | + primary_author: Some("Primary".to_owned()), |
| 96 | + ..Default::default() |
| 97 | + } |
| 98 | + ) |
| 99 | + } |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn it_trims_spaces_and_punctuation_from_primary_author() { |
| 103 | + let record = Record::from_breaker("=100 \\$a Primary, $e author").unwrap(); |
| 104 | + assert_eq!( |
| 105 | + AuthorRoles::from(&record), |
| 106 | + AuthorRoles { |
| 107 | + primary_author: Some("Primary".to_owned()), |
| 108 | + ..Default::default() |
| 109 | + } |
| 110 | + ) |
| 111 | + } |
| 112 | + |
| 113 | + #[test] |
| 114 | + fn it_can_find_all_types_of_author() { |
| 115 | + let record = Record::from_breaker( |
| 116 | + r#"=100 \\$aLahiri, Jhumpa |
| 117 | +=700 \\$aEugenides, Jeffrey$4edt |
| 118 | +=700 \\$aCole, Teju$4com |
| 119 | +=700 \\$aNikolakopoulou, Evangelia$4trl |
| 120 | +=700 \\$aMorrison, Toni$4aaa |
| 121 | +=700 \\$aOates, Joyce Carol |
| 122 | +=700 \\$aMarchesi, Simone$etranslator. |
| 123 | +=700 \\$aFitzgerald, F. Scott$eed."#, |
| 124 | + ) |
| 125 | + .unwrap(); |
| 126 | + assert_eq!( |
| 127 | + AuthorRoles::from(&record), |
| 128 | + AuthorRoles { |
| 129 | + primary_author: Some("Lahiri, Jhumpa".to_owned()), |
| 130 | + secondary_authors: vec![ |
| 131 | + "Morrison, Toni".to_owned(), |
| 132 | + "Oates, Joyce Carol".to_owned(), |
| 133 | + "Fitzgerald, F. Scott".to_owned() |
| 134 | + ], |
| 135 | + translators: vec![ |
| 136 | + "Nikolakopoulou, Evangelia".to_owned(), |
| 137 | + "Marchesi, Simone".to_owned() |
| 138 | + ], |
| 139 | + editors: vec!["Eugenides, Jeffrey".to_owned()], |
| 140 | + compilers: vec!["Cole, Teju".to_owned()] |
| 141 | + } |
| 142 | + ) |
| 143 | + } |
| 144 | +} |
0 commit comments