dfinity / cdk-rs

Rust canister development kit for the Internet Computer.
Apache License 2.0
201 stars 90 forks source link

feat: Candid checker #490

Open chenyan-dfinity opened 6 months ago

chenyan-dfinity commented 6 months ago

Given a did file and a Rust file, check if the Rust code implements the Candid interface correctly.

To try it out:

$ candid-checker test.rs test.did test.toml
error: Checking Candid method f
   ┌─ test.rs:14:11
   │
14 │ #[ic_cdk::update]
   │           ^^^^^^
   │           │
   │           Expect mode: query
   │           Expect attribute: name = "f"
   │           Expect attribute: composite = true
15 │ #[candid_method(update)]
16 │ pub async fn f(test: MyType, argument: Vec/* whatever */<Profile>) -> (List, u8) {
   │     ---------^-------^^^^^^----------------------------------------^^^^^^^^^^^^^
   │              │       │                                             │
   │              │       │                                             Argument count mismatch
   │              │       │                                             Suggestion: -> List
   │              │       Expect type: CanisterId2
   │              Expect function name: FFF

error: Checking Candid method g
   ┌─ test.rs:29:19
   │
29 │ #[::ic_cdk::query(name="test")]
   │                   ^^^^^^^^^^^ You may want to remove the name attribute
30 │ fn g() -> () {
   │    - This function name matches the Candid method name

error: Checking Candid method inner
   ┌─ test.rs:25:5
   │
25 │   #[query(composite = true)]
   │     ^^^^^ Expect mode: update
26 │   async fn inner(a: List) -> Result<List> {}
   │   --------------^^^^^^^^^-^^^^^^^^^^^^^^^
   │                 │         │
   │                 │         Argument count mismatch
   │                 │         Suggestion: remove the return type
   │                 Argument count mismatch
   │                 Suggestion: ()

error: Checking Candid method missing
 = Method "missing" is missing from Rust code. Use this signature to get started:
   #[query]
   fn missing(arg0: candid::Int) -> candid::Nat

error: Checking init args
   ┌─ test.rs:22:13
   │
22 │ fn take_init() {}
   │             ^^
   │             │
   │             Argument count mismatch
   │             Suggestion: (arg0: Profile)

warning: Function h doesn't appear in Candid file
   ┌─ test.rs:33:5
   │
33 │     #[query]
   │ ╭─────'
34 │ │   fn h(&mut self) -> (u8,u16) {}
   │ ╰─────────────────────────────'