hyperware_process_lib/types/
send_error.rs

1use crate::{Address, LazyLoadBlob, Message, _wit_message_to_message};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Deserialize, Serialize)]
5pub struct SendError {
6    pub kind: SendErrorKind,
7    pub target: Address,
8    pub message: Message,
9    pub lazy_load_blob: Option<LazyLoadBlob>,
10    pub context: Option<Vec<u8>>,
11}
12
13impl SendError {
14    pub fn kind(&self) -> &SendErrorKind {
15        &self.kind
16    }
17    pub fn target(&self) -> &Address {
18        &self.target
19    }
20    pub fn message(&self) -> &Message {
21        &self.message
22    }
23    pub fn blob(&self) -> Option<&LazyLoadBlob> {
24        self.lazy_load_blob.as_ref()
25    }
26    pub fn context(&self) -> Option<&[u8]> {
27        self.context.as_deref()
28    }
29}
30
31impl std::fmt::Display for SendError {
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        match &self.kind {
34            SendErrorKind::Offline => write!(f, "Offline"),
35            SendErrorKind::Timeout => write!(f, "Timeout"),
36        }
37    }
38}
39
40impl std::error::Error for SendError {
41    fn description(&self) -> &str {
42        match &self.kind {
43            SendErrorKind::Offline => "Offline",
44            SendErrorKind::Timeout => "Timeout",
45        }
46    }
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub enum SendErrorKind {
51    Offline,
52    Timeout,
53}
54
55impl SendErrorKind {
56    pub fn is_offline(&self) -> bool {
57        matches!(self, SendErrorKind::Offline)
58    }
59    pub fn is_timeout(&self) -> bool {
60        matches!(self, SendErrorKind::Timeout)
61    }
62}
63
64pub fn _wit_send_error_to_send_error(
65    send_err: crate::hyperware::process::standard::SendError,
66    context: Option<Vec<u8>>,
67) -> SendError {
68    SendError {
69        kind: match send_err.kind {
70            crate::hyperware::process::standard::SendErrorKind::Offline => SendErrorKind::Offline,
71            crate::hyperware::process::standard::SendErrorKind::Timeout => SendErrorKind::Timeout,
72        },
73        target: send_err.target.clone(),
74        message: _wit_message_to_message(send_err.target, send_err.message),
75        lazy_load_blob: send_err.lazy_load_blob,
76        context,
77    }
78}