use std::{fmt, time::Duration};
use super::{hooks::Hooks, Client, ClientConfig, Connector, Hook, Timeouts};
#[must_use = "builder does nothing itself, use `.build()` to build it"]
pub struct ClientBuilder<M>
where
M: Connector,
{
pub(crate) connector: M,
pub(crate) config: ClientConfig,
pub(crate) hooks: Hooks<M>,
}
impl<M> fmt::Debug for ClientBuilder<M>
where
M: fmt::Debug + Connector,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ClientBuilder")
.field("Connector", &self.connector)
.field("config", &self.config)
.field("hooks", &self.hooks)
.finish()
}
}
impl<M> ClientBuilder<M>
where
M: Connector,
{
pub(crate) fn new(connector: M) -> Self {
Self {
connector,
config: ClientConfig::default(),
hooks: Hooks::default(),
}
}
pub fn build(self) -> Client<M> {
Client::from_builder(self)
}
pub fn config(mut self, value: ClientConfig) -> Self {
self.config = value;
self
}
pub fn max_size(mut self, value: usize) -> Self {
self.config.max_size = value;
self
}
pub fn timeouts(mut self, value: Timeouts) -> Self {
self.config.timeouts = value;
self
}
pub fn wait_timeout(mut self, value: Option<Duration>) -> Self {
self.config.timeouts.wait = value;
self
}
pub fn connect_timeout(mut self, value: Option<Duration>) -> Self {
self.config.timeouts.connect = value;
self
}
pub fn reuse_timeout(mut self, value: Option<Duration>) -> Self {
self.config.timeouts.reuse = value;
self
}
pub fn post_create(mut self, hook: impl Into<Hook<M>>) -> Self {
self.hooks.post_connect.push(hook.into());
self
}
pub fn pre_reuse(mut self, hook: impl Into<Hook<M>>) -> Self {
self.hooks.pre_reuse.push(hook.into());
self
}
pub fn post_reuse(mut self, hook: impl Into<Hook<M>>) -> Self {
self.hooks.post_reuse.push(hook.into());
self
}
}