use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ReadRouting {
#[default]
Any,
Primary,
Replica {
#[serde(default, skip_serializing_if = "Option::is_none")]
max_lag_ms: Option<u64>,
},
}
impl ReadRouting {
#[must_use]
pub const fn is_default(&self) -> bool {
matches!(self, Self::Any)
}
#[must_use]
pub const fn allows_replica(&self) -> bool {
!matches!(self, Self::Primary)
}
#[must_use]
pub const fn honours_write_pin(&self) -> bool {
!matches!(self, Self::Replica { .. })
}
#[must_use]
pub const fn allows_cached_result(&self) -> bool {
!matches!(self, Self::Primary)
}
#[must_use]
pub const fn effective_max_lag_ms(&self, server_max_lag_ms: Option<u64>) -> Option<u64> {
match self {
Self::Replica {
max_lag_ms: Some(ms),
} => Some(*ms),
_ => server_max_lag_ms,
}
}
}
#[cfg(test)]
mod tests;