use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AhpRequest {
pub jsonrpc: String,
pub id: String,
pub method: String,
pub params: serde_json::Value,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AhpResponse {
pub jsonrpc: String,
pub id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<AhpErrorObject>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AhpNotification {
pub jsonrpc: String,
pub method: String,
pub params: serde_json::Value,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AhpErrorObject {
pub code: i32,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<serde_json::Value>,
}
impl AhpRequest {
pub fn new(method: impl Into<String>, params: serde_json::Value) -> Self {
Self {
jsonrpc: "2.0".to_string(),
id: uuid::Uuid::new_v4().to_string(),
method: method.into(),
params,
}
}
}
impl AhpNotification {
pub fn new(method: impl Into<String>, params: serde_json::Value) -> Self {
Self {
jsonrpc: "2.0".to_string(),
method: method.into(),
params,
}
}
}
impl AhpResponse {
pub fn success(id: impl Into<String>, result: serde_json::Value) -> Self {
Self {
jsonrpc: "2.0".to_string(),
id: id.into(),
result: Some(result),
error: None,
}
}
pub fn error(id: impl Into<String>, code: i32, message: impl Into<String>) -> Self {
Self {
jsonrpc: "2.0".to_string(),
id: id.into(),
result: None,
error: Some(AhpErrorObject {
code,
message: message.into(),
data: None,
}),
}
}
}