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
|
use std::{fmt, str::FromStr};
use strum::EnumString;
use crate::syntax::bibtex::Field;
use super::text::TextFieldData;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, EnumString)]
#[strum(ascii_case_insensitive)]
pub enum NumberField {
Edition,
Number,
Pages,
PageTotal,
Part,
Volume,
Volumes,
}
impl NumberField {
pub fn parse(input: &str) -> Option<Self> {
Self::from_str(input).ok()
}
}
#[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(field: &Field) -> Option<Self> {
let TextFieldData { text } = TextFieldData::parse(field)?;
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)))
}
}
|