use std::{error::Error as StdError, sync::Arc};
use async_trait::async_trait;
#[derive(Debug, PartialEq)]
pub enum Status {
Closing,
Closed,
Connecting,
Connected,
Disconnected,
}
#[async_trait]
pub trait GmqConnection: Send + Sync {
fn status(&self) -> Status;
fn add_handler(&mut self, handler: Arc<dyn EventHandler>) -> String;
fn remove_handler(&mut self, id: &str);
fn connect(&mut self) -> Result<(), Box<dyn StdError>>;
async fn close(&mut self) -> Result<(), Box<dyn StdError + Send + Sync>>;
}
#[async_trait]
pub trait EventHandler: Send + Sync {
async fn on_error(
&self,
handler_id: String,
conn: Arc<dyn GmqConnection>,
err: Box<dyn StdError + Send + Sync>,
);
async fn on_status(&self, handler_id: String, conn: Arc<dyn GmqConnection>, status: Status);
}
impl Copy for Status {}
impl Clone for Status {
fn clone(&self) -> Status {
*self
}
}