#![cfg_attr(feature = "doc_cfg", feature(doc_cfg))]
#![deny(
nonstandard_style,
rust_2018_idioms,
rustdoc::broken_intra_doc_links,
rustdoc::private_intra_doc_links
)]
#![forbid(unsafe_code)]
#![warn(
non_ascii_idents,
deprecated_in_future,
missing_copy_implementations,
missing_debug_implementations,
missing_docs,
unreachable_pub,
unused_import_braces,
unused_labels,
unused_lifetimes,
unused_qualifications,
unused_results,
clippy::pedantic,
clippy::cargo,
unsafe_op_in_unsafe_fn
)]
#![allow(clippy::cargo_common_metadata, clippy::no_effect_underscore_binding)]
use deadpool::async_trait;
#[cfg(any(feature = "native-tls", feature = "doc_cfg"))]
#[cfg_attr(feature = "doc_cfg", doc(cfg(feature = "native-tls")))]
pub mod native_tls;
#[cfg(any(feature = "rustls-tls", feature = "doc_cfg"))]
#[cfg_attr(feature = "doc_cfg", doc(cfg(feature = "rustls-tls")))]
pub mod rustls_tls;
pub mod reexports;
pub type Pool<C> = deadpool::managed::Pool<Manager<C>, deadpool::managed::Object<Manager<C>>>;
pub type PoolBuilder<C> =
deadpool::managed::PoolBuilder<Manager<C>, deadpool::managed::Object<Manager<C>>>;
pub type BuildError = deadpool::managed::BuildError<fantoccini::error::NewSessionError>;
pub type CreatePoolError = deadpool::managed::CreatePoolError<
std::convert::Infallible,
fantoccini::error::NewSessionError,
>;
pub type PoolError = deadpool::managed::PoolError<fantoccini::error::NewSessionError>;
pub type Object<C> = deadpool::managed::Object<Manager<C>>;
pub type Hook<C> = deadpool::managed::Hook<Manager<C>>;
pub type HookError<C> = deadpool::managed::HookError<Manager<C>>;
pub type HookErrorCause<C> = deadpool::managed::HookErrorCause<Manager<C>>;
#[derive(Debug, Clone)]
pub struct Manager<C>
where
C: 'static + hyper::client::connect::Connect + Send + Sync + Clone + Unpin,
{
client_builder: fantoccini::ClientBuilder<C>,
webdriver_url: String,
}
impl<C> Manager<C>
where
C: 'static + hyper::client::connect::Connect + Send + Sync + Clone + Unpin,
{
pub fn new(
webdriver_url: impl Into<String>,
client_builder: fantoccini::ClientBuilder<C>,
) -> Self {
Self {
client_builder,
webdriver_url: webdriver_url.into(),
}
}
pub fn webdriver_url(&self) -> &str {
&self.webdriver_url
}
pub fn session_builder(&self) -> &fantoccini::ClientBuilder<C> {
&self.client_builder
}
}
#[async_trait]
impl<C> deadpool::managed::Manager for Manager<C>
where
C: 'static + hyper::client::connect::Connect + Send + Sync + Clone + Unpin,
{
type Type = fantoccini::Client;
type Error = fantoccini::error::NewSessionError;
async fn create(&self) -> Result<Self::Type, Self::Error> {
self.client_builder
.connect(self.webdriver_url.as_str())
.await
}
async fn recycle(
&self,
_obj: &mut Self::Type,
) -> deadpool::managed::RecycleResult<Self::Error> {
Ok(())
}
}
#[async_trait]
pub trait PoolShutdown {
type Error;
async fn shutdown(self) -> Result<(), Self::Error>;
}
#[async_trait]
impl<C> PoolShutdown for Pool<C>
where
C: 'static + hyper::client::connect::Connect + Send + Sync + Clone + Unpin,
{
type Error = fantoccini::error::CmdError;
async fn shutdown(self) -> Result<(), Self::Error> {
while self.status().size > 0 {
match self.get().await {
Ok(o) => {
self.resize(self.status().max_size - 1);
deadpool::managed::Object::take(o).close().await?;
}
Err(e) => panic!("{e:#?}"),
}
}
self.close();
Ok(())
}
}