mcp_core_rs/
annotation.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4use super::Role;
5
6#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
7#[serde(rename_all = "camelCase")]
8pub struct Annotation {
9    #[serde(skip_serializing_if = "Option::is_none")]
10    pub audience: Option<Vec<Role>>,
11    #[serde(skip_serializing_if = "Option::is_none")]
12    pub priority: Option<f32>,
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub timestamp: Option<DateTime<Utc>>,
15}
16
17impl Annotation {
18    /// Create annotations with a given priority and the current timestamp.
19    pub fn new_with_priority(priority: f32) -> Self {
20        assert!(
21            (0.0 ..= 1.0).contains(&priority),
22            "Priority {priority} must be between 0.0 and 1.0"
23        );
24        Self {
25            priority: Some(priority),
26            timestamp: Some(Utc::now()),
27            audience: None,
28        }
29    }
30
31    /// Create annotations with a given audience and the current timestamp.
32    pub fn new_with_audience(audience: Vec<Role>) -> Self {
33        Self {
34            audience: Some(audience),
35            priority: None,
36            timestamp: Some(Utc::now()),
37        }
38    }
39
40    /// Create annotations with a given priority and audience, and the current timestamp.
41    pub fn new_with_priority_and_audience(priority: f32, audience: Vec<Role>) -> Self {
42        assert!(
43            (0.0 ..= 1.0).contains(&priority),
44            "Priority {priority} must be between 0.0 and 1.0"
45        );
46        Self {
47            audience: Some(audience),
48            priority: Some(priority),
49            timestamp: Some(Utc::now()),
50        }
51    }
52
53    pub fn set_priority(&mut self, priority: f32) {
54        assert!(
55            (0.0 ..= 1.0).contains(&priority),
56            "Priority must be between 0.0 and 1.0"
57        );
58        self.priority = Some(priority);
59    }
60
61    /// 设置新的受众角色
62    pub fn set_audience(&mut self, audience: Vec<Role>) {
63        self.audience = Some(audience);
64    }
65
66    /// 更新时间戳为当前时间
67    pub fn update_timestamp(&mut self) {
68        self.timestamp = Some(Utc::now());
69    }
70}
71
72impl Default for Annotation {
73    fn default() -> Self {
74        Self::new_with_priority(0.0)
75    }
76}