use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use super::{Annotations, Request, Notification};
use super::messages::MessageResult;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Resource {
pub uri: String,
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(rename = "mimeType", skip_serializing_if = "Option::is_none")]
pub mime_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub annotations: Option<Annotations>,
#[serde(skip_serializing_if = "Option::is_none")]
pub size: Option<i64>,
}
impl Resource {
pub fn new(uri: impl Into<String>, name: impl Into<String>) -> Self {
Self {
uri: uri.into(),
name: name.into(),
description: None,
mime_type: None,
annotations: None,
size: None,
}
}
pub fn with_description(uri: impl Into<String>, name: impl Into<String>, description: impl Into<String>) -> Self {
Self {
uri: uri.into(),
name: name.into(),
description: Some(description.into()),
mime_type: None,
annotations: None,
size: None,
}
}
pub fn with_mime_type(mut self, mime_type: impl Into<String>) -> Self {
self.mime_type = Some(mime_type.into());
self
}
pub fn with_annotations(mut self, annotations: Annotations) -> Self {
self.annotations = Some(annotations);
self
}
pub fn with_size(mut self, size: i64) -> Self {
self.size = Some(size);
self
}
}
impl Default for Resource {
fn default() -> Self {
Self {
uri: String::new(),
name: String::new(),
description: None,
mime_type: None,
annotations: None,
size: None,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ResourceTemplate {
#[serde(rename = "uriTemplate")]
pub uri_template: String,
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(rename = "mimeType", skip_serializing_if = "Option::is_none")]
pub mime_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub annotations: Option<Annotations>,
}
impl ResourceTemplate {
pub fn new(uri_template: impl Into<String>, name: impl Into<String>) -> Self {
Self {
uri_template: uri_template.into(),
name: name.into(),
description: None,
mime_type: None,
annotations: None,
}
}
pub fn with_description(
uri_template: impl Into<String>,
name: impl Into<String>,
description: impl Into<String>,
) -> Self {
Self {
uri_template: uri_template.into(),
name: name.into(),
description: Some(description.into()),
mime_type: None,
annotations: None,
}
}
pub fn with_mime_type(mut self, mime_type: impl Into<String>) -> Self {
self.mime_type = Some(mime_type.into());
self
}
pub fn with_annotations(mut self, annotations: Annotations) -> Self {
self.annotations = Some(annotations);
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ResourceContents {
pub uri: String,
#[serde(rename = "mimeType", skip_serializing_if = "Option::is_none")]
pub mime_type: Option<String>,
}
impl ResourceContents {
pub fn new(uri: impl Into<String>) -> Self {
Self {
uri: uri.into(),
mime_type: None,
}
}
pub fn with_mime_type(uri: impl Into<String>, mime_type: impl Into<String>) -> Self {
Self {
uri: uri.into(),
mime_type: Some(mime_type.into()),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TextResourceContents {
pub uri: String,
#[serde(rename = "mimeType", skip_serializing_if = "Option::is_none")]
pub mime_type: Option<String>,
pub text: String,
}
impl TextResourceContents {
pub fn new(uri: impl Into<String>, text: impl Into<String>) -> Self {
Self {
uri: uri.into(),
mime_type: None,
text: text.into(),
}
}
pub fn with_mime_type(uri: impl Into<String>, text: impl Into<String>, mime_type: impl Into<String>) -> Self {
Self {
uri: uri.into(),
mime_type: Some(mime_type.into()),
text: text.into(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct BlobResourceContents {
pub uri: String,
#[serde(rename = "mimeType", skip_serializing_if = "Option::is_none")]
pub mime_type: Option<String>,
pub blob: String,
}
impl BlobResourceContents {
pub fn new(uri: impl Into<String>, blob: impl Into<String>) -> Self {
Self {
uri: uri.into(),
mime_type: None,
blob: blob.into(),
}
}
pub fn with_mime_type(uri: impl Into<String>, blob: impl Into<String>, mime_type: impl Into<String>) -> Self {
Self {
uri: uri.into(),
mime_type: Some(mime_type.into()),
blob: blob.into(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ListResourcesRequest {
pub method: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<ListResourcesParams>,
}
impl Request for ListResourcesRequest {
const METHOD: &'static str = "resources/list";
fn method(&self) -> &str {
&self.method
}
fn params(&self) -> Option<&serde_json::Value> {
None
}
}
impl ListResourcesRequest {
pub fn new() -> Self {
Self {
method: Self::METHOD.to_string(),
params: None,
}
}
pub fn with_cursor(cursor: impl Into<String>) -> Self {
Self {
method: Self::METHOD.to_string(),
params: Some(ListResourcesParams {
cursor: Some(cursor.into()),
}),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ListResourcesParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub cursor: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ListResourcesResult {
pub resources: Vec<Resource>,
#[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<super::messages::ResultMeta>,
}
impl MessageResult for ListResourcesResult {}
impl ListResourcesResult {
pub fn new(resources: Vec<Resource>) -> Self {
Self {
resources,
next_cursor: None,
meta: None,
}
}
pub fn with_next_cursor(resources: Vec<Resource>, next_cursor: impl Into<String>) -> Self {
Self {
resources,
next_cursor: Some(next_cursor.into()),
meta: None,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ReadResourceRequest {
pub method: String,
pub params: ReadResourceParams,
}
impl Request for ReadResourceRequest {
const METHOD: &'static str = "resources/read";
fn method(&self) -> &str {
&self.method
}
fn params(&self) -> Option<&serde_json::Value> {
None
}
}
impl ReadResourceRequest {
pub fn new(uri: impl Into<String>) -> Self {
Self {
method: Self::METHOD.to_string(),
params: ReadResourceParams {
uri: uri.into(),
},
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ReadResourceParams {
pub uri: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ReadResourceResult {
pub contents: Vec<ResourceContent>,
#[serde(rename = "_meta", skip_serializing_if = "Option::is_none")]
pub meta: Option<super::messages::ResultMeta>,
}
impl ReadResourceResult {
pub fn new(contents: Vec<ResourceContent>) -> Self {
Self {
contents,
meta: None,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum ResourceContent {
Text(TextResourceContents),
Blob(BlobResourceContents),
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SubscribeRequest {
pub method: String,
pub params: SubscribeParams,
}
impl Request for SubscribeRequest {
const METHOD: &'static str = "resources/subscribe";
fn method(&self) -> &str {
&self.method
}
fn params(&self) -> Option<&serde_json::Value> {
None
}
}
impl SubscribeRequest {
pub fn new(uri: impl Into<String>) -> Self {
Self {
method: Self::METHOD.to_string(),
params: SubscribeParams {
uri: uri.into(),
},
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SubscribeParams {
pub uri: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct UnsubscribeRequest {
pub method: String,
pub params: UnsubscribeParams,
}
impl Request for UnsubscribeRequest {
const METHOD: &'static str = "resources/unsubscribe";
fn method(&self) -> &str {
&self.method
}
fn params(&self) -> Option<&serde_json::Value> {
None
}
}
impl UnsubscribeRequest {
pub fn new(uri: impl Into<String>) -> Self {
Self {
method: Self::METHOD.to_string(),
params: UnsubscribeParams {
uri: uri.into(),
},
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct UnsubscribeParams {
pub uri: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ResourceUpdatedNotification {
pub method: String,
pub params: ResourceUpdatedParams,
}
impl Notification for ResourceUpdatedNotification {
const METHOD: &'static str = "notifications/resources/updated";
fn method(&self) -> &str {
&self.method
}
fn params(&self) -> Option<&serde_json::Value> {
None
}
}
impl ResourceUpdatedNotification {
pub fn new(uri: impl Into<String>) -> Self {
Self {
method: "notifications/resources/updated".to_string(),
params: ResourceUpdatedParams {
uri: uri.into(),
},
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ResourceUpdatedParams {
pub uri: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ResourceListChangedNotification {
pub method: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<super::messages::NotificationParams>,
}
impl ResourceListChangedNotification {
pub fn new() -> Self {
Self {
method: "notifications/resources/list_changed".to_string(),
params: None,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct EmbeddedResource {
pub r#type: String,
pub resource: ResourceContent,
#[serde(skip_serializing_if = "Option::is_none")]
pub annotations: Option<Annotations>,
}
impl EmbeddedResource {
pub fn text(uri: impl Into<String>, text: impl Into<String>) -> Self {
Self {
r#type: "resource".to_string(),
resource: ResourceContent::Text(TextResourceContents::new(uri, text)),
annotations: None,
}
}
pub fn blob(uri: impl Into<String>, blob: impl Into<String>) -> Self {
Self {
r#type: "resource".to_string(),
resource: ResourceContent::Blob(BlobResourceContents::new(uri, blob)),
annotations: None,
}
}
pub fn with_annotations(mut self, annotations: Annotations) -> Self {
self.annotations = Some(annotations);
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ResourceReference {
pub r#type: String,
pub uri: String,
}
impl ResourceReference {
pub fn new(uri: impl Into<String>) -> Self {
Self {
r#type: "ref/resource".to_string(),
uri: uri.into(),
}
}
}