cdp-use-rs 0.1.1

Type-safe Chrome DevTools Protocol client for Rust with auto-generated bindings from official CDP specs
Documentation
use std::fmt;

/// Errors that can occur when communicating with Chrome via CDP.
#[derive(Debug)]
pub enum CdpError {
    /// CDP protocol error returned by the browser (contains error code and message).
    Protocol {
        code: i64,
        message: String,
        data: Option<String>,
    },
    /// WebSocket connection was closed.
    ConnectionClosed,
    /// CDP command timed out waiting for a response.
    Timeout,
    /// JSON serialization/deserialization error.
    Serialization(serde_json::Error),
    /// WebSocket transport error.
    WebSocket(tokio_tungstenite::tungstenite::Error),
}

impl fmt::Display for CdpError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CdpError::Protocol {
                code,
                message,
                data,
            } => {
                write!(f, "CDP protocol error {}: {}", code, message)?;
                if let Some(d) = data {
                    write!(f, " ({})", d)?;
                }
                Ok(())
            }
            CdpError::ConnectionClosed => write!(f, "WebSocket connection closed"),
            CdpError::Timeout => write!(f, "CDP command timed out"),
            CdpError::Serialization(e) => write!(f, "Serialization error: {}", e),
            CdpError::WebSocket(e) => write!(f, "WebSocket error: {}", e),
        }
    }
}

impl std::error::Error for CdpError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            CdpError::Serialization(e) => Some(e),
            CdpError::WebSocket(e) => Some(e),
            _ => None,
        }
    }
}

impl From<serde_json::Error> for CdpError {
    fn from(e: serde_json::Error) -> Self {
        CdpError::Serialization(e)
    }
}

impl From<tokio_tungstenite::tungstenite::Error> for CdpError {
    fn from(e: tokio_tungstenite::tungstenite::Error) -> Self {
        CdpError::WebSocket(e)
    }
}