Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

experiment: modify candid tooling to preserve source occurrences of blob types when parsing and pretty-printing candid sources #537

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions rust/candid/src/pretty/candid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,18 @@ fn pp_service(serv: &[(String, Type)]) -> RcDoc {
}

fn pp_defs(env: &TypeEnv) -> RcDoc {
lines(env.0.iter().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 {
Expand Down
4 changes: 3 additions & 1 deletion rust/candid/src/types/type_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ pub struct TypeEnv(pub BTreeMap<String, Type>);

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 {
Expand Down
10 changes: 8 additions & 2 deletions rust/candid_parser/src/bindings/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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), ")")),
Expand Down Expand Up @@ -199,7 +205,7 @@ 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)
Expand Down
28 changes: 19 additions & 9 deletions rust/candid_parser/src/bindings/motoko.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,15 @@ 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) => enclose("[", pp_ty(t), "]"),
Record(ref fs) => {
if is_tuple(ty) {
Expand Down Expand Up @@ -220,13 +225,18 @@ fn pp_service(serv: &[(String, Type)]) -> RcDoc {
}

fn pp_defs(env: &TypeEnv) -> RcDoc {
lines(env.0.iter().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 {
Expand Down
14 changes: 9 additions & 5 deletions rust/candid_parser/src/bindings/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,15 @@ fn pp_ty<'a>(ty: &'a Type, recs: &RecPoints) -> RcDoc<'a> {
Reserved => str("candid::Reserved"),
Empty => str("candid::Empty"),
Var(ref id) => {
let name = ident(id, Some(Case::Pascal));
if recs.contains(id.as_str()) {
str("Box<").append(name).append(">")
if id == "blob" {
str("serde_bytes::ByteBuf")
} 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"),
Expand Down Expand Up @@ -219,7 +223,7 @@ 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 ";
Expand Down
6 changes: 4 additions & 2 deletions rust/candid_parser/src/bindings/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +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 is_ref {
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(_)) {
pp_ty(env, ty, false)
Expand Down Expand Up @@ -142,7 +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().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")
Expand Down
2 changes: 1 addition & 1 deletion rust/candid_parser/src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub Typ: IDLType = {
PrimTyp => <>,
"opt" <Typ> => IDLType::OptT(Box::new(<>)),
"vec" <Typ> => IDLType::VecT(Box::new(<>)),
"blob" => IDLType::VecT(Box::new(IDLType::PrimT(PrimType::Nat8))),
"blob" => IDLType::VarT("blob".to_string()),
"record" "{" <Sp<SepBy<RecordFieldTyp, ";">>> "}" =>? {
let mut id: u32 = 0;
let span = <>.1.clone();
Expand Down
2 changes: 1 addition & 1 deletion rust/candid_parser/tests/assets/ok/example.did
Original file line number Diff line number Diff line change
Expand Up @@ -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) -> (
Expand Down
4 changes: 2 additions & 2 deletions rust/candid_parser/tests/assets/ok/keyword.did
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) -> ();
}
36 changes: 18 additions & 18 deletions rust/candid_parser/tests/assets/ok/management.did
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,31 +23,31 @@ 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;
};
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 : (
Expand All @@ -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 }) -> (
Expand All @@ -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;
Expand All @@ -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 }) -> ();
Expand Down
Loading