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
use pest::error::Error as PestError;
use pest::error::ErrorVariant;
use pest::iterators::Pair;
use url::Url;
use std::str::FromStr;
use crate::error::Error;
use crate::error::SyntaxError;
use crate::parser::Rule;
pub trait FromPair<'i>: Sized {
const RULE: Rule;
unsafe fn from_pair_unchecked(pair: Pair<'i, Rule>) -> Result<Self, SyntaxError>;
#[inline]
fn from_pair(pair: Pair<'i, Rule>) -> Result<Self, SyntaxError> {
if pair.as_rule() != Self::RULE {
return Err(SyntaxError::UnexpectedRule {
actual: pair.as_rule(),
expected: Self::RULE,
});
}
unsafe { Self::from_pair_unchecked(pair) }
}
}
impl<'i> FromPair<'i> for bool {
const RULE: Rule = Rule::Boolean;
unsafe fn from_pair_unchecked(pair: Pair<'i, Rule>) -> Result<Self, SyntaxError> {
Ok(bool::from_str(pair.as_str()).expect("cannot fail."))
}
}
impl<'i> FromPair<'i> for Url {
const RULE: Rule = Rule::Iri;
unsafe fn from_pair_unchecked(pair: Pair<'i, Rule>) -> Result<Self, SyntaxError> {
Url::parse(pair.as_str()).map_err(|e| {
SyntaxError::from(PestError::new_from_span(
ErrorVariant::CustomError {
message: format!("could not parse URL: {}", e),
},
pair.as_span(),
))
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ast::*;
use crate::parser::OboParser;
mod url {
use super::*;
#[test]
fn from_pair() {
let pairs = OboParser::parse(Rule::UnquotedString, "http://not an url");
let pair = pairs.unwrap().next().unwrap();
unsafe { assert!(Url::from_pair_unchecked(pair).is_err()) }
}
}
mod boolean {
use super::*;
#[test]
fn from_pair() {
let pairs = OboParser::parse(Rule::Boolean, "true");
let pair = pairs.unwrap().next().unwrap();
assert_eq!(bool::from_pair(pair).unwrap(), true);
let pairs = OboParser::parse(Rule::Boolean, "false");
let pair = pairs.unwrap().next().unwrap();
assert_eq!(bool::from_pair(pair).unwrap(), false);
}
}
#[test]
fn unexpected_rule() {
let pairs = OboParser::parse(Rule::Boolean, "true");
let pair = pairs.unwrap().next().unwrap();
let err = Ident::from_pair(pair).unwrap_err();
match err {
SyntaxError::UnexpectedRule {
ref actual,
ref expected,
} => {
assert_eq!(actual, &Rule::Boolean);
assert_eq!(expected, &Rule::Id);
}
e => panic!("unexpected error: {:?}", e),
}
}
}