golem_wasi_http/wasi/
conversions.rs

1use crate::error::Kind;
2use http::Method;
3use wasi::http::types;
4use wasi::io::streams;
5
6pub(crate) fn encode_method(method: Method) -> types::Method {
7    match method {
8        Method::GET => types::Method::Get,
9        Method::POST => types::Method::Post,
10        Method::PUT => types::Method::Put,
11        Method::DELETE => types::Method::Delete,
12        Method::HEAD => types::Method::Head,
13        Method::OPTIONS => types::Method::Options,
14        Method::CONNECT => types::Method::Connect,
15        Method::PATCH => types::Method::Patch,
16        Method::TRACE => types::Method::Trace,
17        other => types::Method::Other(other.to_string()),
18    }
19}
20
21impl From<types::ErrorCode> for crate::Error {
22    fn from(value: types::ErrorCode) -> Self {
23        crate::Error::new(
24            Kind::Request,
25            Some(std::io::Error::other(format!("{:?}", value))),
26        )
27    }
28}
29
30impl From<streams::StreamError> for crate::Error {
31    fn from(value: streams::StreamError) -> Self {
32        crate::Error::new(
33            Kind::Request,
34            Some(std::io::Error::other(format!("{:?}", value))),
35        )
36    }
37}
38
39impl From<types::HeaderError> for crate::Error {
40    fn from(value: types::HeaderError) -> Self {
41        crate::Error::new(
42            Kind::Request,
43            Some(std::io::Error::other(format!("{:?}", value))),
44        )
45    }
46}
47
48pub(crate) fn failure_point(s: &str, _: ()) -> crate::Error {
49    crate::Error::new(Kind::Request, Some(std::io::Error::other(s)))
50}