use serde::{Deserialize, Serialize};
use crate::cdp::Cdp;
use crate::cdp::command::{CdpCommand, CdpEvent, Empty};
use crate::common::protocol::string_enum;
use crate::error::WebDriverResult;
string_enum! {
pub enum LogLevel {
Verbose = "verbose",
Info = "info",
Warning = "warning",
Error = "error",
}
}
string_enum! {
pub enum LogSource {
Xml = "xml",
Javascript = "javascript",
Network = "network",
Storage = "storage",
AppCache = "appcache",
Rendering = "rendering",
Security = "security",
Deprecation = "deprecation",
Worker = "worker",
Violation = "violation",
Intervention = "intervention",
Recommendation = "recommendation",
Other = "other",
}
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct Enable;
impl CdpCommand for Enable {
const METHOD: &'static str = "Log.enable";
type Returns = Empty;
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct Disable;
impl CdpCommand for Disable {
const METHOD: &'static str = "Log.disable";
type Returns = Empty;
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct Clear;
impl CdpCommand for Clear {
const METHOD: &'static str = "Log.clear";
type Returns = Empty;
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LogEntry {
pub source: LogSource,
pub level: LogLevel,
pub text: String,
pub timestamp: f64,
pub url: Option<String>,
pub line_number: Option<u32>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct EntryAdded {
pub entry: LogEntry,
}
impl CdpEvent for EntryAdded {
const METHOD: &'static str = "Log.entryAdded";
const ENABLE: Option<&'static str> = Some("Log.enable");
}
#[derive(Debug)]
pub struct LogDomain<'a> {
cdp: &'a Cdp,
}
impl<'a> LogDomain<'a> {
pub(crate) fn new(cdp: &'a Cdp) -> Self {
Self {
cdp,
}
}
pub async fn enable(&self) -> WebDriverResult<()> {
self.cdp.send(Enable).await?;
Ok(())
}
pub async fn disable(&self) -> WebDriverResult<()> {
self.cdp.send(Disable).await?;
Ok(())
}
pub async fn clear(&self) -> WebDriverResult<()> {
self.cdp.send(Clear).await?;
Ok(())
}
}