Add a separate trait for converting errors into server responses.

This commit is contained in:
2021-11-21 21:08:29 -08:00
parent 774591cb54
commit 74f66ef747
4 changed files with 34 additions and 11 deletions

View File

@@ -4,7 +4,7 @@ use crate::network::datagram::GenericDatagramSocket;
use crate::network::listener::GenericListener;
use crate::network::stream::GenericStream;
use async_trait::async_trait;
use std::fmt::Display;
use std::fmt::{Debug, Display};
#[async_trait]
pub trait Networklike {
@@ -12,7 +12,7 @@ pub trait Networklike {
/// for using only one; if you have a use case for separating your errors,
/// please shoot the author(s) and email to split this into multiple types, one
/// for each trait function.
type Error: Display + Into<ServerResponseStatus> + Send;
type Error: Debug + Display + IntoErrorResponse + Send;
/// Connect to the given address and port, over this kind of network. The
/// underlying stream should behave somewhat like a TCP stream ... which
@@ -46,3 +46,16 @@ pub trait Networklike {
port: u16,
) -> Result<GenericDatagramSocket<Self::Error>, Self::Error>;
}
/// This trait is a hack; sorry about that. The thing is, we want to be able to
/// convert Errors from the `Networklike` trait into a `ServerResponseStatus`,
/// but want to do so on references to the error object rather than the actual
/// object. This is for the paired reason that (a) we want to be able to use
/// the errors in multiple places -- for example, to return a value to the client
/// and then also to whoever called the function -- and (b) some common errors
/// (I'm looking at you, `io::Error`) aren't `Clone`. So ... hence this overly-
/// specific trait.
pub trait IntoErrorResponse {
#[allow(clippy::wrong_self_convention)]
fn into_response(&self) -> ServerResponseStatus;
}