Alloy_sol_macro but for candid

Has anyone tried writing a Rust macro for candid that’s similar to sol! from alloy_sol_macro?

The idea would be to have something like:

can!(
    icp_ledger,
    "../deps/candid/ryjl3-tyaaa-aaaaa-aaaba-cai.did"
);

And the macro creates a module icp_ledger that contains generated Rust from the given candid file. At the minimum it would generate types, but it could also provide canister objects with async methods corresponding to the endpoints (like js-agent and Motoko do). It could also have different call implementations depending on the compilation target such that it works both on-chain and off-chain.

I think it would greatly simplify programming in Rust for ICP by removing a lot of manual work.

I remember there was some work done in generating Rust from Candid, but I cannot find it anymore. All search results I see are about generation of Candid from Rust.

Nice to see you again Ulan!

Do you know about ic-cdk-bindgen? It does almost that. I used it in this project if you want to see how it in action.

Maybe @lwshang can comment a bit more about the topic?

2 Likes

Hello and thank a lot Severin!

Yep, that was the project I was remembering, but my google fu failed me.

I see that the project relies on build.rs and env variables for candid paths. I wonder if it’s possible to get the simplicity of sol! that can be placed into a regular Rust file without additional build steps/setup.

I will explore that direction (unless DFINITY is planning to do it).