Introduces DecoderError

pull/46/head
Tpt 4 years ago
parent 3b207f7239
commit 81743f464e
  1. 7
      lib/src/sparql/error.rs
  2. 96
      lib/src/store/numeric_encoder.rs
  3. 2
      lib/src/store/rocksdb.rs
  4. 2
      lib/src/store/sled.rs

@ -1,5 +1,6 @@
use crate::error::Infallible; use crate::error::Infallible;
use crate::sparql::ParseError; use crate::sparql::ParseError;
use crate::store::numeric_encoder::DecoderError;
use std::error; use std::error;
use std::fmt; use std::fmt;
use std::io; use std::io;
@ -107,3 +108,9 @@ impl From<io::Error> for EvaluationError {
Self::Io(error) Self::Io(error)
} }
} }
impl<E: Into<io::Error>> From<DecoderError<E>> for EvaluationError {
fn from(error: DecoderError<E>) -> Self {
io::Error::from(error).into()
}
}

@ -12,7 +12,7 @@ use std::hash::Hash;
use std::hash::Hasher; use std::hash::Hasher;
use std::io::Read; use std::io::Read;
use std::mem::size_of; use std::mem::size_of;
use std::{io, str}; use std::{fmt, io, str};
#[derive(Ord, PartialOrd, Eq, PartialEq, Debug, Copy, Clone, Hash)] #[derive(Ord, PartialOrd, Eq, PartialEq, Debug, Copy, Clone, Hash)]
#[repr(transparent)] #[repr(transparent)]
@ -1097,35 +1097,38 @@ pub fn parse_day_time_duration_str(value: &str) -> Option<EncodedTerm> {
value.parse().map(EncodedTerm::DayTimeDurationLiteral).ok() value.parse().map(EncodedTerm::DayTimeDurationLiteral).ok()
} }
pub(crate) trait Decoder { pub(crate) trait Decoder: StrLookup {
fn decode_term(&self, encoded: EncodedTerm) -> Result<Term, io::Error>; fn decode_term(&self, encoded: EncodedTerm) -> Result<Term, DecoderError<Self::Error>>;
fn decode_named_or_blank_node( fn decode_named_or_blank_node(
&self, &self,
encoded: EncodedTerm, encoded: EncodedTerm,
) -> Result<NamedOrBlankNode, io::Error> { ) -> Result<NamedOrBlankNode, DecoderError<Self::Error>> {
match self.decode_term(encoded)? { match self.decode_term(encoded)? {
Term::NamedNode(named_node) => Ok(named_node.into()), Term::NamedNode(named_node) => Ok(named_node.into()),
Term::BlankNode(blank_node) => Ok(blank_node.into()), Term::BlankNode(blank_node) => Ok(blank_node.into()),
Term::Literal(_) => Err(invalid_data_error( Term::Literal(_) => Err(DecoderError::Decoder {
"A literal has ben found instead of a named node", msg: "A literal has ben found instead of a named node".to_owned(),
)), }),
} }
} }
fn decode_named_node(&self, encoded: EncodedTerm) -> Result<NamedNode, io::Error> { fn decode_named_node(
&self,
encoded: EncodedTerm,
) -> Result<NamedNode, DecoderError<Self::Error>> {
match self.decode_term(encoded)? { match self.decode_term(encoded)? {
Term::NamedNode(named_node) => Ok(named_node), Term::NamedNode(named_node) => Ok(named_node),
Term::BlankNode(_) => Err(invalid_data_error( Term::BlankNode(_) => Err(DecoderError::Decoder {
"A blank node has been found instead of a named node", msg: "A blank node has been found instead of a named node".to_owned(),
)), }),
Term::Literal(_) => Err(invalid_data_error( Term::Literal(_) => Err(DecoderError::Decoder {
"A literal has ben found instead of a named node", msg: "A literal has ben found instead of a named node".to_owned(),
)), }),
} }
} }
fn decode_triple(&self, encoded: &EncodedQuad) -> Result<Triple, io::Error> { fn decode_triple(&self, encoded: &EncodedQuad) -> Result<Triple, DecoderError<Self::Error>> {
Ok(Triple::new( Ok(Triple::new(
self.decode_named_or_blank_node(encoded.subject)?, self.decode_named_or_blank_node(encoded.subject)?,
self.decode_named_node(encoded.predicate)?, self.decode_named_node(encoded.predicate)?,
@ -1133,7 +1136,7 @@ pub(crate) trait Decoder {
)) ))
} }
fn decode_quad(&self, encoded: &EncodedQuad) -> Result<Quad, io::Error> { fn decode_quad(&self, encoded: &EncodedQuad) -> Result<Quad, DecoderError<Self::Error>> {
Ok(Quad::new( Ok(Quad::new(
self.decode_named_or_blank_node(encoded.subject)?, self.decode_named_or_blank_node(encoded.subject)?,
self.decode_named_node(encoded.predicate)?, self.decode_named_node(encoded.predicate)?,
@ -1147,11 +1150,11 @@ pub(crate) trait Decoder {
} }
impl<S: StrLookup> Decoder for S { impl<S: StrLookup> Decoder for S {
fn decode_term(&self, encoded: EncodedTerm) -> Result<Term, io::Error> { fn decode_term(&self, encoded: EncodedTerm) -> Result<Term, DecoderError<Self::Error>> {
match encoded { match encoded {
EncodedTerm::DefaultGraph => Err(invalid_data_error( EncodedTerm::DefaultGraph => Err(DecoderError::Decoder {
"The default graph tag is not a valid term", msg: "The default graph tag is not a valid term".to_owned(),
)), }),
EncodedTerm::NamedNode { iri_id } => { EncodedTerm::NamedNode { iri_id } => {
Ok(NamedNode::new_unchecked(get_required_str(self, iri_id)?).into()) Ok(NamedNode::new_unchecked(get_required_str(self, iri_id)?).into())
} }
@ -1193,13 +1196,52 @@ impl<S: StrLookup> Decoder for S {
} }
} }
fn get_required_str(lookup: &impl StrLookup, id: StrHash) -> Result<String, io::Error> { fn get_required_str<L: StrLookup>(
lookup.get_str(id).map_err(|e| e.into())?.ok_or_else(|| { lookup: &L,
invalid_data_error(format!( id: StrHash,
"Not able to find the string with id {:?} in the string store", ) -> Result<String, DecoderError<L::Error>> {
id lookup
)) .get_str(id)
}) .map_err(DecoderError::Store)?
.ok_or_else(|| DecoderError::Decoder {
msg: format!(
"Not able to find the string with id {:?} in the string store",
id
),
})
}
#[derive(Debug)]
pub(crate) enum DecoderError<E> {
Store(E),
Decoder { msg: String },
}
impl<E: fmt::Display> fmt::Display for DecoderError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Store(e) => e.fmt(f),
Self::Decoder { msg } => write!(f, "{}", msg),
}
}
}
impl<E: Error + 'static> Error for DecoderError<E> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::Store(e) => Some(e),
Self::Decoder { .. } => None,
}
}
}
impl<E: Into<io::Error>> From<DecoderError<E>> for io::Error {
fn from(e: DecoderError<E>) -> Self {
match e {
DecoderError::Store(e) => e.into(),
DecoderError::Decoder { msg } => invalid_data_error(msg),
}
}
} }
#[test] #[test]

@ -127,7 +127,7 @@ impl RocksDbStore {
let graph_name = graph_name.map(|g| g.into()); let graph_name = graph_name.map(|g| g.into());
let store = self.clone(); let store = self.clone();
self.encoded_quads_for_pattern(subject, predicate, object, graph_name) self.encoded_quads_for_pattern(subject, predicate, object, graph_name)
.map(move |quad| store.decode_quad(&quad?)) .map(move |quad| Ok(store.decode_quad(&quad?)?))
} }
/// Checks if this store contains a given quad /// Checks if this store contains a given quad

@ -125,7 +125,7 @@ impl SledStore {
let graph_name = graph_name.map(|g| g.into()); let graph_name = graph_name.map(|g| g.into());
let this = self.clone(); let this = self.clone();
self.encoded_quads_for_pattern(subject, predicate, object, graph_name) self.encoded_quads_for_pattern(subject, predicate, object, graph_name)
.map(move |quad| this.decode_quad(&quad?)) .map(move |quad| Ok(this.decode_quad(&quad?)?))
} }
/// Checks if this store contains a given quad /// Checks if this store contains a given quad

Loading…
Cancel
Save