borderless-sdk-macros 0.2.1

Core implementation of all macros related to contracts or software-agents.
Documentation
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::quote;
use syn::{
    parse::{Parse, ParseStream},
    Ident, Item, LitBool, Result, Token,
};

use crate::{
    action::{get_actions, match_action, ActionFn},
    schedule::get_schedules,
    state::get_state,
};

pub fn parse_module_content(
    mod_span: Span,
    mod_items: &[Item],
    use_websocket: bool,
) -> Result<TokenStream2> {
    let read_input = quote! {
        let input = read_register(REGISTER_INPUT).context("missing input register")?;
    };

    let state = get_state(mod_items, &mod_span)?;
    let mut actions = get_actions(&state, mod_items)?;

    let schedules = get_schedules(&state, mod_items)?;

    // Since schedules are also treated as actions, just add them to the list of actions.
    // TODO: Duplicate check !
    actions.extend(schedules.iter().map(|s| s.to_action()));

    let schedules = schedules.into_iter().map(|s| s.into_schedule_tokens());

    let action_types = actions.iter().map(ActionFn::gen_type_tokens);

    let call_action: Vec<_> = actions.iter().map(|a| a.gen_call_tokens(&state)).collect(); // <- TODO: This contains logic only for contracts !
    let action_names: Vec<_> = actions.iter().map(ActionFn::method_name).collect();
    let action_ids: Vec<_> = actions.iter().map(ActionFn::method_id).collect();

    // let _args: __ArgsType = ::borderless::serialize::from_value(action.params.clone())?;
    let check_action: Vec<_> = actions
        .iter()
        .map(|a| {
            a.gen_check_tokens(
                quote! { ::borderless::serialize::from_value(action.params.clone())? },
            )
        })
        .collect();

    // let _args: __ArgsType = ::borderless::serialize::from_slice(&payload)?;
    let check_payload: Vec<_> = actions
        .iter()
        .map(|a| a.gen_check_tokens(quote! { ::borderless::serialize::from_slice(&payload)? }))
        .collect();

    let action_symbols = quote! {
        #[doc(hidden)]
        #[automatically_derived]
        const ACTION_SYMBOLS: &[(&str, u32)] = &[
            #(
                (#action_names, #action_ids)
            ),*
        ];
    };

    // let actions_enum = impl_actions_enum(&actions);

    // Generate the nested match block for matching the action method by name or id
    // match &action.method { ... => match method_name => { ... => FUNC } }
    let match_and_call_action = match_action(&action_names, &action_ids, &call_action);
    let match_and_check_action = match_action(&action_names, &action_ids, &check_action);

    let exec_post = quote! {
        #[automatically_derived]
        fn post_action_response(path: String, payload: Vec<u8>) -> Result<CallAction> {
            let path = path.replace("-", "_"); // Convert from kebab-case to snake_case
            let path = path.strip_prefix('/').unwrap_or(&path); // stip leading "/"

            let content = String::from_utf8(payload.clone()).unwrap_or_default();

            #[allow(unreachable_code)]
            match path {
                "" => {
                    let action = CallAction::from_bytes(&payload).context("failed to parse action")?;
                    #match_and_check_action
                    // At this point, the action is validated and can be returned
                    Ok(action)
                }
                #(
                #action_names => {
                    #check_payload
                    let value = ::borderless::serialize::to_value(&_args)?;
                    Ok(CallAction::by_method(#action_names, value))
                }
                )*
                other => Err(new_error!("unknown method: {other}")),
            }
        }
    };

    let as_state = quote! {
        <#state as ::borderless::__private::storage_traits::State>
    };
    let get_symbols = quote! {
        #[automatically_derived]
        pub(crate) fn get_symbols() -> Result<()> {
            let symbols = Symbols::from_symbols(#as_state::symbols(), ACTION_SYMBOLS);
            let bytes = symbols.to_bytes()?;
            write_register(REGISTER_OUTPUT, &bytes);
            Ok(())
        }
    };

    let exec_basic_fns = quote! {
        #[automatically_derived]
        pub(crate) fn exec_action() -> Result<()> {
            #read_input

            let action = CallAction::from_bytes(&input)?;
            let s = action.pretty_print()?;
            let mut state = #as_state::load()?;
            #match_and_call_action
            let events = _match_result?;
            if !events.is_empty() {
                let bytes = events.to_bytes()?;
                write_register(REGISTER_OUTPUT, &bytes);
            }
            #as_state::commit(state);
            Ok(())
        }
        #[automatically_derived]
        pub(crate) fn exec_introduction() -> Result<()> {
            // Parse initial state
            let state = exec_parse_state()?;
            // Commit state
            #as_state::commit(state);
            Ok(())
        }
        #[automatically_derived]
        pub(crate) fn exec_revocation() -> Result<()> {
            #read_input
            let r = Revocation::from_bytes(&input)?;
            Ok(())
        }

        #[automatically_derived]
        pub(crate) fn exec_parse_state() -> Result<#state> {
            #read_input
            // Parse initial state for test
            let initial_state: ::borderless::serialize::Value = ::borderless::serialize::from_slice(&input)?;
            let state = #as_state::init(initial_state)?;
            Ok(state)
        }
    };

    let init_ws = if use_websocket {
        quote! {
            my_init.ws_config = Some(get_ws_config()?);
        }
    } else {
        quote! {}
    };

    let exec_init_shutdown = quote! {
        #[automatically_derived]
        pub(crate) fn exec_init() -> Result<()> {
            let mut my_init = ::borderless::agents::Init {
                schedules: Vec::new(),
                ws_config: None,
            };
            #(
            my_init.schedules.push(#schedules);
            )*
            #init_ws

            // Write output
            let bytes = my_init.to_bytes()?;
            write_register(REGISTER_OUTPUT, &bytes);
            Ok(())
        }

        #[automatically_derived]
        pub(crate) fn exec_shutdown() -> Result<()> {
            Ok(())
        }
    };

    let exec_http = quote! {
        #[automatically_derived]
        pub(crate) fn exec_get_state() -> Result<()> {
            let path = read_string_from_register(REGISTER_INPUT_HTTP_PATH).context("missing http-path")?;
            let result = #as_state::http_get(path)?;
            let status: u16 = if result.is_some() { 200 } else { 404 };
            let payload = result.unwrap_or_default();
            write_register(REGISTER_OUTPUT_HTTP_STATUS, status.to_be_bytes());
            write_string_to_register(REGISTER_OUTPUT_HTTP_RESULT, payload);
            Ok(())
        }
        #[automatically_derived]
        pub(crate) fn exec_post_action() -> Result<()> {
            let path = read_string_from_register(REGISTER_INPUT_HTTP_PATH).context("missing http-path")?;
            let payload = read_register(REGISTER_INPUT_HTTP_PAYLOAD).context("missing http-payload")?;
            // TODO: This can be optimized, as we now parse the action two times, if we use the general route
            // -> Also, we could generate the code for process_action a little bit different, to make this function
            //    reusable here. This would reduce the size of the generated code, as we basically just copy-and-paste
            //    the same function body here.
            // -> Also, we return the action we executed in the http output, and the events in the normal output.
            // I am not super sure, if this is a good design; but I also don't know, what *should* be returned.
            // So for now, we roll with it.
            match post_action_response(path, payload) {
                Ok(action) => {
                    let action_bytes = action.to_bytes()?;
                    let mut state = #as_state::load()?;
                    #match_and_call_action
                    let events = _match_result?;
                    if !events.is_empty() {
                        let bytes = events.to_bytes()?;
                        write_register(REGISTER_OUTPUT, &bytes);
                    }
                    #as_state::commit(state);
                    write_register(REGISTER_OUTPUT_HTTP_STATUS, 200u16.to_be_bytes());
                    write_register(REGISTER_OUTPUT_HTTP_RESULT, action_bytes);
                }
                Err(e) => {
                    write_register(REGISTER_OUTPUT_HTTP_STATUS, 400u16.to_be_bytes());
                    write_string_to_register(REGISTER_OUTPUT_HTTP_RESULT, e.to_string());
                }
            };
            Ok(())
        }
    };

    let as_ws_handler = quote! {
        <#state as ::borderless::agents::WebsocketHandler>
    };

    let exec_ws = if use_websocket {
        quote! {
            #[automatically_derived]
            pub(crate) fn get_ws_config() -> Result<::borderless::agents::WsConfig> {
                // Load state
                let state = #as_state::load()?;
                let ws_config = #as_ws_handler::open_ws(&state);
                Ok(ws_config)
            }

            #[automatically_derived]
            pub(crate) fn on_ws_open() -> Result<()> {
                // Load state
                let mut state = #as_state::load()?;
                let action_output = #as_ws_handler::on_open(&mut state).map_err(::borderless::Error::msg)?.unwrap_or_default();
                let events = action_output.convert_out_events()?;
                if !events.is_empty() {
                    let bytes = events.to_bytes()?;
                    write_register(REGISTER_OUTPUT, &bytes);
                }
                // Commit state
                #as_state::commit(state);
                Ok(())
            }

            #[automatically_derived]
            pub(crate) fn on_ws_msg() -> Result<()> {
                #read_input

                // Load state
                let mut state = #as_state::load()?;
                let action_output = #as_ws_handler::on_message(&mut state, input).map_err(::borderless::Error::msg)?.unwrap_or_default();
                let events = action_output.convert_out_events()?;
                if !events.is_empty() {
                    let bytes = events.to_bytes()?;
                    write_register(REGISTER_OUTPUT, &bytes);
                }
                // Commit state
                #as_state::commit(state);
                Ok(())
            }

            #[automatically_derived]
            pub(crate) fn on_ws_close() -> Result<()> {
                // Load state
                let mut state = #as_state::load()?;
                let action_output = #as_ws_handler::on_close(&mut state).map_err(::borderless::Error::msg)?.unwrap_or_default();
                let events = action_output.convert_out_events()?;
                if !events.is_empty() {
                    let bytes = events.to_bytes()?;
                    write_register(REGISTER_OUTPUT, &bytes);
                }
                // Commit state
                #as_state::commit(state);
                Ok(())
            }

            #[automatically_derived]
            pub(crate) fn on_ws_error() -> Result<()> {
                // Load state
                let mut state = #as_state::load()?;
                let action_output = #as_ws_handler::on_error(&mut state).map_err(::borderless::Error::msg)?.unwrap_or_default();
                let events = action_output.convert_out_events()?;
                if !events.is_empty() {
                    let bytes = events.to_bytes()?;
                    write_register(REGISTER_OUTPUT, &bytes);
                }
                // Commit state
                #as_state::commit(state);
                Ok(())
            }
        }
    } else {
        quote! {}
    };

    // Combine everything in the __derived module:
    let derived = quote! {
        #[doc(hidden)]
        #[automatically_derived]
        pub(super) mod __derived {
            use super::*;
            use ::borderless::prelude::*;
            use ::borderless::__private::{
                read_field, read_register, read_string_from_register, registers::*,
                storage_keys::make_user_key, write_field, write_register, write_string_to_register,
            };
            #action_symbols
            #(#action_types)*
            #exec_post
            #get_symbols
            #exec_basic_fns
            #exec_init_shutdown
            #exec_http
            #exec_ws
        }
    };
    /*
    * // DEPRECATED - we do not require the actions enum anymore
       pub(super) mod actions {
           use super::*;
           use super::__derived::*;
           #actions_enum
       }
    * */
    Ok(derived)
}

pub fn generate_ws_wasm_exports(mod_ident: &Ident) -> TokenStream2 {
    let derived = quote! { #mod_ident::__derived };

    quote! {
    #[no_mangle]
    pub extern "C" fn on_ws_open() {
        let result = #derived::on_ws_open();
        match result {
            Ok(()) => ::borderless::debug!("on_ws_open: success."),
            Err(e) => ::borderless::error!("on_ws_open execution failed: {e:?}"),
        }
    }

    #[no_mangle]
    pub extern "C" fn on_ws_msg() {
        let result = #derived::on_ws_msg();
        match result {
            Ok(()) => ::borderless::debug!("on_ws_msg: success."),
            Err(e) => ::borderless::error!("on_ws_msg execution failed: {e:?}"),
        }
    }

    #[no_mangle]
    pub extern "C" fn on_ws_error() {
        let result = #derived::on_ws_error();
        match result {
            Ok(()) => ::borderless::debug!("on_ws_error: success."),
            Err(e) => ::borderless::error!("on_ws_error execution failed: {e:?}"),
        }
    }

    #[no_mangle]
    pub extern "C" fn on_ws_close() {
        let result = #derived::on_ws_close();
        match result {
            Ok(()) => ::borderless::debug!("on_ws_close: success."),
            Err(e) => ::borderless::error!("on_ws_close execution failed: {e:?}"),
        }
    }
    }
}

pub fn generate_wasm_exports(mod_ident: &Ident) -> TokenStream2 {
    let derived = quote! { #mod_ident::__derived };

    quote! {
    #[no_mangle]
    #[automatically_derived]
    pub extern "C" fn on_init() {
        let result = #derived::exec_init();
        match result {
            Ok(()) => ::borderless::debug!("initialization: success"),
            Err(e) => ::borderless::error!("initialization failed: {e:?}"),
        }
    }

    #[no_mangle]
    #[automatically_derived]
    pub extern "C" fn on_shutdown() {
        let result = #derived::exec_shutdown();
        match result {
            Ok(()) => ::borderless::debug!("shutdown: success"),
            Err(e) => ::borderless::error!("shutdown failed: {e:?}"),
        }
    }

    #[no_mangle]
    #[automatically_derived]
    pub extern "C" fn process_action() {
        let result = #derived::exec_action();
        match result {
            Ok(()) => ::borderless::debug!("process-action: success"),
            Err(e) => ::borderless::error!("process-action - execution failed: {e:?}"),
        }
    }

    #[no_mangle]
    #[automatically_derived]
    pub extern "C" fn process_introduction() {
        let result = #derived::exec_introduction();
        match result {
            Ok(()) => ::borderless::debug!("process-introduction: success"),
            Err(e) => {
                ::borderless::error!("process-introduction - execution failed: {e:?}");
                ::borderless::__private::abort();
            }
        }
    }

    #[no_mangle]
    #[automatically_derived]
    pub extern "C" fn process_revocation() {
        let result = #derived::exec_revocation();
        match result {
            Ok(()) => ::borderless::debug!("process-revocation: success"),
            Err(e) => {
                ::borderless::error!("process-revocation - execution failed: {e:?}");
                ::borderless::__private::abort();
            }
        }
    }

    #[no_mangle]
    #[automatically_derived]
    pub extern "C" fn parse_state()  {
        let result = #derived::exec_parse_state();
        match result {
            Ok(_) => {
                ::borderless::info!("Parsing state: success");
            }
            Err(e) => {
                ::borderless::error!("Parsing state failed: {e:?}");
                ::borderless::__private::abort();
            }
        }
    }

    #[no_mangle]
    #[automatically_derived]
    pub extern "C" fn http_get_state() {
        let result = #derived::exec_get_state();
        if let Err(e) = result {
            ::borderless::error!("http-get failed: {e:?}");
        }
    }

    #[no_mangle]
    #[automatically_derived]
    pub extern "C" fn http_post_action() {
        let result = #derived::exec_post_action();
        if let Err(e) = result {
            ::borderless::error!("http-post failed: {e:?}");
        }
    }

    #[no_mangle]
    #[automatically_derived]
    pub extern "C" fn get_symbols() {
        let result = #derived::get_symbols();
        if let Err(e) = result {
            ::borderless::error!("get-symbols failed: {e:?}");
        }
    }
    }
}
#[derive(Debug, Default)]
pub struct AgentArgs {
    pub websocket: Option<bool>, // None if not specified, Some(true) or Some(false) if specified
}

impl Parse for AgentArgs {
    fn parse(input: ParseStream) -> Result<Self> {
        let mut args = AgentArgs::default();

        while !input.is_empty() {
            // If we find the 'websocket' argument
            if input.peek(Ident) && input.peek2(Token![=]) {
                let ident: Ident = input.parse()?; // Parse the 'websocket'
                if ident != "websocket" {
                    return Err(input.error("Expected 'websocket' argument"));
                }
                let _eq_token: Token![=] = input.parse()?; // Parse the '=' token
                let value: LitBool = input.parse()?; // Parse the boolean value (true or false)
                args.websocket = Some(value.value()); // Set the value for 'websocket'
            } else {
                break; // If we encounter anything else, stop parsing
            }
        }

        Ok(args)
    }
}