diff --git a/CHANGELOG.md b/CHANGELOG.md index 3042523..eb07c9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,8 +14,8 @@ Versioning](https://semver.org/spec/v2.0.0.html). ### Removed -- `RecvError` is no longer used. The read functions return `std::io::Error` - instead. +- `RecvError` and `SendError` are no longer used. The recv/send functions return + `std::io::Error` instead. - Bump dependencies. - Update quinn to version 0.11. - Update rustls to version 0.23. diff --git a/Cargo.toml b/Cargo.toml index b9a1567..fef2a2d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,6 @@ num_enum = "0.7" quinn = "0.11" quinn-proto = "0.11" serde = { version = "1", features = ["derive"] } -thiserror = "1" tokio = "1" [dev-dependencies] diff --git a/src/frame.rs b/src/frame.rs index 63feb2e..d243f2c 100644 --- a/src/frame.rs +++ b/src/frame.rs @@ -4,7 +4,6 @@ use bincode::Options; use quinn::{RecvStream, SendStream}; use serde::{Deserialize, Serialize}; use std::{io, mem}; -use thiserror::Error; /// Receives and deserializes a message with a big-endian 4-byte length header. /// @@ -132,32 +131,24 @@ fn from_transport_error_to_io_error(e: quinn_proto::TransportError) -> io::Error } } -/// The error type for sending a message as a frame. -#[derive(Debug, Error)] -pub enum SendError { - #[error("message is too large")] - MessageTooLarge, - #[error("failed to write to a stream")] - WriteError(#[from] quinn::WriteError), -} - /// Sends a message as a stream of bytes with a big-endian 4-byte length header. /// /// `buf` will be cleared after the message is sent. /// /// # Errors /// -/// * `SendError::MessageTooLarge`: if the message is too large -/// * `SendError::WriteError`: if the message could not be written -pub async fn send(send: &mut SendStream, buf: &mut Vec, msg: T) -> Result<(), SendError> +/// Returns [`std::io::ErrorKind::InvalidData`] if the message is too large, or +/// other errors if the message could not be written to the stream. +pub async fn send(send: &mut SendStream, buf: &mut Vec, msg: T) -> io::Result<()> where T: Serialize, { buf.resize(mem::size_of::(), 0); bincode::DefaultOptions::new() .serialize_into(&mut *buf, &msg) - .map_err(|_| SendError::MessageTooLarge)?; - let len = u32::try_from(buf.len() - 4).map_err(|_| SendError::MessageTooLarge)?; + .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; + let len = + u32::try_from(buf.len() - 4).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; buf[..mem::size_of::()].clone_from_slice(&len.to_be_bytes()); send.write_all(buf).await?; buf.clear(); @@ -168,10 +159,10 @@ where /// /// # Errors /// -/// * `SendError::MessageTooLarge`: if the message is too large -/// * `SendError::WriteError`: if the message could not be written -pub async fn send_raw(send: &mut SendStream, buf: &[u8]) -> Result<(), SendError> { - let len = u32::try_from(buf.len()).map_err(|_| SendError::MessageTooLarge)?; +/// Returns an error if the message is too large or could not be written. +pub async fn send_raw(send: &mut SendStream, buf: &[u8]) -> io::Result<()> { + let len = + u32::try_from(buf.len()).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; send.write_all(&len.to_be_bytes()).await?; send.write_all(buf).await?; Ok(()) diff --git a/src/message.rs b/src/message.rs index 41f5200..b7c090a 100644 --- a/src/message.rs +++ b/src/message.rs @@ -1,6 +1,6 @@ //! Functions and errors for handling messages. -use crate::frame::{self, SendError}; +use crate::frame; use bincode::Options; use quinn::{RecvStream, SendStream}; use serde::Serialize; @@ -39,14 +39,14 @@ pub async fn recv_request_raw<'b>( /// /// # Errors /// -/// * `SendError::MessageTooLarge` if the message is too long to be serialized -/// * `SendError::WriteError` if the message could not be written +/// Returns [`std::io::ErrorKind::InvalidData`] if the message is too large, or +/// other errors if the message could not be written to the stream. pub async fn send_request( send: &mut SendStream, buf: &mut Vec, code: C, body: B, -) -> Result<(), SendError> +) -> io::Result<()> where C: Into, B: Serialize, @@ -57,7 +57,7 @@ where buf.extend_from_slice(&code.into().to_le_bytes()); bincode::DefaultOptions::new() .serialize_into(buf as &mut dyn Write, &body) - .map_err(|_| SendError::MessageTooLarge)?; + .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; frame::send_raw(send, buf).await?; buf.clear(); Ok(()) @@ -69,15 +69,14 @@ where /// /// # Errors /// -/// * `SendError::MessageTooLarge` if `body` is too large to be serialized -/// * `SendError::WriteError` if the message could not be written +/// Returns [`std::io::ErrorKind::InvalidData`] if the message is too large, or +/// other errors if the message could not be written to the stream. pub async fn send_ok( send: &mut SendStream, buf: &mut Vec, body: T, -) -> Result<(), SendError> { - frame::send(send, buf, Ok(body) as Result).await?; - Ok(()) +) -> io::Result<()> { + frame::send(send, buf, Ok(body) as Result).await } /// Sends an `Err` response. @@ -86,15 +85,14 @@ pub async fn send_ok( /// /// # Errors /// -/// * `SendError::MessageTooLarge` if `e` is too large to be serialized -/// * `SendError::WriteError` if the message could not be written +/// Returns [`std::io::ErrorKind::InvalidData`] if the message is too large, or +/// other errors if the message could not be written to the stream. pub async fn send_err( send: &mut SendStream, buf: &mut Vec, e: E, -) -> Result<(), SendError> { - frame::send(send, buf, Err(format!("{e:#}")) as Result<(), String>).await?; - Ok(()) +) -> io::Result<()> { + frame::send(send, buf, Err(format!("{e:#}")) as Result<(), String>).await } #[cfg(test)] diff --git a/src/request.rs b/src/request.rs index b20fc08..24ad878 100644 --- a/src/request.rs +++ b/src/request.rs @@ -27,16 +27,20 @@ pub fn parse_args<'de, T: Deserialize<'de>>(args: &'de [u8]) -> io::Result { /// /// # Errors /// -/// * `SendError::MessageTooLarge` if `e` is too large to be serialized -/// * `SendError::WriteError` if the message could not be written +/// Returns [`std::io::ErrorKind::InvalidData`] if the message is too large, or +/// other errors if the message could not be written to the stream. pub async fn send_response( send: &mut SendStream, buf: &mut Vec, body: T, -) -> Result<(), frame::SendError> { - match frame::send(send, buf, body).await { - Ok(()) => Ok(()), - Err(frame::SendError::WriteError(e)) => Err(frame::SendError::WriteError(e)), - Err(e) => message::send_err(send, buf, e).await, +) -> io::Result<()> { + if let Err(e) = frame::send(send, buf, body).await { + if e.kind() == io::ErrorKind::InvalidData { + message::send_err(send, buf, e).await + } else { + Err(e) + } + } else { + Ok(()) } }