Skip to content
Merged
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
36 changes: 18 additions & 18 deletions rust/candid/src/pretty/candid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ fn ident_string(id: &str) -> String {
}
}

pub fn pp_text(id: &str) -> RcDoc {
pub fn pp_text(id: &str) -> RcDoc<'_> {
RcDoc::text(ident_string(id))
}

pub fn pp_ty(ty: &Type) -> RcDoc {
pub fn pp_ty(ty: &Type) -> RcDoc<'_> {
pp_ty_inner(ty.as_ref())
}

pub fn pp_ty_inner(ty: &TypeInner) -> RcDoc {
pub fn pp_ty_inner(ty: &TypeInner) -> RcDoc<'_> {
use TypeInner::*;
match ty {
Null => str("null"),
Expand Down Expand Up @@ -130,18 +130,18 @@ pub fn pp_docs<'a>(docs: &'a [String]) -> RcDoc<'a> {
/// This function is kept for backward compatibility.
///
/// It is recommended to use [`pp_label_raw`] instead, which accepts a [`Label`].
pub fn pp_label(id: &SharedLabel) -> RcDoc {
pub fn pp_label(id: &SharedLabel) -> RcDoc<'_> {
pp_label_raw(id.as_ref())
}

pub fn pp_label_raw(id: &Label) -> RcDoc {
pub fn pp_label_raw(id: &Label) -> RcDoc<'_> {
match id {
Label::Named(id) => pp_text(id),
Label::Id(_) | Label::Unnamed(_) => RcDoc::as_string(id),
}
}

pub(crate) fn pp_field(field: &Field, is_variant: bool) -> RcDoc {
pub(crate) fn pp_field(field: &Field, is_variant: bool) -> RcDoc<'_> {
let ty_doc = if is_variant && *field.ty == TypeInner::Null {
RcDoc::nil()
} else {
Expand All @@ -150,12 +150,12 @@ pub(crate) fn pp_field(field: &Field, is_variant: bool) -> RcDoc {
pp_label_raw(&field.id).append(ty_doc)
}

fn pp_fields(fs: &[Field], is_variant: bool) -> RcDoc {
fn pp_fields(fs: &[Field], is_variant: bool) -> RcDoc<'_> {
let fields = fs.iter().map(|f| pp_field(f, is_variant));
enclose_space("{", concat(fields, ";"), "}")
}

pub fn pp_function(func: &Function) -> RcDoc {
pub fn pp_function(func: &Function) -> RcDoc<'_> {
let args = pp_named_args(&func.args);
let rets = pp_rets(&func.rets);
let modes = pp_modes(&func.modes);
Expand All @@ -168,7 +168,7 @@ pub fn pp_function(func: &Function) -> RcDoc {
/// Pretty-prints named arguments in the form of `(name1 : type1, name2 : type2)`.
///
/// To print unnamed arguments, use [`pp_args`] instead.
pub fn pp_named_args(args: &[ArgType]) -> RcDoc {
pub fn pp_named_args(args: &[ArgType]) -> RcDoc<'_> {
let args = args.iter().map(|arg| {
if let Some(name) = &arg.name {
pp_text(name).append(kwd(" :")).append(pp_ty(&arg.typ))
Expand All @@ -183,24 +183,24 @@ pub fn pp_named_args(args: &[ArgType]) -> RcDoc {
/// Pretty-prints arguments in the form of `(type1, type2)`.
///
/// To print named arguments, use [`pp_named_args`] instead.
pub fn pp_args(args: &[Type]) -> RcDoc {
pub fn pp_args(args: &[Type]) -> RcDoc<'_> {
let doc = concat(args.iter().map(pp_ty), ",");
enclose("(", doc, ")")
}

/// Pretty-prints return types in the form of `(type1, type2)`.
pub fn pp_rets(args: &[Type]) -> RcDoc {
pub fn pp_rets(args: &[Type]) -> RcDoc<'_> {
pp_args(args)
}

pub fn pp_mode(mode: &FuncMode) -> RcDoc {
pub fn pp_mode(mode: &FuncMode) -> RcDoc<'_> {
match mode {
FuncMode::Oneway => RcDoc::text("oneway"),
FuncMode::Query => RcDoc::text("query"),
FuncMode::CompositeQuery => RcDoc::text("composite_query"),
}
}
pub fn pp_modes(modes: &[FuncMode]) -> RcDoc {
pub fn pp_modes(modes: &[FuncMode]) -> RcDoc<'_> {
RcDoc::concat(modes.iter().map(|m| RcDoc::space().append(pp_mode(m))))
}

Expand All @@ -223,7 +223,7 @@ fn pp_service<'a>(serv: &'a [(String, Type)], docs: Option<&'a DocComments>) ->
enclose_space("{", doc, "}")
}

fn pp_defs(env: &TypeEnv) -> RcDoc {
fn pp_defs(env: &TypeEnv) -> RcDoc<'_> {
lines(env.to_sorted_iter().map(|(id, ty)| {
kwd("type")
.append(ident(id.as_str()))
Expand Down Expand Up @@ -523,7 +523,7 @@ pub mod value {
}
}

fn pp_field(depth: usize, field: &IDLField, is_variant: bool) -> RcDoc {
fn pp_field(depth: usize, field: &IDLField, is_variant: bool) -> RcDoc<'_> {
let val_doc = if is_variant && field.val == IDLValue::Null {
RcDoc::nil()
} else {
Expand All @@ -532,7 +532,7 @@ pub mod value {
pp_label_raw(&field.id).append(val_doc)
}

fn pp_fields(depth: usize, fields: &[IDLField]) -> RcDoc {
fn pp_fields(depth: usize, fields: &[IDLField]) -> RcDoc<'_> {
let fs = concat(fields.iter().map(|f| pp_field(depth, f, false)), ";");
enclose_space("{", fs, "}")
}
Expand All @@ -545,7 +545,7 @@ pub mod value {
format!("\\{v:02x}")
}
}
pub fn pp_value(depth: usize, v: &IDLValue) -> RcDoc {
pub fn pp_value(depth: usize, v: &IDLValue) -> RcDoc<'_> {
use IDLValue::*;
if depth == 0 {
return RcDoc::as_string(format!("{v:?}"));
Expand Down Expand Up @@ -579,7 +579,7 @@ pub mod value {
}
}

pub fn pp_args(args: &IDLArgs) -> RcDoc {
pub fn pp_args(args: &IDLArgs) -> RcDoc<'_> {
let args = args
.args
.iter()
Expand Down
8 changes: 4 additions & 4 deletions rust/candid/src/pretty/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,19 @@ where
RcDoc::concat(docs.map(|doc| doc.append(RcDoc::hardline())))
}

pub fn kwd<U: std::fmt::Display + ?Sized>(str: &U) -> RcDoc {
pub fn kwd<U: std::fmt::Display + ?Sized>(str: &U) -> RcDoc<'_> {
RcDoc::as_string(str).append(RcDoc::space())
}

pub fn str(str: &str) -> RcDoc {
pub fn str(str: &str) -> RcDoc<'_> {
RcDoc::text(str)
}

pub fn ident(id: &str) -> RcDoc {
pub fn ident(id: &str) -> RcDoc<'_> {
kwd(id)
}

pub fn quote_ident(id: &str) -> RcDoc {
pub fn quote_ident(id: &str) -> RcDoc<'_> {
str("'")
.append(format!("{}", id.escape_debug()))
.append("'")
Expand Down
30 changes: 15 additions & 15 deletions rust/candid_parser/src/bindings/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ static KEYWORDS: [&str; 64] = [
"with",
"yield",
];
pub(crate) fn ident(id: &str) -> RcDoc {
pub(crate) fn ident(id: &str) -> RcDoc<'_> {
if KEYWORDS.contains(&id) {
str(id).append("_")
} else {
str(id)
}
}

fn pp_ty(ty: &Type) -> RcDoc {
fn pp_ty(ty: &Type) -> RcDoc<'_> {
use TypeInner::*;
match ty.as_ref() {
Null => str("IDL.Null"),
Expand Down Expand Up @@ -139,7 +139,7 @@ fn pp_ty(ty: &Type) -> RcDoc {
}
}

fn pp_label(id: &SharedLabel) -> RcDoc {
fn pp_label(id: &SharedLabel) -> RcDoc<'_> {
match &**id {
Label::Named(str) => quote_ident(str),
Label::Id(n) | Label::Unnamed(n) => str("_")
Expand All @@ -149,36 +149,36 @@ fn pp_label(id: &SharedLabel) -> RcDoc {
}
}

fn pp_field(field: &Field) -> RcDoc {
fn pp_field(field: &Field) -> RcDoc<'_> {
pp_label(&field.id)
.append(kwd(":"))
.append(pp_ty(&field.ty))
}

fn pp_fields(fs: &[Field]) -> RcDoc {
fn pp_fields(fs: &[Field]) -> RcDoc<'_> {
let fields = concat(fs.iter().map(pp_field), ",");
enclose_space("({", fields, "})")
}

fn pp_function(func: &Function) -> RcDoc {
fn pp_function(func: &Function) -> RcDoc<'_> {
let args = pp_args(&func.args);
let rets = pp_rets(&func.rets);
let modes = pp_modes(&func.modes);
let doc = concat([args, rets, modes].into_iter(), ",");
enclose("(", doc, ")").nest(INDENT_SPACE)
}

fn pp_args(args: &[ArgType]) -> RcDoc {
fn pp_args(args: &[ArgType]) -> RcDoc<'_> {
let doc = concat(args.iter().map(|arg| pp_ty(&arg.typ)), ",");
enclose("[", doc, "]")
}

fn pp_rets(args: &[Type]) -> RcDoc {
fn pp_rets(args: &[Type]) -> RcDoc<'_> {
let doc = concat(args.iter().map(pp_ty), ",");
enclose("[", doc, "]")
}

fn pp_modes(modes: &[candid::types::FuncMode]) -> RcDoc {
fn pp_modes(modes: &[candid::types::FuncMode]) -> RcDoc<'_> {
let doc = concat(
modes
.iter()
Expand All @@ -188,7 +188,7 @@ fn pp_modes(modes: &[candid::types::FuncMode]) -> RcDoc {
enclose("[", doc, "]")
}

fn pp_service(serv: &[(String, Type)]) -> RcDoc {
fn pp_service(serv: &[(String, Type)]) -> RcDoc<'_> {
let doc = concat(
serv.iter()
.map(|(id, func)| quote_ident(id).append(kwd(":")).append(pp_ty(func))),
Expand Down Expand Up @@ -298,7 +298,7 @@ pub mod value {
_ => false,
}
}
fn pp_label(id: &Label) -> RcDoc {
fn pp_label(id: &Label) -> RcDoc<'_> {
match id {
Label::Named(str) => quote_ident(str),
Label::Id(n) | Label::Unnamed(n) => str("_")
Expand All @@ -307,17 +307,17 @@ pub mod value {
.append(RcDoc::space()),
}
}
fn pp_field(field: &IDLField) -> RcDoc {
fn pp_field(field: &IDLField) -> RcDoc<'_> {
pp_label(&field.id)
.append(": ")
.append(pp_value(&field.val))
}

fn pp_fields(fields: &[IDLField]) -> RcDoc {
fn pp_fields(fields: &[IDLField]) -> RcDoc<'_> {
concat(fields.iter().map(pp_field), ",")
}

pub fn pp_value(v: &IDLValue) -> RcDoc {
pub fn pp_value(v: &IDLValue) -> RcDoc<'_> {
use IDLValue::*;
match v {
Number(_) | Int(_) | Nat(_) | Int64(_) | Nat64(_) => {
Expand Down Expand Up @@ -362,7 +362,7 @@ pub mod value {
}
}

pub fn pp_args(args: &IDLArgs) -> RcDoc {
pub fn pp_args(args: &IDLArgs) -> RcDoc<'_> {
let body = concat(args.args.iter().map(pp_value), ",");
enclose("[", body, "]")
}
Expand Down
12 changes: 6 additions & 6 deletions rust/candid_parser/src/bindings/motoko.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static KEYWORDS: [&str; 48] = [
"while",
"with",
];
fn escape(id: &str, is_method: bool) -> RcDoc {
fn escape(id: &str, is_method: bool) -> RcDoc<'_> {
if KEYWORDS.contains(&id) {
str(id).append("_")
} else if is_valid_as_id(id) {
Expand Down Expand Up @@ -118,7 +118,7 @@ fn pp_ty_rich<'a>(ty: &'a Type, syntax: Option<&'a IDLType>) -> RcDoc<'a> {
}
}

fn pp_ty(ty: &Type) -> RcDoc {
fn pp_ty(ty: &Type) -> RcDoc<'_> {
use TypeInner::*;
match ty.as_ref() {
Null => str("Null"),
Expand Down Expand Up @@ -151,7 +151,7 @@ fn pp_ty(ty: &Type) -> RcDoc {
}
}

fn pp_label(id: &SharedLabel) -> RcDoc {
fn pp_label(id: &SharedLabel) -> RcDoc<'_> {
match &**id {
Label::Named(str) => escape(str, false),
Label::Id(n) | Label::Unnamed(n) => str("_")
Expand All @@ -161,7 +161,7 @@ fn pp_label(id: &SharedLabel) -> RcDoc {
}
}

fn pp_function(func: &Function) -> RcDoc {
fn pp_function(func: &Function) -> RcDoc<'_> {
let args = pp_args(&func.args);
let rets = pp_rets(&func.rets);
match func.modes.as_slice() {
Expand All @@ -185,7 +185,7 @@ fn pp_function(func: &Function) -> RcDoc {
}
.nest(INDENT_SPACE)
}
fn pp_args(args: &[ArgType]) -> RcDoc {
fn pp_args(args: &[ArgType]) -> RcDoc<'_> {
match args {
[ty] => {
let typ = if is_tuple(&ty.typ) {
Expand All @@ -212,7 +212,7 @@ fn pp_args(args: &[ArgType]) -> RcDoc {
}
}

fn pp_rets(args: &[Type]) -> RcDoc {
fn pp_rets(args: &[Type]) -> RcDoc<'_> {
match args {
[ty] => {
if is_tuple(ty) {
Expand Down
8 changes: 4 additions & 4 deletions rust/candid_parser/src/bindings/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ static KEYWORDS: [&str; 51] = [
"while", "async", "await", "dyn", "abstract", "become", "box", "do", "final", "macro",
"override", "priv", "typeof", "unsized", "virtual", "yield", "try",
];
fn ident_(id: &str, case: Option<Case>) -> (RcDoc, bool) {
fn ident_(id: &str, case: Option<Case>) -> (RcDoc<'_>, bool) {
if id.is_empty()
|| id.starts_with(|c: char| !c.is_ascii_alphabetic() && c != '_')
|| id.chars().any(|c| !c.is_ascii_alphanumeric() && c != '_')
Expand All @@ -152,7 +152,7 @@ fn ident_(id: &str, case: Option<Case>) -> (RcDoc, bool) {
(RcDoc::text(id), is_rename)
}
}
fn ident(id: &str, case: Option<Case>) -> RcDoc {
fn ident(id: &str, case: Option<Case>) -> RcDoc<'_> {
ident_(id, case).0
}
fn pp_vis<'a>(vis: &Option<String>) -> RcDoc<'a> {
Expand Down Expand Up @@ -1032,7 +1032,7 @@ impl<'b> NominalState<'_, 'b> {
if let Some(syntax) = syntax {
self.generated_types.insert(new_var.clone(), syntax);
}
TypeInner::Var(new_var.into())
TypeInner::Var(new_var)
}
}
TypeInner::Variant(fs) => {
Expand Down Expand Up @@ -1078,7 +1078,7 @@ impl<'b> NominalState<'_, 'b> {
if let Some(syntax) = syntax {
self.generated_types.insert(new_var.clone(), syntax);
}
TypeInner::Var(new_var.into())
TypeInner::Var(new_var)
}
}
TypeInner::Func(func) => match path.last() {
Expand Down
2 changes: 1 addition & 1 deletion rust/candid_parser/src/bindings/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fn pp_inline_service<'a>() -> RcDoc<'a> {
str("Principal")
}

fn pp_label(id: &SharedLabel) -> RcDoc {
fn pp_label(id: &SharedLabel) -> RcDoc<'_> {
match &**id {
Label::Named(str) => quote_ident(str),
Label::Id(n) | Label::Unnamed(n) => str("_")
Expand Down
Loading