refactor(proto): replace anyhow with thiserror in writers

This commit is contained in:
Jindřich Moravec 2023-12-15 15:58:11 +01:00
parent bb39d138d8
commit 58c69928a1
5 changed files with 21 additions and 6 deletions

View file

@ -3,10 +3,11 @@ use crate::writer::oneway::OneWayProtoWriter;
use crate::writer::protowriter::ProtoWriter; use crate::writer::protowriter::ProtoWriter;
use async_trait::async_trait; use async_trait::async_trait;
use tokio::io::{AsyncWrite, AsyncWriteExt}; use tokio::io::{AsyncWrite, AsyncWriteExt};
use crate::writer::errors::ProtoWriteError;
#[async_trait] #[async_trait]
pub trait BackendProtoWriter: OneWayProtoWriter<BackendMessage> { pub trait BackendProtoWriter: OneWayProtoWriter<BackendMessage> {
async fn write_ssl_reject(&mut self) -> anyhow::Result<()>; async fn write_ssl_reject(&mut self) -> Result<(), ProtoWriteError>;
} }
#[async_trait] #[async_trait]
@ -14,7 +15,7 @@ impl<W> BackendProtoWriter for ProtoWriter<W>
where where
W: AsyncWrite + Unpin + Send, W: AsyncWrite + Unpin + Send,
{ {
async fn write_ssl_reject(&mut self) -> anyhow::Result<()> { async fn write_ssl_reject(&mut self) -> Result<(), ProtoWriteError> {
self.inner.write_u8(b'N').await?; self.inner.write_u8(b'N').await?;
Ok(()) Ok(())
} }

View file

@ -0,0 +1,11 @@
use thiserror::Error;
use tokio::io;
use crate::message::errors::ProtoSerializeError;
#[derive(Debug, Error)]
pub enum ProtoWriteError {
#[error("writing to socket failed")]
Io(#[from] io::Error),
#[error("serialization of inner data failed")]
Serialize(#[from] ProtoSerializeError),
}

View file

@ -2,3 +2,4 @@ pub mod backend;
pub mod frontend; pub mod frontend;
pub mod oneway; pub mod oneway;
pub mod protowriter; pub mod protowriter;
pub mod errors;

View file

@ -2,13 +2,14 @@ use crate::message::proto_message::ProtoMessage;
use crate::writer::protowriter::ProtoWriter; use crate::writer::protowriter::ProtoWriter;
use async_trait::async_trait; use async_trait::async_trait;
use tokio::io::{AsyncWrite, AsyncWriteExt}; use tokio::io::{AsyncWrite, AsyncWriteExt};
use crate::writer::errors::ProtoWriteError;
#[async_trait] #[async_trait]
pub trait OneWayProtoWriter<T> pub trait OneWayProtoWriter<T>
where where
T: ProtoMessage, T: ProtoMessage,
{ {
async fn write_proto(&mut self, message: T) -> anyhow::Result<()>; async fn write_proto(&mut self, message: T) -> Result<(), ProtoWriteError>;
} }
#[async_trait] #[async_trait]
@ -17,7 +18,7 @@ where
W: AsyncWrite + Unpin + Send, W: AsyncWrite + Unpin + Send,
T: ProtoMessage + Send + 'static, T: ProtoMessage + Send + 'static,
{ {
async fn write_proto(&mut self, message: T) -> anyhow::Result<()> { async fn write_proto(&mut self, message: T) -> Result<(), ProtoWriteError> {
let variant = message.variant(); let variant = message.variant();
let mut data = message.serialize()?; let mut data = message.serialize()?;
let length = data.len() as i32 + 4; let length = data.len() as i32 + 4;

View file

@ -1,4 +1,5 @@
use async_trait::async_trait; use async_trait::async_trait;
use tokio::io;
use tokio::io::{AsyncWrite, AsyncWriteExt}; use tokio::io::{AsyncWrite, AsyncWriteExt};
pub struct ProtoWriter<W> pub struct ProtoWriter<W>
@ -19,7 +20,7 @@ where
#[async_trait] #[async_trait]
pub trait ProtoFlush { pub trait ProtoFlush {
async fn flush(&mut self) -> anyhow::Result<()>; async fn flush(&mut self) -> Result<(), io::Error>;
} }
#[async_trait] #[async_trait]
@ -27,7 +28,7 @@ impl<W> ProtoFlush for ProtoWriter<W>
where where
W: AsyncWrite + Unpin + Send, W: AsyncWrite + Unpin + Send,
{ {
async fn flush(&mut self) -> anyhow::Result<()> { async fn flush(&mut self) -> Result<(), io::Error> {
self.inner.flush().await?; self.inner.flush().await?;
Ok(()) Ok(())
} }