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
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use std::fmt::Write;
use std::str::FromStr;
use pest::iterators::Pair;
use url::Url;
use crate::ast::*;
use crate::error::Result;
use crate::parser::FromPair;
use crate::parser::Rule;
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Import {
Abbreviated(Ident),
Url(Url),
}
impl Import {
pub fn into_url(self) -> Url {
match self {
Import::Url(u) => u,
Import::Abbreviated(id) => Url::parse(
&format!("http://purl.obolibrary.org/obo/{}.owl", id)
).unwrap(),
}
}
}
impl From<Url> for Import {
fn from(url: Url) -> Self {
Import::Url(url)
}
}
impl From<Ident> for Import {
fn from(id: Ident) -> Self {
Import::Abbreviated(id)
}
}
impl From<Import> for Url {
fn from(import: Import) -> Self {
import.into_url()
}
}
impl Display for Import {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
use self::Import::*;
match self {
Url(url) => url.fmt(f),
Abbreviated(id) => id.fmt(f),
}
}
}
impl<'i> FromPair<'i> for Import {
const RULE: Rule = Rule::Import;
unsafe fn from_pair_unchecked(pair: Pair<'i, Rule>) -> Result<Self> {
let inner = pair.into_inner().next().unwrap();
match inner.as_rule() {
Rule::Iri => Ok(Url::parse(inner.as_str()).unwrap().into()),
Rule::Id => Ident::from_pair_unchecked(inner).map(From::from),
_ => unreachable!(),
}
}
}
impl_fromstr!(Import);
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn into_url() {
let i = Import::Abbreviated(Ident::from(UnprefixedIdent::new("go")));
assert_eq!(i.into_url(), Url::parse("http://purl.obolibrary.org/obo/go.owl").unwrap());
let i = Import::Url(Url::parse("http://ontologies.berkeleybop.org/ms.obo").unwrap());
assert_eq!(i.into_url(), Url::parse("http://ontologies.berkeleybop.org/ms.obo").unwrap());
}
}