pipey 0.2.1

A lightweight HTTP-to-WebSocket event delivery service.
Documentation
#![allow(unused)]
use actix_ws::{Closed, Session};
use arifa::prelude::*;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use uuid::Uuid;

#[derive(Debug, Error)]
pub enum WsSendError {
    #[error("serialization error: {0}")]
    Serde(#[from] serde_json::Error),
    #[error("websocket session closed")]
    Closed(#[from] Closed),
}

pub struct ActixWsSession {
    pub ws: Session,
}

impl ActixWsSession {
    pub fn new(ws: Session) -> Self {
        Self { ws }
    }
}

#[async_trait]
impl WsSession for ActixWsSession {
    type Error = WsSendError;

    async fn send(&self, event: WsMessage) -> Result<(), Self::Error> {
        let text = serde_json::to_string(&event)?;
        let mut session = self.ws.clone();
        session.text(text).await?;
        Ok(())
    }
}

/// Query parameters used to establish a connection.
///
/// from HTTP query parameters and must be parsed before use.
#[derive(Debug, Deserialize)]
pub struct ConnectRequestQuery {
    pub user_id: Uuid,
}