From 5715c9b98f518d9eff509f35c2ca95f138be4dfa Mon Sep 17 00:00:00 2001 From: Linwei Shang Date: Thu, 12 Oct 2023 16:47:09 -0400 Subject: [PATCH] rename bindings::candid as pretty_printer --- rust/candid/src/bindings/mod.rs | 4 ---- rust/candid/src/lib.rs | 2 +- .../{bindings/candid.rs => pretty_printer.rs} | 9 ++------- rust/candid/src/types/internal.rs | 16 ++++------------ rust/candid/src/types/subtype.rs | 2 +- rust/candid_derive/src/func.rs | 2 +- rust/candid_parser/src/bindings/javascript.rs | 2 +- rust/candid_parser/src/bindings/mod.rs | 3 --- rust/candid_parser/src/bindings/motoko.rs | 2 +- rust/candid_parser/src/bindings/rust.rs | 2 +- rust/candid_parser/tests/parse_type.rs | 4 ++-- tools/didc/src/main.rs | 4 ++-- 12 files changed, 16 insertions(+), 36 deletions(-) delete mode 100644 rust/candid/src/bindings/mod.rs rename rust/candid/src/{bindings/candid.rs => pretty_printer.rs} (98%) diff --git a/rust/candid/src/bindings/mod.rs b/rust/candid/src/bindings/mod.rs deleted file mode 100644 index 51e6868b..00000000 --- a/rust/candid/src/bindings/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -//! Candid bindings for different languages. -// This module assumes the input are type checked, it is safe to use unwrap. - -pub mod candid; diff --git a/rust/candid/src/lib.rs b/rust/candid/src/lib.rs index 4ea8e526..c4d5217a 100644 --- a/rust/candid/src/lib.rs +++ b/rust/candid/src/lib.rs @@ -250,7 +250,7 @@ pub mod utils; pub use utils::{decode_args, decode_one, encode_args, encode_one, write_args}; pub mod pretty; -pub mod bindings; +pub mod pretty_printer; // Candid hash function comes from // https://caml.inria.fr/pub/papers/garrigue-polymorphic_variants-ml98.pdf diff --git a/rust/candid/src/bindings/candid.rs b/rust/candid/src/pretty_printer.rs similarity index 98% rename from rust/candid/src/bindings/candid.rs rename to rust/candid/src/pretty_printer.rs index 2fc79a03..fd513436 100644 --- a/rust/candid/src/bindings/candid.rs +++ b/rust/candid/src/pretty_printer.rs @@ -211,7 +211,7 @@ pub fn compile(env: &TypeEnv, actor: &Option) -> String { } pub mod value { - use crate::bindings::candid::{ident_string, pp_text}; + use super::{ident_string, pp_text}; use crate::pretty::*; use crate::types::value::{IDLArgs, IDLField, IDLValue}; use crate::types::{number::pp_num_str, Label}; @@ -325,12 +325,7 @@ pub mod value { Reserved => write!(f, "null : reserved"), Principal(id) => write!(f, "principal \"{id}\""), Service(id) => write!(f, "service \"{id}\""), - Func(id, meth) => write!( - f, - "func \"{}\".{}", - id, - crate::bindings::candid::ident_string(meth) - ), + Func(id, meth) => write!(f, "func \"{}\".{}", id, ident_string(meth)), Opt(v) if has_type_annotation(v) => write!(f, "opt ({v:?})"), Opt(v) => write!(f, "opt {v:?}"), Vec(vs) => { diff --git a/rust/candid/src/types/internal.rs b/rust/candid/src/types/internal.rs index c9daf333..9c296d36 100644 --- a/rust/candid/src/types/internal.rs +++ b/rust/candid/src/types/internal.rs @@ -283,16 +283,12 @@ impl Type { } impl fmt::Display for Type { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", crate::bindings::candid::pp_ty(self).pretty(80)) + write!(f, "{}", crate::pretty_printer::pp_ty(self).pretty(80)) } } impl fmt::Display for TypeInner { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{}", - crate::bindings::candid::pp_ty_inner(self).pretty(80) - ) + write!(f, "{}", crate::pretty_printer::pp_ty_inner(self).pretty(80)) } } pub(crate) fn text_size(t: &Type, limit: i32) -> Result { @@ -410,7 +406,7 @@ impl fmt::Display for Field { write!( f, "{}", - crate::bindings::candid::pp_field(self, false).pretty(80) + crate::pretty_printer::pp_field(self, false).pretty(80) ) } } @@ -478,11 +474,7 @@ pub struct Function { } impl fmt::Display for Function { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{}", - crate::bindings::candid::pp_function(self).pretty(80) - ) + write!(f, "{}", crate::pretty_printer::pp_function(self).pretty(80)) } } impl Function { diff --git a/rust/candid/src/types/subtype.rs b/rust/candid/src/types/subtype.rs index 552c9b85..85c6e334 100644 --- a/rust/candid/src/types/subtype.rs +++ b/rust/candid/src/types/subtype.rs @@ -1,5 +1,5 @@ use super::internal::{find_type, Field, Label, Type, TypeInner}; -use crate::bindings::candid::pp_args; +use crate::pretty_printer::pp_args; use crate::types::TypeEnv; use crate::{Error, Result}; use anyhow::Context; diff --git a/rust/candid_derive/src/func.rs b/rust/candid_derive/src/func.rs index b88afb22..c060d76f 100644 --- a/rust/candid_derive/src/func.rs +++ b/rust/candid_derive/src/func.rs @@ -146,7 +146,7 @@ pub(crate) fn export_service(path: Option) -> TokenStream { fn __export_service() -> String { #service #actor - let result = #candid::bindings::candid::compile(&env.env, &actor); + let result = #candid::pretty_printer::compile(&env.env, &actor); format!("{}", result) } }; diff --git a/rust/candid_parser/src/bindings/javascript.rs b/rust/candid_parser/src/bindings/javascript.rs index 39d8f29b..479f3d05 100644 --- a/rust/candid_parser/src/bindings/javascript.rs +++ b/rust/candid_parser/src/bindings/javascript.rs @@ -267,8 +267,8 @@ pub fn compile(env: &TypeEnv, actor: &Option) -> String { } pub mod value { - use super::super::candid::value::number_to_string; use candid::pretty::*; + use candid::pretty_printer::value::number_to_string; use candid::types::value::{IDLArgs, IDLField, IDLValue}; use candid::types::Label; use pretty::RcDoc; diff --git a/rust/candid_parser/src/bindings/mod.rs b/rust/candid_parser/src/bindings/mod.rs index a21ec35c..ae2a95b4 100644 --- a/rust/candid_parser/src/bindings/mod.rs +++ b/rust/candid_parser/src/bindings/mod.rs @@ -1,9 +1,6 @@ //! Candid bindings for different languages. // This module assumes the input are type checked, it is safe to use unwrap. -// pub mod candid; -use ::candid::bindings::candid; - pub mod analysis; pub mod javascript; pub mod motoko; diff --git a/rust/candid_parser/src/bindings/motoko.rs b/rust/candid_parser/src/bindings/motoko.rs index e78c8621..ef4079b5 100644 --- a/rust/candid_parser/src/bindings/motoko.rs +++ b/rust/candid_parser/src/bindings/motoko.rs @@ -1,8 +1,8 @@ // This module implements the Candid to Motoko binding as specified in // https://github.com/dfinity/motoko/blob/master/design/IDL-Motoko.md -use super::candid::is_valid_as_id; use candid::pretty::*; +use candid::pretty_printer::is_valid_as_id; use candid::types::FuncMode; use candid::types::{Field, Function, Label, SharedLabel, Type, TypeEnv, TypeInner}; use pretty::RcDoc; diff --git a/rust/candid_parser/src/bindings/rust.rs b/rust/candid_parser/src/bindings/rust.rs index dc2ab8db..256d2ef8 100644 --- a/rust/candid_parser/src/bindings/rust.rs +++ b/rust/candid_parser/src/bindings/rust.rs @@ -269,7 +269,7 @@ fn pp_args(args: &[Type]) -> RcDoc { fn pp_ty_func(f: &Function) -> RcDoc { let args = pp_args(&f.args); let rets = pp_args(&f.rets); - let modes = super::candid::pp_modes(&f.modes); + let modes = candid::pretty_printer::pp_modes(&f.modes); args.append(" ->") .append(RcDoc::space()) .append(rets.append(modes)) diff --git a/rust/candid_parser/tests/parse_type.rs b/rust/candid_parser/tests/parse_type.rs index 667d793e..1f50b686 100644 --- a/rust/candid_parser/tests/parse_type.rs +++ b/rust/candid_parser/tests/parse_type.rs @@ -1,4 +1,4 @@ -use candid::bindings::candid as candid_export; +use candid::pretty_printer::compile; use candid::types::TypeEnv; use candid_parser::bindings::{javascript, motoko, rust, typescript}; use candid_parser::parser::types::IDLProg; @@ -42,7 +42,7 @@ fn compiler_test(resource: &str) { Ok((env, actor)) => { { let mut output = mint.new_goldenfile(filename.with_extension("did")).unwrap(); - let content = candid_export::compile(&env, &actor); + let content = compile(&env, &actor); // Type check output let ast = content.parse::().unwrap(); check_prog(&mut TypeEnv::new(), &ast).unwrap(); diff --git a/tools/didc/src/main.rs b/tools/didc/src/main.rs index 2721a9ed..b6c5b818 100644 --- a/tools/didc/src/main.rs +++ b/tools/didc/src/main.rs @@ -200,7 +200,7 @@ fn main() -> Result<()> { let content = match target.as_str() { "js" => candid_parser::bindings::javascript::compile(&env, &actor), "ts" => candid_parser::bindings::typescript::compile(&env, &actor), - "did" => candid::bindings::candid::compile(&env, &actor), + "did" => candid::pretty_printer::compile(&env, &actor), "mo" => candid_parser::bindings::motoko::compile(&env, &actor), "rs" => { let config = candid_parser::bindings::rust::Config::new(); @@ -257,7 +257,7 @@ fn main() -> Result<()> { "blob" => { let mut res = String::new(); for ch in bytes.iter() { - res.push_str(&candid::bindings::candid::value::pp_char(*ch)); + res.push_str(&candid::pretty_printer::value::pp_char(*ch)); } format!("blob \"{res}\"") }