use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Serialize)]
pub struct CdpRequest {
pub id: u64,
pub method: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<Value>,
#[serde(rename = "sessionId", skip_serializing_if = "Option::is_none")]
pub session_id: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct CdpResponse {
pub id: u64,
pub result: Option<Value>,
pub error: Option<CdpResponseError>,
#[serde(rename = "sessionId")]
pub session_id: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct CdpResponseError {
pub code: i64,
pub message: String,
pub data: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct CdpEvent {
pub method: String,
pub params: Option<Value>,
#[serde(rename = "sessionId")]
pub session_id: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
pub enum CdpMessage {
Response(CdpResponse),
Event(CdpEvent),
}
impl CdpMessage {
pub fn is_response_for(&self, id: u64) -> bool {
matches!(self, Self::Response(resp) if resp.id == id)
}
pub fn into_response(self) -> Option<CdpResponse> {
match self {
Self::Response(resp) => Some(resp),
Self::Event(_) => None,
}
}
pub fn into_event(self) -> Option<CdpEvent> {
match self {
Self::Event(evt) => Some(evt),
Self::Response(_) => None,
}
}
}
#[cfg(test)]
mod tests;