1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use std::borrow::Cow;
use serde::{de, ser};
use thiserror::Error;
use crate::parser;
type NomError<T> = nom::error::Error<T>;
fn transform_parse_err<T, I>(
err: nom::Err<NomError<I>>,
map_input: impl FnOnce(I) -> T,
) -> nom::Err<NomError<T>> {
let make_err = |e: NomError<I>| NomError {
input: map_input(e.input),
code: e.code,
};
match err {
nom::Err::Error(e) => nom::Err::Error(make_err(e)),
nom::Err::Failure(e) => nom::Err::Failure(make_err(e)),
nom::Err::Incomplete(needed) => nom::Err::Incomplete(needed),
}
}
#[derive(Debug, Error)]
pub enum Error<'a> {
#[error("{0}")]
Message(Cow<'a, str>),
#[error("io error: {0}")]
Io(#[from] std::io::Error),
#[error("parse error: {0}")]
Parse(parser::Error<'a>),
#[error("Redis error: {0}")]
Redis(Cow<'a, str>),
}
impl Error<'_> {
pub fn into_owned(self) -> Error<'static> {
match self {
Self::Message(msg) => Error::Message(msg.into_owned().into()),
Self::Io(err) => Error::Io(err),
Self::Parse(err) => Error::Parse(transform_parse_err(err, |i| i.into_owned().into())),
Self::Redis(msg) => Error::Redis(msg.into_owned().into()),
}
}
pub fn is_transient(&self) -> bool {
matches!(self, Self::Redis(_))
}
}
impl ser::Error for Error<'_> {
fn custom<T>(msg: T) -> Self
where
T: std::fmt::Display,
{
Self::Message(msg.to_string().into())
}
}
impl de::Error for Error<'_> {
fn custom<T>(msg: T) -> Self
where
T: std::fmt::Display,
{
Self::Message(msg.to_string().into())
}
}
impl<'a> From<parser::Error<'a>> for Error<'a> {
fn from(err: parser::Error<'a>) -> Self {
Self::Parse(err)
}
}
impl<'a> From<parser::RawError<'a>> for Error<'a> {
fn from(err: parser::RawError<'a>) -> Self {
Self::Parse(transform_parse_err(err, |i| i.into()))
}
}
pub type Result<'a, T, E = Error<'a>> = std::result::Result<T, E>;