aredis/
error.rs

1// Copyright 2022 tison <wander4096@gmail.com>.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use thiserror::Error;
16
17pub type ParseResult<T> = Result<T, ParseError>;
18
19#[derive(Debug, Error)]
20pub enum ParseError {
21    #[error("EndOfStream")]
22    EndOfStream,
23    #[error("OtherParseError({0})")]
24    Other(String),
25}
26
27#[derive(Debug, Error)]
28pub enum Error {
29    #[error(transparent)]
30    Io(#[from] std::io::Error),
31    #[error("InternalError({0}")]
32    Internal(String),
33    #[error("ServerError({0})")]
34    Server(String),
35}
36
37impl From<std::string::FromUtf8Error> for ParseError {
38    fn from(e: std::string::FromUtf8Error) -> Self {
39        Self::Other(e.to_string())
40    }
41}
42
43impl From<std::num::ParseFloatError> for ParseError {
44    fn from(e: std::num::ParseFloatError) -> Self {
45        Self::Other(e.to_string())
46    }
47}
48
49impl<E: Into<ParseError>> From<E> for Error {
50    fn from(e: E) -> Self {
51        match e.into() {
52            ParseError::EndOfStream => unreachable!("EndOfStream should be handled internally."),
53            ParseError::Other(reason) => Self::Internal(reason),
54        }
55    }
56}