1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2024 Google LLC. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 #include "google/protobuf/compiler/rust/rust_keywords.h"
9
10 #include <string>
11
12 #include "absl/container/flat_hash_set.h"
13 #include "absl/strings/string_view.h"
14
15 namespace google {
16 namespace protobuf {
17 namespace compiler {
18 namespace rust {
19
IsLegalRawIdentifierName(absl::string_view str_without_r_prefix)20 bool IsLegalRawIdentifierName(absl::string_view str_without_r_prefix) {
21 // These keywords cannot be used even with an r# prefix.
22 // https://doc.rust-lang.org/reference/identifiers.html
23 static const auto* illegal_raw_identifiers =
24 new absl::flat_hash_set<std::string>{"crate", "self", "super", "Self"};
25 return !illegal_raw_identifiers->contains(str_without_r_prefix);
26 }
27
IsRustKeyword(absl::string_view str)28 bool IsRustKeyword(absl::string_view str) {
29 // https://doc.rust-lang.org/reference/keywords.html#reserved-keywords
30 static const auto* rust_keywords = new absl::flat_hash_set<std::string>{
31 "as", "async", "await", "break", "const", "continue",
32 "crate", "dyn", "else", "enum", "extern", "false",
33 "fn", "for", "if", "impl", "in", "let",
34 "loop", "match", "mod", "move", "mut", "pub",
35 "ref", "return", "Self", "self", "static", "struct",
36 "super", "trait", "true", "type", "union", "unsafe",
37 "use", "where", "while", "abstract", "become", "box",
38 "do", "final", "macro", "override", "priv", "try",
39 "typeof", "unsized", "virtual", "yield"};
40 return rust_keywords->contains(str);
41 }
42
43 } // namespace rust
44 } // namespace compiler
45 } // namespace protobuf
46 } // namespace google
47