nestrs_core/
execution_context.rs1use axum::http::request::Parts;
2
3#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum HostType {
6 Http,
7 Rpc,
8 Ws,
9 Unknown,
10}
11
12#[derive(Clone, Debug)]
15pub struct ExecutionContext {
16 host_type: HostType,
17 pub method: axum::http::Method,
18 pub path_and_query: String,
19}
20
21impl ExecutionContext {
22 pub fn from_http_parts(parts: &Parts) -> Self {
23 let path_and_query = parts
24 .uri
25 .path_and_query()
26 .map(|pq| pq.as_str().to_owned())
27 .unwrap_or_else(|| parts.uri.path().to_owned());
28 Self {
29 host_type: HostType::Http,
30 method: parts.method.clone(),
31 path_and_query,
32 }
33 }
34
35 pub fn get_type(&self) -> HostType {
36 self.host_type
37 }
38
39 pub fn switch_to_http(&self) -> HttpExecutionArguments<'_> {
40 HttpExecutionArguments { ctx: self }
41 }
42}
43
44pub struct HttpExecutionArguments<'a> {
46 ctx: &'a ExecutionContext,
47}
48
49impl<'a> HttpExecutionArguments<'a> {
50 pub fn method(&self) -> &'a axum::http::Method {
51 &self.ctx.method
52 }
53
54 pub fn path_and_query(&self) -> &'a str {
55 self.ctx.path_and_query.as_str()
56 }
57}