From dc3f092d0782fd7b39bb29aa424caa04ada8de3c Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Thu, 16 Jun 2022 12:03:43 +0200 Subject: [PATCH] Do not leak an internal grenad Error --- milli/src/documents/mod.rs | 2 +- milli/src/documents/reader.rs | 32 ++++++++++++++++++++++++++++++-- milli/src/error.rs | 7 +++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/milli/src/documents/mod.rs b/milli/src/documents/mod.rs index ee3593bf8..66a05b7b6 100644 --- a/milli/src/documents/mod.rs +++ b/milli/src/documents/mod.rs @@ -7,7 +7,7 @@ use std::io; use bimap::BiHashMap; pub use builder::DocumentsBatchBuilder; use obkv::KvReader; -pub use reader::{DocumentsBatchCursor, DocumentsBatchReader}; +pub use reader::{DocumentsBatchCursor, DocumentsBatchCursorError, DocumentsBatchReader}; use serde::{Deserialize, Serialize}; use crate::error::{FieldIdMapMissingEntry, InternalError}; diff --git a/milli/src/documents/reader.rs b/milli/src/documents/reader.rs index 3dff999f5..720b403b9 100644 --- a/milli/src/documents/reader.rs +++ b/milli/src/documents/reader.rs @@ -1,5 +1,5 @@ use std::convert::TryInto; -use std::io; +use std::{error, fmt, io}; use obkv::KvReader; @@ -79,7 +79,9 @@ impl DocumentsBatchCursor { impl DocumentsBatchCursor { /// Returns the next document, starting from the first one. Subsequent calls to /// `next_document` advance the document reader until all the documents have been read. - pub fn next_document(&mut self) -> Result>, grenad::Error> { + pub fn next_document( + &mut self, + ) -> Result>, DocumentsBatchCursorError> { match self.cursor.move_on_next()? { Some((key, value)) if key != DOCUMENTS_BATCH_INDEX_KEY => { Ok(Some(KvReader::new(value))) @@ -88,3 +90,29 @@ impl DocumentsBatchCursor { } } } + +/// The possible error thrown by the `DocumentsBatchCursor` when iterating on the documents. +#[derive(Debug)] +pub struct DocumentsBatchCursorError { + inner: grenad::Error, +} + +impl From for DocumentsBatchCursorError { + fn from(error: grenad::Error) -> DocumentsBatchCursorError { + DocumentsBatchCursorError { inner: error } + } +} + +impl Into for DocumentsBatchCursorError { + fn into(self) -> grenad::Error { + self.inner + } +} + +impl error::Error for DocumentsBatchCursorError {} + +impl fmt::Display for DocumentsBatchCursorError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.inner.fmt(f) + } +} diff --git a/milli/src/error.rs b/milli/src/error.rs index d05acbe1c..d9dca287d 100644 --- a/milli/src/error.rs +++ b/milli/src/error.rs @@ -7,6 +7,7 @@ use rayon::ThreadPoolBuildError; use serde_json::Value; use thiserror::Error; +use crate::documents::DocumentsBatchCursorError; use crate::{CriterionError, DocumentId, FieldId, Object, SortError}; pub fn is_reserved_keyword(keyword: &str) -> bool { @@ -209,6 +210,12 @@ where } } +impl From for Error { + fn from(error: DocumentsBatchCursorError) -> Error { + Error::from(Into::::into(error)) + } +} + impl From for Error { fn from(_error: Infallible) -> Error { unreachable!()