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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use std::cmp::PartialOrd;
use std::cmp::Ordering;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use std::fmt::Write;

use pest::iterators::Pair;

use crate::share::Share;
use crate::share::Redeem;
use crate::share::Cow;
use crate::error::Result;
use crate::parser::FromPair;
use crate::parser::Rule;

use super::PrefixedId;
use super::PrefixedIdent;
use super::UnprefixedId;
use super::UnprefixedIdent;
use super::Url;

/// An identifier, either prefixed, unprefixed, or a valid URL.
#[derive(Clone, Debug, PartialEq, Hash, Eq, Ord)]
pub enum Ident {
    Prefixed(PrefixedIdent),
    Unprefixed(UnprefixedIdent),
    Url(Url),
}

impl Display for Ident {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        use self::Ident::*;
        match self {
            Prefixed(id) => id.fmt(f),
            Unprefixed(id) => id.fmt(f),
            Url(url) => url.fmt(f),
        }
    }
}

impl From<PrefixedIdent> for Ident {
    fn from(id: PrefixedIdent) -> Self {
        Ident::Prefixed(id)
    }
}

impl From<UnprefixedIdent> for Ident {
    fn from(id: UnprefixedIdent) -> Self {
        Ident::Unprefixed(id)
    }
}

impl From<Url> for Ident {
    fn from(url: Url) -> Self {
        Ident::Url(url)
    }
}

impl<'i> FromPair<'i> for Ident {
    const RULE: Rule = Rule::Id;
    unsafe fn from_pair_unchecked(pair: Pair<'i, Rule>) -> Result<Self> {
        let inner = pair.into_inner().next().unwrap();
        match inner.as_rule() {
            Rule::PrefixedId => PrefixedIdent::from_pair_unchecked(inner).map(From::from),
            Rule::UnprefixedId => UnprefixedIdent::from_pair_unchecked(inner).map(From::from),
            // FIXME(@althonos): need proper error report if the parser fails.
            Rule::UrlId => Url::from_pair_unchecked(inner).map(Ident::Url),
            _ => unreachable!(),
        }
    }
}
impl_fromstr!(Ident);

impl PartialOrd for Ident {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        use self::Ident::*;
        match (self, other) {
            (Prefixed(l), Prefixed(r)) => l.partial_cmp(r),
            (Unprefixed(l), Unprefixed(r)) => l.partial_cmp(r),
            (Url(l), Url(r)) => l.partial_cmp(r),
            (l, r) => l.to_string().partial_cmp(&r.to_string())
        }
    }
}

impl<'a> Share<'a, Id<'a>> for Ident {
    fn share(&'a self) -> Id<'a> {
        use self::Ident::*;
        match self {
            Ident::Prefixed(ref id) => Id::Prefixed(Cow::Borrowed(id.share())),
            Ident::Unprefixed(ref id) => Id::Unprefixed(Cow::Borrowed(id)),
            Ident::Url(ref url) => Id::Url(Cow::Borrowed(url)),
        }
    }
}

/// A borrowed `Identifier`.
pub enum Id<'a> {
    Prefixed(Cow<'a, PrefixedId<'a>>),
    Unprefixed(Cow<'a, &'a UnprefixedId>),
    Url(Cow<'a, &'a Url>),
}

impl<'a> Display for Id<'a> {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        use self::Id::*;
        match self {
            Prefixed(id) => id.fmt(f),
            Unprefixed(id) => id.fmt(f),
            Url(url) => url.fmt(f),
        }
    }
}

impl<'a> From<PrefixedId<'a>> for Id<'a> {
     fn from(id: PrefixedId<'a>) -> Self {
         Id::Prefixed(Cow::Borrowed(id))
     }
}

impl<'a> From<&'a UnprefixedId> for Id<'a> {
    fn from(id: &'a UnprefixedId) -> Self {
        Id::Unprefixed(Cow::Borrowed(id))
    }
}

impl<'a> From<&'a Url> for Id<'a> {
    fn from(url: &'a Url) -> Self {
        Id::Url(Cow::Borrowed(url))
    }
}

impl<'i> FromPair<'i> for Id<'i> {
    const RULE: Rule = Rule::Id;
    unsafe fn from_pair_unchecked(pair: Pair<'i, Rule>) -> Result<Self> {
        let inner = pair.into_inner().next().unwrap();
        match inner.as_rule() {
            Rule::PrefixedId => Cow::<PrefixedId>::from_pair_unchecked(inner).map(Id::Prefixed),
            Rule::UnprefixedId => Cow::<&UnprefixedId>::from_pair_unchecked(inner).map(Id::Unprefixed),
            Rule::UrlId => Url::from_pair_unchecked(inner).map(Cow::Owned).map(Id::Url),
            _ => unreachable!()
        }
    }
}
impl_fromslice!('i, Id<'i>);

impl<'a> Redeem<'a> for Id<'a> {
    type Owned = Ident;
    fn redeem(&'a self) -> Ident {
        match self {
            Id::Prefixed(cow) => Ident::Prefixed(cow.redeem()),
            Id::Unprefixed(cow) => Ident::Unprefixed(cow.redeem()),
            Id::Url(cow) => Ident::Url(cow.redeem())
        }
    }
}


#[cfg(test)]
mod tests {

    use pretty_assertions::assert_eq;
    use std::str::FromStr;
    use std::string::ToString;
    use super::*;

    #[test]
    fn from_str() {
        let actual = Ident::from_str("http://purl.obolibrary.org/obo/po.owl").unwrap();
        let expected = Ident::Url(Url::parse("http://purl.obolibrary.org/obo/po.owl").unwrap());
        assert_eq!(actual, expected);

        let actual = Ident::from_str("GO:0046154").unwrap();
        let expected = Ident::from(PrefixedIdent::new("GO", "0046154"));
        assert_eq!(actual, expected);

        let actual = Ident::from_str("goslim_plant").unwrap();
        let expected = Ident::from(UnprefixedIdent::new("goslim_plant"));
        assert_eq!(actual, expected);
    }

    #[test]
    fn partial_cmp() {
        let lp = Ident::Prefixed(PrefixedIdent::new("GO", "001"));
        let rp = Ident::Prefixed(PrefixedIdent::new("GO", "002"));
        assert!(lp < rp);

        let lu = Ident::Unprefixed(UnprefixedIdent::new("has_part"));
        let ru = Ident::Unprefixed(UnprefixedIdent::new("part_of"));
        assert!(lu < ru);

        let lurl = Ident::Url(Url::parse("http://doi.org/").unwrap());
        let rurl = Ident::Url(Url::parse("http://nih.org").unwrap());
        assert!(lurl < rurl);

        assert!(lp < ru);
        assert!(lurl < ru);
    }
}