use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use super::ProgressToken;
pub trait Request {
const METHOD: &'static str;
fn method(&self) -> &str;
fn params(&self) -> Option<&serde_json::Value>;
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RequestImpl {
pub method: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<serde_json::Value>,
}
impl Request for RequestImpl {
const METHOD: &'static str = "";
fn method(&self) -> &str {
&self.method
}
fn params(&self) -> Option<&serde_json::Value> {
self.params.as_ref()
}
}
impl RequestImpl {
pub fn new(method: impl Into<String>) -> Self {
Self {
method: method.into(),
params: None,
}
}
pub fn with_params(method: impl Into<String>, params: serde_json::Value) -> Self {
Self {
method: method.into(),
params: Some(params),
}
}
}
impl RequestImpl {
pub fn with_progress_token(method: impl Into<String>, progress_token: ProgressToken) -> Self {
let mut params = serde_json::Map::new();
params.insert("progressToken".to_string(), serde_json::to_value(progress_token).unwrap());
Self {
method: method.into(),
params: Some(serde_json::Value::Object(params)),
}
}
}
pub trait Notification {
const METHOD: &'static str;
fn method(&self) -> &str;
fn params(&self) -> Option<&serde_json::Value>;
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct NotificationImpl {
pub method: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<serde_json::Value>,
}
impl Notification for NotificationImpl {
const METHOD: &'static str = "";
fn method(&self) -> &str {
&self.method
}
fn params(&self) -> Option<&serde_json::Value> {
self.params.as_ref()
}
}
impl NotificationImpl {
pub fn new(method: impl Into<String>) -> Self {
Self {
method: method.into(),
params: None,
}
}
pub fn with_params(method: impl Into<String>, params: serde_json::Value) -> Self {
Self {
method: method.into(),
params: Some(params),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RequestParams {
#[serde(rename = "_meta", skip_serializing_if = "Option::is_none")]
pub meta: Option<RequestMeta>,
#[serde(flatten)]
pub additional: HashMap<String, serde_json::Value>,
}
impl RequestParams {
pub fn new() -> Self {
Self {
meta: None,
additional: HashMap::new(),
}
}
pub fn set_progress_token(&mut self, token: ProgressToken) {
if self.meta.is_none() {
self.meta = Some(RequestMeta {
progress_token: Some(token),
});
} else if let Some(meta) = &mut self.meta {
meta.progress_token = Some(token);
}
}
pub fn add<T: Serialize>(&mut self, name: impl Into<String>, value: &T) -> std::result::Result<(), serde_json::Error> {
self.additional.insert(name.into(), serde_json::to_value(value)?);
Ok(())
}
}
impl Default for RequestParams {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RequestMeta {
#[serde(rename = "progressToken", skip_serializing_if = "Option::is_none")]
pub progress_token: Option<ProgressToken>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct NotificationParams {
#[serde(rename = "_meta", skip_serializing_if = "Option::is_none")]
pub meta: Option<NotificationMeta>,
#[serde(flatten)]
pub additional: HashMap<String, serde_json::Value>,
}
impl NotificationParams {
pub fn new() -> Self {
Self {
meta: None,
additional: HashMap::new(),
}
}
pub fn add<T: Serialize>(&mut self, name: impl Into<String>, value: &T) -> std::result::Result<(), serde_json::Error> {
self.additional.insert(name.into(), serde_json::to_value(value)?);
Ok(())
}
}
impl Default for NotificationParams {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct NotificationMeta {
#[serde(flatten)]
pub additional: HashMap<String, serde_json::Value>,
}
impl NotificationMeta {
pub fn new() -> Self {
Self {
additional: HashMap::new(),
}
}
pub fn add<T: Serialize>(&mut self, name: impl Into<String>, value: &T) -> std::result::Result<(), serde_json::Error> {
self.additional.insert(name.into(), serde_json::to_value(value)?);
Ok(())
}
}
impl Default for NotificationMeta {
fn default() -> Self {
Self::new()
}
}
pub trait MessageResult {}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Result {
#[serde(rename = "_meta", skip_serializing_if = "Option::is_none")]
pub meta: Option<ResultMeta>,
#[serde(flatten)]
pub additional: HashMap<String, serde_json::Value>,
}
impl MessageResult for Result {}
impl Result {
pub fn new() -> Self {
Self {
meta: None,
additional: HashMap::new(),
}
}
pub fn add<T: Serialize>(&mut self, name: impl Into<String>, value: &T) -> std::result::Result<(), serde_json::Error> {
self.additional.insert(name.into(), serde_json::to_value(value)?);
Ok(())
}
}
impl Default for Result {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ResultMeta {
#[serde(flatten)]
pub additional: HashMap<String, serde_json::Value>,
}
impl ResultMeta {
pub fn new() -> Self {
Self {
additional: HashMap::new(),
}
}
pub fn add<T: Serialize>(&mut self, name: impl Into<String>, value: &T) -> std::result::Result<(), serde_json::Error> {
self.additional.insert(name.into(), serde_json::to_value(value)?);
Ok(())
}
}
impl Default for ResultMeta {
fn default() -> Self {
Self::new()
}
}
pub type EmptyResult = Result;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PaginatedRequest {
pub method: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<PaginatedRequestParams>,
}
impl PaginatedRequest {
pub fn new(method: impl Into<String>) -> Self {
Self {
method: method.into(),
params: None,
}
}
pub fn with_cursor(method: impl Into<String>, cursor: impl Into<String>) -> Self {
let params = PaginatedRequestParams {
cursor: Some(cursor.into()),
meta: None,
additional: HashMap::new(),
};
Self {
method: method.into(),
params: Some(params),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PaginatedRequestParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub cursor: Option<String>,
#[serde(rename = "_meta", skip_serializing_if = "Option::is_none")]
pub meta: Option<RequestMeta>,
#[serde(flatten)]
pub additional: HashMap<String, serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PaginatedResult {
#[serde(rename = "nextCursor", skip_serializing_if = "Option::is_none")]
pub next_cursor: Option<String>,
#[serde(rename = "_meta", skip_serializing_if = "Option::is_none")]
pub meta: Option<ResultMeta>,
#[serde(flatten)]
pub additional: HashMap<String, serde_json::Value>,
}
impl PaginatedResult {
pub fn new() -> Self {
Self {
next_cursor: None,
meta: None,
additional: HashMap::new(),
}
}
pub fn with_next_cursor(next_cursor: impl Into<String>) -> Self {
Self {
next_cursor: Some(next_cursor.into()),
meta: None,
additional: HashMap::new(),
}
}
pub fn add<T: Serialize>(&mut self, name: impl Into<String>, value: &T) -> std::result::Result<(), serde_json::Error> {
self.additional.insert(name.into(), serde_json::to_value(value)?);
Ok(())
}
}
impl Default for PaginatedResult {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CancelledNotification {
pub method: String,
pub params: CancelledParams,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CancelledParams {
#[serde(rename = "requestId")]
pub request_id: super::RequestId,
#[serde(skip_serializing_if = "Option::is_none")]
pub reason: Option<String>,
}
impl CancelledNotification {
pub fn new<I: Into<super::RequestId>>(request_id: I) -> Self {
Self {
method: "notifications/cancelled".to_string(),
params: CancelledParams {
request_id: request_id.into(),
reason: None,
},
}
}
pub fn with_reason<I: Into<super::RequestId>>(request_id: I, reason: impl Into<String>) -> Self {
Self {
method: "notifications/cancelled".to_string(),
params: CancelledParams {
request_id: request_id.into(),
reason: Some(reason.into()),
},
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ProgressNotification {
pub method: String,
pub params: ProgressParams,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ProgressParams {
#[serde(rename = "progressToken")]
pub progress_token: ProgressToken,
pub progress: f64,
#[serde(skip_serializing_if = "Option::is_none")]
pub total: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub message: Option<String>,
}
impl ProgressNotification {
pub fn new(progress_token: ProgressToken, progress: f64) -> Self {
Self {
method: "notifications/progress".to_string(),
params: ProgressParams {
progress_token,
progress,
total: None,
message: None,
},
}
}
pub fn with_total(progress_token: ProgressToken, progress: f64, total: f64) -> Self {
Self {
method: "notifications/progress".to_string(),
params: ProgressParams {
progress_token,
progress,
total: Some(total),
message: None,
},
}
}
pub fn with_message(progress_token: ProgressToken, progress: f64, message: impl Into<String>) -> Self {
Self {
method: "notifications/progress".to_string(),
params: ProgressParams {
progress_token,
progress,
total: None,
message: Some(message.into()),
},
}
}
pub fn with_total_and_message(
progress_token: ProgressToken,
progress: f64,
total: f64,
message: impl Into<String>,
) -> Self {
Self {
method: "notifications/progress".to_string(),
params: ProgressParams {
progress_token,
progress,
total: Some(total),
message: Some(message.into()),
},
}
}
}