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_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)
}
};
}
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"
);