browser-protocol
A high-performance, zero-allocation, fully compile-safe Rust representation of the Chrome DevTools Protocol (CDP), generated directly from the official protocol definitions.
🚀 Key Design Goals & Features
Most auto-generated CDP crates output raw, unidiomatic APIs with substantial runtime allocation overhead. This library is designed from the ground up to solve these issues:
1. Idiomatic Rust Naming Conventions
- All generated struct fields, getters, and builder setter methods are translated from the protocol's raw
camelCaseto standard Rustsnake_case(e.g.,transitionTypebecomestransition_type, andbackendDOMNodeIdbecomesbackend_dom_node_id). - Standard
#[serde(rename = "...")]attributes ensure the serialized JSON wire protocol matches the exact formats required by Chrome.
2. Zero-Copy String Management
- Utilizes
Cow<'a, str>instead of allocating heap memory (String) for string properties. - String arguments in builders use
impl Into<Cow<'a, str>>, allowing you to pass static string literals (&str) or owned strings without unnecessary heap allocations.
3. Compile-Time Argument Safety
- The builder pattern differentiates between required and optional parameters. Required parameters are passed directly as arguments to the
builder(...)function, guaranteeing protocol compliance at compile time:// `url` is required (passed to builder), `transition_type` is optional (chained) let nav = builder .transition_type .build;
4. Zero Dependencies & No Async Runtime Lock-in
- Contains zero bloated dependencies (depends only on
serdeandserde_json). - Does not include a WebSocket client or force a specific async runtime (like
tokio). This keeps the package extremely lightweight, compile-fast, and compatible with any async runtime or network stack.
📦 Installation
Add this to your Cargo.toml:
[]
= { = "0.1.3", = ["full"] }
= { = "1.0", = ["derive"] }
= "1.0"
🛠 Usage Examples
1. Constructing a Request with Optional Parameters
use ;
2. Handling Command Request/Response Types
Every parameter struct implements crate::CdpCommand<'a> which binds it to its command method and its corresponding response (Returns) type:
use ;
use NodeId;
use CdpCommand;
🏗 Code Generation Mechanics
The code is dynamically compiled using a custom Python script that performs advanced schema analysis:
- Fixed-Point Lifetime Propagation Pass: The generator performs iterative analysis over the CDP types to detect circular type references, nesting, and dependency hierarchies. It automatically determines which types must have a lifetime parameter (
<'a>) and wraps recursive structures insideBoxto prevent infinite-size compilation errors. - HTML/Markdown Escaping: Schema documentation from the Chrome DevTools Protocol contains raw markdown and HTML brackets. The generator cleans and escapes these brackets into valid Rustdoc format, keeping compilation entirely warning-free.
- Domain-Specific Feature Flags: Every CDP domain is represented by a Rust feature flag. You can optimize compile times by only compiling the domains your project needs:
# Compile only the page and dom domains = { = "0.1.3", = false, = ["page", "dom"] }
Regenerating the Code
To regenerate the Rust modules from a local protocol file:
⚖ License
Distributed under the MIT License. See LICENSE for more information.