pub struct ContextPropagator;
Expand description
Context propagator for middleware integration.
§Examples
use pmcp::shared::context::{ContextPropagator, RequestContext};
use pmcp::types::RequestId;
use std::collections::HashMap;
// Extract context from incoming HTTP headers
let mut headers = HashMap::new();
headers.insert("traceparent".to_string(),
"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01".to_string());
headers.insert("x-request-id".to_string(), "12345".to_string());
headers.insert("x-user-id".to_string(), "user-789".to_string());
headers.insert("baggage-tenant".to_string(), "acme-corp".to_string());
let context = ContextPropagator::extract(&headers);
assert!(context.is_some());
if let Some(ctx) = context {
assert_eq!(ctx.user_id, Some("user-789".to_string()));
assert_eq!(ctx.baggage.get("tenant"), Some(&"acme-corp".to_string()));
}
// Inject context into outgoing HTTP headers
let context = RequestContext::new(RequestId::from(999i64))
.with_user_id("admin".to_string());
let outgoing_headers = ContextPropagator::inject(&context);
assert!(outgoing_headers.contains_key("traceparent"));
assert_eq!(outgoing_headers.get("x-user-id"), Some(&"admin".to_string()));
Implementations§
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ContextPropagator
impl RefUnwindSafe for ContextPropagator
impl Send for ContextPropagator
impl Sync for ContextPropagator
impl Unpin for ContextPropagator
impl UnwindSafe for ContextPropagator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more