use serde::{Deserialize, Serialize};
use crate::cdp::Cdp;
use crate::cdp::command::{CdpCommand, Empty};
use crate::common::protocol::string_enum;
use crate::error::WebDriverResult;
string_enum! {
pub enum TimeDomain {
TimeTicks = "timeTicks",
ThreadTicks = "threadTicks",
}
}
#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Enable {
#[serde(skip_serializing_if = "Option::is_none")]
pub time_domain: Option<TimeDomain>,
}
impl CdpCommand for Enable {
const METHOD: &'static str = "Performance.enable";
type Returns = Empty;
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct Disable;
impl CdpCommand for Disable {
const METHOD: &'static str = "Performance.disable";
type Returns = Empty;
}
#[derive(Debug, Clone, Deserialize)]
pub struct Metric {
pub name: String,
pub value: f64,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct GetMetrics;
#[derive(Debug, Clone, Deserialize)]
pub struct GetMetricsResult {
pub metrics: Vec<Metric>,
}
impl CdpCommand for GetMetrics {
const METHOD: &'static str = "Performance.getMetrics";
type Returns = GetMetricsResult;
}
#[derive(Debug)]
pub struct PerformanceDomain<'a> {
cdp: &'a Cdp,
}
impl<'a> PerformanceDomain<'a> {
pub(crate) fn new(cdp: &'a Cdp) -> Self {
Self {
cdp,
}
}
pub async fn enable(&self) -> WebDriverResult<()> {
self.cdp.send(Enable::default()).await?;
Ok(())
}
pub async fn disable(&self) -> WebDriverResult<()> {
self.cdp.send(Disable).await?;
Ok(())
}
pub async fn get_metrics(&self) -> WebDriverResult<Vec<Metric>> {
Ok(self.cdp.send(GetMetrics).await?.metrics)
}
}