mod topology_description;
use std::time::Duration;
use serde::{Deserialize, Serialize};
use crate::{
bson::{oid::ObjectId, Document},
error::Error,
options::ServerAddress,
serde_util,
};
pub use crate::sdam::public::TopologyType;
pub use topology_description::TopologyDescription;
pub type ServerDescription = crate::sdam::public::ServerInfo<'static>;
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ServerDescriptionChangedEvent {
pub address: ServerAddress,
pub topology_id: ObjectId,
pub previous_description: ServerDescription,
pub new_description: ServerDescription,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ServerOpeningEvent {
pub address: ServerAddress,
#[serde(skip, default = "ObjectId::new")]
pub topology_id: ObjectId,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ServerClosedEvent {
pub address: ServerAddress,
#[serde(skip, default = "ObjectId::new")]
pub topology_id: ObjectId,
}
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TopologyDescriptionChangedEvent {
pub topology_id: ObjectId,
pub previous_description: TopologyDescription,
pub new_description: TopologyDescription,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TopologyOpeningEvent {
#[serde(skip, default = "ObjectId::new")]
pub topology_id: ObjectId,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TopologyClosedEvent {
#[serde(skip, default = "ObjectId::new")]
pub topology_id: ObjectId,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ServerHeartbeatStartedEvent {
pub server_address: ServerAddress,
pub awaited: bool,
pub driver_connection_id: u32,
pub server_connection_id: Option<i64>,
}
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ServerHeartbeatSucceededEvent {
pub duration: Duration,
pub reply: Document,
pub server_address: ServerAddress,
pub awaited: bool,
pub driver_connection_id: u32,
pub server_connection_id: Option<i64>,
}
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ServerHeartbeatFailedEvent {
pub duration: Duration,
#[serde(serialize_with = "serde_util::serialize_error_as_string")]
pub failure: Error,
pub server_address: ServerAddress,
pub awaited: bool,
pub driver_connection_id: u32,
pub server_connection_id: Option<i64>,
}
#[derive(Clone, Debug, Serialize)]
#[allow(missing_docs)]
#[non_exhaustive]
#[serde(untagged)]
pub enum SdamEvent {
ServerDescriptionChanged(Box<ServerDescriptionChangedEvent>),
ServerOpening(ServerOpeningEvent),
ServerClosed(ServerClosedEvent),
TopologyDescriptionChanged(Box<TopologyDescriptionChangedEvent>),
TopologyOpening(TopologyOpeningEvent),
TopologyClosed(TopologyClosedEvent),
ServerHeartbeatStarted(ServerHeartbeatStartedEvent),
ServerHeartbeatSucceeded(ServerHeartbeatSucceededEvent),
ServerHeartbeatFailed(ServerHeartbeatFailedEvent),
}
#[deprecated = "use the EventHandler API"]
pub trait SdamEventHandler: Send + Sync {
fn handle_server_description_changed_event(&self, _event: ServerDescriptionChangedEvent) {}
fn handle_server_opening_event(&self, _event: ServerOpeningEvent) {}
fn handle_server_closed_event(&self, _event: ServerClosedEvent) {}
fn handle_topology_description_changed_event(&self, _event: TopologyDescriptionChangedEvent) {}
fn handle_topology_opening_event(&self, _event: TopologyOpeningEvent) {}
fn handle_topology_closed_event(&self, _event: TopologyClosedEvent) {}
fn handle_server_heartbeat_started_event(&self, _event: ServerHeartbeatStartedEvent) {}
fn handle_server_heartbeat_succeeded_event(&self, _event: ServerHeartbeatSucceededEvent) {}
fn handle_server_heartbeat_failed_event(&self, _event: ServerHeartbeatFailedEvent) {}
}