use std::time::Duration;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LoadMode {
#[default]
Normal,
Eager,
None,
}
impl LoadMode {
pub(crate) fn cdp_event(self) -> Option<&'static str> {
match self {
LoadMode::Normal => Some("Page.loadEventFired"),
LoadMode::Eager => Some("Page.domContentEventFired"),
LoadMode::None => None,
}
}
}
#[derive(Debug, Clone)]
pub struct GetOptions {
pub retry: u32,
pub interval: Duration,
pub timeout: Option<Duration>,
pub load_mode: Option<LoadMode>,
pub referer: Option<String>,
}
impl Default for GetOptions {
fn default() -> Self {
Self {
retry: 0,
interval: Duration::from_secs(1),
timeout: None,
load_mode: None,
referer: None,
}
}
}
impl GetOptions {
pub fn new() -> Self {
Self::default()
}
pub fn retry(mut self, n: u32) -> Self {
self.retry = n;
self
}
pub fn interval(mut self, secs: f64) -> Self {
self.interval = Duration::from_secs_f64(secs.max(0.0));
self
}
pub fn timeout(mut self, d: Duration) -> Self {
self.timeout = Some(d);
self
}
pub fn load_mode(mut self, m: LoadMode) -> Self {
self.load_mode = Some(m);
self
}
pub fn referer(mut self, r: impl Into<String>) -> Self {
self.referer = Some(r.into());
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ImageFormat {
#[default]
Png,
Jpeg,
}
impl ImageFormat {
pub(crate) fn cdp_format(self) -> &'static str {
match self {
ImageFormat::Png => "png",
ImageFormat::Jpeg => "jpeg",
}
}
}
#[derive(Debug, Clone, Default)]
pub struct ShotOpts {
pub full_page: bool,
pub region: Option<((f64, f64), (f64, f64))>,
pub format: ImageFormat,
pub quality: Option<u8>,
}
impl ShotOpts {
pub fn new() -> Self {
Self::default()
}
pub fn full_page(mut self, yes: bool) -> Self {
self.full_page = yes;
self
}
pub fn region(mut self, left_top: (f64, f64), right_bottom: (f64, f64)) -> Self {
self.region = Some((left_top, right_bottom));
self
}
pub fn format(mut self, format: ImageFormat) -> Self {
self.format = format;
self
}
pub fn quality(mut self, q: u8) -> Self {
self.quality = Some(q);
self
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PageRect {
pub window_width: f64,
pub window_height: f64,
pub page_width: f64,
pub page_height: f64,
pub scroll_x: f64,
pub scroll_y: f64,
pub device_pixel_ratio: f64,
}
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
#[serde(default)]
pub struct Cookie {
pub name: String,
pub value: String,
pub domain: String,
pub path: String,
pub expires: f64,
pub http_only: bool,
pub secure: bool,
}
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
#[serde(default)]
pub struct CookieParam {
pub name: String,
pub value: String,
pub url: Option<String>,
pub domain: Option<String>,
pub path: Option<String>,
pub secure: Option<bool>,
pub http_only: Option<bool>,
pub expires: Option<f64>,
}
#[derive(Debug, Clone, Default)]
pub struct DialogInfo {
pub message: String,
pub dialog_type: String,
pub default_prompt: String,
}
#[derive(Debug, Clone, Default)]
pub struct DownloadInfo {
pub url: String,
pub suggested_filename: String,
pub path: String,
pub state: String,
}
impl CookieParam {
pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
Self {
name: name.into(),
value: value.into(),
..Default::default()
}
}
pub fn url(mut self, url: impl Into<String>) -> Self {
self.url = Some(url.into());
self
}
pub fn domain(mut self, domain: impl Into<String>) -> Self {
self.domain = Some(domain.into());
self
}
}