Simplifies numeric_encoder errors

pull/46/head
Tpt 4 years ago
parent d7dc9c88a5
commit f38c5577cc
  1. 36
      lib/src/store/numeric_encoder.rs
  2. 8
      lib/src/store/sled.rs

@ -12,10 +12,9 @@ use std::collections::HashMap;
use std::error::Error; use std::error::Error;
use std::hash::Hash; use std::hash::Hash;
use std::hash::Hasher; use std::hash::Hasher;
use std::io;
use std::io::Read; use std::io::Read;
use std::mem::size_of; use std::mem::size_of;
use std::{fmt, str}; use std::{io, str};
#[derive(Ord, PartialOrd, Eq, PartialEq, Debug, Copy, Clone, Hash)] #[derive(Ord, PartialOrd, Eq, PartialEq, Debug, Copy, Clone, Hash)]
#[repr(transparent)] #[repr(transparent)]
@ -731,9 +730,7 @@ impl<R: Read> TermReader for R {
DayTimeDuration::from_be_bytes(buffer), DayTimeDuration::from_be_bytes(buffer),
)) ))
} }
_ => Err(DecoderError::build( _ => Err(invalid_data_error("the term buffer has an invalid type id")),
"the term buffer has an invalid type id",
)),
} }
} }
@ -1229,7 +1226,7 @@ pub(crate) trait Decoder {
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(DecoderError::build( Term::Literal(_) => Err(invalid_data_error(
"A literal has ben found instead of a named node", "A literal has ben found instead of a named node",
)), )),
} }
@ -1238,10 +1235,10 @@ pub(crate) trait Decoder {
fn decode_named_node(&self, encoded: EncodedTerm) -> Result<NamedNode, io::Error> { fn decode_named_node(&self, encoded: EncodedTerm) -> Result<NamedNode, io::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(DecoderError::build( Term::BlankNode(_) => Err(invalid_data_error(
"A blank node has been found instead of a named node", "A blank node has been found instead of a named node",
)), )),
Term::Literal(_) => Err(DecoderError::build( Term::Literal(_) => Err(invalid_data_error(
"A literal has ben found instead of a named node", "A literal has ben found instead of a named node",
)), )),
} }
@ -1271,7 +1268,7 @@ 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, io::Error> {
match encoded { match encoded {
EncodedTerm::DefaultGraph => Err(DecoderError::build( EncodedTerm::DefaultGraph => Err(invalid_data_error(
"The default graph tag is not a valid term", "The default graph tag is not a valid term",
)), )),
EncodedTerm::NamedNode { iri_id } => { EncodedTerm::NamedNode { iri_id } => {
@ -1317,32 +1314,17 @@ impl<S: StrLookup> Decoder for S {
fn get_required_str(lookup: &impl StrLookup, id: StrHash) -> Result<String, io::Error> { fn get_required_str(lookup: &impl StrLookup, id: StrHash) -> Result<String, io::Error> {
lookup.get_str(id).map_err(|e| e.into())?.ok_or_else(|| { lookup.get_str(id).map_err(|e| e.into())?.ok_or_else(|| {
DecoderError::build(format!( invalid_data_error(format!(
"Not able to find the string with id {:?} in the string store", "Not able to find the string with id {:?} in the string store",
id id
)) ))
}) })
} }
#[derive(Debug)] fn invalid_data_error(msg: impl Into<String>) -> io::Error {
pub struct DecoderError { io::Error::new(io::ErrorKind::InvalidData, msg.into())
msg: String,
}
impl DecoderError {
pub fn build(msg: impl Into<String>) -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, Self { msg: msg.into() })
}
}
impl fmt::Display for DecoderError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.msg)
}
} }
impl Error for DecoderError {}
#[test] #[test]
fn test_encoding() { fn test_encoding() {
let mut store = MemoryStrStore::default(); let mut store = MemoryStrStore::default();

@ -833,10 +833,10 @@ fn decode_quad(encoded: &[u8]) -> Result<EncodedQuad, io::Error> {
GSPO_PREFIX => Ok(cursor.read_gspo_quad()?), GSPO_PREFIX => Ok(cursor.read_gspo_quad()?),
GPOS_PREFIX => Ok(cursor.read_gpos_quad()?), GPOS_PREFIX => Ok(cursor.read_gpos_quad()?),
GOSP_PREFIX => Ok(cursor.read_gosp_quad()?), GOSP_PREFIX => Ok(cursor.read_gosp_quad()?),
_ => Err(DecoderError::build(format!( _ => Err(io::Error::new(
"Invalid quad type identifier: {}", io::ErrorKind::InvalidData,
encoded[0] format!("Invalid quad type identifier: {}", encoded[0]),
))), )),
} }
} }

Loading…
Cancel
Save