diff --git a/Cargo.lock b/Cargo.lock index ffa9b36e..d8ee7276 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -234,7 +234,7 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "candid" -version = "0.10.5" +version = "0.10.6" dependencies = [ "anyhow", "bincode", diff --git a/rust/candid/Cargo.toml b/rust/candid/Cargo.toml index c570ad55..733fecbd 100644 --- a/rust/candid/Cargo.toml +++ b/rust/candid/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "candid" -version = "0.10.5" +version = "0.10.6" edition = "2021" rust-version.workspace = true authors = ["DFINITY Team"] diff --git a/rust/candid/src/pretty/candid.rs b/rust/candid/src/pretty/candid.rs index 29ebc0c4..60aa1c89 100644 --- a/rust/candid/src/pretty/candid.rs +++ b/rust/candid/src/pretty/candid.rs @@ -98,6 +98,7 @@ pub fn pp_ty_inner(ty: &TypeInner) -> RcDoc { Var(ref s) => str(s), Principal => str("principal"), Opt(ref t) => kwd("opt").append(pp_ty(t)), + Vec(ref t) if matches!(t.as_ref(), Nat8) => str("blob"), Vec(ref t) => kwd("vec").append(pp_ty(t)), Record(ref fs) => { let t = Type(ty.clone().into()); @@ -119,7 +120,7 @@ pub fn pp_ty_inner(ty: &TypeInner) -> RcDoc { _ => unreachable!(), } } - Knot(ref id) => RcDoc::text(format!("{id}")), + Knot(ref id) => str(id.name), Unknown => str("unknown"), Future => str("future"), } diff --git a/rust/candid/src/types/internal.rs b/rust/candid/src/types/internal.rs index 9fa2c186..2d61d3d0 100644 --- a/rust/candid/src/types/internal.rs +++ b/rust/candid/src/types/internal.rs @@ -329,7 +329,7 @@ pub fn text_size(t: &Type, limit: i32) -> Result { Float32 | Float64 => 7, Reserved => 8, Principal => 9, - Knot(_) => 5, + Knot(id) => id.name.len() as i32, Var(id) => id.len() as i32, Opt(t) => 4 + text_size(t, limit - 4)?, Vec(t) => 4 + text_size(t, limit - 4)?, @@ -371,7 +371,8 @@ pub fn text_size(t: &Type, limit: i32) -> Result { } 10 + cnt } - Class(_, _) | Future | Unknown => unimplemented!(), + Future | Unknown => 7, + Class(..) => unreachable!(), }; if cost > limit { Err(()) diff --git a/rust/candid_parser/tests/assets/ok/example.did b/rust/candid_parser/tests/assets/ok/example.did index 36db367f..feb17030 100644 --- a/rust/candid_parser/tests/assets/ok/example.did +++ b/rust/candid_parser/tests/assets/ok/example.did @@ -29,7 +29,7 @@ type tree = variant { service : { bbbbb : (b) -> (); f : t; - f1 : (list, vec nat8, opt bool) -> () oneway; + f1 : (list, blob, opt bool) -> () oneway; g : (list) -> (B, tree, stream); g1 : (my_type, List, opt List, nested) -> (int, broker) query; h : (vec opt text, variant { A : nat; B : opt text }, opt List) -> ( diff --git a/rust/candid_parser/tests/assets/ok/keyword.did b/rust/candid_parser/tests/assets/ok/keyword.did index f366756f..59116873 100644 --- a/rust/candid_parser/tests/assets/ok/keyword.did +++ b/rust/candid_parser/tests/assets/ok/keyword.did @@ -15,9 +15,9 @@ service : { fieldnat : (record { 2 : int; "2" : nat }) -> (record { int }); "oneway" : (nat8) -> () oneway; oneway_ : (nat8) -> () oneway; - "query" : (vec nat8) -> (vec nat8) query; + "query" : (blob) -> (blob) query; return : (o) -> (o); "service" : t; - tuple : (record { int; vec nat8; text }) -> (record { int; nat8 }); + tuple : (record { int; blob; text }) -> (record { int; nat8 }); "variant" : (variant { A; B; C; D : float64 }) -> (); } diff --git a/rust/candid_parser/tests/assets/ok/management.did b/rust/candid_parser/tests/assets/ok/management.did index 4d28f784..4af88507 100644 --- a/rust/candid_parser/tests/assets/ok/management.did +++ b/rust/candid_parser/tests/assets/ok/management.did @@ -1,6 +1,6 @@ type bitcoin_address = text; type bitcoin_network = variant { mainnet; testnet }; -type block_hash = vec nat8; +type block_hash = blob; type canister_id = principal; type canister_settings = record { freezing_threshold : opt nat; @@ -23,11 +23,11 @@ type get_balance_request = record { type get_current_fee_percentiles_request = record { network : bitcoin_network }; type get_utxos_request = record { network : bitcoin_network; - filter : opt variant { page : vec nat8; min_confirmations : nat32 }; + filter : opt variant { page : blob; min_confirmations : nat32 }; address : bitcoin_address; }; type get_utxos_response = record { - next_page : opt vec nat8; + next_page : opt blob; tip_height : nat32; tip_block_hash : block_hash; utxos : vec utxo; @@ -35,19 +35,19 @@ type get_utxos_response = record { type http_header = record { value : text; name : text }; type http_response = record { status : nat; - body : vec nat8; + body : blob; headers : vec http_header; }; type millisatoshi_per_byte = nat64; -type outpoint = record { txid : vec nat8; vout : nat32 }; +type outpoint = record { txid : blob; vout : nat32 }; type satoshi = nat64; type send_transaction_request = record { - transaction : vec nat8; + transaction : blob; network : bitcoin_network; }; type user_id = principal; type utxo = record { height : nat32; value : satoshi; outpoint : outpoint }; -type wasm_module = vec nat8; +type wasm_module = blob; service : { bitcoin_get_balance : (get_balance_request) -> (satoshi); bitcoin_get_current_fee_percentiles : ( @@ -62,7 +62,7 @@ service : { cycles : nat; settings : definite_canister_settings; idle_cycles_burned_per_day : nat; - module_hash : opt vec nat8; + module_hash : opt blob; }, ); create_canister : (record { settings : opt canister_settings }) -> ( @@ -74,27 +74,27 @@ service : { record { key_id : record { name : text; curve : ecdsa_curve }; canister_id : opt canister_id; - derivation_path : vec vec nat8; + derivation_path : vec blob; }, - ) -> (record { public_key : vec nat8; chain_code : vec nat8 }); + ) -> (record { public_key : blob; chain_code : blob }); http_request : ( record { url : text; method : variant { get; head; post }; max_response_bytes : opt nat64; - body : opt vec nat8; + body : opt blob; transform : opt record { function : func ( - record { context : vec nat8; response : http_response }, + record { context : blob; response : http_response }, ) -> (http_response) query; - context : vec nat8; + context : blob; }; headers : vec http_header; }, ) -> (http_response); install_code : ( record { - arg : vec nat8; + arg : blob; wasm_module : wasm_module; mode : variant { reinstall; upgrade; install }; canister_id : canister_id; @@ -110,14 +110,14 @@ service : { provisional_top_up_canister : ( record { canister_id : canister_id; amount : nat }, ) -> (); - raw_rand : () -> (vec nat8); + raw_rand : () -> (blob); sign_with_ecdsa : ( record { key_id : record { name : text; curve : ecdsa_curve }; - derivation_path : vec vec nat8; - message_hash : vec nat8; + derivation_path : vec blob; + message_hash : blob; }, - ) -> (record { signature : vec nat8 }); + ) -> (record { signature : blob }); start_canister : (record { canister_id : canister_id }) -> (); stop_canister : (record { canister_id : canister_id }) -> (); uninstall_code : (record { canister_id : canister_id }) -> ();