viceroy_lib/component/
types.rs

1use {
2    super::fastly::api::types,
3    crate::{
4        error::{self, HandleError},
5        linking::ComponentCtx,
6    },
7    http::header::InvalidHeaderName,
8};
9
10pub enum TrappableError {
11    Error(types::Error),
12    Trap(anyhow::Error),
13}
14
15impl types::Host for ComponentCtx {
16    fn convert_error(&mut self, err: TrappableError) -> wasmtime::Result<types::Error> {
17        match err {
18            TrappableError::Error(err) => Ok(err),
19            TrappableError::Trap(err) => Err(err),
20        }
21    }
22}
23
24impl From<wasmtime::component::ResourceTableError> for TrappableError {
25    fn from(e: wasmtime::component::ResourceTableError) -> Self {
26        Self::Trap(e.into())
27    }
28}
29
30impl From<types::Error> for TrappableError {
31    fn from(e: types::Error) -> Self {
32        Self::Error(e)
33    }
34}
35
36impl From<HandleError> for TrappableError {
37    fn from(_: HandleError) -> Self {
38        Self::Error(types::Error::BadHandle)
39    }
40}
41
42impl From<InvalidHeaderName> for TrappableError {
43    fn from(_: InvalidHeaderName) -> Self {
44        Self::Error(types::Error::GenericError)
45    }
46}
47
48impl From<error::Error> for TrappableError {
49    fn from(e: error::Error) -> Self {
50        match e {
51            error::Error::FatalError(_) => Self::Trap(anyhow::anyhow!(e.to_string())),
52            _ => Self::Error(e.into()),
53        }
54    }
55}
56
57impl From<std::net::IpAddr> for types::IpAddress {
58    fn from(addr: std::net::IpAddr) -> Self {
59        match addr {
60            std::net::IpAddr::V4(addr) => types::IpAddress::Ipv4(addr.octets().into()),
61            std::net::IpAddr::V6(addr) => types::IpAddress::Ipv6(addr.segments().into()),
62        }
63    }
64}