use std::collections::HashMap;
#[derive(Debug, Clone, Default)]
pub struct ConnectionContext {
pub connection_id: u64,
pub reconnect_count: u64,
pub is_reconnection: bool,
pub attempt: u32,
pub metadata: HashMap<String, String>,
}
impl ConnectionContext {
#[must_use]
pub fn new(connection_id: u64) -> Self {
Self {
connection_id,
reconnect_count: 0,
is_reconnection: false,
attempt: 1,
metadata: HashMap::new(),
}
}
#[must_use]
pub const fn with_reconnect_count(mut self, count: u64) -> Self {
self.reconnect_count = count;
self.is_reconnection = count > 0;
self
}
#[must_use]
pub const fn with_attempt(mut self, attempt: u32) -> Self {
self.attempt = attempt;
self
}
#[must_use]
pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.metadata.insert(key.into(), value.into());
self
}
pub fn put_metadata(&mut self, key: impl Into<String>, value: impl Into<String>) {
self.metadata.insert(key.into(), value.into());
}
#[must_use]
pub fn get_metadata(&self, key: &str) -> Option<&String> {
self.metadata.get(key)
}
pub fn update(&mut self, connection_id: u64, reconnect_count: u64) {
self.connection_id = connection_id;
self.reconnect_count = reconnect_count;
self.is_reconnection = reconnect_count > 0;
}
}