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
|
use std::{borrow::Cow, fmt};
use human_name::Name;
use itertools::Itertools;
use syntax::bibtex::Value;
use super::text::TextFieldData;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
pub enum AuthorField {
Afterword,
Annotator,
Author,
Commentator,
Editor,
EditorA,
EditorB,
EditorC,
Foreword,
Introduction,
Translator,
}
impl AuthorField {
pub fn parse(input: &str) -> Option<Self> {
Some(match input.to_ascii_lowercase().as_str() {
"afterword" => Self::Afterword,
"annotator" => Self::Annotator,
"author" => Self::Author,
"commentator" => Self::Commentator,
"editor" => Self::Editor,
"editora" => Self::EditorA,
"editorb" => Self::EditorB,
"editorc" => Self::EditorC,
"foreword" => Self::Foreword,
"introduction" => Self::Introduction,
"translator" => Self::Translator,
_ => return None,
})
}
}
#[derive(Debug, Clone, Default)]
pub struct AuthorFieldData {
pub authors: Vec<Name>,
}
impl fmt::Display for AuthorFieldData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let names = self.authors.iter().map(Name::display_initial_surname);
for part in Itertools::intersperse(names, Cow::Borrowed(", ")) {
write!(f, "{}", part)?;
}
Ok(())
}
}
impl AuthorFieldData {
pub fn parse(value: &Value) -> Option<Self> {
let TextFieldData { text } = TextFieldData::parse(value)?;
let mut authors = Vec::new();
let mut words = Vec::new();
for word in text.split_whitespace() {
if word.eq_ignore_ascii_case("and") {
authors.push(Name::parse(&words.join(" "))?);
words.clear();
} else {
words.push(word);
}
}
if !words.is_empty() {
authors.push(Name::parse(&words.join(" "))?);
}
Some(Self { authors })
}
}
|