redis/errors/
parsing_error.rs

1use std::{ffi::NulError, fmt, str::Utf8Error, string::FromUtf8Error};
2
3use arcstr::ArcStr;
4
5/// Describes a type conversion or parsing failure.
6#[derive(Clone, Debug, PartialEq)]
7pub struct ParsingError {
8    pub(crate) description: ArcStr,
9}
10
11impl std::fmt::Display for ParsingError {
12    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13        f.write_str("Incompatible type - ")?;
14        self.description.fmt(f)
15    }
16}
17
18impl std::error::Error for ParsingError {}
19
20impl From<NulError> for ParsingError {
21    fn from(err: NulError) -> ParsingError {
22        format!("Value contains interior nul terminator: {err}",).into()
23    }
24}
25
26impl From<Utf8Error> for ParsingError {
27    fn from(_: Utf8Error) -> ParsingError {
28        arcstr::literal!("Invalid UTF-8").into()
29    }
30}
31
32#[cfg(feature = "uuid")]
33impl From<uuid::Error> for ParsingError {
34    fn from(err: uuid::Error) -> ParsingError {
35        format!("Value is not a valid UUID: {err}").into()
36    }
37}
38
39impl From<FromUtf8Error> for ParsingError {
40    fn from(err: FromUtf8Error) -> ParsingError {
41        format!("Cannot convert from UTF-8: {err}").into()
42    }
43}
44
45impl From<String> for ParsingError {
46    fn from(err: String) -> ParsingError {
47        ParsingError {
48            description: err.into(),
49        }
50    }
51}
52
53impl<'a> From<&'a str> for ParsingError {
54    fn from(err: &'a str) -> ParsingError {
55        ParsingError {
56            description: err.into(),
57        }
58    }
59}
60
61impl From<ArcStr> for ParsingError {
62    fn from(err: ArcStr) -> ParsingError {
63        ParsingError { description: err }
64    }
65}