use super::*;
#[derive(Clone)]
pub struct ConnectParamsOwned {
pub host: String,
pub tcp: i32,
pub udp: i32,
pub encrypted: bool,
}
impl ConnectParamsOwned {
pub fn new(host: impl Into<String>, tcp: i32, udp: i32, encrypted: bool) -> Self {
Self {
host: host.into(),
tcp,
udp,
encrypted,
}
}
pub fn as_params(&self) -> ConnectParams<'_> {
ConnectParams {
host: &self.host,
tcp: self.tcp,
udp: self.udp,
encrypted: self.encrypted,
}
}
}
impl From<ConnectParams<'_>> for ConnectParamsOwned {
fn from(params: ConnectParams<'_>) -> Self {
Self::new(params.host, params.tcp, params.udp, params.encrypted)
}
}
#[derive(Clone)]
pub struct ReconnectSettings {
pub params: ConnectParamsOwned,
pub config: ReconnectConfig,
pub extra_events: Vec<Event>,
}
impl ReconnectSettings {
pub fn new(params: ConnectParamsOwned, config: ReconnectConfig) -> Self {
Self {
params,
config,
extra_events: Vec::new(),
}
}
pub fn with_extra_events(mut self, extra_events: Vec<Event>) -> Self {
self.extra_events = extra_events;
self
}
}
#[derive(Clone)]
pub struct ClientConfig {
pub poll_timeout_ms: i32,
pub reconnect: Option<ReconnectSettings>,
}
impl Default for ClientConfig {
fn default() -> Self {
Self {
poll_timeout_ms: 100,
reconnect: None,
}
}
}
impl ClientConfig {
pub fn new() -> Self {
Self::default()
}
pub fn poll_timeout_ms(mut self, timeout_ms: i32) -> Self {
self.poll_timeout_ms = timeout_ms;
self
}
pub fn reconnect(mut self, params: ConnectParamsOwned, config: ReconnectConfig) -> Self {
self.reconnect = Some(ReconnectSettings::new(params, config));
self
}
pub fn reconnect_with_events(
mut self,
params: ConnectParamsOwned,
config: ReconnectConfig,
extra_events: Vec<Event>,
) -> Self {
self.reconnect =
Some(ReconnectSettings::new(params, config).with_extra_events(extra_events));
self
}
pub fn without_reconnect(mut self) -> Self {
self.reconnect = None;
self
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum DispatchFlow {
Continue,
Stop,
}
#[derive(Clone, Copy)]
pub struct EventContext<'a> {
pub(super) event: Event,
pub(super) message: &'a Message,
pub(super) client: Option<&'a Client>,
}
impl<'a> EventContext<'a> {
pub fn event(&self) -> Event {
self.event
}
pub fn message(&self) -> &Message {
self.message
}
pub fn client(&self) -> Option<&Client> {
self.client
}
}