ndjson_stream/
fallible.rs

1//! This module defines the error- and result-type used for fallible NDJSON, i.e. where the data
2//! source can raise an error. See [FallibleNdjsonError] for more details.
3
4use serde_json::Error as JsonError;
5
6use std::convert::Infallible;
7
8use thiserror::Error;
9
10/// The errors which can occur when using a fallible-input-interface, such as
11/// [FallibleNdjsonIter](crate::driver::iter::FallibleNdjsonIter) or
12/// [FallibleNdjsonStream](crate::driver::stream::FallibleNdjsonStream).
13#[derive(Error, Debug)]
14pub enum FallibleNdjsonError<E> {
15
16    /// Reading the fallible input failed. The error returned by the input on trying to read is
17    /// wrapped in this variant.
18    #[error("error reading input: {0}")]
19    InputError(E),
20
21    /// Parsing a JSON-line failed. The [serde_json::Error] is wrapped in this variant.
22    #[error("error parsing line: {0}")]
23    JsonError(JsonError)
24}
25
26// TODO replace with never-type once available (https://github.com/rust-lang/rust/issues/35121)
27
28impl FallibleNdjsonError<Infallible> {
29    pub(crate) fn unwrap_json_error(self) -> JsonError {
30        match self {
31            FallibleNdjsonError::JsonError(err) => err,
32            FallibleNdjsonError::InputError(err) => match err { }
33        }
34    }
35}
36
37/// Syntactic sugar for a [Result] with the given value type `V` and a [FallibleNdjsonError] whose
38/// input error type is the given error type `E`.
39pub type FallibleNdjsonResult<V, E> = Result<V, FallibleNdjsonError<E>>;