vantage_table/
write_context.rs1use std::fmt;
11use std::future::Future;
12use std::net::IpAddr;
13
14use indexmap::IndexMap;
15
16tokio::task_local! {
17 static CONTEXT: WriteContext;
18}
19
20#[derive(Clone, Debug, Default, PartialEq)]
23#[non_exhaustive]
24pub struct WriteContext {
25 pub principal: Option<ciborium::Value>,
27 pub group: Option<String>,
29 pub request: Option<RequestContext>,
31}
32
33impl WriteContext {
34 pub fn new() -> Self {
36 Self::default()
37 }
38
39 pub fn with_principal(mut self, principal: ciborium::Value) -> Self {
40 self.principal = Some(principal);
41 self
42 }
43
44 pub fn with_group(mut self, group: impl Into<String>) -> Self {
45 self.group = Some(group.into());
46 self
47 }
48
49 pub fn with_request(mut self, request: RequestContext) -> Self {
50 self.request = Some(request);
51 self
52 }
53
54 pub async fn scope<F: Future>(self, fut: F) -> F::Output {
56 CONTEXT.scope(self, fut).await
57 }
58
59 pub fn current() -> Option<WriteContext> {
61 CONTEXT.try_with(|c| c.clone()).ok()
62 }
63}
64
65pub const REQUEST_HEADER_ALLOW_LIST: &[&str] = &[
69 "user-agent",
70 "referer",
71 "accept-language",
72 "x-posthog-distinct-id",
73 "x-posthog-session-id",
74];
75
76#[derive(Clone, Copy, Debug, PartialEq, Eq)]
78#[non_exhaustive]
79pub enum Transport {
80 Ws,
81 Rest,
82 Mcp,
83 Internal,
84}
85
86impl Transport {
87 pub fn as_str(&self) -> &'static str {
88 match self {
89 Transport::Ws => "ws",
90 Transport::Rest => "rest",
91 Transport::Mcp => "mcp",
92 Transport::Internal => "internal",
93 }
94 }
95}
96
97#[derive(Clone, PartialEq)]
101#[non_exhaustive]
102pub struct RequestContext {
103 pub transport: Transport,
104 pub ip: Option<IpAddr>,
105 pub headers: IndexMap<String, String>,
107 pub cookies: IndexMap<String, String>,
108}
109
110impl fmt::Debug for RequestContext {
111 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112 let masked = |map: &IndexMap<String, String>| -> Vec<String> {
114 map.keys().map(|name| format!("{name}=***")).collect()
115 };
116 f.debug_struct("RequestContext")
117 .field("transport", &self.transport)
118 .field("ip", &self.ip)
119 .field("headers", &masked(&self.headers))
120 .field("cookies", &masked(&self.cookies))
121 .finish()
122 }
123}
124
125impl RequestContext {
126 pub fn new(transport: Transport) -> Self {
127 Self {
128 transport,
129 ip: None,
130 headers: IndexMap::new(),
131 cookies: IndexMap::new(),
132 }
133 }
134
135 pub fn with_ip(mut self, ip: IpAddr) -> Self {
136 self.ip = Some(ip);
137 self
138 }
139
140 pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
141 self.headers
142 .insert(name.into().to_ascii_lowercase(), value.into());
143 self
144 }
145
146 pub fn with_cookie(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
147 self.cookies.insert(name.into(), value.into());
148 self
149 }
150
151 pub fn header(&self, name: &str) -> Option<&str> {
153 self.headers
154 .get(&name.to_ascii_lowercase())
155 .map(String::as_str)
156 }
157
158 pub fn cookie(&self, name: &str) -> Option<&str> {
161 self.cookies.get(name).map(String::as_str)
162 }
163}