Skip to content

Commit

Permalink
Use AgentError instead of associated Error type
Browse files Browse the repository at this point in the history
Signed-off-by: Wiktor Kwapisiewicz <[email protected]>
  • Loading branch information
wiktor-k committed Feb 22, 2024
1 parent d7c0e24 commit 9b755d8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 12 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ use async_trait::async_trait;
use tokio::net::UnixListener;
use ssh_agent_lib::agent::Agent;
use ssh_agent_lib::error::AgentError;
use ssh_agent_lib::proto::message::{Message, SignRequest};
struct MyAgent;
#[async_trait]
impl Agent for MyAgent {
type Error = ();
async fn handle(&self, message: Message) -> Result<Message, ()> {
async fn handle(&self, message: Message) -> Result<Message, AgentError> {
match message {
Message::SignRequest(request) => {
// get the signature by signing `request.data`
Expand Down
5 changes: 2 additions & 3 deletions examples/key_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use log::info;
use tokio::net::UnixListener;

use ssh_agent_lib::agent::Agent;
use ssh_agent_lib::error::AgentError;
use ssh_agent_lib::proto::message::{self, Message, SignRequest};
use ssh_agent_lib::proto::private_key::{PrivateKey, RsaPrivateKey};
use ssh_agent_lib::proto::public_key::PublicKey;
Expand Down Expand Up @@ -147,9 +148,7 @@ impl KeyStorage {

#[async_trait]
impl Agent for KeyStorage {
type Error = ();

async fn handle(&self, message: Message) -> Result<Message, ()> {
async fn handle(&self, message: Message) -> Result<Message, AgentError> {
self.handle_message(message).or_else(|error| {
println!("Error handling message - {:?}", error);
Ok(Message::Failure)
Expand Down
9 changes: 3 additions & 6 deletions src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use tokio::io::{AsyncRead, AsyncWrite};
use tokio::net::{TcpListener, TcpStream, UnixListener, UnixStream};
use tokio_util::codec::{Decoder, Encoder, Framed};

use std::error::Error;
use std::fmt;
use std::io;
use std::marker::Unpin;
Expand Down Expand Up @@ -111,11 +110,9 @@ impl ListeningSocket for TcpListener {

#[async_trait]
pub trait Agent: 'static + Sync + Send + Sized {
type Error: fmt::Debug + Send + Sync;
async fn handle(&self, message: Message) -> Result<Message, AgentError>;

async fn handle(&self, message: Message) -> Result<Message, Self::Error>;

async fn listen<S>(self, socket: S) -> Result<(), Box<dyn Error + Send + Sync>>
async fn listen<S>(self, socket: S) -> Result<(), AgentError>
where
S: ListeningSocket + fmt::Debug + Send,
{
Expand All @@ -136,7 +133,7 @@ pub trait Agent: 'static + Sync + Send + Sized {
}
Err(e) => {
error!("Failed to accept socket; error = {:?}", e);
return Err(Box::new(e));
return Err(AgentError::IO(e));
}
}
}
Expand Down

0 comments on commit 9b755d8

Please sign in to comment.