1use std::borrow::Cow;
2use std::fmt;
3use std::fmt::Debug;
4use std::ops::Deref;
5
6use serde::de::DeserializeOwned;
7use serde::{Deserialize, Serialize};
8
9pub type MethodId = Cow<'static, str>;
10
11#[derive(Serialize, Debug, PartialEq, Eq)]
13pub struct MethodCall {
14 pub id: CallId,
18 pub method: MethodId,
20 #[serde(rename = "sessionId", skip_serializing_if = "Option::is_none")]
22 pub session_id: Option<String>,
23 pub params: serde_json::Value,
25}
26
27#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
31pub struct CallId(usize);
32
33impl fmt::Display for CallId {
34 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35 write!(f, "CallId({})", self.0)
36 }
37}
38
39impl CallId {
40 pub fn new(id: usize) -> Self {
42 CallId(id)
43 }
44}
45
46pub trait Command: serde::ser::Serialize + Method {
48 type Response: serde::de::DeserializeOwned + fmt::Debug;
50
51 fn response_from_value(response: serde_json::Value) -> serde_json::Result<Self::Response> {
53 serde_json::from_value(response)
54 }
55}
56
57pub struct CommandResponse<T>
60where
61 T: fmt::Debug,
62{
63 pub id: CallId,
64 pub result: T,
65 pub method: MethodId,
66}
67
68pub type CommandResult<T> = Result<CommandResponse<T>, Error>;
73
74impl<T: fmt::Debug> Deref for CommandResponse<T> {
75 type Target = T;
76
77 fn deref(&self) -> &Self::Target {
78 &self.result
79 }
80}
81
82#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
85pub struct CdpJsonEventMessage {
86 pub method: MethodId,
88 pub session_id: Option<String>,
90 pub params: serde_json::Value,
92}
93
94impl Method for CdpJsonEventMessage {
95 fn identifier(&self) -> MethodId {
96 self.method.clone()
97 }
98}
99
100impl EventMessage for CdpJsonEventMessage {
101 fn session_id(&self) -> Option<&str> {
102 self.params.get("sessionId").and_then(|x| x.as_str())
103 }
104}
105
106pub trait EventMessage: Method + DeserializeOwned + Debug {
108 fn session_id(&self) -> Option<&str>;
110}
111
112pub trait Method {
115 fn identifier(&self) -> MethodId;
117
118 fn domain_name(&self) -> MethodId {
120 self.split().0
121 }
122
123 fn method_name(&self) -> MethodId {
125 self.split().1
126 }
127
128 fn split(&self) -> (MethodId, MethodId) {
130 match self.identifier() {
131 Cow::Borrowed(id) => {
132 let mut iter = id.split('.');
133 (iter.next().unwrap().into(), iter.next().unwrap().into())
134 }
135 Cow::Owned(id) => {
136 let mut iter = id.split('.');
137 (
138 Cow::Owned(iter.next().unwrap().into()),
139 Cow::Owned(iter.next().unwrap().into()),
140 )
141 }
142 }
143 }
144}
145
146pub trait MethodType {
148 fn method_id() -> MethodId
150 where
151 Self: Sized;
152}
153
154#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
156pub struct Request {
157 pub method: MethodId,
159 #[serde(rename = "sessionId", skip_serializing_if = "Option::is_none")]
161 pub session_id: Option<String>,
162 pub params: serde_json::Value,
164}
165
166impl Request {
167 pub fn new(method: MethodId, params: serde_json::Value) -> Self {
168 Self {
169 method,
170 params,
171 session_id: None,
172 }
173 }
174
175 pub fn with_session(
176 method: MethodId,
177 params: serde_json::Value,
178 session_id: impl Into<String>,
179 ) -> Self {
180 Self {
181 method,
182 params,
183 session_id: Some(session_id.into()),
184 }
185 }
186}
187
188#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
190pub struct Response {
191 pub id: CallId,
193 pub result: Option<serde_json::Value>,
195 pub error: Option<Error>,
197}
198
199#[derive(Deserialize, Debug, Clone)]
203#[serde(untagged)]
204#[allow(clippy::large_enum_variant)]
205pub enum Message<T = CdpJsonEventMessage> {
206 Response(Response),
208 Event(T),
210}
211
212#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
216pub struct ResponseError {
217 pub id: CallId,
218 pub code: usize,
220 pub message: String,
222}
223
224#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
227pub struct Error {
228 pub code: i64,
230 pub message: String,
232}
233
234impl fmt::Display for Error {
235 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
236 write!(f, "Error {}: {}", self.code, self.message)
237 }
238}
239
240impl std::error::Error for Error {}
241
242#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
244pub struct Binary(String);
245
246impl AsRef<str> for Binary {
247 fn as_ref(&self) -> &str {
248 self.0.as_str()
249 }
250}
251
252impl AsRef<[u8]> for Binary {
253 fn as_ref(&self) -> &[u8] {
254 self.0.as_bytes()
255 }
256}
257
258impl From<Binary> for String {
259 fn from(b: Binary) -> String {
260 b.0
261 }
262}
263
264impl From<String> for Binary {
265 fn from(expr: String) -> Self {
266 Self(expr)
267 }
268}