Skip to content
Open
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
27 changes: 23 additions & 4 deletions compiler/cpp/src/thrift/generate/t_rs_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,19 @@ static const string SYNC_CLIENT_GENERIC_BOUNDS("where IP: TInputProtocol, OP: TO

class t_rs_generator : public t_generator {
public:
t_rs_generator(t_program* program, const std::map<std::string, std::string>&, const std::string&)
t_rs_generator(t_program* program, const std::map<std::string, std::string>& parsed_options, const std::string&)
: t_generator(program) {
gen_dir_ = get_out_dir();
crate_prefix_ = "crate";

std::map<std::string, std::string>::const_iterator iter;
for (iter = parsed_options.begin(); iter != parsed_options.end(); ++iter) {
if (iter->first.compare("crate_prefix") == 0) {
crate_prefix_ = iter->second;
} else {
throw "unknown option: " + iter->first;
}
}

fprintf(stderr, "We are sorry, but for the lack of active maintainers, the RUST compiler target is being deprecated and may be removed in the next version. Feel free to contact the dev mailing list (dev@thrift.apache.org) for further details.\n");

Expand Down Expand Up @@ -91,6 +101,10 @@ class t_rs_generator : public t_generator {
// Directory to which generated code is written.
string gen_dir_;

// Rust path prefix for cross-file imports (default: "crate").
// Use --gen rs:crate_prefix=super when generated files are in a submodule.
string crate_prefix_;

// File to which generated code is written.
ofstream_with_content_based_conditional_update f_gen_;

Expand Down Expand Up @@ -617,9 +631,9 @@ void t_rs_generator::render_attributes_and_includes() {
string_replace(module_namespace, ".", "::");

if (module_namespace.empty()) {
f_gen_ << "use crate::" << rust_snake_case(module_name) << ";" << '\n';
f_gen_ << "use " << crate_prefix_ << "::" << rust_snake_case(module_name) << ";" << '\n';
} else {
f_gen_ << "use crate::" << module_namespace << "::" << rust_snake_case(module_name) << ";"
f_gen_ << "use " << crate_prefix_ << "::" << module_namespace << "::" << rust_snake_case(module_name) << ";"
<< '\n';
}
}
Expand Down Expand Up @@ -3384,4 +3398,9 @@ std::string t_rs_generator::display_name() const {
return "Rust";
}

THRIFT_REGISTER_GENERATOR(rs, "Rust", "\n") // no Rust-generator-specific options
THRIFT_REGISTER_GENERATOR(
rs,
"Rust",
" crate_prefix=p: Rust path prefix for cross-file imports (default: crate).\n"
" Use 'super' when generated files are in a submodule.\n"
)