use crate::{RpcTransport, TransportError};
#[derive(Debug, Clone, Default)]
pub struct NegotiationConfig {
pub timeout_ms: Option<u64>,
pub prefer_webtransport: bool,
pub enable_websocket: bool,
pub enable_http_batch: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransportPreference {
WebTransport,
WebSocket,
HttpBatch,
}
pub struct NegotiationResult {
pub transport: Box<dyn RpcTransport>,
pub transport_type: TransportPreference,
pub negotiation_time_ms: u64,
}
impl std::fmt::Debug for NegotiationResult {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("NegotiationResult")
.field("transport_type", &self.transport_type)
.field("negotiation_time_ms", &self.negotiation_time_ms)
.field("transport", &"Box<dyn RpcTransport>")
.finish()
}
}
pub async fn negotiate(
url: &str,
config: Option<NegotiationConfig>,
) -> Result<NegotiationResult, TransportError> {
let _config = config.unwrap_or_default();
let _url = url;
Err(TransportError::Protocol(
"Transport negotiation not yet implemented".to_string(),
))
}
#[cfg(feature = "webtransport")]
#[allow(dead_code)]
async fn try_webtransport(
url: &str,
timeout_ms: Option<u64>,
) -> Result<Box<dyn RpcTransport>, TransportError> {
let _url = url;
let _timeout = timeout_ms;
Err(TransportError::Protocol(
"WebTransport not available".to_string(),
))
}
#[cfg(feature = "websocket")]
#[allow(dead_code)]
async fn try_websocket(
url: &str,
timeout_ms: Option<u64>,
) -> Result<Box<dyn RpcTransport>, TransportError> {
let _url = url;
let _timeout = timeout_ms;
Err(TransportError::Protocol(
"WebSocket not available".to_string(),
))
}
#[cfg(feature = "http-batch")]
#[allow(dead_code)]
async fn try_http_batch(
url: &str,
timeout_ms: Option<u64>,
) -> Result<Box<dyn RpcTransport>, TransportError> {
let _url = url;
let _timeout = timeout_ms;
Err(TransportError::Protocol(
"HTTP batch not available".to_string(),
))
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_negotiate_returns_error_when_not_implemented() {
let result = negotiate("https://example.com", None).await;
assert!(result.is_err());
}
#[test]
fn test_negotiation_config_default() {
let config = NegotiationConfig::default();
assert_eq!(config.timeout_ms, None);
assert!(!config.prefer_webtransport);
assert!(!config.enable_websocket);
assert!(!config.enable_http_batch);
}
}