use std::time::Duration;
use crate::error::{S9Result, S9WebSocketError};
#[derive(Debug, Clone, Default)]
pub(crate) struct SharedOptions {
pub(crate) spin_wait_duration: Option<Duration>,
pub(crate) nodelay: Option<bool>,
pub(crate) ttl: Option<u32>,
}
#[derive(Debug, Clone, Default)]
pub struct NonBlockingOptions {
pub(crate) shared: SharedOptions,
}
impl NonBlockingOptions {
pub fn new() -> Self {
Self::default()
}
pub fn spin_wait_duration(mut self, duration: Option<Duration>) -> S9Result<Self> {
if let Some(duration) = duration {
if duration.is_zero() {
return Err(S9WebSocketError::InvalidConfiguration("Spin wait duration cannot be zero".to_string()).into());
}
}
self.shared.spin_wait_duration = duration;
Ok(self)
}
pub fn nodelay(mut self, nodelay: bool) -> Self {
self.shared.nodelay = Some(nodelay);
self
}
pub fn ttl(mut self, ttl: Option<u32>) -> S9Result<Self> {
self.shared.ttl = ttl;
Ok(self)
}
}
#[derive(Debug, Clone, Default)]
pub struct BlockingOptions {
pub(crate) shared: SharedOptions,
pub(crate) read_timeout: Option<Duration>,
pub(crate) write_timeout: Option<Duration>,
}
impl BlockingOptions {
pub fn new() -> Self {
Self::default()
}
pub fn spin_wait_duration(mut self, duration: Option<Duration>) -> S9Result<Self> {
if let Some(duration) = duration {
if duration.is_zero() {
return Err(S9WebSocketError::InvalidConfiguration("Spin wait duration cannot be zero".to_string()).into());
}
}
self.shared.spin_wait_duration = duration;
Ok(self)
}
pub fn nodelay(mut self, nodelay: bool) -> Self {
self.shared.nodelay = Some(nodelay);
self
}
pub fn ttl(mut self, ttl: Option<u32>) -> S9Result<Self> {
self.shared.ttl = ttl;
Ok(self)
}
pub fn read_timeout(mut self, timeout: Option<Duration>) -> S9Result<Self> {
if let Some(timeout) = timeout {
if timeout.is_zero() {
return Err(S9WebSocketError::InvalidConfiguration("Read timeout duration cannot be zero".to_string()).into());
}
}
self.read_timeout = timeout;
Ok(self)
}
pub fn write_timeout(mut self, timeout: Option<Duration>) -> S9Result<Self> {
if let Some(timeout) = timeout {
if timeout.is_zero() {
return Err(S9WebSocketError::InvalidConfiguration("Write timeout duration cannot be zero".to_string()).into());
}
}
self.write_timeout = timeout;
Ok(self)
}
}