Skip to content

Commit

Permalink
fix: makes typescript bindings accurately permissive (#390)
Browse files Browse the repository at this point in the history
* makes typescript bindings accurately permissive
for Nat and Int arrays

* clippy

* cargo fmt
  • Loading branch information
krpeacock authored Nov 7, 2022
1 parent c7b41a0 commit 1581b5d
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 32 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions rust/candid/src/bindings/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ pub fn chase_types<'a>(env: &'a TypeEnv, tys: &'a [Type]) -> Result<Vec<&'a str>
}

/// Given a `def_list` produced by the `chase_actor` function, infer which types are recursive
pub fn infer_rec<'a>(env: &'a TypeEnv, def_list: &'a [&'a str]) -> Result<BTreeSet<&'a str>> {
pub fn infer_rec<'a>(_env: &'a TypeEnv, def_list: &'a [&'a str]) -> Result<BTreeSet<&'a str>> {
let mut seen = BTreeSet::new();
let mut res = BTreeSet::new();
fn go<'a>(
seen: &mut BTreeSet<&'a str>,
res: &mut BTreeSet<&'a str>,
env: &'a TypeEnv,
_env: &'a TypeEnv,
t: &'a Type,
) -> Result<()> {
use Type::*;
Expand All @@ -81,35 +81,35 @@ pub fn infer_rec<'a>(env: &'a TypeEnv, def_list: &'a [&'a str]) -> Result<BTreeS
res.insert(id);
}
}
Opt(ty) | Vec(ty) => go(seen, res, env, ty)?,
Opt(ty) | Vec(ty) => go(seen, res, _env, ty)?,
Record(fs) | Variant(fs) => {
for f in fs.iter() {
go(seen, res, env, &f.ty)?;
go(seen, res, _env, &f.ty)?;
}
}
Func(f) => {
for ty in f.args.iter().chain(f.rets.iter()) {
go(seen, res, env, ty)?;
go(seen, res, _env, ty)?;
}
}
Service(ms) => {
for (_, ty) in ms.iter() {
go(seen, res, env, ty)?;
go(seen, res, _env, ty)?;
}
}
Class(args, t) => {
for arg in args.iter() {
go(seen, res, env, arg)?;
go(seen, res, _env, arg)?;
}
go(seen, res, env, t)?;
go(seen, res, _env, t)?;
}
_ => (),
}
Ok(())
}
for var in def_list.iter() {
let t = env.0.get(*var).unwrap();
go(&mut seen, &mut res, env, t)?;
let t = _env.0.get(*var).unwrap();
go(&mut seen, &mut res, _env, t)?;
seen.insert(var);
}
Ok(res)
Expand Down
16 changes: 8 additions & 8 deletions rust/candid/src/bindings/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ fn pp_ty<'a>(env: &'a TypeEnv, ty: &'a Type, is_ref: bool) -> RcDoc<'a> {
_ => t,
};
match *ty {
Nat8 => str("Uint8Array"),
Nat16 => str("Uint16Array"),
Nat32 => str("Uint32Array"),
Nat64 => str("BigUint64Array"),
Int8 => str("Int8Array"),
Int16 => str("Int16Array"),
Int32 => str("Int32Array"),
Int64 => str("BigInt64Array"),
Nat8 => str("Uint8Array | number[]"),
Nat16 => str("Uint16Array | number[]"),
Nat32 => str("Uint32Array | number[]"),
Nat64 => str("BigUint64Array | bigint[]"),
Int8 => str("Int8Array | number[]"),
Int16 => str("Int16Array | number[]"),
Int32 => str("Int32Array | number[]"),
Int64 => str("BigInt64Array | bigint[]"),
_ => str("Array").append(enclose("<", pp_ty(env, t, is_ref), ">")),
}
}
Expand Down
2 changes: 1 addition & 1 deletion rust/candid/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ where
let e = Error::from(e);
let writer = StandardStream::stderr(term::termcolor::ColorChoice::Auto);
let config = term::Config::default();
let str = hex::encode(&reader.get_ref());
let str = hex::encode(reader.get_ref());
let file = SimpleFile::new("binary", &str);
term::emit(&mut writer.lock(), &config, &file, &e.report())?;
Err(e)
Expand Down
6 changes: 3 additions & 3 deletions rust/candid/src/parser/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,8 @@ fn check_file_(file: &Path, is_pretty: bool) -> Result<(TypeEnv, Option<Type>)>
.unwrap()
.to_path_buf()
};
let prog = std::fs::read_to_string(&file)
.map_err(|_| Error::msg(format!("Cannot open {:?}", file)))?;
let prog =
std::fs::read_to_string(file).map_err(|_| Error::msg(format!("Cannot open {:?}", file)))?;
let prog = if is_pretty {
pretty_parse::<IDLProg>(file.to_str().unwrap(), &prog)?
} else {
Expand All @@ -421,7 +421,7 @@ fn check_file_(file: &Path, is_pretty: bool) -> Result<(TypeEnv, Option<Type>)>
pre: false,
};
for import in imports.iter() {
let code = std::fs::read_to_string(&import)?;
let code = std::fs::read_to_string(import)?;
let code = code.parse::<IDLProg>()?;
check_decs(&mut env, &code.decs)?;
}
Expand Down
3 changes: 1 addition & 2 deletions rust/candid/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ impl<'a> types::Serializer for &'a mut ValueSerializer {
type Error = Error;
type Compound = Compound<'a>;
fn serialize_bool(self, v: bool) -> Result<()> {
let v = if v { 1 } else { 0 };
self.write(&[v])?;
self.write(&[v as u8])?;
Ok(())
}
fn serialize_int(self, v: &crate::Int) -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion rust/candid/tests/assets/ok/example.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type tree = {
} |
{ 'leaf' : bigint };
export interface _SERVICE {
'f' : ActorMethod<[list, Uint8Array, [] | [boolean]], undefined>,
'f' : ActorMethod<[list, Uint8Array | number[], [] | [boolean]], undefined>,
'g' : ActorMethod<[my_type, List, [] | [List], nested], [bigint, Principal]>,
'h' : ActorMethod<
[
Expand Down
7 changes: 5 additions & 2 deletions rust/candid/tests/assets/ok/keyword.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ export interface _SERVICE {
'fieldnat' : ActorMethod<[{ _2_ : bigint, '2' : bigint }], [bigint]>,
'oneway' : ActorMethod<[number], undefined>,
'oneway_' : ActorMethod<[number], undefined>,
'query' : ActorMethod<[Uint8Array], Uint8Array>,
'query' : ActorMethod<[Uint8Array | number[]], Uint8Array | number[]>,
'return' : ActorMethod<[o], o>,
'service' : t,
'tuple' : ActorMethod<[[bigint, Uint8Array, string]], [bigint, number]>,
'tuple' : ActorMethod<
[[bigint, Uint8Array | number[], string]],
[bigint, number]
>,
'variant' : ActorMethod<
[{ 'A' : null } | { 'B' : null } | { 'C' : null } | { 'D' : number }],
undefined
Expand Down
2 changes: 1 addition & 1 deletion rust/candid/tests/principal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ fn self_authenticating_ok() {
// 1. Sha224 hash is used
// 2. 0x02 was added in the end
// 3. total length is 29
let p1 = Principal::self_authenticating(&[]);
let p1 = Principal::self_authenticating([]);
let p2 = Principal::try_from_slice(&[
209, 74, 2, 140, 42, 58, 43, 201, 71, 97, 2, 187, 40, 130, 52, 196, 21, 162, 176, 31, 130,
142, 166, 42, 197, 179, 228, 47, 2,
Expand Down
4 changes: 2 additions & 2 deletions tools/candiff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ pub fn value_diff_rec(v1: &Value, v2: &Value, _t: &Option<Type>) -> ValueEdit<Rc
match (v1, v2) {
(Opt(x), Opt(y)) => {
let d = value_diff(
&**x,
&**y,
x,
y,
// to do
&Option::None,
);
Expand Down

0 comments on commit 1581b5d

Please sign in to comment.