From beb7192e22dc3c67ebd1b5b63560655d61044fd4 Mon Sep 17 00:00:00 2001 From: Tpt Date: Fri, 3 Apr 2020 19:29:01 +0200 Subject: [PATCH] Removes ModelError We could keep one error class and avoid allocation for common errors by adding variants to the main ErrorKind enumeration --- lib/src/error.rs | 14 +++++++------- lib/src/model/error.rs | 37 ------------------------------------- lib/src/model/mod.rs | 2 -- lib/src/model/named_node.rs | 5 ++--- 4 files changed, 9 insertions(+), 49 deletions(-) delete mode 100644 lib/src/model/error.rs diff --git a/lib/src/error.rs b/lib/src/error.rs index bcb24a00..58ecd0c5 100644 --- a/lib/src/error.rs +++ b/lib/src/error.rs @@ -1,6 +1,6 @@ -use crate::model::ModelError; use peg::error::ParseError; use peg::str::LineCol; +use rio_api::iri::IriParseError; use rio_turtle::TurtleError; use rio_xml::RdfXmlError; use std::error; @@ -21,7 +21,7 @@ impl fmt::Display for Error { ErrorKind::Io(e) => e.fmt(f), ErrorKind::FromUtf8(e) => e.fmt(f), 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), } } @@ -34,7 +34,7 @@ impl error::Error for Error { ErrorKind::Io(e) => Some(e), ErrorKind::FromUtf8(e) => Some(e), ErrorKind::Poison => None, - ErrorKind::Model(e) => Some(e), + ErrorKind::Iri(e) => Some(e), ErrorKind::Other(e) => Some(e.as_ref()), } } @@ -62,7 +62,7 @@ enum ErrorKind { Io(io::Error), FromUtf8(FromUtf8Error), Poison, - Model(ModelError), + Iri(IriParseError), Other(Box), } @@ -82,10 +82,10 @@ impl From for Error { } } -impl> From for Error { - fn from(error: E) -> Self { +impl From for Error { + fn from(error: IriParseError) -> Self { Self { - inner: ErrorKind::Model(error.into()), + inner: ErrorKind::Iri(error), } } } diff --git a/lib/src/model/error.rs b/lib/src/model/error.rs deleted file mode 100644 index dc6c95ae..00000000 --- a/lib/src/model/error.rs +++ /dev/null @@ -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 for ModelError { - fn from(error: IriParseError) -> Self { - Self { - inner: ErrorKind::Iri(error), - } - } -} diff --git a/lib/src/model/mod.rs b/lib/src/model/mod.rs index d3a713ca..76effb32 100644 --- a/lib/src/model/mod.rs +++ b/lib/src/model/mod.rs @@ -3,7 +3,6 @@ //! Inspired by [RDFjs](http://rdf.js.org/) and [Apache Commons RDF](http://commons.apache.org/proper/commons-rdf/) mod blank_node; -mod error; mod graph; mod isomorphism; mod literal; @@ -13,7 +12,6 @@ pub mod vocab; pub(crate) mod xsd; pub use crate::model::blank_node::BlankNode; -pub use crate::model::error::ModelError; pub use crate::model::graph::SimpleGraph; pub use crate::model::literal::Literal; pub use crate::model::named_node::NamedNode; diff --git a/lib/src/model/named_node.rs b/lib/src/model/named_node.rs index fb3dbef4..3e8df04a 100644 --- a/lib/src/model/named_node.rs +++ b/lib/src/model/named_node.rs @@ -1,8 +1,7 @@ -use crate::model::ModelError; +use crate::Result; use rio_api::iri::Iri; use rio_api::model as rio; use std::fmt; -use std::result::Result; /// A RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri) /// @@ -23,7 +22,7 @@ pub struct NamedNode { impl NamedNode { /// Builds and validate a RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri) - pub fn parse(iri: impl Into) -> Result { + pub fn parse(iri: impl Into) -> Result { Ok(Self::new_from_iri(Iri::parse(iri.into())?)) }