From 01ba57b9fe5c5ee146a53f5a11d3b53e31aa9a8c Mon Sep 17 00:00:00 2001 From: Claudio Russo Date: Mon, 18 Mar 2024 18:27:42 +0000 Subject: [PATCH] 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")