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
use thiserror::Error;

#[derive(Error, Debug)]
pub enum NameError {
    #[error("Error parsing name entry: {0}")]
    PEGError(#[from] peg::error::ParseError<peg::str::LineCol>),
    #[error("Could not match {0}")]
    #[allow(dead_code)]
    Unmatched(String),
}

#[derive(Debug, PartialEq, Eq)]
pub enum NameFmt {
    Single(String),
    TwoPart(String, String),
    Empty,
}

impl NameFmt {
    pub fn simplify(self) -> NameFmt {
        match self {
            NameFmt::TwoPart(l, f) => {
                if f.is_empty() && l.is_empty() {
                    NameFmt::Empty
                } else if f.is_empty() {
                    NameFmt::Single(l)
                } else if l.is_empty() {
                    NameFmt::Single(f)
                } else {
                    NameFmt::TwoPart(l, f)
                }
            }
            NameFmt::Single(n) if n.is_empty() => NameFmt::Empty,
            _ => self,
        }
    }
}

#[derive(Debug, PartialEq, Eq)]
pub struct NameEntry {
    pub name: NameFmt,
    pub year: Option<String>,
}

impl From<(NameFmt, String)> for NameEntry {
    fn from(t: (NameFmt, String)) -> NameEntry {
        let (name, year) = t;
        NameEntry {
            name,
            year: Some(year),
        }
    }
}

impl From<NameFmt> for NameEntry {
    fn from(name: NameFmt) -> NameEntry {
        NameEntry { name, year: None }
    }
}