-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathvalidators.rs
More file actions
167 lines (165 loc) · 5.57 KB
/
validators.rs
File metadata and controls
167 lines (165 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/// Validates that a deserialized string field matches a given constant value.
///
/// This function is intended for use with `#[serde(deserialize_with)]` to enforce
/// that a field in a struct always has a fixed, expected string value during deserialization.
///
/// # Parameters
/// - `struct_name`: The name of the struct where this validation is applied.
/// - `field_name`: The name of the field being validated.
/// - `expected`: The expected constant string value for the field.
/// - `deserializer`: The Serde deserializer for the field.
///
/// # Returns
/// - `Ok(String)` if the deserialized value matches the expected value.
/// - `Err(D::Error)` if the value differs, with an error message indicating
/// which struct and field failed validation.
///
pub fn const_str_validator<'de, D>(
struct_name: &'static str,
field_name: &'static str,
expected: &'static str,
deserializer: D,
) -> Result<String, D::Error>
where
D: serde::de::Deserializer<'de>,
{
let value: String = serde::Deserialize::deserialize(deserializer)?;
if value == expected {
Ok(value)
} else {
Err(serde::de::Error::custom(format!(
"Expected field `{field_name}` in struct `{struct_name}` as const value '{expected}', but got '{value}'",
)))
}
}
/// Macro to generate a field-specific validator function for use with Serde.
///
/// This avoids repetitive boilerplate when you have multiple fields/structs
/// requiring constant string validation.
///
/// # Syntax
/// ```ignore
/// validate!(fn_name, "StructName", "field_name", "expected_value");
/// ```
///
/// - `fn_name`: The function name to generate.
/// - `StructName`: The name of the struct (for error messages).
/// - `field_name`: The name of the field (for error messages).
/// - `expected_value`: The required constant string value.
///
macro_rules! validate {
($func_name:ident, $struct:expr, $field:expr, $expected:expr $(,)?) => {
pub(crate) fn $func_name<'de, D>(deserializer: D) -> Result<String, D::Error>
where
D: serde::de::Deserializer<'de>,
{
const_str_validator($struct, $field, $expected, deserializer)
}
};
}
//* Validator Functions *//
validate!(audio_content_type_, "AudioContent", "type_", "audio");
validate!(call_tool_request_method, "CallToolRequest", "method", "tools/call");
validate!(
cancelled_notification_method,
"CancelledNotification",
"method",
"notifications/cancelled"
);
validate!(complete_request_method, "CompleteRequest", "method", "completion/complete");
validate!(
create_message_request_method,
"CreateMessageRequest",
"method",
"sampling/createMessage"
);
validate!(embedded_resource_type_, "EmbeddedResource", "type_", "resource");
validate!(get_prompt_request_method, "GetPromptRequest", "method", "prompts/get");
validate!(image_content_type_, "ImageContent", "type_", "image");
validate!(initialize_request_method, "InitializeRequest", "method", "initialize");
validate!(
initialized_notification_method,
"InitializedNotification",
"method",
"notifications/initialized"
);
validate!(jsonrpc_error_jsonrpc, "JsonrpcError", "jsonrpc", "2.0");
validate!(jsonrpc_notification_jsonrpc, "JsonrpcNotification", "jsonrpc", "2.0");
validate!(jsonrpc_request_jsonrpc, "JsonrpcRequest", "jsonrpc", "2.0");
validate!(jsonrpc_response_jsonrpc, "JsonrpcResponse", "jsonrpc", "2.0");
validate!(list_prompts_request_method, "ListPromptsRequest", "method", "prompts/list");
validate!(
list_resource_templates_request_method,
"ListResourceTemplatesRequest",
"method",
"resources/templates/list"
);
validate!(
list_resources_request_method,
"ListResourcesRequest",
"method",
"resources/list"
);
validate!(list_roots_request_method, "ListRootsRequest", "method", "roots/list");
validate!(list_tools_request_method, "ListToolsRequest", "method", "tools/list");
validate!(
logging_message_notification_method,
"LoggingMessageNotification",
"method",
"notifications/message"
);
validate!(ping_request_method, "PingRequest", "method", "ping");
validate!(
progress_notification_method,
"ProgressNotification",
"method",
"notifications/progress"
);
validate!(
prompt_list_changed_notification_method,
"PromptListChangedNotification",
"method",
"notifications/prompts/list_changed"
);
validate!(prompt_reference_type_, "PromptReference", "type_", "ref/prompt");
validate!(
read_resource_request_method,
"ReadResourceRequest",
"method",
"resources/read"
);
validate!(
resource_list_changed_notification_method,
"ResourceListChangedNotification",
"method",
"notifications/resources/list_changed"
);
validate!(resource_reference_type_, "ResourceReference", "type_", "ref/resource");
validate!(
resource_updated_notification_method,
"ResourceUpdatedNotification",
"method",
"notifications/resources/updated"
);
validate!(
roots_list_changed_notification_method,
"RootsListChangedNotification",
"method",
"notifications/roots/list_changed"
);
validate!(set_level_request_method, "SetLevelRequest", "method", "logging/setLevel");
validate!(subscribe_request_method, "SubscribeRequest", "method", "resources/subscribe");
validate!(text_content_type_, "TextContent", "type_", "text");
validate!(tool_input_schema_type_, "ToolInputSchema", "type_", "object");
validate!(
tool_list_changed_notification_method,
"ToolListChangedNotification",
"method",
"notifications/tools/list_changed"
);
validate!(
unsubscribe_request_method,
"UnsubscribeRequest",
"method",
"resources/unsubscribe"
);