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
/// A macro to provide `FromStr` implementation to `FromPair` implementors.
///
/// Ideally, a blanket implementation would be sufficient:
/// ```rust,nocompile
/// impl<T: FromPair> FromStr for T {
///     type Error = Error;
///     fn from_str(s: &str) -> Result<Self> {
///        ...
///     }
/// }
/// ```
/// but this is not permitted because of Rust trait implementation hygiene,
/// which prevents implementing a foreign trait on foreign type.
macro_rules! impl_fromstr {
    ($type:ty) => {
        impl std::str::FromStr for $type {
            type Err = $crate::error::Error;
            fn from_str(s: &str) -> $crate::error::Result<Self> {
                use $crate::error::Error;
                use $crate::parser::OboParser;
                use $crate::pest::error::ErrorVariant;
                use $crate::pest::Parser;
                use $crate::pest::Position;

                // Parse the input string
                let mut pairs = OboParser::parse(Self::RULE, s)?;
                let pair = pairs.next().unwrap();
                // Check EOI was reached
                if pair.as_span().end() != s.len() {
                    let span = pair
                        .as_span()
                        .end_pos()
                        .span(&Position::new(s, s.len()).unwrap());
                    let variant = ErrorVariant::CustomError {
                        message: "remaining input".to_string(),
                    };
                    Err($crate::pest::error::Error::new_from_span(variant, span).into())
                } else {
                    unsafe { <Self as FromPair>::from_pair_unchecked(pair) }
                }
            }
        }
    };
}

// FIXME: Proper type !
/// A macro to provide `FromSlice` implementation to `FromPair` implementors.
macro_rules! impl_fromslice {
    ($life:lifetime, $type:ty) => {
        impl<$life> $crate::parser::FromSlice<$life> for $type {
            type Err = $crate::error::Error;
            fn from_slice(s: &$life str) -> $crate::error::Result<Self> {
                use $crate::error::Error;
                use $crate::parser::OboParser;
                use $crate::pest::error::ErrorVariant;
                use $crate::pest::Parser;
                use $crate::pest::Position;

                // Parse the input string
                let mut pairs = OboParser::parse(Self::RULE, s)?;
                let pair = pairs.next().unwrap();
                // Check EOI was reached
                if pair.as_span().end() != s.len() {
                    let span = pair
                        .as_span()
                        .end_pos()
                        .span(&Position::new(s, s.len()).unwrap());
                    let variant = ErrorVariant::CustomError {
                        message: "remaining input".to_string(),
                    };
                    Err($crate::pest::error::Error::new_from_span(variant, span).into())
                } else {
                    unsafe { <Self as FromPair>::from_pair_unchecked(pair) }
                }
            }
        }
    };
}