cdk-rs icon indicating copy to clipboard operation
cdk-rs copied to clipboard

`guard` cannot be used in a submodule

Open hansl opened this issue 3 years ago • 1 comments

hansl avatar Apr 11 '21 21:04 hansl

I believe we are experiencing this issue at Demergent Labs. Suppose you have the following files:

lib.rs

mod some_module;

fn guard_function() -> Result<(), String> {
    // ...
}

#[query(guard = "guard_function")]
fn query_function() {
    // ...
}

some_module.rs

fn guard_function() -> Result<(), String> {
    // ...
}

#[query(guard = "guard_function")]
fn query_function() {
    // ...
}

I think you would expect that each query_function gets tied to the corresponding guard_function in the same file. However, because they are just strings then there is a clash. It's likely (though I haven't confirmed) that you can't even write what's in some_module.rs.

I think a step in the right direction would be to use a callback of some sort rather than a string.

Also, at Demergent Labs, we are building a general canister-building framework, not concrete canisters. So being able to use specific code from other languages (run by rust still) as a guard for a canister method is what we're needing.

dansteren avatar Mar 17 '23 20:03 dansteren

#[query(guard = "guard_function")]
fn query_function() {
    // ...
}

is simply a syntax sugar which equals to

#[query]
fn query_function() {
    let r: Result<(), String> = guard_function();
    if let Err(e) = r {
        ic_cdk::api::call::reject(&e);
        return;
    }
    // ...
}

If guard_function() is defined in a parent module, the child module should simply import the guard function with use super::guard_function;.

Also, at Demergent Labs, we are building a general canister-building framework, not concrete canisters. So being able to use specific code from other languages (run by rust still) as a guard for a canister method is what we're needing.

We are exploring the wasm component model which will enable inter-operability among different languages in the same wasm module. But that is very immature and may takes a long time to be in IC.

lwshang avatar Apr 12 '24 19:04 lwshang