1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
pub(crate) use serde_json::Value as JsonValue;
mod timestamp;
pub use timestamp::Timestamp;
mod actor_wasm;
mod common;
pub use common::{
client, context, deserialize, serialize, Message, MessageDispatch, RpcError, Transport,
};
pub mod channel_log;
pub mod provider;
pub(crate) mod provider_main;
mod wasmbus_model;
pub mod model {
pub use crate::wasmbus_model::*;
}
#[cfg(not(target_arch = "wasm32"))]
pub(crate) mod rpc_client;
#[cfg(not(target_arch = "wasm32"))]
pub use rpc_client::{RpcClient, RpcClientSync};
#[doc(hidden)]
pub const WASMBUS_RPC_VERSION: u32 = 0;
#[doc(hidden)]
pub const WASMBUS_RPC_IMPORT_NAME: &str = "wapc";
pub const WELD_CRATE_VERSION: &str = env!("CARGO_PKG_VERSION");
pub type CallResult = std::result::Result<Vec<u8>, Box<dyn std::error::Error + Sync + Send>>;
pub type HandlerResult<T> = std::result::Result<T, Box<dyn std::error::Error + Sync + Send>>;
pub type TomlMap = toml::value::Map<String, toml::value::Value>;
mod wasmbus_core;
pub mod core {
pub use crate::wasmbus_core::*;
cfg_if::cfg_if! {
if #[cfg(not(target_arch = "wasm32"))] {
const TEST_HARNESS: &str = "_TEST_";
const NATS_PING_INTERVAL_SEC: u16 = 15;
const NATS_PING_FAIL_COUNT: u16 = 8;
const NATS_RECONNECT_INTERVAL: u64 = 15;
impl HostData {
pub fn is_test(&self) -> bool {
self.host_id == TEST_HARNESS
}
pub fn nats_options(&self) -> ratsio::NatsClientOptions {
ratsio::NatsClientOptions {
ping_interval: NATS_PING_INTERVAL_SEC,
ping_max_out: NATS_PING_FAIL_COUNT,
reconnect_timeout: NATS_RECONNECT_INTERVAL,
ensure_connect: true,
subscribe_on_reconnect: true,
cluster_uris: if self.lattice_rpc_url.is_empty() {
Vec::new()
} else {
vec![self.lattice_rpc_url.clone()]
}
.into(),
..Default::default()
}
}
}
}
}
pub const URL_SCHEME: &str = "wasmbus";
impl std::fmt::Display for WasmCloudEntity {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.url())
}
}
impl WasmCloudEntity {
pub fn new_actor<T: ToString>(id: T) -> WasmCloudEntity {
WasmCloudEntity {
public_key: id.to_string(),
contract_id: String::new(),
link_name: String::new(),
}
}
pub fn from_link(link: &LinkDefinition) -> Self {
WasmCloudEntity {
public_key: link.provider_id.clone(),
contract_id: link.contract_id.clone(),
link_name: link.link_name.clone(),
}
}
pub fn new_provider<T1: ToString, T2: ToString, T3: ToString>(
id: T1,
contract_id: T2,
link_name: T3,
) -> WasmCloudEntity {
WasmCloudEntity {
public_key: id.to_string(),
contract_id: contract_id.to_string(),
link_name: link_name.to_string(),
}
}
pub fn url(&self) -> String {
if self.public_key.to_uppercase().starts_with('M') {
format!("{}://{}", crate::core::URL_SCHEME, self.public_key)
} else {
format!(
"{}://{}/{}/{}",
URL_SCHEME,
self.contract_id
.replace(":", "/")
.replace(" ", "_")
.to_lowercase(),
self.link_name.replace(" ", "_").to_lowercase(),
self.public_key
)
}
}
pub fn public_key(&self) -> String {
self.public_key.to_string()
}
}
impl From<&str> for WasmCloudEntity {
fn from(target: &str) -> WasmCloudEntity {
WasmCloudEntity::new_actor(target.to_string())
}
}
impl From<String> for WasmCloudEntity {
fn from(target: String) -> WasmCloudEntity {
WasmCloudEntity::new_actor(target)
}
}
}
pub mod actor {
pub mod prelude {
pub use crate::common::{client, context, Message, MessageDispatch, RpcError};
pub use async_trait::async_trait;
pub use wasmbus_macros::Actor;
#[cfg(feature = "BigInteger")]
pub use num_bigint::BigInt as BigInteger;
#[cfg(feature = "BigDecimal")]
pub use bigdecimal::BigDecimal;
cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] {
pub use crate::actor_wasm::{console_log, WasmHost};
} else {
#[derive(Clone, Debug, Default)]
pub struct WasmHost {}
#[async_trait]
impl crate::Transport for WasmHost {
async fn send(&self, _: &context::Context<'_>, _: &client::SendConfig,
_: Message<'_>, ) -> std::result::Result<Message<'_>, RpcError> {
unimplemented!();
}
}
pub fn console_log(_s: &str) {}
}
}
}
}