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
|
use std::fmt;
use syntax::bibtex::Value;
use super::text::TextFieldData;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
pub enum NumberField {
Edition,
Number,
Pages,
PageTotal,
Part,
Volume,
Volumes,
}
impl NumberField {
pub fn parse(input: &str) -> Option<Self> {
Some(match input.to_ascii_lowercase().as_str() {
"edition" => Self::Edition,
"number" => Self::Number,
"pages" => Self::Pages,
"pagetotal" => Self::PageTotal,
"part" => Self::Part,
"volume" => Self::Volume,
"volumes" => Self::Volumes,
_ => return None,
})
}
}
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub enum NumberFieldData {
Scalar(u32),
Range(u32, u32),
Other(String),
}
impl fmt::Display for NumberFieldData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Scalar(value) => write!(f, "{}", value),
Self::Range(start, end) => write!(f, "{}-{}", start, end),
Self::Other(value) => write!(f, "{}", value.replace("--", "-")),
}
}
}
impl NumberFieldData {
pub fn parse(value: &Value) -> Option<Self> {
let TextFieldData { text } = TextFieldData::parse(value)?;
text.split_once("--")
.or_else(|| text.split_once('-'))
.and_then(|(a, b)| Some((a.parse().ok()?, b.parse().ok()?)))
.map(|(a, b)| Self::Range(a, b))
.or_else(|| text.parse().ok().map(Self::Scalar))
.or(Some(Self::Other(text)))
}
}
|