bililive_core/
errors.rs

1//! Error types.
2use std::fmt::Debug;
3
4use nom::Needed;
5use thiserror::Error;
6
7/// The result returned by parsing functions.
8///
9/// * `Ok` indicates a successful parse.
10/// * `Incomplete` means that more data is needed to complete the parsing.
11/// The `Needed` enum can contain how many additional bytes are necessary.
12/// * `Err` indicates an error.
13pub enum IncompleteResult<T> {
14    Ok(T),
15    Incomplete(Needed),
16    Err(ParseError),
17}
18
19/// Errors that may occur when parsing a packet.
20#[derive(Debug, Error)]
21pub enum ParseError {
22    #[error("json error: {0}")]
23    Json(#[from] serde_json::Error),
24    #[error("not a valid int32 big endian")]
25    Int32BE,
26    #[error("unknown websocket pack protocol")]
27    UnknownProtocol,
28    #[error("error when parsing packet struct")]
29    PacketError(String),
30    #[error("error when decompressing packet buffer: {0}")]
31    ZlibError(#[from] std::io::Error),
32}
33
34#[cfg(feature = "not-send")]
35pub(crate) type BoxedError = Box<dyn std::error::Error>;
36
37#[cfg(not(feature = "not-send"))]
38pub(crate) type BoxedError = Box<dyn std::error::Error + Send + Sync>;
39
40/// Errors that may occur when making HTTP requests through builder.
41#[derive(Debug, Error)]
42#[error("error when making http request: {0}")]
43pub struct BuildError(#[source] pub(crate) BoxedError);
44
45/// Errors that may occur when consuming a stream.
46///
47/// `E` is determined by the underlying websocket implementation.
48#[derive(Debug, Error)]
49pub enum StreamError<E> {
50    #[error("parse error: {0}")]
51    Parse(#[from] ParseError),
52    #[error("ws error: {0}")]
53    WebSocket(E),
54    #[error("io error: {0}")]
55    IO(#[from] std::io::Error),
56}
57
58impl<E> StreamError<E> {
59    pub const fn from_ws_error(e: E) -> Self {
60        Self::WebSocket(e)
61    }
62}