bulwark_wasm_sdk/
from.rs

1use {
2    crate::{BodyChunk, Decision, Outcome, Request, Response},
3    std::net::{IpAddr, Ipv4Addr, Ipv6Addr},
4};
5
6impl From<Request> for crate::bulwark_host::RequestInterface {
7    fn from(request: Request) -> Self {
8        crate::bulwark_host::RequestInterface {
9            method: request.method().to_string(),
10            uri: request.uri().to_string(),
11            version: format!("{:?}", request.version()),
12            headers: request
13                .headers()
14                .iter()
15                .map(|(name, value)| (name.to_string(), value.as_bytes().to_vec()))
16                .collect(),
17            body_received: request.body().received,
18            chunk: request.body().content.clone(),
19            chunk_start: request.body().start,
20            chunk_length: request.body().size,
21            end_of_stream: request.body().end_of_stream,
22        }
23    }
24}
25
26impl From<crate::bulwark_host::ResponseInterface> for Response {
27    fn from(response: crate::bulwark_host::ResponseInterface) -> Self {
28        let mut builder = http::response::Builder::new();
29        builder = builder.status::<u16>(response.status.try_into().unwrap());
30        for (name, value) in response.headers {
31            builder = builder.header(name, value);
32        }
33        builder
34            .body(BodyChunk {
35                received: response.body_received,
36                end_of_stream: response.end_of_stream,
37                size: response.chunk.len().try_into().unwrap(),
38                start: 0,
39                content: response.chunk,
40            })
41            .unwrap()
42    }
43}
44
45impl From<crate::bulwark_host::IpInterface> for IpAddr {
46    fn from(ip: crate::bulwark_host::IpInterface) -> Self {
47        match ip {
48            crate::bulwark_host::IpInterface::V4(v4) => {
49                Self::V4(Ipv4Addr::new(v4.0, v4.1, v4.2, v4.3))
50            }
51            crate::bulwark_host::IpInterface::V6(v6) => Self::V6(Ipv6Addr::new(
52                v6.0, v6.1, v6.2, v6.3, v6.4, v6.5, v6.6, v6.7,
53            )),
54        }
55    }
56}
57
58// TODO: can we avoid conversions, perhaps by moving bindgen into lib.rs?
59
60impl From<crate::bulwark_host::DecisionInterface> for Decision {
61    fn from(decision: crate::bulwark_host::DecisionInterface) -> Self {
62        Decision {
63            accept: decision.accepted,
64            restrict: decision.restricted,
65            unknown: decision.unknown,
66        }
67    }
68}
69
70impl From<Decision> for crate::bulwark_host::DecisionInterface {
71    fn from(decision: Decision) -> Self {
72        crate::bulwark_host::DecisionInterface {
73            accepted: decision.accept,
74            restricted: decision.restrict,
75            unknown: decision.unknown,
76        }
77    }
78}
79
80impl From<crate::bulwark_host::OutcomeInterface> for Outcome {
81    fn from(outcome: crate::bulwark_host::OutcomeInterface) -> Self {
82        match outcome {
83            crate::bulwark_host::OutcomeInterface::Trusted => Outcome::Trusted,
84            crate::bulwark_host::OutcomeInterface::Accepted => Outcome::Accepted,
85            crate::bulwark_host::OutcomeInterface::Suspected => Outcome::Suspected,
86            crate::bulwark_host::OutcomeInterface::Restricted => Outcome::Restricted,
87        }
88    }
89}
90
91impl From<&str> for BodyChunk {
92    fn from(content: &str) -> Self {
93        BodyChunk {
94            received: true,
95            end_of_stream: true,
96            size: content.len() as u64,
97            start: 0,
98            content: content.as_bytes().to_owned(),
99        }
100    }
101}
102
103impl From<String> for BodyChunk {
104    fn from(content: String) -> Self {
105        BodyChunk {
106            received: true,
107            end_of_stream: true,
108            size: content.len() as u64,
109            start: 0,
110            content: content.into_bytes(),
111        }
112    }
113}
114
115impl From<Vec<u8>> for BodyChunk {
116    fn from(content: Vec<u8>) -> Self {
117        BodyChunk {
118            received: true,
119            end_of_stream: true,
120            size: content.len() as u64,
121            start: 0,
122            content,
123        }
124    }
125}