Skip to content

Commit

Permalink
rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
crusso committed Mar 18, 2024
1 parent f0f437a commit 01ba57b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 42 deletions.
21 changes: 12 additions & 9 deletions rust/candid/src/pretty/candid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 4 additions & 5 deletions rust/candid/src/types/type_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ pub struct TypeEnv(pub BTreeMap<String, Type>);
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> {
Expand Down Expand Up @@ -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),
Expand Down
10 changes: 4 additions & 6 deletions rust/candid_parser/src/bindings/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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), ")")),
Expand Down Expand Up @@ -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)
Expand Down
31 changes: 17 additions & 14 deletions rust/candid_parser/src/bindings/motoko.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions rust/candid_parser/src/bindings/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ";
Expand Down
8 changes: 3 additions & 5 deletions rust/candid_parser/src/bindings/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 01ba57b

Please sign in to comment.