Skip to main content

tokio_libtls/
error.rs

1// Copyright (c) 2019, 2020 Reyk Floeter <contact@reykfloeter.com>
2//
3// Permission to use, copy, modify, and distribute this software for any
4// purpose with or without fee is hereby granted, provided that the above
5// copyright notice and this permission notice appear in all copies.
6//
7// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15use crate::AsyncTlsStream;
16use libtls::error::Error as TlsError;
17use std::{error, fmt, io};
18
19/// An error returned by [`AsyncTls`].
20///
21/// This error includes the detailed error message of a failed async
22/// `libtls` operation.
23///
24/// [`AsyncTls`]: ../struct.AsyncTls.html
25#[derive(Debug)]
26pub enum Error {
27    /// The connection is readable.
28    Readable(AsyncTlsStream),
29    /// The connection is writeable.
30    Writeable(AsyncTlsStream),
31    /// The connection is doing a handshake.
32    Handshake(AsyncTlsStream),
33    /// A generic error.
34    Error(TlsError),
35}
36
37/// An error returned by [`AsyncTls`].
38#[deprecated(
39    since = "1.1.1",
40    note = "Please use `Error` instead of `AsyncTlsError`"
41)]
42pub type AsyncTlsError = Error;
43
44impl fmt::Display for Error {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        match self {
47            Error::Readable(_) => write!(f, "Readable I/O in progress"),
48            Error::Writeable(_) => write!(f, "Writable I/O in progress"),
49            Error::Handshake(_) => write!(f, "Handshake I/O in progress"),
50            Error::Error(err) => err.fmt(f),
51        }
52    }
53}
54
55impl error::Error for Error {
56    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
57        None
58    }
59}
60
61impl From<TlsError> for Error {
62    fn from(err: TlsError) -> Self {
63        Error::Error(err)
64    }
65}
66
67impl From<io::Error> for Error {
68    fn from(err: io::Error) -> Self {
69        err.into()
70    }
71}
72
73impl From<Error> for io::Error {
74    fn from(err: Error) -> Self {
75        io::Error::new(io::ErrorKind::Other, err)
76    }
77}