Skip to content

Commit

Permalink
Use whisker for Rust
Browse files Browse the repository at this point in the history
Summary:
Whisker is a replacement for mustache and is largely backward compatible. It brings several quality of life improvements like... error checking.

You can read more about the details [in this post](https://fb.workplace.com/groups/498666380550247/permalink/2051389461944590/).

This diff moves `t_mstch_rust_generator` to use whisker instead of mstch.

This requires some changes to the templates to work around the bugs in mstch that have been previously worked around.

#buildall

Reviewed By: dtolnay

Differential Revision: D67562456

fbshipit-source-id: 832a0d588f3b903a5b8047e68fdbe1de6d9f7ead
  • Loading branch information
praihan authored and facebook-github-bot committed Jan 2, 2025
1 parent 09d386f commit a23c846
Show file tree
Hide file tree
Showing 83 changed files with 229 additions and 138 deletions.
9 changes: 9 additions & 0 deletions thrift/compiler/generate/t_mstch_rust_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,15 @@ class t_mstch_rust_generator : public t_mstch_generator {
public:
using t_mstch_generator::t_mstch_generator;

std::optional<whisker_options> use_whisker() const override {
whisker_options opts;
opts.allowed_undefined_variables = {
"typedef:newtype?",
"function:name",
};
return opts;
}

std::string template_prefix() const override { return "rust"; }

void generate_program() override;
Expand Down
5 changes: 3 additions & 2 deletions thrift/compiler/generate/templates/rust/client.rs.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ pub(crate) use {{program:types}}::services;
{{#program:interactions}}{{> lib/client}}
{{/program:interactions}}

{{#program:services}}{{> lib/client}}{{!
}}{{/program:services}}
{{#program:services}}
{{> lib/client}}
{{/program:services}}
{{/program:services?}}
{{^program:services?}}
#![doc(hidden)]
Expand Down
2 changes: 0 additions & 2 deletions thrift/compiler/generate/templates/rust/consts.rs.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,3 @@
{{#program:has_const_tests?}}
{{> lib/adapter/const_test}}
{{/program:has_const_tests?}}

{{!newline}}
11 changes: 6 additions & 5 deletions thrift/compiler/generate/templates/rust/errors.rs.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@

//! Thrift error definitions for `{{program:name}}`.

{{#program:interactions}}{{> lib/errors}}{{!
}}{{/program:interactions}}{{!
}}{{#program:services}}{{> lib/errors}}{{!
}}{{/program:services}}
{{!newline}}
{{#program:interactions}}
{{> lib/errors}}
{{/program:interactions}}
{{#program:services}}
{{> lib/errors}}
{{/program:services}}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@ mod tests {
// This is here to ensure you enable unittests on your Thrift library.
#[cfg(feature = "thrift_library_unittests_disabled")]
compile_error!("You are using Rust adapters in a Thrift library that does not have unittests enabled. For safety reasons, this is forbidden. Please enable Rust unittests for this Thrift library.");
{{!newline}}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,3 @@ mod tests {
// This is here to ensure you enable unittests on your Thrift library.
#[cfg(feature = "thrift_library_unittests_disabled")]
compile_error!("You are using Rust adapters in a Thrift library that does not have unittests enabled. For safety reasons, this is forbidden. Please enable Rust unittests for this Thrift library.");
{{!newline}}
1 change: 0 additions & 1 deletion thrift/compiler/generate/templates/rust/lib/const.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,3 @@ pub static {{constant:name}}{{!
{{/constant:lazy?}}
{{/constant:type}}
{{/constant:rust}}
{{!newline}}
2 changes: 0 additions & 2 deletions thrift/compiler/generate/templates/rust/lib/enum.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ impl ::std::str::FromStr for {{enum:rust_name}} {
impl ::fbthrift::GetTType for {{enum:rust_name}} {
const TTYPE: ::fbthrift::TType = ::fbthrift::TType::I32;
}
{{!newline}}
{{#enum:thrift_uri}}
impl ::fbthrift::GetUri for self::{{enum:rust_name}} {
Expand Down Expand Up @@ -160,4 +159,3 @@ where
::std::result::Result::Ok(Self::from(::anyhow::Context::context(p.read_i32(), "Expected a number indicating enum variant")?))
}
}
{{!newline}}
Original file line number Diff line number Diff line change
Expand Up @@ -450,4 +450,4 @@ pub mod {{service:snake}} {
#[allow(ambiguous_glob_reexports)]
pub use self::{{service:snake}}::*;

{{!newline}}
{{!}}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Annotations on generated patch structs implicitly assume all dependencies
of `thrift/annotation/patch.thrift` are immediately available in the
crate, e.g. they require a dependency on `thrift/annotation/cpp.thrift`.
This might be fine in C++, but Rust crates have isolated dependencies, so
this doesn't work for us. Disable generating annotations on patch structs
for now.
Expand All @@ -29,7 +29,7 @@ impl ::fbthrift::metadata::ThriftAnnotations for {{struct:rust_name}} {
fn get_structured_annotation<T: Sized + 'static>() -> ::std::option::Option<T> {
{{! We can't use a match statement as `TypeId::of` must be const, which is blocked by
https://github.com/rust-lang/rust/issues/77125.
We assume there won't be a large number of annotations, so linear search should
be fine here.
}}
Expand Down Expand Up @@ -90,4 +90,3 @@ impl ::fbthrift::metadata::ThriftAnnotations for {{struct:rust_name}} {
}
}
{{/struct:generated?}}
{{!newline}}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ mod adapted_types {{> lib/block}}{{!
}

pub use self::adapted_types::*;
{{!newline}}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ pub mod adapters {
}};
{{/program:adapters}}
}
{{!newline}}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ pub mod unadapted {
{{/program:adapted_structs}}

}
{{!newline}}
Original file line number Diff line number Diff line change
Expand Up @@ -493,4 +493,3 @@ where
}
}
{{/service:interaction?}}
{{!newline}}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@
{{/function:stream_has_first_response?}}
{{/function:stream?}}
{{^function:stream?}}{{^function:sink?}}
{{#function:return_type}}{{> lib/return_type}}{{/function:return_type}}
{{#function:return_type}}
{{> lib/return_type}}
{{/function:return_type}}
{{/function:sink?}}{{/function:stream?}}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
See the License for the specific language governing permissions and
limitations under the License.
}}{{!
}}::std::result::Result<{{!
}}
::std::result::Result<{{!
}}{{#function:return_type}}{{!
}}{{#function:creates_interaction?}}{{!
}}{{#function:stream?}}{{!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,3 @@ pub mod {{service:snake}} {{> lib/block}}{{!
}
{{/function:sink?}}{{/function:starts_interaction?}}{{/service:rustFunctions}}
}
{{!newline}}
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,3 @@ pub enum {{struct:rust_name}} {{> lib/block}}{{!
UnknownField(::std::primitive::i32),
}
{{/struct:union?}}
{{!newline}}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ impl ::std::panic::UnwindSafe for self::{{struct:rust_name}} {}
impl ::fbthrift::GetTType for self::{{struct:rust_name}} {
const TTYPE: ::fbthrift::TType = ::fbthrift::TType::Struct;
}
{{!newline}}
{{#struct:thrift_uri}}

impl ::fbthrift::GetUri for self::{{struct:rust_name}} {
Expand Down Expand Up @@ -227,4 +226,3 @@ where
{{> lib/metadata/struct}}
{{/program:rust_gen_native_metadata?}}
{{/struct:union?}}
{{!newline}}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,3 @@ pub struct {{typedef:rust_name}}(pub {{!
}}{{/typedef:has_adapter?}}{{!
}});
{{/typedef:newtype?}}
{{!newline}}
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,3 @@ where

{{/typedef:nonstandard?}}
{{/typedef:newtype?}}
{{!newline}}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ impl ::std::default::Default for {{struct:rust_name}} {
impl ::fbthrift::GetTType for {{struct:rust_name}} {
const TTYPE: ::fbthrift::TType = ::fbthrift::TType::Struct;
}
{{!newline}}
{{#struct:thrift_uri}}

impl ::fbthrift::GetUri for self::{{struct:rust_name}} {
Expand Down Expand Up @@ -129,4 +128,3 @@ impl {{struct:rust_name}} {
{{> lib/metadata/struct}}
{{/program:rust_gen_native_metadata?}}
{{/struct:union?}}
{{!newline}}
5 changes: 3 additions & 2 deletions thrift/compiler/generate/templates/rust/server.rs.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
}}// @{{!}}generated by Thrift for {{program:autogen_path}}
// This file is probably not the place you want to edit!

{{#program:include_docs}}
#![doc = include_str!("{{.}}")]
{{/program:include_docs}}
{{#program:services?}}

//! Server definitions for `{{program:name}}`.

{{^program:multifile?}}
Expand All @@ -41,7 +41,8 @@ pub mod errors {{> lib/block}}{{#program:services}}
{{> lib/server_errors}}

{{/program:services}}
{{#program:interactions}} {{> lib/server_errors}}
{{#program:interactions}}
{{> lib/server_errors}}

{{/program:interactions}}
}
Expand Down
14 changes: 9 additions & 5 deletions thrift/compiler/generate/templates/rust/types.rs.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,15 @@ pub(crate) use {{program:crate}} as types;
}}{{/struct:has_adapter?}}{{!
}}{{/program:structs}}{{!
}}{{#program:enums}}{{> lib/enum}}{{/program:enums}}{{!
}}{{#program:typedefs}}{{> lib/typedefimpl}}{{/program:typedefs}}{{!
}}{{#program:structs}}{{^struct:has_adapter?}}{{> lib/structimpl}}{{> lib/unionimpl}}{{/struct:has_adapter?}}{{/program:structs}}{{!
}}
{{#program:enums}}
{{> lib/enum}}
{{/program:enums}}
{{#program:typedefs}}
{{> lib/typedefimpl}}
{{/program:typedefs}}
{{#program:structs}}{{^struct:has_adapter?}}{{> lib/structimpl}}{{> lib/unionimpl}}{{/struct:has_adapter?}}{{/program:structs}}{{!
}}{{#program:has_adapted_structs?}}{{!
}}{{> lib/mod.unadapted}}{{!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -888,3 +888,4 @@ where
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ pub mod service {
}
}
}

/// Service definitions for `AdapterService`.
pub mod adapter_service {
#[derive(Clone, Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ pub struct RenamedStructWithStructAdapterAndFieldAdapter {
pub _dot_dot_Default_default: self::dot_dot::OtherFields,
}


#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Color(pub ::std::primitive::i32);

Expand Down Expand Up @@ -647,7 +648,6 @@ where
::std::result::Result::Ok(Self::from(::anyhow::Context::context(p.read_i32(), "Expected a number indicating enum variant")?))
}
}

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct ThriftAdaptedEnum(pub ::std::primitive::i32);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ pub type BadInteractionClient = ::std::sync::Arc<BadInteractionDynClient>;




pub trait MyService: ::std::marker::Send {
fn ping(
&self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2340,9 +2340,9 @@ pub mod bad_service {
fn get_method_metadata(&self) -> &'static [::fbthrift::processor::MethodMetadata] {
&[
// From module.BadInteraction:
// Interaction's method names are never queried directly.
// They are always queried from the "main" processor.
]
// Interaction's method names are never queried directly.
// They are always queried from the "main" processor.
]
}

async fn on_termination(&self) {
Expand All @@ -2351,7 +2351,7 @@ pub mod bad_service {
}
}


}

#[::async_trait::async_trait]
Expand Down Expand Up @@ -3322,3 +3322,4 @@ where
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ pub mod bad_interaction {
}
}
}

/// Service definitions for `MyService`.
pub mod my_service {
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -728,7 +727,6 @@ pub mod my_service {
}
}
}

/// Service definitions for `MyServicePrioParent`.
pub mod my_service_prio_parent {
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -911,7 +909,6 @@ pub mod my_service_prio_parent {
}
}
}

/// Service definitions for `MyServicePrioChild`.
pub mod my_service_prio_child {
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -1004,7 +1001,6 @@ pub mod my_service_prio_child {
}
}
}

/// Service definitions for `BadService`.
pub mod bad_service {
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -1097,7 +1093,6 @@ pub mod bad_service {
}
}
}

/// Service definitions for `FooBarBazService`.
pub mod foo_bar_baz_service {
#[derive(Clone, Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub struct SecretStruct {
pub _dot_dot_Default_default: self::dot_dot::OtherFields,
}


#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct MyEnum(pub ::std::primitive::i32);

Expand Down Expand Up @@ -999,6 +1000,7 @@ impl ::fbthrift::metadata::ThriftAnnotations for SecretStruct {
}



mod dot_dot {
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct OtherFields(pub(crate) ());
Expand Down Expand Up @@ -1035,3 +1037,4 @@ pub(crate) mod r#impl {
::std::result::Result::Ok(value.0)
}
}

Loading

0 comments on commit a23c846

Please sign in to comment.