Removes ModelError

We could keep one error class and avoid allocation for common errors by adding variants to the main ErrorKind enumeration
pull/26/head
Tpt 5 years ago
parent 23f5ac78ed
commit beb7192e22
  1. 14
      lib/src/error.rs
  2. 37
      lib/src/model/error.rs
  3. 2
      lib/src/model/mod.rs
  4. 5
      lib/src/model/named_node.rs

@ -1,6 +1,6 @@
use crate::model::ModelError;
use peg::error::ParseError; use peg::error::ParseError;
use peg::str::LineCol; use peg::str::LineCol;
use rio_api::iri::IriParseError;
use rio_turtle::TurtleError; use rio_turtle::TurtleError;
use rio_xml::RdfXmlError; use rio_xml::RdfXmlError;
use std::error; use std::error;
@ -21,7 +21,7 @@ impl fmt::Display for Error {
ErrorKind::Io(e) => e.fmt(f), ErrorKind::Io(e) => e.fmt(f),
ErrorKind::FromUtf8(e) => e.fmt(f), ErrorKind::FromUtf8(e) => e.fmt(f),
ErrorKind::Poison => write!(f, "Mutex was poisoned"), ErrorKind::Poison => write!(f, "Mutex was poisoned"),
ErrorKind::Model(e) => e.fmt(f), ErrorKind::Iri(e) => e.fmt(f),
ErrorKind::Other(e) => e.fmt(f), ErrorKind::Other(e) => e.fmt(f),
} }
} }
@ -34,7 +34,7 @@ impl error::Error for Error {
ErrorKind::Io(e) => Some(e), ErrorKind::Io(e) => Some(e),
ErrorKind::FromUtf8(e) => Some(e), ErrorKind::FromUtf8(e) => Some(e),
ErrorKind::Poison => None, ErrorKind::Poison => None,
ErrorKind::Model(e) => Some(e), ErrorKind::Iri(e) => Some(e),
ErrorKind::Other(e) => Some(e.as_ref()), ErrorKind::Other(e) => Some(e.as_ref()),
} }
} }
@ -62,7 +62,7 @@ enum ErrorKind {
Io(io::Error), Io(io::Error),
FromUtf8(FromUtf8Error), FromUtf8(FromUtf8Error),
Poison, Poison,
Model(ModelError), Iri(IriParseError),
Other(Box<dyn error::Error + Send + Sync + 'static>), Other(Box<dyn error::Error + Send + Sync + 'static>),
} }
@ -82,10 +82,10 @@ impl From<FromUtf8Error> for Error {
} }
} }
impl<E: Into<ModelError>> From<E> for Error { impl From<IriParseError> for Error {
fn from(error: E) -> Self { fn from(error: IriParseError) -> Self {
Self { Self {
inner: ErrorKind::Model(error.into()), inner: ErrorKind::Iri(error),
} }
} }
} }

@ -1,37 +0,0 @@
use rio_api::iri::IriParseError;
use std::error;
use std::fmt;
#[derive(Debug)]
pub struct ModelError {
inner: ErrorKind,
}
impl fmt::Display for ModelError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.inner {
ErrorKind::Iri(e) => e.fmt(f),
}
}
}
impl error::Error for ModelError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match &self.inner {
ErrorKind::Iri(e) => Some(e),
}
}
}
#[derive(Debug)]
enum ErrorKind {
Iri(IriParseError),
}
impl From<IriParseError> for ModelError {
fn from(error: IriParseError) -> Self {
Self {
inner: ErrorKind::Iri(error),
}
}
}

@ -3,7 +3,6 @@
//! Inspired by [RDFjs](http://rdf.js.org/) and [Apache Commons RDF](http://commons.apache.org/proper/commons-rdf/) //! Inspired by [RDFjs](http://rdf.js.org/) and [Apache Commons RDF](http://commons.apache.org/proper/commons-rdf/)
mod blank_node; mod blank_node;
mod error;
mod graph; mod graph;
mod isomorphism; mod isomorphism;
mod literal; mod literal;
@ -13,7 +12,6 @@ pub mod vocab;
pub(crate) mod xsd; pub(crate) mod xsd;
pub use crate::model::blank_node::BlankNode; pub use crate::model::blank_node::BlankNode;
pub use crate::model::error::ModelError;
pub use crate::model::graph::SimpleGraph; pub use crate::model::graph::SimpleGraph;
pub use crate::model::literal::Literal; pub use crate::model::literal::Literal;
pub use crate::model::named_node::NamedNode; pub use crate::model::named_node::NamedNode;

@ -1,8 +1,7 @@
use crate::model::ModelError; use crate::Result;
use rio_api::iri::Iri; use rio_api::iri::Iri;
use rio_api::model as rio; use rio_api::model as rio;
use std::fmt; use std::fmt;
use std::result::Result;
/// A RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri) /// A RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri)
/// ///
@ -23,7 +22,7 @@ pub struct NamedNode {
impl NamedNode { impl NamedNode {
/// Builds and validate a RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri) /// Builds and validate a RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri)
pub fn parse(iri: impl Into<String>) -> Result<Self, ModelError> { pub fn parse(iri: impl Into<String>) -> Result<Self> {
Ok(Self::new_from_iri(Iri::parse(iri.into())?)) Ok(Self::new_from_iri(Iri::parse(iri.into())?))
} }

Loading…
Cancel
Save