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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;

use fastobo_derive_internal::FromStr;

use crate::ast::*;
use crate::error::SyntaxError;
use crate::parser::Cache;
use crate::parser::FromPair;
use crate::syntax::pest::iterators::Pair;
use crate::syntax::Rule;

/// The creation date of an OBO entity.
///
/// The OBO format version 1.4 specifies that the value of `creation_date`
/// clauses on entities should be that of an [ISO8601](https://en.wikipedia.org/wiki/ISO_8601)
/// formatted datetime. However, there are many cases in the wild where
/// OBO ontologies only providing a date instead of a datetime:
///
/// ```obo
/// [Typedef]
/// id: RO:0017001
/// name: utilizes
/// holds_over_chain: RO:0002215 RO:0002233
/// creation_date: 2021-11-08
/// ```
///
/// Since the guide is vague on this particular issue, we decided not to
/// make this a hard error, and instead to support incomplete dates, for
/// intercompatibility with older ontologies that are otherwise parsing fine.
///
#[derive(Clone, Debug, FromStr, Hash, PartialOrd, Ord, Eq, PartialEq)]
pub enum CreationDate {
    /// A creation date missing the time component of an ISO8601 datetime.
    Date(Box<IsoDate>),
    /// A creation date specified as a complete ISO8601 datetime string.
    DateTime(Box<IsoDateTime>),
}

impl AsRef<IsoDate> for CreationDate {
    fn as_ref(&self) -> &IsoDate {
        match self {
            CreationDate::Date(d) => d,
            CreationDate::DateTime(dt) => dt.date(),
        }
    }
}

impl Display for CreationDate {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        match self {
            CreationDate::Date(d) => d.fmt(f),
            CreationDate::DateTime(d) => d.fmt(f),
        }
    }
}

impl Date for CreationDate {
    fn year(&self) -> u16 {
        <Self as AsRef<IsoDate>>::as_ref(self).year()
    }

    fn month(&self) -> u8 {
        <Self as AsRef<IsoDate>>::as_ref(self).month()
    }

    fn day(&self) -> u8 {
        <Self as AsRef<IsoDate>>::as_ref(self).day()
    }
}

impl From<Box<IsoDateTime>> for CreationDate {
    fn from(b: Box<IsoDateTime>) -> Self {
        CreationDate::DateTime(b)
    }
}

impl From<Box<IsoDate>> for CreationDate {
    fn from(b: Box<IsoDate>) -> Self {
        CreationDate::Date(b)
    }
}

impl From<IsoDate> for CreationDate {
    fn from(b: IsoDate) -> Self {
        Self::from(Box::new(b))
    }
}

impl From<IsoDateTime> for CreationDate {
    fn from(b: IsoDateTime) -> Self {
        Self::from(Box::new(b))
    }
}

impl<'i> FromPair<'i> for CreationDate {
    const RULE: Rule = Rule::CreationDate;
    unsafe fn from_pair_unchecked(
        pair: Pair<'i, Rule>,
        cache: &Cache,
    ) -> Result<Self, SyntaxError> {
        let inner = pair.into_inner().next().unwrap();
        match inner.as_rule() {
            Rule::Iso8601DateTime => IsoDateTime::from_pair(inner, cache).map(From::from),
            Rule::Iso8601Date => IsoDate::from_pair(inner, cache).map(From::from),
            rule => unreachable!("unexpected rule in CreationDate::from_pair: {:?}", rule),
        }
    }
}