use async_trait::async_trait;
use tungstenite::Message;
use crate::context::ConnectionContext;
use crate::error::ExtensionError;
#[async_trait]
pub trait Extension: Send + Sync {
fn name(&self) -> &'static str;
fn version(&self) -> &'static str {
"1.0.0"
}
fn description(&self) -> &'static str {
""
}
fn handles_lifecycle(&self) -> bool {
true
}
fn handles_messages(&self) -> bool {
false
}
async fn on_init(&self, _ctx: &ConnectionContext) -> Result<(), ExtensionError> {
Ok(())
}
async fn on_connect(&self, _ctx: &ConnectionContext) -> Result<(), ExtensionError> {
Ok(())
}
async fn on_disconnect(&self, _ctx: &ConnectionContext) -> Result<(), ExtensionError> {
Ok(())
}
async fn on_shutdown(&self, _ctx: &ConnectionContext) -> Result<(), ExtensionError> {
Ok(())
}
async fn on_message(
&self,
message: &Message,
_ctx: &ConnectionContext,
) -> Result<Option<Message>, ExtensionError> {
Ok(Some(message.clone()))
}
}
pub type BoxExtension = Box<dyn Extension>;
pub fn boxed<E: Extension + 'static>(ext: E) -> BoxExtension {
Box::new(ext)
}