mod hub;
mod session;
mod wss;
#[cfg(test)]
mod integration;
pub use wss::{ServerConfig, serve};
use crate::protocol::ProtocolError;
pub type AdminAllowlist = std::collections::HashMap<String, Option<String>>;
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
pub enum AclError {
#[error("not authorized: admin role required")]
NotAdmin,
#[error("not a member of channel `{0}`")]
NotMember(String),
#[error("channel `{0}` is private")]
ChannelPrivate(String),
#[error("channel `{0}` not found")]
ChannelNotFound(String),
}
impl From<AclError> for ProtocolError {
fn from(err: AclError) -> Self {
let message = err.to_string();
match err {
AclError::ChannelNotFound(_) => Self::NotFound(message),
_ => Self::Unauthorized(message),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn acl_errors_map_onto_wire_protocol_errors() {
assert!(matches!(ProtocolError::from(AclError::NotAdmin), ProtocolError::Unauthorized(_)));
assert!(matches!(ProtocolError::from(AclError::ChannelNotFound("ops".to_owned())), ProtocolError::NotFound(_)));
}
#[test]
fn acl_error_messages_are_descriptive() {
assert_eq!(AclError::NotMember("ops".to_owned()).to_string(), "not a member of channel `ops`");
}
}