mongodb_cursor_pagination/error/
mod.rs

1use std::{error, fmt, io, str::Utf8Error};
2
3/// Possible errors that can arise during parsing and creating a cursor.
4#[derive(Debug)]
5pub enum CursorError {
6    IoError(io::Error),
7    InvalidCursor(String),
8    Unknown(String),
9    InvalidId(String),
10}
11
12impl From<io::Error> for CursorError {
13    fn from(err: io::Error) -> Self {
14        Self::IoError(err)
15    }
16}
17
18impl From<Utf8Error> for CursorError {
19    fn from(err: Utf8Error) -> Self {
20        Self::InvalidCursor(err.to_string())
21    }
22}
23
24impl From<base64::DecodeError> for CursorError {
25    fn from(err: base64::DecodeError) -> Self {
26        Self::InvalidCursor(err.to_string())
27    }
28}
29
30impl fmt::Display for CursorError {
31    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match *self {
33            Self::IoError(ref inner) => inner.fmt(fmt),
34            Self::InvalidCursor(ref cursor) => {
35                write!(fmt, "Invalid cursor - unable to parse: {cursor:?}")
36            }
37            Self::Unknown(ref inner) => inner.fmt(fmt),
38            Self::InvalidId(ref id) => write!(fmt, "Invalid id - {id:?}"),
39        }
40    }
41}
42
43#[allow(deprecated)]
44impl error::Error for CursorError {
45    fn description(&self) -> &str {
46        match *self {
47            Self::IoError(ref inner) => inner.description(),
48            Self::InvalidCursor(_) => "Invalid cursor value",
49            Self::Unknown(ref inner) => inner,
50            Self::InvalidId(_) => "Invalid mongodbid",
51        }
52    }
53
54    fn cause(&self) -> Option<&dyn error::Error> {
55        match *self {
56            Self::IoError(ref inner) => Some(inner),
57            _ => None,
58        }
59    }
60}