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
517
518
519
use convert_case::{Case, Casing};
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{format_ident, quote};
use syn::parse::{Parse, ParseStream};
use syn::spanned::Spanned;
use syn::{
    Error, FnArg, ImplItem, ItemImpl, LitBool, LitStr, Meta, Pat, PatIdent, Result, ReturnType,
    Token, Type,
};
use syn::{Ident, Item};
use xxhash_rust::const_xxh3::xxh3_64;

use crate::utils::check_if_action;

/// Helper struct to bundle all necessary information for our action-functions
pub struct ActionFn {
    /// Ident (name) of the action
    pub ident: Ident,
    /// Associated method-id - either calculated or overriden by the user
    pub method_id: u32,
    /// Indicates that the method-name does not match the function name
    pub name_override: Option<String>,
    /// Weather or not the action should be exposed via api
    pub web_api: bool,
    /// Roles, that are allowed to call the function. Empty means no restrictions.
    pub roles: Vec<String>,
    /// Weather or not the function requires &mut self
    pub mut_self: bool,
    /// Return type of the function
    pub output: ReturnType,
    /// Arguments of the function
    pub args: Vec<(Ident, Box<Type>)>,
    pub _span: Span,
}

impl ActionFn {
    /// Generates an enum field for the Actions-Enum
    ///
    /// An action like `fn set_switch(&mut self, switch: bool)` would become `SetSwitch { switch: bool }`
    #[allow(unused)]
    pub fn gen_field(&self) -> TokenStream2 {
        let fields = self.args.iter().map(|a| a.0.clone());
        let types = self.args.iter().map(|a| a.1.clone());
        let ident = self.field_ident();
        quote! {
            #ident { #( #fields: #types ),* }
        }
    }

    /// Generates the match block for the Actions-Enum
    ///
    /// An action like `fn set_switch(&mut self, switch: bool)` would become:
    /// ```no_compile
    /// Actions::SetSwitch { switch } => {
    ///     let args = SetSwitchArgs { switch };
    ///     let args_value = to_value(&args);
    ///     CallAction::by_method("set_switch", args_value)
    /// }
    /// ```
    #[allow(unused)]
    pub fn gen_field_match(&self) -> TokenStream2 {
        let fields: Vec<_> = self.args.iter().map(|a| a.0.clone()).collect();
        let field_ident = self.field_ident();
        let method_name = self.method_name();
        let args_ident = self.args_ident(); // NOTE: This requires use super::__derived::*;
        quote! {
            Actions::#field_ident { #(#fields),* } => {
                let args = #args_ident { #(#fields),* };
                let args_value = ::borderless::serialize::to_value(&args)?;
                ::borderless::events::CallAction::by_method(#method_name, args_value)
            }
        }
    }

    /// Generates the arguments struct from the action
    ///
    /// An action like `fn set_switch(&mut self, switch: bool)` would generate `pub(crate) __SetSwitchArgs { switch: bool }`
    pub fn gen_type_tokens(&self) -> TokenStream2 {
        let args_ident = self.args_ident();
        let fields = self.args.iter().map(|a| a.0.clone());
        let types = self.args.iter().map(|a| a.1.clone());
        quote! {
            #[doc(hidden)]
            #[automatically_derived]
            #[derive(serde::Serialize, serde::Deserialize)]
            pub(crate) struct #args_ident {
                #(
                    pub(crate) #fields: #types
                ),*
            }
        }
    }

    /// Generates the parsing of the function arguments + calling of the associated state function.
    ///
    /// References 'action' and 'state' in generated tokens
    pub fn gen_call_tokens(&self, state_ident: &Ident) -> TokenStream2 {
        let args_ident = self.args_ident();
        let fn_ident = &self.ident;
        let return_type = match &self.output {
            ReturnType::Default => quote! { () },
            ReturnType::Type(_, ty) => quote! { #ty },
        };
        let mut_state = if self.mut_self {
            quote! { &mut state }
        } else {
            quote! { &state }
        };
        let access_check = self.writer_access();
        if self.args.is_empty() {
            quote! {
                #access_check
                let result = #state_ident::#fn_ident(#mut_state);
                <#return_type as ::borderless::events::ActionOutput>::convert_out_events(result)
            }
        } else {
            let arg_idents = self.args.iter().map(|a| a.0.clone());
            quote! {
                #access_check
                let args: __derived::#args_ident = ::borderless::serialize::from_value(action.params)?;
                let result = #state_ident::#fn_ident(#mut_state, #(args.#arg_idents),*);
                <#return_type as ::borderless::events::ActionOutput>::convert_out_events(result)
            }
        }
    }

    /// Generates the check, to parse the args from action.params (used in the http-post function)
    ///
    /// References 'action' in generated tokens
    pub fn gen_check_tokens(&self, value: TokenStream2) -> TokenStream2 {
        let args_ident = self.args_ident();
        let access_check = self.writer_access();
        if self.web_api {
            quote! {
                #access_check
                let _args: __derived::#args_ident = #value;
            }
        } else {
            let err_msg = format!("action '{}' cannot be called via web-api", self.ident);
            quote! {
                return Err(new_error!(#err_msg));
                let _args: __derived::#args_ident = #value;
            }
        }
    }

    /// Returns the method name
    ///
    /// This is either the name of the function, or the user-defined method-name
    /// that was passed to the action macro.
    pub fn method_name(&self) -> String {
        if let Some(rename) = &self.name_override {
            rename.clone()
        } else {
            self.ident.to_string()
        }
    }

    pub fn method_id(&self) -> u32 {
        self.method_id
    }

    fn writer_access(&self) -> TokenStream2 {
        let roles = self.roles.iter();
        if self.roles.is_empty() {
            quote! { /* no access restriction */ }
        } else {
            let fn_name = self.ident.to_string();
            quote! {
                let writer_roles = ::borderless::contracts::env::writer_roles();
                if !writer_roles.iter().any(|role| #( role.eq_ignore_ascii_case(#roles) )||* ) {
                    let writer = ::borderless::contracts::env::writer();
                    return Err(new_error!("writer {} has no access to action '{}'", writer, #fn_name));
                }
            }
        }
    }

    /// Returns the identifier for the generated arguments struct of the action
    ///
    /// An action like `fn set_switch(&mut self, switch: bool)` would generate `__SetSwitchArgs { switch: bool }`
    fn args_ident(&self) -> Ident {
        format_ident!("__{}Args", self.ident.to_string().to_case(Case::Pascal))
    }

    /// Returns the identifier for the enum fields
    ///
    /// Converts from snake_case to PascalCase
    #[allow(unused)]
    fn field_ident(&self) -> Ident {
        format_ident!("{}", self.ident.to_string().to_case(Case::Pascal))
    }
}

/// Returns a list of parsed `ActionFn`
///
/// Takes the identifier of the `State` and the list of module Items as input.
pub fn get_actions(state_ident: &Ident, mod_items: &[Item]) -> Result<Vec<ActionFn>> {
    // First, get the impl-block of our state:
    for item in mod_items {
        // Filter out everything irrelevant
        let item_impl = match item {
            Item::Impl(i) => i,
            _ => continue,
        };
        let type_path = match item_impl.self_ty.as_ref() {
            Type::Path(p) => p,
            _ => continue,
        };
        match type_path.path.segments.last() {
            Some(last_segment) => {
                if last_segment.ident != *state_ident {
                    continue;
                }
            }
            _ => continue,
        }
        // Then extract the actions from it
        return get_actions_from_impl(state_ident, item_impl);
    }
    Err(Error::new_spanned(
        state_ident,
        format!("No impl Block defined for '{state_ident}'"),
    ))
}

/// Creates the doubly nested match block required to match actions either by method-name or method-id.
///
/// The result of `call_fns` will be captured as an outer variable `_match_result`.
pub fn match_action(
    action_names: &[String],
    action_ids: &[u32],
    call_fns: &[TokenStream2],
) -> TokenStream2 {
    quote! {
    let _match_result = match &action.method {
        MethodOrId::ByName { method } => match method.as_str() {
            #(
            #action_names => {
                #call_fns
            }
            )*
            other => { return Err(new_error!("Unknown method: {other}")) }
        }
        MethodOrId::ById { method_id } => match method_id {
            #(
            #action_ids => {
                #call_fns
            }
            )*
            other => { return Err(new_error!("Unknown method-id: 0x{other:04x}")) }
        }
    };
    }
}

fn get_actions_from_impl(state_ident: &Ident, impl_block: &ItemImpl) -> Result<Vec<ActionFn>> {
    let mut actions = Vec::new();
    for item in impl_block.items.iter() {
        let impl_fn = match item {
            ImplItem::Fn(f) => f,
            _ => continue,
        };
        let mut action_args = None;
        for attr in impl_fn.attrs.iter() {
            if check_if_action(attr) {
                let args = if let Meta::List(list) = &attr.meta {
                    (
                        syn::parse2::<ActionArgs>(list.tokens.clone())?,
                        list.tokens.span(),
                    )
                } else {
                    (ActionArgs::default(), attr.span())
                };
                action_args = Some(args);
                break;
            }
        }
        // Ignore, if parsing went wrong
        let (action_args, args_span) = match action_args {
            Some(a) => a,
            None => continue,
        };
        // At this point, the function is an action
        let mut has_self = false;
        let mut mut_self = false;
        let mut args: Vec<(Ident, Box<Type>)> = Vec::new();
        for input in impl_fn.sig.inputs.iter() {
            match input {
                FnArg::Receiver(s) => {
                    has_self = true;
                    mut_self = s.mutability.is_some();
                    if s.reference.is_none() {
                        return Err(Error::new_spanned(
                            item,
                            "Action functions must not consume state - use either &self or &mut self",
                        ));
                    }
                }
                FnArg::Typed(t) => {
                    // Extract the name from the pattern
                    if let Pat::Ident(PatIdent { ident, .. }) = &*t.pat {
                        args.push((ident.clone(), t.ty.clone()));
                    } else {
                        return Err(Error::new_spanned(
                            &t.pat,
                            "Only simple named arguments are supported in action functions",
                        ));
                    }
                }
            }
        }
        if !has_self {
            return Err(Error::new_spanned(
                item,
                "Action functions must act on state - so either &self or &mut self",
            ));
        }

        // Check, that there are no actions with the same ID
        let method_id = calc_method_id(state_ident, &impl_fn.sig.ident, action_args.rename.clone());
        if let Some(rename) = &action_args.rename {
            if !rename.is_case(Case::Snake) {
                return Err(Error::new(
                    args_span,
                    format!("invalid method name '{rename}' - method names must be snake_case"),
                ));
            }
        }
        if let Some(a) = actions
            .iter()
            .find(|a: &&ActionFn| a.method_id == method_id)
        {
            return Err(Error::new_spanned(
                impl_fn,
                format!(
                    "duplicate method-id for {} and {} - this is likely because you used 'rename' on one action",
                    a.ident, impl_fn.sig.ident
                ),
            ));
        }
        actions.push(ActionFn {
            ident: impl_fn.sig.ident.clone(),
            method_id,
            name_override: action_args.rename,
            web_api: action_args.expose_api,
            roles: action_args.roles,
            mut_self,
            output: impl_fn.sig.output.clone(),
            args,
            _span: impl_fn.span(),
        });
    }
    if actions.is_empty() {
        Err(Error::new_spanned(
            impl_block,
            format!("No actions defined for '{state_ident}'"),
        ))
    } else {
        Ok(actions)
    }
}

/// Calculate the method-id based on the state and action name.
pub fn calc_method_id(state_ident: &Ident, action_ident: &Ident, rename: Option<String>) -> u32 {
    let action_name = rename.unwrap_or_else(|| action_ident.to_string());
    let full_name = format!("{}::{}", state_ident, action_name).to_uppercase();
    // Since we only want 32-bits, we simply truncate the output:
    xxh3_64(full_name.as_bytes()) as u32
}

// pub fn impl_actions_enum(actions: &[ActionFn]) -> TokenStream2 {
//     let fields = actions.iter().map(ActionFn::gen_field);
//     let match_items = actions.iter().map(ActionFn::gen_field_match);
//     quote! {
//         #[allow(private_interfaces)]
//         pub enum Actions {
//             #( #fields ),*
//         }

//         #[automatically_derived]
//         impl TryFrom<Actions> for ::borderless::events::CallAction {
//             type Error = ::borderless::serialize::Error;

//             fn try_from(value: Actions) -> ::std::result::Result<::borderless::events::CallAction, Self::Error> {
//                 let action = match value {
//                     #(
//                     #match_items
//                     )*
//                 };
//                 Ok(action)
//             }
//         }
//     }
// }

// #[derive(Debug, FromMeta)]
// pub struct ActionArgs {
//     #[darling(default)]
//     pub rename: Option<String>,

//     #[darling(default, rename = "web_api")]
//     pub web_api: bool,

//     #[darling(default)]
//     pub roles: String,
// }
//
#[allow(dead_code)]
struct Rename {
    id_token: kw::rename,
    eq_token: Token![=],
    value: LitStr,
}

#[allow(dead_code)]
struct Roles {
    id_token: kw::roles,
    eq_token: Token![=],
    values: Vec<String>,
}

#[allow(dead_code)]
struct ExposeApi {
    web_api_token: kw::web_api,
    eq_token: Token![=],
    value: LitBool,
}

#[allow(dead_code)]
struct ActionArgs {
    rename: Option<String>,
    expose_api: bool,
    roles: Vec<String>,
}

impl Default for ActionArgs {
    fn default() -> Self {
        Self {
            rename: None,
            expose_api: true,
            roles: Vec::new(),
        }
    }
}

impl Parse for Rename {
    fn parse(input: ParseStream) -> Result<Self> {
        Ok(Rename {
            id_token: input.parse::<kw::rename>()?,
            eq_token: input.parse()?,
            value: input.parse()?,
        })
    }
}

impl Parse for ExposeApi {
    fn parse(input: ParseStream) -> Result<Self> {
        Ok(ExposeApi {
            web_api_token: input.parse::<kw::web_api>()?,
            eq_token: input.parse()?,
            value: input.parse()?,
        })
    }
}

impl Parse for Roles {
    fn parse(input: ParseStream) -> Result<Self> {
        let id_token = input.parse::<kw::roles>()?;
        let eq_token = input.parse()?;
        let value: LitStr = input.parse()?;
        let values = value.value().split(',').map(ToString::to_string).collect();
        Ok(Roles {
            id_token,
            eq_token,
            values,
        })
    }
}

impl Parse for ActionArgs {
    fn parse(input: ParseStream) -> Result<Self> {
        let mut rename = None;
        let mut expose_api = false;
        let mut roles = Vec::new();

        while !input.is_empty() {
            let lookahead = input.lookahead1();
            if lookahead.peek(kw::rename) {
                let parsed: Rename = input.parse()?;
                rename = Some(parsed.value.value());
            } else if lookahead.peek(kw::web_api) {
                let parsed: ExposeApi = input.parse()?;
                expose_api = parsed.value.value();
            } else if lookahead.peek(kw::roles) {
                let parsed: Roles = input.parse()?;
                roles = parsed.values;
            } else {
                return Err(lookahead.error());
            }
            // If there is still something to parse, it should be separated by a ","
            if !input.is_empty() {
                let _sep: Token![,] = input.parse()?;
            }
        }

        Ok(Self {
            rename,
            expose_api,
            roles,
        })
    }
}

mod kw {
    syn::custom_keyword!(rename);
    syn::custom_keyword!(roles);
    syn::custom_keyword!(web_api);
}