use std::future::Future;
use std::pin::Pin;
#[derive(Debug, Clone)]
pub struct BrowserBackendError {
message: String,
}
impl BrowserBackendError {
#[must_use]
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
}
}
}
impl std::fmt::Display for BrowserBackendError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "browser backend error: {}", self.message)
}
}
impl std::error::Error for BrowserBackendError {}
pub trait BrowserBackend: Send + Sync {
fn cdp_endpoint(
&self,
) -> Pin<Box<dyn Future<Output = Result<String, BrowserBackendError>> + Send + '_>>;
}
#[derive(Debug, Clone)]
pub struct StaticCdpBrowser {
endpoint: String,
}
impl StaticCdpBrowser {
#[must_use]
pub fn new(endpoint: impl Into<String>) -> Self {
Self {
endpoint: endpoint.into(),
}
}
}
impl BrowserBackend for StaticCdpBrowser {
fn cdp_endpoint(
&self,
) -> Pin<Box<dyn Future<Output = Result<String, BrowserBackendError>> + Send + '_>> {
Box::pin(async move { Ok(self.endpoint.clone()) })
}
}