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
use std::collections::BTreeSet;

use curie::PrefixMapping;
use horned_owl::model::*;

use crate::error::Error;
use crate::error::Result;
use crate::from_pair::FromPair;
use crate::parser::OwlFunctionalParser;

/// A trait for OWL elements that can be deserialized from OWL strings.
///
/// The deserialization will fail if the entirety of the input string cannot
/// be deserialized into the declared type.
pub trait FromFunctional: Sized + FromPair {
    /// Deserialize a string containing an OWL element in functional syntax.
    fn from_ofn(s: &str, build: &Build, prefixes: &PrefixMapping) -> Result<Self>;

    #[inline]
    fn from_ofn_with_build(s: &str, build: &Build) -> Result<Self> {
        Self::from_ofn(s, build, &PrefixMapping::default())
    }

    #[inline]
    fn from_ofn_with_prefixes(s: &str, prefixes: &PrefixMapping) -> Result<Self> {
        Self::from_ofn(s, &Build::default(), prefixes)
    }

    #[inline]
    fn from_ofn_str(s: &str) -> Result<Self> {
        Self::from_ofn(s, &Build::default(), &PrefixMapping::default())
    }
}

// We use a macro instead of a blanket impl to have all types displayed in
// the documentation.
macro_rules! implement {
    ($($ty:ty),+) => {
        $(impl FromFunctional for $ty {
            fn from_ofn(s: &str, build: &Build, prefixes: &PrefixMapping) -> Result<Self> {
                for rule in Self::RULES {
                    if let Ok(mut pairs) = OwlFunctionalParser::parse(*rule, s) {
                        if pairs.as_str().len() == s.len() {
                            return Self::from_pair(pairs.next().unwrap(), build, prefixes);
                        } else {
                            return Err(
                                Error::from(
                                    pest::error::Error::new_from_span(
                                        pest::error::ErrorVariant::CustomError {
                                            message: "remaining input".to_string(),
                                        },
                                        pest::Span::new(s, pairs.as_str().len(), s.len()).unwrap()
                                    )
                                )
                            )
                        }
                    }
                }

                return Err(
                    Error::from(
                        pest::error::Error::new_from_span(
                            pest::error::ErrorVariant::ParsingError {
                                positives: Vec::new(),
                                negatives: Self::RULES.iter().cloned().collect(),
                            },
                            pest::Span::new(s, 0, s.len()).unwrap()
                        )
                    )
                );
            }
        })*
    }
}

implement!(
    AnnotationProperty,
    AnnotatedAxiom,
    Annotation,
    AnnotationValue,
    BTreeSet<Annotation>,
    Class,
    ClassExpression,
    DataProperty,
    DataRange,
    Datatype,
    DeclareClass,
    DeclareDatatype,
    DeclareObjectProperty,
    DeclareDataProperty,
    DeclareAnnotationProperty,
    DeclareNamedIndividual,
    Facet,
    FacetRestriction,
    Import,
    IRI,
    NamedIndividual,
    Literal,
    ObjectPropertyExpression,
    ObjectProperty,
    Ontology,
    OntologyAnnotation,
    (Ontology, PrefixMapping),
    String,
    SubObjectPropertyExpression,
    u32
);



#[cfg(test)]
mod tests {

    use horned_owl::model::DeclareClass;
    use super::*;

    #[test]
    fn test_remaining_input() {
        match DeclareClass::from_ofn_str("Class(<http://example.com/a>) Class(<http://example.com/b>)") {
            Ok(ok) => panic!("unexpected success: {:?}", ok),
            Err(Error::PestError(e)) => {
                assert_eq!(e.variant, pest::error::ErrorVariant::CustomError {
                    message: "remaining input".to_string(),
                })
            }
            Err(other) => panic!("unexpected error: {:?}", other)
        }
    }
}