use serde::{Deserialize, Serialize};
use crate::error::CommandError;
use crate::session::WebDriverBiDiSession;
pub async fn send_command<C, R>(
session: &mut WebDriverBiDiSession,
command: C,
) -> Result<R, CommandError>
where
C: Serialize,
R: for<'de> Deserialize<'de>,
{
match session.send_command::<C, R>(command).await {
Ok(rslt) => {
Ok(rslt)
}
Err(e) => {
Err(e)
}
}
}
#[macro_export]
macro_rules! define_command {
($cmd_name:ident, $cmd_type:ty, $params_type:ty, $fn_name:ident, $result_type:ty) => {
#[derive(Debug, Serialize, Deserialize)]
struct $cmd_name {
id: u64,
#[serde(flatten)]
params: $cmd_type,
}
impl $cmd_name {
fn new(params: $params_type) -> Self {
let id = id::get_next_id();
let params = <$cmd_type>::new(params);
Self { id, params }
}
}
pub async fn $fn_name(
session: &mut WebDriverBiDiSession,
params: $params_type,
) -> Result<$result_type, CommandError> {
let cmd = $cmd_name::new(params);
utils::send_command(session, cmd).await
}
};
}