Skip to main content

cratestack_client_rust/runtime/
wire.rs

1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4
5use crate::error::ClientError;
6
7#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
8pub enum RuntimeCodecConfig {
9    #[default]
10    Cbor,
11    Json,
12}
13
14#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
15pub enum RuntimeEnvelopeConfig {
16    #[default]
17    None,
18    CoseSign1,
19}
20
21#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
22pub struct RuntimeTransportConfig {
23    pub codec: RuntimeCodecConfig,
24    pub envelope: RuntimeEnvelopeConfig,
25}
26
27#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
28pub struct RuntimeHeader {
29    pub name: String,
30    pub value: String,
31}
32
33#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
34pub struct RuntimeRequestWire {
35    pub method: String,
36    pub path: String,
37    pub canonical_query: Option<String>,
38    pub headers: Vec<RuntimeHeader>,
39    pub body: Vec<u8>,
40}
41
42#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
43pub struct RuntimeResponseWire {
44    pub status_code: u16,
45    pub headers: Vec<RuntimeHeader>,
46    pub body: Vec<u8>,
47}
48
49#[repr(u32)]
50#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
51pub enum RuntimeErrorCode {
52    Transport = 1,
53    Codec = 2,
54    State = 3,
55    InvalidResponse = 4,
56    Remote = 5,
57    BadInput = 6,
58}
59
60#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
61pub struct RuntimeErrorWire {
62    pub code: RuntimeErrorCode,
63    pub http_status: Option<u16>,
64    pub message: String,
65    pub remote_code: Option<String>,
66    pub remote_body: Option<Vec<u8>>,
67}
68
69impl From<ClientError> for RuntimeErrorWire {
70    fn from(value: ClientError) -> Self {
71        match value {
72            ClientError::Transport(error) => Self {
73                code: RuntimeErrorCode::Transport,
74                http_status: None,
75                message: error.to_string(),
76                remote_code: None,
77                remote_body: None,
78            },
79            ClientError::Codec(error) => Self {
80                code: RuntimeErrorCode::Codec,
81                http_status: Some(error.status_code().as_u16()),
82                message: error.to_string(),
83                remote_code: Some(error.code().to_owned()),
84                remote_body: None,
85            },
86            ClientError::State(message) => Self {
87                code: RuntimeErrorCode::State,
88                http_status: None,
89                message,
90                remote_code: None,
91                remote_body: None,
92            },
93            ClientError::InvalidResponse(message) => Self {
94                code: RuntimeErrorCode::InvalidResponse,
95                http_status: None,
96                message,
97                remote_code: None,
98                remote_body: None,
99            },
100            ClientError::BadInput(message) => Self {
101                code: RuntimeErrorCode::BadInput,
102                http_status: None,
103                message,
104                remote_code: None,
105                remote_body: None,
106            },
107            ClientError::Remote {
108                status,
109                error,
110                message,
111            } => Self {
112                code: RuntimeErrorCode::Remote,
113                http_status: Some(status.as_u16()),
114                remote_code: error.as_ref().map(|value| value.code.clone()),
115                remote_body: error
116                    .as_ref()
117                    .and_then(|value| serde_json::to_vec(value).ok()),
118                message,
119            },
120        }
121    }
122}
123
124#[derive(Debug, Clone, PartialEq, Eq)]
125pub enum RuntimeStateStoreConfig {
126    InMemory,
127    JsonFile { path: PathBuf },
128}
129
130#[derive(Debug, Clone, PartialEq, Eq)]
131pub struct RuntimeConfigWire {
132    pub base_url: String,
133    pub state_store: RuntimeStateStoreConfig,
134    pub transport: RuntimeTransportConfig,
135}