trapeze/types/encoding/
error.rs

1use std::borrow::Cow;
2use std::io::{Error as IoError, ErrorKind as IoErrorKind};
3
4use thiserror::Error;
5
6#[derive(Error, Debug, Clone)]
7#[error("Invalid input: {0}")]
8pub struct InvalidInput(pub Cow<'static, str>);
9
10#[derive(Error, Debug, Clone)]
11pub enum EncodeError {
12    #[error("Error encoding message: {0}")]
13    InvalidInput(#[from] InvalidInput),
14
15    #[error("Insufficient buffer capacity ({required} bytes > {capacity} bytes)")]
16    InsuficientCapacity { required: usize, capacity: usize },
17}
18
19#[derive(Error, Debug, Clone)]
20pub enum DecodeError {
21    #[error("Unexpected EOF reading input byffer")]
22    UnexpectedEof,
23
24    #[error("Remaining bytes in input buffer: {0} bytes")]
25    RemainingBytes(usize),
26
27    #[error("Error decoding message: {0}")]
28    InvalidInput(#[from] InvalidInput),
29
30    #[error("Error decoding message: Invalid protobuf stream: {0}")]
31    InvalidProtobufStream(#[from] prost::DecodeError),
32}
33
34impl<T: Into<Cow<'static, str>>> From<T> for InvalidInput {
35    fn from(msg: T) -> Self {
36        Self(msg.into())
37    }
38}
39
40impl From<prost::EncodeError> for EncodeError {
41    fn from(err: prost::EncodeError) -> Self {
42        EncodeError::InsuficientCapacity {
43            required: err.required_capacity(),
44            capacity: err.remaining(),
45        }
46    }
47}
48
49impl From<InvalidInput> for std::io::Error {
50    fn from(value: InvalidInput) -> Self {
51        IoError::new(IoErrorKind::InvalidInput, value)
52    }
53}