diff src/error.rs @ 0:a65053246c29

Initial commit
author Martin Habovstiak <martin.habovstiak@gmail.com>
date Thu, 26 Nov 2020 22:53:35 +0100
parents
children cabc4aafdd85
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/error.rs	Thu Nov 26 22:53:35 2020 +0100
@@ -0,0 +1,94 @@
+//! Error types that can occur when dealing with `SocketAddr`
+//!
+//! This module separates the error types from root module to avoid clutter.
+
+
+use thiserror::Error;
+use std::io;
+
+/// Error that can occur during parsing of `SocketAddr` from a string
+///
+/// This encapsulates possible errors that can occur when parsing the input.
+/// It is currently opaque because the representation is not certain yet.
+/// It can be displayed using the standard `Error` trait.
+#[derive(Debug, Error)]
+#[error(transparent)]
+pub struct ParseError(#[from] pub(crate) ParseErrorInner);
+
+#[derive(Debug, Error)]
+pub(crate) enum ParseErrorInner {
+    #[error("failed to parse socket address")]
+    SocketAddr(std::net::AddrParseError),
+    #[error("invalid character '{c}' in systemd socket name {string} at position {pos}")]
+    InvalidCharacter { string: String, c: char, pos: usize, }
+}
+
+/// Error that can occur during parsing of `SocketAddr` from a `OsStr`/`OsString`
+///
+/// As opposed to parsing from `&str` or `String`, parsing from `OsStr` can fail due to one more
+/// reason: invalid UTF-8. This error type expresses this possibility and is returned whenever such
+/// conversion is attempted. It is not opaque because the possible variants are pretty much
+/// certain, but it may contain `ParseError` which is opaque.
+///
+/// This error can be displayed using standard `Error` trait.
+/// See `ParseError` for more information.
+#[derive(Debug, Error)]
+pub enum ParseOsStrError {
+    /// The input was not a valid UTF-8 string
+    #[error("the address is not a valid UTF-8 string")]
+    InvalidUtf8,
+    /// The input was a valid UTF-8 string but the address was invalid
+    #[error(transparent)]
+    InvalidAddress(#[from] ParseError),
+}
+
+/// Error that can occur during binding of a socket
+///
+/// This encapsulates possible errors that can occur when binding a socket or receiving a socket
+/// from systemd.
+/// It is currently opaque because the representation is not certain yet.
+/// It can be displayed using the standard `Error` trait.
+#[derive(Debug, Error)]
+#[error(transparent)]
+pub struct BindError(#[from] pub(crate) BindErrorInner);
+
+#[derive(Debug, Error)]
+pub(crate) enum BindErrorInner {
+    #[error("failed to bind {addr}")]
+    BindFailed { addr: std::net::SocketAddr, #[source] error: io::Error, },
+    #[error("failed to receive descriptors with names")]
+    ReceiveDescriptors(#[source] crate::systemd_sockets::Error),
+    #[error("missing systemd socket {0} - a typo or an attempt to bind twice")]
+    MissingDescriptor(String),
+    #[error("the systemd socket {0} is not an internet socket")]
+    NotInetSocket(String),
+}
+
+/// Error that can happen when binding Tokio socket.
+///
+/// As opposed to `std` and `async_std` sockets, tokio sockets can fail to convert.
+/// This error type expresses this possibility.
+#[cfg(any(feature = "tokio_0_2", feature = "tokio_0_3"))]
+#[derive(Debug, Error)]
+#[error(transparent)]
+pub enum TokioBindError {
+    /// Either binding of socket or receiving systemd socket failed
+    Bind(#[from] BindError),
+    /// Conversion from std `std::net::TcpListener` to `tokio::net::TcpListener` failed
+    Convert(#[from] TokioConversionError),
+}
+
+/// Error that can happen when converting Tokio socket.
+///
+/// As opposed to `std` and `async_std` sockets, tokio sockets can fail to convert.
+/// This error type encapsulates conversion error together with additional information so that it
+/// can be displayed nicely. The encapsulation also allows for future-proofing.
+#[cfg(any(feature = "tokio_0_2", feature = "tokio_0_3"))]
+#[derive(Debug, Error)]
+#[error("failed to convert std socket {addr} into tokio socket")]
+pub struct TokioConversionError {
+    pub(crate) addr: crate::SocketAddrInner,
+    #[source]
+    pub(crate) error: io::Error,
+}
+