app_bridge 3.0.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,21 +4,19 @@ use std::collections::HashMap;
4
4
  use wasmtime::Store;
5
5
 
6
6
  use crate::app_state::AppState;
7
- use crate::component::standout::app::types::{
8
- AppError, ErrorCode, TriggerContext, TriggerResponse, ActionContext, ActionResponse
9
- };
10
- use crate::component::{app, build_engine, build_linker, build_store, Bridge};
7
+ use crate::component::{app, build_engine, build_linker, build_store, BridgeWrapper};
8
+ use crate::types::{ActionContext, ActionResponse, AppError, ErrorCode, TriggerContext, TriggerResponse};
11
9
  use super::{
12
- trigger_context::RTriggerContext,
13
- trigger_response::RTriggerResponse,
14
10
  action_context::RActionContext,
15
11
  action_response::RActionResponse,
12
+ trigger_context::RTriggerContext,
13
+ trigger_response::RTriggerResponse,
16
14
  };
17
15
 
18
16
  #[derive(Default)]
19
17
  pub struct RApp {
20
18
  component_path: String,
21
- instance: RefCell<Option<Bridge>>,
19
+ instance: RefCell<Option<BridgeWrapper>>,
22
20
  store: RefCell<Option<Store<AppState>>>,
23
21
  }
24
22
 
@@ -27,13 +25,28 @@ pub struct RApp {
27
25
  pub struct MutRApp(RefCell<RApp>);
28
26
 
29
27
  impl MutRApp {
28
+ /// Returns the WIT version this component was built against (e.g., "3.0.0", "4.0.0")
29
+ pub fn wit_version(&self) -> Result<String, Error> {
30
+ let binding = self.0.borrow();
31
+ let instance = binding.instance.borrow();
32
+
33
+ if let Some(instance) = &*instance {
34
+ Ok(instance.wit_version().to_string())
35
+ } else {
36
+ Err(Error::new(
37
+ magnus::exception::runtime_error(),
38
+ "App not initialized",
39
+ ))
40
+ }
41
+ }
42
+
30
43
  pub fn initialize(&self, component_path: String, env_vars: HashMap<String, String>) -> Result<(), Error> {
31
44
  let mut this = self.0.borrow_mut();
32
45
  let engine = build_engine();
33
46
  let linker = build_linker(&engine).map_err(|e| {
34
47
  Error::new(
35
48
  magnus::exception::runtime_error(),
36
- format!("Failed to build linker: {}", e)
49
+ format!("Failed to build linker: {}", e),
37
50
  )
38
51
  })?;
39
52
  let mut store = if env_vars.is_empty() {
@@ -44,19 +57,16 @@ impl MutRApp {
44
57
 
45
58
  let app = app(component_path.clone(), engine, &mut store, linker).map_err(|e| {
46
59
  if e.to_string().contains("Incompatible WASM file version") {
47
- Error::new(
48
- magnus::exception::runtime_error(),
49
- e.to_string()
50
- )
60
+ Error::new(magnus::exception::runtime_error(), e.to_string())
51
61
  } else {
52
62
  Error::new(
53
63
  magnus::exception::runtime_error(),
54
- format!("Failed to initialize app: {}", e)
64
+ format!("Failed to initialize app: {}", e),
55
65
  )
56
66
  }
57
67
  })?;
58
68
 
59
- this.component_path = component_path.to_string();
69
+ this.component_path = component_path;
60
70
  *this.instance.borrow_mut() = Some(app);
61
71
  *this.store.borrow_mut() = Some(store);
62
72
 
@@ -65,102 +75,87 @@ impl MutRApp {
65
75
 
66
76
  pub fn trigger_ids(&self) -> Result<Vec<String>, Error> {
67
77
  let binding = self.0.borrow();
68
-
69
78
  let mut instance = binding.instance.borrow_mut();
70
79
  let mut store = binding.store.borrow_mut();
71
80
 
72
81
  if let (Some(instance), Some(store)) = (&mut *instance, &mut *store) {
73
- match instance.standout_app_triggers().call_trigger_ids(store) {
74
- Ok(result) => {
75
- match result {
76
- Ok(ids) => Ok(ids),
77
- Err(err) => Err(err.into())
78
- }
79
- },
82
+ match instance.call_trigger_ids(store) {
83
+ Ok(result) => result.map_err(Into::into),
80
84
  Err(err) => {
81
- if let Some(wit_err) = err.downcast_ref::<AppError>() {
82
- Err(wit_err.clone().into())
83
- } else {
84
- Err(Error::new(
85
- magnus::exception::runtime_error(),
86
- format!("Unexpected error: {:?}", err)
87
- ))
88
- }
89
- },
85
+ if let Some(wit_err) = err.downcast_ref::<AppError>() {
86
+ Err(wit_err.clone().into())
87
+ } else {
88
+ Err(Error::new(
89
+ magnus::exception::runtime_error(),
90
+ format!("Unexpected error: {:?}", err),
91
+ ))
92
+ }
93
+ }
90
94
  }
91
95
  } else {
92
96
  Err(AppError {
93
97
  code: ErrorCode::InternalError,
94
98
  message: "App instance couldn't be initialized".to_string(),
95
- }.into())
99
+ }
100
+ .into())
96
101
  }
97
102
  }
98
103
 
99
104
  pub fn trigger_input_schema(&self, context: RTriggerContext) -> Result<String, Error> {
100
105
  let binding = self.0.borrow();
101
-
102
106
  let mut instance = binding.instance.borrow_mut();
103
107
  let mut store = binding.store.borrow_mut();
104
108
 
105
109
  if let (Some(instance), Some(store)) = (&mut *instance, &mut *store) {
106
110
  let context_ctx = context.into();
107
- match instance.standout_app_triggers().call_input_schema(store, &context_ctx) {
108
- Ok(result) => {
109
- match result {
110
- Ok(schema) => Ok(schema),
111
- Err(err) => Err(err.into())
112
- }
113
- },
111
+ match instance.call_trigger_input_schema(store, &context_ctx) {
112
+ Ok(result) => result.map_err(Into::into),
114
113
  Err(err) => {
115
- if let Some(wit_err) = err.downcast_ref::<AppError>() {
116
- Err(wit_err.clone().into())
117
- } else {
118
- Err(Error::new(
119
- magnus::exception::runtime_error(),
120
- format!("Unexpected error: {:?}", err)
121
- ))
122
- }
123
- },
114
+ if let Some(wit_err) = err.downcast_ref::<AppError>() {
115
+ Err(wit_err.clone().into())
116
+ } else {
117
+ Err(Error::new(
118
+ magnus::exception::runtime_error(),
119
+ format!("Unexpected error: {:?}", err),
120
+ ))
121
+ }
122
+ }
124
123
  }
125
124
  } else {
126
125
  Err(AppError {
127
126
  code: ErrorCode::InternalError,
128
127
  message: "App instance couldn't be initialized".to_string(),
129
- }.into())
128
+ }
129
+ .into())
130
130
  }
131
131
  }
132
132
 
133
133
  pub fn trigger_output_schema(&self, context: RTriggerContext) -> Result<String, Error> {
134
134
  let binding = self.0.borrow();
135
-
136
135
  let mut instance = binding.instance.borrow_mut();
137
136
  let mut store = binding.store.borrow_mut();
138
137
 
139
138
  if let (Some(instance), Some(store)) = (&mut *instance, &mut *store) {
140
139
  let context_ctx = context.into();
141
- match instance.standout_app_triggers().call_output_schema(store, &context_ctx) {
142
- Ok(result) => {
143
- match result {
144
- Ok(schema) => Ok(schema),
145
- Err(err) => Err(err.into())
146
- }
147
- },
140
+ match instance.call_trigger_output_schema(store, &context_ctx) {
141
+ Ok(result) => result.map_err(Into::into),
148
142
  Err(err) => {
149
- if let Some(wit_err) = err.downcast_ref::<AppError>() {
150
- Err(wit_err.clone().into())
151
- } else {
152
- Err(Error::new(
153
- magnus::exception::runtime_error(),
154
- format!("Unexpected error: {:?}", err)
155
- ))
156
- }
157
- },
143
+ if let Some(wit_err) = err.downcast_ref::<AppError>() {
144
+ Err(wit_err.clone().into())
145
+ } else {
146
+ Err(Error::new(
147
+ magnus::exception::runtime_error(),
148
+ format!("Unexpected error: {:?}", err),
149
+ ))
150
+ }
151
+ }
158
152
  }
159
153
  } else {
160
154
  Err(AppError {
161
155
  code: ErrorCode::InternalError,
162
156
  message: "App instance couldn't be initialized".to_string(),
163
- }.into())
157
+ }
158
+ .into())
164
159
  }
165
160
  }
166
161
 
@@ -169,30 +164,22 @@ impl MutRApp {
169
164
  let response = self.fetch_events(context.into());
170
165
 
171
166
  match response {
172
- Ok(response) => Ok(response.into()),
173
- Err(err) => Err(err.into())
167
+ Ok(response) => Ok(response.into()),
168
+ Err(err) => Err(err.into()),
174
169
  }
175
170
  }
176
171
 
177
172
  fn fetch_events(&self, context: TriggerContext) -> Result<TriggerResponse, AppError> {
178
173
  let binding = self.0.borrow();
179
-
180
174
  let mut instance = binding.instance.borrow_mut();
181
175
  let mut store = binding.store.borrow_mut();
182
176
 
183
177
  if let (Some(instance), Some(store)) = (&mut *instance, &mut *store) {
184
- match instance
185
- .standout_app_triggers()
186
- .call_fetch_events(store, &context) {
187
- Ok(response) => {
188
- match response {
189
- Ok(res) => Ok(res),
190
- Err(err) => Err(err),
191
- }
192
- },
178
+ match instance.call_fetch_events(store, &context) {
179
+ Ok(response) => response,
193
180
  Err(err) => {
194
181
  if let Some(wit_err) = err.downcast_ref::<AppError>() {
195
- Err(AppError::from(wit_err.clone()))
182
+ Err(wit_err.clone())
196
183
  } else {
197
184
  Err(AppError {
198
185
  code: ErrorCode::InternalError,
@@ -211,102 +198,87 @@ impl MutRApp {
211
198
 
212
199
  pub fn action_ids(&self) -> Result<Vec<String>, Error> {
213
200
  let binding = self.0.borrow();
214
-
215
201
  let mut instance = binding.instance.borrow_mut();
216
202
  let mut store = binding.store.borrow_mut();
217
203
 
218
204
  if let (Some(instance), Some(store)) = (&mut *instance, &mut *store) {
219
- match instance.standout_app_actions().call_action_ids(store) {
220
- Ok(result) => {
221
- match result {
222
- Ok(ids) => Ok(ids),
223
- Err(err) => Err(err.into())
224
- }
225
- },
205
+ match instance.call_action_ids(store) {
206
+ Ok(result) => result.map_err(Into::into),
226
207
  Err(err) => {
227
- if let Some(wit_err) = err.downcast_ref::<AppError>() {
228
- Err(wit_err.clone().into())
229
- } else {
230
- Err(Error::new(
231
- magnus::exception::runtime_error(),
232
- format!("Unexpected error: {:?}", err)
233
- ))
234
- }
235
- },
208
+ if let Some(wit_err) = err.downcast_ref::<AppError>() {
209
+ Err(wit_err.clone().into())
210
+ } else {
211
+ Err(Error::new(
212
+ magnus::exception::runtime_error(),
213
+ format!("Unexpected error: {:?}", err),
214
+ ))
215
+ }
216
+ }
236
217
  }
237
218
  } else {
238
219
  Err(AppError {
239
220
  code: ErrorCode::InternalError,
240
221
  message: "App instance couldn't be initialized".to_string(),
241
- }.into())
222
+ }
223
+ .into())
242
224
  }
243
225
  }
244
226
 
245
227
  pub fn action_input_schema(&self, context: RActionContext) -> Result<String, Error> {
246
228
  let binding = self.0.borrow();
247
-
248
229
  let mut instance = binding.instance.borrow_mut();
249
230
  let mut store = binding.store.borrow_mut();
250
231
 
251
232
  if let (Some(instance), Some(store)) = (&mut *instance, &mut *store) {
252
233
  let context_ctx = context.into();
253
- match instance.standout_app_actions().call_input_schema(store, &context_ctx) {
254
- Ok(result) => {
255
- match result {
256
- Ok(schema) => Ok(schema),
257
- Err(err) => Err(err.into())
258
- }
259
- },
234
+ match instance.call_action_input_schema(store, &context_ctx) {
235
+ Ok(result) => result.map_err(Into::into),
260
236
  Err(err) => {
261
- if let Some(wit_err) = err.downcast_ref::<AppError>() {
262
- Err(wit_err.clone().into())
263
- } else {
264
- Err(Error::new(
265
- magnus::exception::runtime_error(),
266
- format!("Unexpected error: {:?}", err)
267
- ))
268
- }
269
- },
237
+ if let Some(wit_err) = err.downcast_ref::<AppError>() {
238
+ Err(wit_err.clone().into())
239
+ } else {
240
+ Err(Error::new(
241
+ magnus::exception::runtime_error(),
242
+ format!("Unexpected error: {:?}", err),
243
+ ))
244
+ }
245
+ }
270
246
  }
271
247
  } else {
272
248
  Err(AppError {
273
249
  code: ErrorCode::InternalError,
274
250
  message: "App instance couldn't be initialized".to_string(),
275
- }.into())
251
+ }
252
+ .into())
276
253
  }
277
254
  }
278
255
 
279
256
  pub fn action_output_schema(&self, context: RActionContext) -> Result<String, Error> {
280
257
  let binding = self.0.borrow();
281
-
282
258
  let mut instance = binding.instance.borrow_mut();
283
259
  let mut store = binding.store.borrow_mut();
284
260
 
285
261
  if let (Some(instance), Some(store)) = (&mut *instance, &mut *store) {
286
262
  let context_ctx = context.into();
287
- match instance.standout_app_actions().call_output_schema(store, &context_ctx) {
288
- Ok(result) => {
289
- match result {
290
- Ok(schema) => Ok(schema),
291
- Err(err) => Err(err.into())
292
- }
293
- },
263
+ match instance.call_action_output_schema(store, &context_ctx) {
264
+ Ok(result) => result.map_err(Into::into),
294
265
  Err(err) => {
295
- if let Some(wit_err) = err.downcast_ref::<AppError>() {
296
- Err(wit_err.clone().into())
297
- } else {
298
- Err(Error::new(
299
- magnus::exception::runtime_error(),
300
- format!("Unexpected error: {:?}", err)
301
- ))
302
- }
303
- },
266
+ if let Some(wit_err) = err.downcast_ref::<AppError>() {
267
+ Err(wit_err.clone().into())
268
+ } else {
269
+ Err(Error::new(
270
+ magnus::exception::runtime_error(),
271
+ format!("Unexpected error: {:?}", err),
272
+ ))
273
+ }
274
+ }
304
275
  }
305
276
  } else {
306
277
  Err(AppError {
307
278
  code: ErrorCode::InternalError,
308
279
  message: "App instance couldn't be initialized".to_string(),
309
- }.into())
280
+ }
281
+ .into())
310
282
  }
311
283
  }
312
284
 
@@ -315,30 +287,22 @@ impl MutRApp {
315
287
  let response = self.execute_action(context.into());
316
288
 
317
289
  match response {
318
- Ok(response) => Ok(response.into()),
319
- Err(err) => Err(err.into())
290
+ Ok(response) => Ok(response.into()),
291
+ Err(err) => Err(err.into()),
320
292
  }
321
293
  }
322
294
 
323
295
  fn execute_action(&self, context: ActionContext) -> Result<ActionResponse, AppError> {
324
296
  let binding = self.0.borrow();
325
-
326
297
  let mut instance = binding.instance.borrow_mut();
327
298
  let mut store = binding.store.borrow_mut();
328
299
 
329
300
  if let (Some(instance), Some(store)) = (&mut *instance, &mut *store) {
330
- match instance
331
- .standout_app_actions()
332
- .call_execute(store, &context) {
333
- Ok(response) => {
334
- match response {
335
- Ok(res) => Ok(res),
336
- Err(err) => Err(err),
337
- }
338
- },
301
+ match instance.call_execute(store, &context) {
302
+ Ok(response) => response,
339
303
  Err(err) => {
340
304
  if let Some(wit_err) = err.downcast_ref::<AppError>() {
341
- Err(AppError::from(wit_err.clone()))
305
+ Err(wit_err.clone())
342
306
  } else {
343
307
  Err(AppError {
344
308
  code: ErrorCode::InternalError,
@@ -1,5 +1,5 @@
1
1
  use magnus::{prelude::*, Error, TryConvert, Value};
2
- use crate::component::standout::app::types::Connection;
2
+ use crate::types::Connection;
3
3
 
4
4
  #[magnus::wrap(class = "AppBridge::Connection")]
5
5
  pub struct RConnection {
@@ -9,9 +9,9 @@ pub struct RConnection {
9
9
  impl RConnection {
10
10
  pub fn new(id: String, name: String, serialized_data: String) -> Self {
11
11
  let inner = Connection {
12
- id: id,
13
- name: name,
14
- serialized_data: serialized_data,
12
+ id,
13
+ name,
14
+ serialized_data,
15
15
  };
16
16
  Self { inner }
17
17
  }
@@ -1,5 +1,5 @@
1
1
  use magnus::{prelude::*, Error, TryConvert, Value};
2
- use crate::component::standout::app::types::TriggerContext;
2
+ use crate::types::TriggerContext;
3
3
  use super::connection::RConnection;
4
4
 
5
5
  #[magnus::wrap(class = "AppBridge::TriggerContext")]
@@ -20,10 +20,10 @@ impl RTriggerContext {
20
20
  };
21
21
 
22
22
  let inner = TriggerContext {
23
- trigger_id: trigger_id,
23
+ trigger_id,
24
24
  connection: wrapped_connection.clone().into(),
25
- store: store,
26
- serialized_input: serialized_input,
25
+ store,
26
+ serialized_input,
27
27
  };
28
28
  Ok(Self {
29
29
  inner,
@@ -65,10 +65,10 @@ impl TryConvert for RTriggerContext {
65
65
  };
66
66
 
67
67
  let inner = TriggerContext {
68
- trigger_id: trigger_id,
68
+ trigger_id,
69
69
  connection: wrapped_connection.clone().inner,
70
- store: store,
71
- serialized_input: serialized_input,
70
+ store,
71
+ serialized_input,
72
72
  };
73
73
 
74
74
  Ok(Self {
@@ -1,5 +1,5 @@
1
1
  use magnus::{prelude::*, Error, TryConvert, Value};
2
- use crate::component::standout::app::types::TriggerEvent;
2
+ use crate::types::TriggerEvent;
3
3
 
4
4
  #[magnus::wrap(class = "AppBridge::TriggerEvent")]
5
5
  pub struct RTriggerEvent {
@@ -8,9 +8,9 @@ pub struct RTriggerEvent {
8
8
 
9
9
  impl RTriggerEvent {
10
10
  pub fn new(id: String, serialized_data: String) -> Self {
11
- let inner: TriggerEvent = TriggerEvent {
12
- id: id,
13
- serialized_data: serialized_data,
11
+ let inner = TriggerEvent {
12
+ id,
13
+ serialized_data,
14
14
  };
15
15
  Self { inner }
16
16
  }
@@ -1,42 +1,43 @@
1
- use magnus::{Error, RArray, TryConvert};
2
- use crate::component::standout::app::types::TriggerResponse;
1
+ use magnus::{Error, Ruby, TryConvert};
2
+ use crate::types::TriggerResponse;
3
3
  use super::trigger_event::RTriggerEvent;
4
4
 
5
5
  #[magnus::wrap(class = "AppBridge::TriggerResponse")]
6
6
  pub struct RTriggerResponse {
7
- inner: TriggerResponse,
7
+ inner: TriggerResponse,
8
8
  }
9
9
 
10
10
  impl RTriggerResponse {
11
- pub fn new(store: String, events: RArray) -> Self {
12
- let iter = events.into_iter();
13
- let res: Vec<RTriggerEvent> = iter
14
- .map(&TryConvert::try_convert)
15
- .collect::<Result<Vec<RTriggerEvent>, Error>>()
16
- .unwrap();
11
+ pub fn new(store: String, events: magnus::RArray) -> Self {
12
+ let iter = events.into_iter();
13
+ let res: Vec<RTriggerEvent> = iter
14
+ .map(&TryConvert::try_convert)
15
+ .collect::<Result<Vec<RTriggerEvent>, Error>>()
16
+ .unwrap();
17
17
 
18
- let inner = TriggerResponse {
19
- store: store,
20
- events: res.iter().map(|e| e.into()).collect(),
21
- };
22
- Self { inner }
23
- }
18
+ let inner = TriggerResponse {
19
+ store,
20
+ events: res.iter().map(|e| e.into()).collect(),
21
+ };
22
+ Self { inner }
23
+ }
24
24
 
25
- pub fn store(&self) -> String {
26
- self.inner.store.clone()
27
- }
25
+ pub fn store(&self) -> String {
26
+ self.inner.store.clone()
27
+ }
28
28
 
29
- pub fn events(&self) -> RArray {
30
- self.inner
31
- .events
32
- .iter()
33
- .map(|e| RTriggerEvent::from(e))
34
- .collect()
35
- }
29
+ pub fn events(&self) -> magnus::RArray {
30
+ let ruby = Ruby::get().unwrap();
31
+ let array = ruby.ary_new();
32
+ for e in &self.inner.events {
33
+ let _ = array.push(RTriggerEvent::from(e));
34
+ }
35
+ array
36
+ }
36
37
  }
37
38
 
38
39
  impl From<TriggerResponse> for RTriggerResponse {
39
- fn from(value: TriggerResponse) -> Self {
40
- Self { inner: value }
41
- }
40
+ fn from(value: TriggerResponse) -> Self {
41
+ Self { inner: value }
42
+ }
42
43
  }
@@ -275,9 +275,12 @@ interface http {
275
275
  }
276
276
  }
277
277
 
278
+ // Note: v3 does NOT include the file interface
279
+
278
280
  world bridge {
279
281
  import http;
280
282
  import environment;
283
+ // Note: file interface is NOT available in v3
281
284
  export triggers;
282
285
  export actions;
283
286
  }