Move whole rmcp crate to offcial rust sdk (#44)
* get rid of async-trait * move rmcp to official-rust-sdk * replace master with main * update readme, change license * fix typo * Update README.md Co-authored-by: Jefry Dewangga <jefrydco@gmail.com> * fix typos --------- Co-authored-by: = <=> Co-authored-by: Jefry Dewangga <jefrydco@gmail.com>
This commit is contained in:
parent
31493cfbcc
commit
0be6067326
1 changed files with 8676 additions and 4466 deletions
220
crates/rmcp/src/model/annotated.rs
Normal file
220
crates/rmcp/src/model/annotated.rs
Normal file
|
|
@ -0,0 +1,220 @@
|
|||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
use super::{
|
||||
RawContent, RawEmbeddedResource, RawImageContent, RawResource, RawResourceTemplate,
|
||||
RawTextContent, Role,
|
||||
};
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Annotations {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub audience: Option<Vec<Role>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub priority: Option<f32>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub timestamp: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
impl Annotations {
|
||||
/// Creates a new Annotations instance specifically for resources
|
||||
/// optional priority, and a timestamp (defaults to now if None)
|
||||
pub fn for_resource(priority: f32, timestamp: DateTime<Utc>) -> Self {
|
||||
assert!(
|
||||
(0.0..=1.0).contains(&priority),
|
||||
"Priority {priority} must be between 0.0 and 1.0"
|
||||
);
|
||||
Annotations {
|
||||
priority: Some(priority),
|
||||
timestamp: Some(timestamp),
|
||||
audience: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Annotated<T: AnnotateAble> {
|
||||
#[serde(flatten)]
|
||||
pub raw: T,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub annotations: Option<Annotations>,
|
||||
}
|
||||
|
||||
impl<T: AnnotateAble> Deref for Annotated<T> {
|
||||
type Target = T;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.raw
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AnnotateAble> DerefMut for Annotated<T> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.raw
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AnnotateAble> Annotated<T> {
|
||||
pub fn new(raw: T, annotations: Option<Annotations>) -> Self {
|
||||
Self { raw, annotations }
|
||||
}
|
||||
pub fn remove_annotation(&mut self) -> Option<Annotations> {
|
||||
self.annotations.take()
|
||||
}
|
||||
pub fn audience(&self) -> Option<&Vec<Role>> {
|
||||
self.annotations.as_ref().and_then(|a| a.audience.as_ref())
|
||||
}
|
||||
pub fn priority(&self) -> Option<f32> {
|
||||
self.annotations.as_ref().and_then(|a| a.priority)
|
||||
}
|
||||
pub fn timestamp(&self) -> Option<DateTime<Utc>> {
|
||||
self.annotations.as_ref().and_then(|a| a.timestamp)
|
||||
}
|
||||
pub fn with_audience(self, audience: Vec<Role>) -> Annotated<T>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
if let Some(annotations) = self.annotations {
|
||||
Annotated {
|
||||
raw: self.raw,
|
||||
annotations: Some(Annotations {
|
||||
audience: Some(audience),
|
||||
..annotations
|
||||
}),
|
||||
}
|
||||
} else {
|
||||
Annotated {
|
||||
raw: self.raw,
|
||||
annotations: Some(Annotations {
|
||||
audience: Some(audience),
|
||||
priority: None,
|
||||
timestamp: None,
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn with_priority(self, priority: f32) -> Annotated<T>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
if let Some(annotations) = self.annotations {
|
||||
Annotated {
|
||||
raw: self.raw,
|
||||
annotations: Some(Annotations {
|
||||
priority: Some(priority),
|
||||
..annotations
|
||||
}),
|
||||
}
|
||||
} else {
|
||||
Annotated {
|
||||
raw: self.raw,
|
||||
annotations: Some(Annotations {
|
||||
priority: Some(priority),
|
||||
timestamp: None,
|
||||
audience: None,
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn with_timestamp(self, timestamp: DateTime<Utc>) -> Annotated<T>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
if let Some(annotations) = self.annotations {
|
||||
Annotated {
|
||||
raw: self.raw,
|
||||
annotations: Some(Annotations {
|
||||
timestamp: Some(timestamp),
|
||||
..annotations
|
||||
}),
|
||||
}
|
||||
} else {
|
||||
Annotated {
|
||||
raw: self.raw,
|
||||
annotations: Some(Annotations {
|
||||
timestamp: Some(timestamp),
|
||||
priority: None,
|
||||
audience: None,
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn with_timestamp_now(self) -> Annotated<T>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
self.with_timestamp(Utc::now())
|
||||
}
|
||||
}
|
||||
|
||||
mod sealed {
|
||||
pub trait Sealed {}
|
||||
}
|
||||
macro_rules! annotate {
|
||||
($T: ident) => {
|
||||
impl sealed::Sealed for $T {}
|
||||
impl AnnotateAble for $T {}
|
||||
};
|
||||
}
|
||||
|
||||
annotate!(RawContent);
|
||||
annotate!(RawTextContent);
|
||||
annotate!(RawImageContent);
|
||||
annotate!(RawEmbeddedResource);
|
||||
annotate!(RawResource);
|
||||
annotate!(RawResourceTemplate);
|
||||
|
||||
pub trait AnnotateAble: sealed::Sealed {
|
||||
fn optional_annotate(self, annotations: Option<Annotations>) -> Annotated<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
Annotated::new(self, annotations)
|
||||
}
|
||||
fn annotate(self, annotations: Annotations) -> Annotated<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
Annotated::new(self, Some(annotations))
|
||||
}
|
||||
fn no_annotation(self) -> Annotated<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
Annotated::new(self, None)
|
||||
}
|
||||
fn with_audience(self, audience: Vec<Role>) -> Annotated<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
self.annotate(Annotations {
|
||||
audience: Some(audience),
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
fn with_priority(self, priority: f32) -> Annotated<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
self.annotate(Annotations {
|
||||
priority: Some(priority),
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
fn with_timestamp(self, timestamp: DateTime<Utc>) -> Annotated<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
self.annotate(Annotations {
|
||||
timestamp: Some(timestamp),
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
fn with_timestamp_now(self) -> Annotated<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
self.with_timestamp(Utc::now())
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue