Automatic generation of Candid from Rust

Hey yeah we’ve been able to generate candid from rust for a while now. Not sure why it’s still kinda hidden from documentation. All the psychedelic projects use it ! You just need to add the CandidType derive, and the
candid_service(query/update/init) macro to all the methods and types that need to be exported. Then, the export_service!() macro will generate function that returns the candid from rust, which can be run differently depending on the project setup

For projects using a main.rs: You can add a main function only run when target is not wasm32. Then, running cargo run would print the candid, which can be easily piped to a file (cargo run > candid.did ). This works well with mono canister architechtures.
Example: nft-marketplace/main.rs at develop · Psychedelic/nft-marketplace · GitHub

for projects using cdylib/lib.rs: You can make a rust test called save_candid, which would save the candid to a file in the project root when running cargo test. This works really well for multi canister architectures.
Example: cap/lib.rs at main · Psychedelic/cap · GitHub

Personally, I’ve been moving solely to the lib.rs/rust test flow, to allow exporting multiple canisters did interfaces with a single command. If you have other tests as well, you can run only the candid tests using cargo test save_candid


We also plan on making this easy and automatic with the ic_kit - Rust, where we’d include the corresponding candid_method with the #[update/query/init] macro automatically

8 Likes