From 978e43a4c12e8443b4f4ccd1b89ce1987a8f3535 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Wed, 22 Dec 2021 11:07:19 +0100 Subject: [PATCH] Consistently return Error type --- src/client.rs | 9 ++++----- src/connection.rs | 11 +++++------ src/error.rs | 21 +++++++++++++++++++++ 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/client.rs b/src/client.rs index d95bcc6..47d4259 100644 --- a/src/client.rs +++ b/src/client.rs @@ -33,7 +33,6 @@ //! } //! ``` -use std::error::Error as StdError; use std::net::SocketAddr; use crate::common::{Certificate, NoExtension, PrivateKey}; @@ -59,14 +58,14 @@ impl EppClient { addr: SocketAddr, hostname: &str, identity: Option<(Vec, PrivateKey)>, - ) -> Result> { + ) -> Result { Ok(Self { connection: EppConnection::connect(registry, addr, hostname, identity).await?, }) } /// Executes an EPP Hello call and returns the response as an `Greeting` - pub async fn hello(&mut self) -> Result> { + pub async fn hello(&mut self) -> Result { let hello_xml = HelloDocument::default().serialize()?; let response = self.connection.transact(&hello_xml).await?; @@ -93,7 +92,7 @@ impl EppClient { /// Accepts raw EPP XML and returns the raw EPP XML response to it. /// Not recommended for direct use but sometimes can be useful for debugging - pub async fn transact_xml(&mut self, xml: &str) -> Result> { + pub async fn transact_xml(&mut self, xml: &str) -> Result { self.connection.transact(xml).await } @@ -107,7 +106,7 @@ impl EppClient { GreetingDocument::deserialize(&self.connection.greeting).map(|obj| obj.data) } - pub async fn shutdown(mut self) -> Result<(), Box> { + pub async fn shutdown(mut self) -> Result<(), Error> { self.connection.shutdown().await } } diff --git a/src/connection.rs b/src/connection.rs index 258b6e7..2876b9f 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -1,7 +1,6 @@ //! Manages registry connections and reading/writing to them use std::convert::TryInto; -use std::error::Error as StdError; use std::net::SocketAddr; use std::sync::Arc; use std::{io, str, u32}; @@ -28,7 +27,7 @@ impl EppConnection { addr: SocketAddr, hostname: &str, identity: Option<(Vec, PrivateKey)>, - ) -> Result> { + ) -> Result { let mut stream = epp_connect(addr, hostname, identity).await?; let mut buf = vec![0u8; 4096]; @@ -45,7 +44,7 @@ impl EppConnection { } /// Constructs an EPP XML request in the required form and sends it to the server - async fn send_epp_request(&mut self, content: &str) -> Result<(), Box> { + async fn send_epp_request(&mut self, content: &str) -> Result<(), Error> { let len = content.len(); let buf_size = len + 4; @@ -63,7 +62,7 @@ impl EppConnection { } /// Receives response from the socket and converts it into an EPP XML string - async fn get_epp_response(&mut self) -> Result> { + async fn get_epp_response(&mut self) -> Result { let mut buf = [0u8; 4]; self.stream.read_exact(&mut buf).await?; @@ -98,7 +97,7 @@ impl EppConnection { /// Sends an EPP XML request to the registry and return the response /// receieved to the request - pub(crate) async fn transact(&mut self, content: &str) -> Result> { + pub(crate) async fn transact(&mut self, content: &str) -> Result { debug!("{}: request: {}", self.registry, content); self.send_epp_request(content).await?; @@ -109,7 +108,7 @@ impl EppConnection { } /// Closes the socket and shuts the connection - pub(crate) async fn shutdown(&mut self) -> Result<(), Box> { + pub(crate) async fn shutdown(&mut self) -> Result<(), Error> { info!("{}: Closing connection", self.registry); self.stream.shutdown().await?; diff --git a/src/error.rs b/src/error.rs index 100f896..1696d08 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,6 +3,9 @@ use std::error::Error as StdError; use std::fmt::{self, Display}; use std::io; +use std::num::TryFromIntError; +use std::str::Utf8Error; +use std::string::FromUtf8Error; use crate::response::ResponseStatus; @@ -46,3 +49,21 @@ impl From for Error { Self::Io(io::Error::from(e)) } } + +impl From for Error { + fn from(e: TryFromIntError) -> Self { + Self::Other(e.into()) + } +} + +impl From for Error { + fn from(e: FromUtf8Error) -> Self { + Self::Other(e.into()) + } +} + +impl From for Error { + fn from(e: Utf8Error) -> Self { + Self::Other(e.into()) + } +}