Skip to content

Commit 35c1a53

Browse files
santiagomedGrok 4.3
authored andcommitted
THRIFT-6059: add crate_prefix option to Rust codegen for cross-file imports
Client: rs The Rust code generator hardcodes 'use crate::' for cross-file imports (e.g., when event.thrift includes trees.thrift). This assumes the generated files are placed at the crate root (src/). When files are placed in a submodule (e.g., src/thrift/), the correct prefix is 'super', and users must post-process with sed. Add a crate_prefix generator option: --gen rs:crate_prefix=super Default remains 'crate' for backward compatibility. An empty value is rejected since it would silently emit 'use ::module;' (an external crate path in Rust 2018+). Unknown options are reported with the generator prefix ('unknown option rs:') per compiler convention. Co-Authored-By: Grok 4.3 <noreply@x.ai>
1 parent 4b0691b commit 35c1a53

1 file changed

Lines changed: 29 additions & 5 deletions

File tree

compiler/cpp/src/thrift/generate/t_rs_generator.cc

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,24 @@ static const string SYNC_CLIENT_GENERIC_BOUNDS("where IP: TInputProtocol, OP: TO
5353

5454
class t_rs_generator : public t_generator {
5555
public:
56-
t_rs_generator(t_program* program, const std::map<std::string, std::string>&, const std::string&)
56+
t_rs_generator(t_program* program,
57+
const std::map<std::string, std::string>& parsed_options,
58+
const std::string&)
5759
: t_generator(program) {
5860
gen_dir_ = get_out_dir();
61+
crate_prefix_ = "crate";
62+
63+
std::map<std::string, std::string>::const_iterator iter;
64+
for (iter = parsed_options.begin(); iter != parsed_options.end(); ++iter) {
65+
if (iter->first.compare("crate_prefix") == 0) {
66+
if (iter->second.empty()) {
67+
throw std::string("crate_prefix requires a non-empty value, e.g. rs:crate_prefix=super");
68+
}
69+
crate_prefix_ = iter->second;
70+
} else {
71+
throw "unknown option rs:" + iter->first;
72+
}
73+
}
5974

6075
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");
6176

@@ -91,6 +106,10 @@ class t_rs_generator : public t_generator {
91106
// Directory to which generated code is written.
92107
string gen_dir_;
93108

109+
// Rust path prefix for cross-file imports (default: "crate").
110+
// Use --gen rs:crate_prefix=super when generated files are in a submodule.
111+
string crate_prefix_;
112+
94113
// File to which generated code is written.
95114
ofstream_with_content_based_conditional_update f_gen_;
96115

@@ -617,10 +636,10 @@ void t_rs_generator::render_attributes_and_includes() {
617636
string_replace(module_namespace, ".", "::");
618637

619638
if (module_namespace.empty()) {
620-
f_gen_ << "use crate::" << rust_snake_case(module_name) << ";" << '\n';
639+
f_gen_ << "use " << crate_prefix_ << "::" << rust_snake_case(module_name) << ";" << '\n';
621640
} else {
622-
f_gen_ << "use crate::" << module_namespace << "::" << rust_snake_case(module_name) << ";"
623-
<< '\n';
641+
f_gen_ << "use " << crate_prefix_ << "::" << module_namespace << "::"
642+
<< rust_snake_case(module_name) << ";" << '\n';
624643
}
625644
}
626645
f_gen_ << '\n';
@@ -3397,4 +3416,9 @@ std::string t_rs_generator::display_name() const {
33973416
return "Rust";
33983417
}
33993418

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

0 commit comments

Comments
 (0)