use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
#[serde(rename_all = "lowercase")]
pub enum TransportProtocol {
Grpc = 0,
WebSocket = 1,
Http = 2,
Stdio = 3,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct TransportEndpoint {
pub protocol: TransportProtocol,
pub url: String,
pub available: bool,
pub protocol_versions: Vec<String>,
}
impl TransportEndpoint {
pub const MAX_PROTOCOL_VERSIONS: usize = 20;
pub const MAX_URL_LEN: usize = 2048;
const MAX_VERSION_LEN: usize = 64;
pub fn validate(&self) -> Result<(), String> {
if self.url.len() > Self::MAX_URL_LEN {
return Err(format!(
"TransportEndpoint url length {} exceeds max {}",
self.url.len(),
Self::MAX_URL_LEN,
));
}
if crate::core::has_dangerous_chars(&self.url) {
return Err("TransportEndpoint url contains control or format characters".to_string());
}
if self.protocol_versions.len() > Self::MAX_PROTOCOL_VERSIONS {
return Err(format!(
"TransportEndpoint protocol_versions count {} exceeds max {}",
self.protocol_versions.len(),
Self::MAX_PROTOCOL_VERSIONS,
));
}
for (i, ver) in self.protocol_versions.iter().enumerate() {
if ver.len() > Self::MAX_VERSION_LEN {
return Err(format!(
"TransportEndpoint protocol_versions[{}] length {} exceeds max {}",
i,
ver.len(),
Self::MAX_VERSION_LEN,
));
}
if ver
.chars()
.any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
{
return Err(format!(
"TransportEndpoint protocol_versions[{i}] contains control or format characters",
));
}
}
Ok(())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SdkTier {
Core = 0,
Standard = 1,
Extended = 2,
Full = 3,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct TransportAttempt {
pub protocol: TransportProtocol,
pub endpoint_url: String,
pub succeeded: bool,
pub duration_ms: u64,
pub error: Option<String>,
}
impl TransportAttempt {
const MAX_URL_LEN: usize = 2048;
const MAX_ERROR_LEN: usize = 4096;
pub fn validate(&self) -> Result<(), String> {
if self.endpoint_url.len() > Self::MAX_URL_LEN {
return Err(format!(
"TransportAttempt endpoint_url length {} exceeds max {}",
self.endpoint_url.len(),
Self::MAX_URL_LEN,
));
}
if crate::core::has_dangerous_chars(&self.endpoint_url) {
return Err(
"TransportAttempt endpoint_url contains control or format characters".to_string(),
);
}
if let Some(ref err) = self.error {
if err.len() > Self::MAX_ERROR_LEN {
return Err(format!(
"TransportAttempt error length {} exceeds max {}",
err.len(),
Self::MAX_ERROR_LEN,
));
}
if crate::core::has_dangerous_chars(err) {
return Err(
"TransportAttempt error contains control or format characters".to_string(),
);
}
}
Ok(())
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct FallbackNegotiationHistory {
pub attempts: Vec<TransportAttempt>,
pub successful_transport: Option<TransportProtocol>,
pub total_duration_ms: u64,
}
impl FallbackNegotiationHistory {
pub const MAX_ATTEMPTS: usize = 100;
pub fn validate(&self) -> Result<(), String> {
if self.attempts.len() > Self::MAX_ATTEMPTS {
return Err(format!(
"FallbackNegotiationHistory attempts count {} exceeds max {}",
self.attempts.len(),
Self::MAX_ATTEMPTS,
));
}
for (i, attempt) in self.attempts.iter().enumerate() {
attempt
.validate()
.map_err(|e| format!("attempts[{i}]: {e}"))?;
}
Ok(())
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct SdkCapabilities {
pub tier: SdkTier,
pub capabilities: Vec<String>,
pub supported_versions: Vec<String>,
}
impl SdkCapabilities {
pub const MAX_CAPABILITIES: usize = 100;
pub const MAX_VERSIONS: usize = 20;
const MAX_CAPABILITY_LEN: usize = 256;
const MAX_VERSION_LEN: usize = 64;
pub fn validate(&self) -> Result<(), String> {
if self.capabilities.len() > Self::MAX_CAPABILITIES {
return Err(format!(
"capabilities count {} exceeds maximum {}",
self.capabilities.len(),
Self::MAX_CAPABILITIES,
));
}
for (i, cap) in self.capabilities.iter().enumerate() {
if cap.len() > Self::MAX_CAPABILITY_LEN {
return Err(format!(
"capabilities[{}] length {} exceeds max {}",
i,
cap.len(),
Self::MAX_CAPABILITY_LEN,
));
}
if cap
.chars()
.any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
{
return Err(format!(
"capabilities[{i}] contains control or format characters",
));
}
}
if self.supported_versions.len() > Self::MAX_VERSIONS {
return Err(format!(
"supported_versions count {} exceeds maximum {}",
self.supported_versions.len(),
Self::MAX_VERSIONS,
));
}
for (i, ver) in self.supported_versions.iter().enumerate() {
if ver.len() > Self::MAX_VERSION_LEN {
return Err(format!(
"supported_versions[{}] length {} exceeds max {}",
i,
ver.len(),
Self::MAX_VERSION_LEN,
));
}
if ver
.chars()
.any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
{
return Err(format!(
"supported_versions[{i}] contains control or format characters",
));
}
}
Ok(())
}
}