From 3bf5bfeea84bd91f1094f6d41750f7e1c4ecedaa Mon Sep 17 00:00:00 2001 From: Claudio Russo Date: Fri, 15 Mar 2024 22:48:11 +0000 Subject: [PATCH 1/8] first stab at preserving blob in source --- rust/candid/src/pretty/candid.rs | 6 ++++-- rust/candid/src/types/type_env.rs | 8 +++++++- rust/candid_parser/src/grammar.lalrpop | 3 ++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/rust/candid/src/pretty/candid.rs b/rust/candid/src/pretty/candid.rs index 29ebc0c4..ebc4e624 100644 --- a/rust/candid/src/pretty/candid.rs +++ b/rust/candid/src/pretty/candid.rs @@ -187,12 +187,14 @@ fn pp_service(serv: &[(String, Type)]) -> RcDoc { } fn pp_defs(env: &TypeEnv) -> RcDoc { - lines(env.0.iter().map(|(id, ty)| { + lines(env.0.iter() + .filter(|(id, _)|{ !(*id == "blob") }) + .map(|(id, ty)| { kwd("type") .append(ident(id)) .append(kwd("=")) .append(pp_ty(ty)) - .append(";") + .append(";") })) } diff --git a/rust/candid/src/types/type_env.rs b/rust/candid/src/types/type_env.rs index 0723f276..203af2d1 100644 --- a/rust/candid/src/types/type_env.rs +++ b/rust/candid/src/types/type_env.rs @@ -7,7 +7,10 @@ pub struct TypeEnv(pub BTreeMap); impl TypeEnv { pub fn new() -> Self { - TypeEnv(BTreeMap::new()) + let mut map = BTreeMap::new(); + map.insert("blob".into(), + TypeInner::Vec(TypeInner::Nat8.into()).into()); + TypeEnv(map) } pub fn merge<'a>(&'a mut self, env: &TypeEnv) -> Result<&'a mut Self> { for (k, v) in &env.0 { @@ -36,6 +39,9 @@ impl TypeEnv { ty.subst(&tau) } pub fn find_type(&self, name: &str) -> Result<&Type> { +// if name == "blob" { +// return Ok(TypeInner::Vec(TypeInner::Nat8.into()).into()); +// }; match self.0.get(name) { None => Err(Error::msg(format!("Unbound type identifier {name}"))), Some(t) => Ok(t), diff --git a/rust/candid_parser/src/grammar.lalrpop b/rust/candid_parser/src/grammar.lalrpop index 5d00fe82..285f31c2 100644 --- a/rust/candid_parser/src/grammar.lalrpop +++ b/rust/candid_parser/src/grammar.lalrpop @@ -165,7 +165,8 @@ pub Typ: IDLType = { PrimTyp => <>, "opt" => IDLType::OptT(Box::new(<>)), "vec" => IDLType::VecT(Box::new(<>)), - "blob" => IDLType::VecT(Box::new(IDLType::PrimT(PrimType::Nat8))), +// "blob" => IDLType::VecT(Box::new(IDLType::PrimT(PrimType::Nat8))), + "blob" => IDLType::VarT("blob".to_string()), "record" "{" >> "}" =>? { let mut id: u32 = 0; let span = <>.1.clone(); From 599e57ec28fd3f20c91f8a6f4e109fb39bfa29a6 Mon Sep 17 00:00:00 2001 From: Claudio Russo Date: Mon, 18 Mar 2024 18:13:16 +0000 Subject: [PATCH 2/8] adjust bindings --- rust/candid_parser/src/bindings/javascript.rs | 12 ++++++++++-- rust/candid_parser/src/bindings/motoko.rs | 14 +++++++++++--- rust/candid_parser/src/bindings/rust.rs | 9 ++++++++- rust/candid_parser/src/bindings/typescript.rs | 7 ++++++- 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/rust/candid_parser/src/bindings/javascript.rs b/rust/candid_parser/src/bindings/javascript.rs index 02476638..f46cb237 100644 --- a/rust/candid_parser/src/bindings/javascript.rs +++ b/rust/candid_parser/src/bindings/javascript.rs @@ -116,7 +116,13 @@ fn pp_ty(ty: &Type) -> RcDoc { Text => str("IDL.Text"), Reserved => str("IDL.Reserved"), Empty => str("IDL.Empty"), - Var(ref s) => ident(s), + Var(ref s) => + if s == "blob" { + str("IDL.Vec").append(enclose("(", str("IDL.Nat8") , ")")) + } else { + ident(s) + } + , Principal => str("IDL.Principal"), Opt(ref t) => str("IDL.Opt").append(enclose("(", pp_ty(t), ")")), Vec(ref t) => str("IDL.Vec").append(enclose("(", pp_ty(t), ")")), @@ -199,7 +205,9 @@ fn pp_defs<'a>( recs.iter() .map(|id| kwd("const").append(ident(id)).append(" = IDL.Rec();")), ); - let defs = lines(def_list.iter().map(|id| { + let defs = lines(def_list.iter(). + filter(|id|{ !(**id == "blob") }). + map(|id| { let ty = env.find_type(id).unwrap(); if recs.contains(id) { ident(id) diff --git a/rust/candid_parser/src/bindings/motoko.rs b/rust/candid_parser/src/bindings/motoko.rs index d0fcdbd4..8c4e5320 100644 --- a/rust/candid_parser/src/bindings/motoko.rs +++ b/rust/candid_parser/src/bindings/motoko.rs @@ -112,10 +112,16 @@ fn pp_ty(ty: &Type) -> RcDoc { Text => str("Text"), Reserved => str("Any"), Empty => str("None"), - Var(ref s) => escape(s, false), + Var(ref s) => + if s == "blob" { + str("Blob") + } + else { + escape(s, false) + }, Principal => str("Principal"), Opt(ref t) => str("?").append(pp_ty(t)), - Vec(ref t) if matches!(t.as_ref(), Nat8) => str("Blob"), +// Vec(ref t) if matches!(t.as_ref(), Nat8) => str("Blob"), // Why? Vec(ref t) => enclose("[", pp_ty(t), "]"), Record(ref fs) => { if is_tuple(ty) { @@ -220,7 +226,9 @@ fn pp_service(serv: &[(String, Type)]) -> RcDoc { } fn pp_defs(env: &TypeEnv) -> RcDoc { - lines(env.0.iter().map(|(id, ty)| { + lines(env.0.iter(). + filter(|(id, _)|{ !(*id == "blob") }). + map(|(id, ty)| { kwd("public type") .append(escape(id, false)) .append(" = ") diff --git a/rust/candid_parser/src/bindings/rust.rs b/rust/candid_parser/src/bindings/rust.rs index ee1b1b8c..238b2106 100644 --- a/rust/candid_parser/src/bindings/rust.rs +++ b/rust/candid_parser/src/bindings/rust.rs @@ -123,12 +123,17 @@ fn pp_ty<'a>(ty: &'a Type, recs: &RecPoints) -> RcDoc<'a> { Reserved => str("candid::Reserved"), Empty => str("candid::Empty"), Var(ref id) => { + if id == "blob" { + str("serde_bytes::ByteBuf") + } + else { let name = ident(id, Some(Case::Pascal)); if recs.contains(id.as_str()) { str("Box<").append(name).append(">") } else { name } + } } Principal => str("Principal"), Opt(ref t) => str("Option").append(enclose("<", pp_ty(t, recs), ">")), @@ -219,7 +224,9 @@ fn pp_defs<'a>( } else { &config.type_attributes }; - lines(def_list.iter().map(|id| { + lines(def_list.iter(). + filter(|id|{ !(**id == "blob") }). + map(|id| { let ty = env.find_type(id).unwrap(); let name = ident(id, Some(Case::Pascal)).append(" "); let vis = "pub "; diff --git a/rust/candid_parser/src/bindings/typescript.rs b/rust/candid_parser/src/bindings/typescript.rs index 1f4f3bfc..748a10c7 100644 --- a/rust/candid_parser/src/bindings/typescript.rs +++ b/rust/candid_parser/src/bindings/typescript.rs @@ -24,6 +24,9 @@ fn pp_ty<'a>(env: &'a TypeEnv, ty: &'a Type, is_ref: bool) -> RcDoc<'a> { Reserved => str("any"), Empty => str("never"), Var(ref id) => { + if id == "blob" { + str("Uint8Array | number[]") + } else if is_ref { let ty = env.rec_find_type(id).unwrap(); if matches!(ty.as_ref(), Service(_) | Func(_)) { @@ -142,7 +145,9 @@ fn pp_service<'a>(env: &'a TypeEnv, serv: &'a [(String, Type)]) -> RcDoc<'a> { } fn pp_defs<'a>(env: &'a TypeEnv, def_list: &'a [&'a str]) -> RcDoc<'a> { - lines(def_list.iter().map(|id| { + lines(def_list.iter(). + filter(|id|{ !(**id == "blob") }). + map(|id| { let ty = env.find_type(id).unwrap(); let export = match ty.as_ref() { TypeInner::Record(_) if !ty.is_tuple() => kwd("export interface") From 84c64382de9d68921501ee1e7af8bd5f45949b7f Mon Sep 17 00:00:00 2001 From: Claudio Russo Date: Mon, 18 Mar 2024 18:15:13 +0000 Subject: [PATCH 3/8] update golden files --- .../candid_parser/tests/assets/ok/example.did | 2 +- .../candid_parser/tests/assets/ok/keyword.did | 4 +-- .../tests/assets/ok/management.did | 36 +++++++++---------- 3 files changed, 21 insertions(+), 21 deletions(-) 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 }) -> (); From f0f437ac3e42b3ca8fd8a734e1fd89bb15bac521 Mon Sep 17 00:00:00 2001 From: Claudio Russo Date: Mon, 18 Mar 2024 18:26:08 +0000 Subject: [PATCH 4/8] clippy suggestions --- rust/candid/src/pretty/candid.rs | 2 +- rust/candid_parser/src/bindings/javascript.rs | 2 +- rust/candid_parser/src/bindings/motoko.rs | 2 +- rust/candid_parser/src/bindings/rust.rs | 15 +++++++-------- rust/candid_parser/src/bindings/typescript.rs | 7 +++---- 5 files changed, 13 insertions(+), 15 deletions(-) diff --git a/rust/candid/src/pretty/candid.rs b/rust/candid/src/pretty/candid.rs index ebc4e624..d9b9b1b2 100644 --- a/rust/candid/src/pretty/candid.rs +++ b/rust/candid/src/pretty/candid.rs @@ -188,7 +188,7 @@ fn pp_service(serv: &[(String, Type)]) -> RcDoc { fn pp_defs(env: &TypeEnv) -> RcDoc { lines(env.0.iter() - .filter(|(id, _)|{ !(*id == "blob") }) + .filter(|(id, _)|{ *id != "blob" }) .map(|(id, ty)| { kwd("type") .append(ident(id)) diff --git a/rust/candid_parser/src/bindings/javascript.rs b/rust/candid_parser/src/bindings/javascript.rs index f46cb237..6f7e1725 100644 --- a/rust/candid_parser/src/bindings/javascript.rs +++ b/rust/candid_parser/src/bindings/javascript.rs @@ -206,7 +206,7 @@ fn pp_defs<'a>( .map(|id| kwd("const").append(ident(id)).append(" = IDL.Rec();")), ); let defs = lines(def_list.iter(). - filter(|id|{ !(**id == "blob") }). + filter(|id|{ **id != "blob" }). map(|id| { let ty = env.find_type(id).unwrap(); if recs.contains(id) { diff --git a/rust/candid_parser/src/bindings/motoko.rs b/rust/candid_parser/src/bindings/motoko.rs index 8c4e5320..d3359def 100644 --- a/rust/candid_parser/src/bindings/motoko.rs +++ b/rust/candid_parser/src/bindings/motoko.rs @@ -227,7 +227,7 @@ fn pp_service(serv: &[(String, Type)]) -> RcDoc { fn pp_defs(env: &TypeEnv) -> RcDoc { lines(env.0.iter(). - filter(|(id, _)|{ !(*id == "blob") }). + filter(|(id, _)|{ *id != "blob" }). map(|(id, ty)| { kwd("public type") .append(escape(id, false)) diff --git a/rust/candid_parser/src/bindings/rust.rs b/rust/candid_parser/src/bindings/rust.rs index 238b2106..7dd466d8 100644 --- a/rust/candid_parser/src/bindings/rust.rs +++ b/rust/candid_parser/src/bindings/rust.rs @@ -125,14 +125,13 @@ fn pp_ty<'a>(ty: &'a Type, recs: &RecPoints) -> RcDoc<'a> { Var(ref id) => { if id == "blob" { str("serde_bytes::ByteBuf") - } - else { - let name = ident(id, Some(Case::Pascal)); - if recs.contains(id.as_str()) { - str("Box<").append(name).append(">") } else { - name - } + let name = ident(id, Some(Case::Pascal)); + if recs.contains(id.as_str()) { + str("Box<").append(name).append(">") + } else { + name + } } } Principal => str("Principal"), @@ -225,7 +224,7 @@ fn pp_defs<'a>( &config.type_attributes }; lines(def_list.iter(). - filter(|id|{ !(**id == "blob") }). + filter(|id|{ **id != "blob" }). map(|id| { let ty = env.find_type(id).unwrap(); let name = ident(id, Some(Case::Pascal)).append(" "); diff --git a/rust/candid_parser/src/bindings/typescript.rs b/rust/candid_parser/src/bindings/typescript.rs index 748a10c7..941dfa79 100644 --- a/rust/candid_parser/src/bindings/typescript.rs +++ b/rust/candid_parser/src/bindings/typescript.rs @@ -26,13 +26,12 @@ fn pp_ty<'a>(env: &'a TypeEnv, ty: &'a Type, is_ref: bool) -> RcDoc<'a> { Var(ref id) => { if id == "blob" { str("Uint8Array | number[]") - } else - if is_ref { + } else if is_ref { let ty = env.rec_find_type(id).unwrap(); if matches!(ty.as_ref(), Service(_) | Func(_)) { pp_ty(env, ty, false) } else { - ident(id) + ident(id) } } else { ident(id) @@ -146,7 +145,7 @@ fn pp_service<'a>(env: &'a TypeEnv, serv: &'a [(String, Type)]) -> RcDoc<'a> { fn pp_defs<'a>(env: &'a TypeEnv, def_list: &'a [&'a str]) -> RcDoc<'a> { lines(def_list.iter(). - filter(|id|{ !(**id == "blob") }). + filter(|id|{ **id != "blob" }). map(|id| { let ty = env.find_type(id).unwrap(); let export = match ty.as_ref() { From 01ba57b9fe5c5ee146a53f5a11d3b53e31aa9a8c Mon Sep 17 00:00:00 2001 From: Claudio Russo Date: Mon, 18 Mar 2024 18:27:42 +0000 Subject: [PATCH 5/8] rustfmt --- rust/candid/src/pretty/candid.rs | 21 +++++++------ rust/candid/src/types/type_env.rs | 9 +++--- rust/candid_parser/src/bindings/javascript.rs | 10 +++--- rust/candid_parser/src/bindings/motoko.rs | 31 ++++++++++--------- rust/candid_parser/src/bindings/rust.rs | 4 +-- rust/candid_parser/src/bindings/typescript.rs | 8 ++--- 6 files changed, 41 insertions(+), 42 deletions(-) diff --git a/rust/candid/src/pretty/candid.rs b/rust/candid/src/pretty/candid.rs index d9b9b1b2..6e399be7 100644 --- a/rust/candid/src/pretty/candid.rs +++ b/rust/candid/src/pretty/candid.rs @@ -187,15 +187,18 @@ fn pp_service(serv: &[(String, Type)]) -> RcDoc { } fn pp_defs(env: &TypeEnv) -> RcDoc { - lines(env.0.iter() - .filter(|(id, _)|{ *id != "blob" }) - .map(|(id, ty)| { - kwd("type") - .append(ident(id)) - .append(kwd("=")) - .append(pp_ty(ty)) - .append(";") - })) + lines( + env.0 + .iter() + .filter(|(id, _)| *id != "blob") + .map(|(id, ty)| { + kwd("type") + .append(ident(id)) + .append(kwd("=")) + .append(pp_ty(ty)) + .append(";") + }), + ) } fn pp_actor(ty: &Type) -> RcDoc { diff --git a/rust/candid/src/types/type_env.rs b/rust/candid/src/types/type_env.rs index 203af2d1..178bb7bf 100644 --- a/rust/candid/src/types/type_env.rs +++ b/rust/candid/src/types/type_env.rs @@ -8,8 +8,7 @@ pub struct TypeEnv(pub BTreeMap); impl TypeEnv { pub fn new() -> Self { let mut map = BTreeMap::new(); - map.insert("blob".into(), - TypeInner::Vec(TypeInner::Nat8.into()).into()); + map.insert("blob".into(), TypeInner::Vec(TypeInner::Nat8.into()).into()); TypeEnv(map) } pub fn merge<'a>(&'a mut self, env: &TypeEnv) -> Result<&'a mut Self> { @@ -39,9 +38,9 @@ impl TypeEnv { ty.subst(&tau) } pub fn find_type(&self, name: &str) -> Result<&Type> { -// if name == "blob" { -// return Ok(TypeInner::Vec(TypeInner::Nat8.into()).into()); -// }; + // if name == "blob" { + // return Ok(TypeInner::Vec(TypeInner::Nat8.into()).into()); + // }; match self.0.get(name) { None => Err(Error::msg(format!("Unbound type identifier {name}"))), Some(t) => Ok(t), diff --git a/rust/candid_parser/src/bindings/javascript.rs b/rust/candid_parser/src/bindings/javascript.rs index 6f7e1725..dff0bf11 100644 --- a/rust/candid_parser/src/bindings/javascript.rs +++ b/rust/candid_parser/src/bindings/javascript.rs @@ -116,13 +116,13 @@ fn pp_ty(ty: &Type) -> RcDoc { Text => str("IDL.Text"), Reserved => str("IDL.Reserved"), Empty => str("IDL.Empty"), - Var(ref s) => + Var(ref s) => { if s == "blob" { - str("IDL.Vec").append(enclose("(", str("IDL.Nat8") , ")")) + str("IDL.Vec").append(enclose("(", str("IDL.Nat8"), ")")) } else { ident(s) } - , + } Principal => str("IDL.Principal"), Opt(ref t) => str("IDL.Opt").append(enclose("(", pp_ty(t), ")")), Vec(ref t) => str("IDL.Vec").append(enclose("(", pp_ty(t), ")")), @@ -205,9 +205,7 @@ fn pp_defs<'a>( recs.iter() .map(|id| kwd("const").append(ident(id)).append(" = IDL.Rec();")), ); - let defs = lines(def_list.iter(). - filter(|id|{ **id != "blob" }). - map(|id| { + let defs = lines(def_list.iter().filter(|id| **id != "blob").map(|id| { let ty = env.find_type(id).unwrap(); if recs.contains(id) { ident(id) diff --git a/rust/candid_parser/src/bindings/motoko.rs b/rust/candid_parser/src/bindings/motoko.rs index d3359def..8c981553 100644 --- a/rust/candid_parser/src/bindings/motoko.rs +++ b/rust/candid_parser/src/bindings/motoko.rs @@ -112,16 +112,16 @@ fn pp_ty(ty: &Type) -> RcDoc { Text => str("Text"), Reserved => str("Any"), Empty => str("None"), - Var(ref s) => + Var(ref s) => { if s == "blob" { str("Blob") + } else { + escape(s, false) } - else { - escape(s, false) - }, + } Principal => str("Principal"), Opt(ref t) => str("?").append(pp_ty(t)), -// Vec(ref t) if matches!(t.as_ref(), Nat8) => str("Blob"), // Why? + // Vec(ref t) if matches!(t.as_ref(), Nat8) => str("Blob"), // Why? Vec(ref t) => enclose("[", pp_ty(t), "]"), Record(ref fs) => { if is_tuple(ty) { @@ -226,15 +226,18 @@ fn pp_service(serv: &[(String, Type)]) -> RcDoc { } fn pp_defs(env: &TypeEnv) -> RcDoc { - lines(env.0.iter(). - filter(|(id, _)|{ *id != "blob" }). - map(|(id, ty)| { - kwd("public type") - .append(escape(id, false)) - .append(" = ") - .append(pp_ty(ty)) - .append(";") - })) + lines( + env.0 + .iter() + .filter(|(id, _)| *id != "blob") + .map(|(id, ty)| { + kwd("public type") + .append(escape(id, false)) + .append(" = ") + .append(pp_ty(ty)) + .append(";") + }), + ) } fn pp_actor(ty: &Type) -> RcDoc { diff --git a/rust/candid_parser/src/bindings/rust.rs b/rust/candid_parser/src/bindings/rust.rs index 7dd466d8..09078a27 100644 --- a/rust/candid_parser/src/bindings/rust.rs +++ b/rust/candid_parser/src/bindings/rust.rs @@ -223,9 +223,7 @@ fn pp_defs<'a>( } else { &config.type_attributes }; - lines(def_list.iter(). - filter(|id|{ **id != "blob" }). - map(|id| { + lines(def_list.iter().filter(|id| **id != "blob").map(|id| { let ty = env.find_type(id).unwrap(); let name = ident(id, Some(Case::Pascal)).append(" "); let vis = "pub "; diff --git a/rust/candid_parser/src/bindings/typescript.rs b/rust/candid_parser/src/bindings/typescript.rs index 941dfa79..591dfadd 100644 --- a/rust/candid_parser/src/bindings/typescript.rs +++ b/rust/candid_parser/src/bindings/typescript.rs @@ -25,13 +25,13 @@ fn pp_ty<'a>(env: &'a TypeEnv, ty: &'a Type, is_ref: bool) -> RcDoc<'a> { Empty => str("never"), Var(ref id) => { if id == "blob" { - str("Uint8Array | number[]") + str("Uint8Array | number[]") } else if is_ref { let ty = env.rec_find_type(id).unwrap(); if matches!(ty.as_ref(), Service(_) | Func(_)) { pp_ty(env, ty, false) } else { - ident(id) + ident(id) } } else { ident(id) @@ -144,9 +144,7 @@ fn pp_service<'a>(env: &'a TypeEnv, serv: &'a [(String, Type)]) -> RcDoc<'a> { } fn pp_defs<'a>(env: &'a TypeEnv, def_list: &'a [&'a str]) -> RcDoc<'a> { - lines(def_list.iter(). - filter(|id|{ **id != "blob" }). - map(|id| { + lines(def_list.iter().filter(|id| **id != "blob").map(|id| { let ty = env.find_type(id).unwrap(); let export = match ty.as_ref() { TypeInner::Record(_) if !ty.is_tuple() => kwd("export interface") From 9477e5a8636fe31e5aa92c2e721f2abbf9d6cd83 Mon Sep 17 00:00:00 2001 From: Claudio Russo Date: Mon, 18 Mar 2024 18:41:18 +0000 Subject: [PATCH 6/8] Update rust/candid/src/types/type_env.rs --- rust/candid/src/types/type_env.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/rust/candid/src/types/type_env.rs b/rust/candid/src/types/type_env.rs index 178bb7bf..0504408e 100644 --- a/rust/candid/src/types/type_env.rs +++ b/rust/candid/src/types/type_env.rs @@ -38,9 +38,6 @@ impl TypeEnv { ty.subst(&tau) } pub fn find_type(&self, name: &str) -> Result<&Type> { - // if name == "blob" { - // return Ok(TypeInner::Vec(TypeInner::Nat8.into()).into()); - // }; match self.0.get(name) { None => Err(Error::msg(format!("Unbound type identifier {name}"))), Some(t) => Ok(t), From 458ba13db9104b2e31c7d84b81289598295ff3c0 Mon Sep 17 00:00:00 2001 From: Claudio Russo Date: Mon, 18 Mar 2024 18:42:24 +0000 Subject: [PATCH 7/8] Update rust/candid_parser/src/bindings/motoko.rs --- rust/candid_parser/src/bindings/motoko.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/rust/candid_parser/src/bindings/motoko.rs b/rust/candid_parser/src/bindings/motoko.rs index 8c981553..fc8f0a61 100644 --- a/rust/candid_parser/src/bindings/motoko.rs +++ b/rust/candid_parser/src/bindings/motoko.rs @@ -121,7 +121,6 @@ fn pp_ty(ty: &Type) -> RcDoc { } Principal => str("Principal"), Opt(ref t) => str("?").append(pp_ty(t)), - // Vec(ref t) if matches!(t.as_ref(), Nat8) => str("Blob"), // Why? Vec(ref t) => enclose("[", pp_ty(t), "]"), Record(ref fs) => { if is_tuple(ty) { From 6ec3c28a37b377e541b4f34dceb32dc27c533a35 Mon Sep 17 00:00:00 2001 From: Claudio Russo Date: Mon, 18 Mar 2024 18:42:34 +0000 Subject: [PATCH 8/8] Apply suggestions from code review --- rust/candid_parser/src/grammar.lalrpop | 1 - 1 file changed, 1 deletion(-) diff --git a/rust/candid_parser/src/grammar.lalrpop b/rust/candid_parser/src/grammar.lalrpop index 285f31c2..554d7fc4 100644 --- a/rust/candid_parser/src/grammar.lalrpop +++ b/rust/candid_parser/src/grammar.lalrpop @@ -165,7 +165,6 @@ pub Typ: IDLType = { PrimTyp => <>, "opt" => IDLType::OptT(Box::new(<>)), "vec" => IDLType::VecT(Box::new(<>)), -// "blob" => IDLType::VecT(Box::new(IDLType::PrimT(PrimType::Nat8))), "blob" => IDLType::VarT("blob".to_string()), "record" "{" >> "}" =>? { let mut id: u32 = 0;