Skip to content

Commit c37d0ce

Browse files
bors[bot]Bromeon
andauthored
Merge #86
86: Rename Godot engine classes and builtins in Rust r=Bromeon a=Bromeon Makes the names more idiomatic for Rust and easier on the eye. Examples: * `AABB` -> `Aabb` * `CPUParticles3D` -> `CpuParticles3D` * `JSONParseResult` -> `JsonParseResult` Co-authored-by: Jan Haller <[email protected]>
2 parents f2369b2 + 62cd271 commit c37d0ce

File tree

20 files changed

+358
-293
lines changed

20 files changed

+358
-293
lines changed

godot-codegen/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ codegen-full = []
1515
quote = "1"
1616
proc-macro2 = "1"
1717
which = "4"
18-
#heck = "0.4"
18+
heck = "0.4"
1919

2020
# Version >= 1.5.5 for security: https://blog.rust-lang.org/2022/03/08/cve-2022-24713.html
2121
# 'unicode-gencat' needed for \d, see: https://docs.rs/regex/1.5.5/regex/#unicode-features

godot-codegen/src/central_generator.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::collections::HashMap;
1010
use std::path::{Path, PathBuf};
1111

1212
use crate::api_parser::*;
13-
use crate::util::to_rust_type;
13+
use crate::util::{to_pascal_case, to_rust_type, to_snake_case};
1414
use crate::{ident, util, Context};
1515

1616
struct CentralItems {
@@ -26,8 +26,8 @@ struct CentralItems {
2626
}
2727

2828
pub(crate) struct TypeNames {
29-
/// "int" or "PackedVector2Array"
30-
pub pascal_case: String,
29+
/// Name in JSON: "int" or "PackedVector2Array"
30+
pub json_builtin_name: String,
3131

3232
/// "packed_vector2_array"
3333
pub snake_case: String,
@@ -379,6 +379,8 @@ fn make_central_items(api: &ExtensionApi, build_config: &str, ctx: &mut Context)
379379
result
380380
}
381381

382+
/// Creates a map from "normalized" class names (lowercase without underscore, makes it easy to map from different conventions)
383+
/// to meta type information, including all the type name variants
382384
fn collect_builtin_classes(api: &ExtensionApi) -> HashMap<String, &BuiltinClass> {
383385
let mut class_map = HashMap::new();
384386
for class in &api.builtin_classes {
@@ -417,34 +419,34 @@ pub(crate) fn collect_builtin_types(api: &ExtensionApi) -> HashMap<String, Built
417419

418420
// TODO cut down on the number of cached functions generated
419421
// e.g. there's no point in providing operator< for int
420-
let pascal_case: String;
422+
let class_name: String;
421423
let has_destructor: bool;
422424
let constructors: Option<&Vec<Constructor>>;
423425
let operators: Option<&Vec<Operator>>;
424426
if let Some(class) = class_map.get(&normalized) {
425-
pascal_case = class.name.clone();
427+
class_name = class.name.clone();
426428
has_destructor = class.has_destructor;
427429
constructors = Some(&class.constructors);
428430
operators = Some(&class.operators);
429431
} else {
430432
assert_eq!(normalized, "object");
431-
pascal_case = "Object".to_string();
433+
class_name = "Object".to_string();
432434
has_destructor = false;
433435
constructors = None;
434436
operators = None;
435437
}
436438

437439
let type_names = TypeNames {
438-
pascal_case,
439-
snake_case: shout_case.to_ascii_lowercase(),
440+
json_builtin_name: class_name.clone(),
441+
snake_case: to_snake_case(&class_name),
440442
//shout_case: shout_case.to_string(),
441443
sys_variant_type: format_ident!("GDEXTENSION_VARIANT_TYPE_{}", shout_case),
442444
};
443445

444446
let value = ty.value;
445447

446448
builtin_types_map.insert(
447-
type_names.pascal_case.clone(),
449+
type_names.json_builtin_name.clone(),
448450
BuiltinTypeInfo {
449451
value,
450452
type_names,
@@ -473,23 +475,23 @@ fn make_enumerator(
473475
ctx: &mut Context,
474476
) -> (Ident, TokenStream, Literal) {
475477
//let shout_name = format_ident!("{}", type_names.shout_case);
476-
let (first, rest) = type_names.pascal_case.split_at(1);
478+
let (first, rest) = type_names.json_builtin_name.split_at(1);
477479

478480
let pascal_name = format_ident!("{}{}", first.to_ascii_uppercase(), rest);
479-
let rust_ty = to_rust_type(&type_names.pascal_case, ctx);
481+
let rust_ty = to_rust_type(&type_names.json_builtin_name, ctx);
480482
let ord = Literal::i32_unsuffixed(value);
481483

482484
(pascal_name, rust_ty.to_token_stream(), ord)
483485
}
484486

485487
fn make_opaque_type(name: &str, size: usize) -> TokenStream {
486-
// Capitalize: "int" -> "Int"
488+
let name = to_pascal_case(name);
487489
let (first, rest) = name.split_at(1);
490+
491+
// Capitalize: "int" -> "Int"
488492
let ident = format_ident!("Opaque{}{}", first.to_ascii_uppercase(), rest);
489-
//let upper = format_ident!("SIZE_{}", name.to_uppercase());
490493
quote! {
491494
pub type #ident = crate::opaque::Opaque<#size>;
492-
//pub const #upper: usize = #size;
493495
}
494496
}
495497

@@ -574,11 +576,11 @@ fn make_construct_fns(
574576
if let Some(args) = &constructors[1].arguments {
575577
assert_eq!(args.len(), 1);
576578
assert_eq!(args[0].name, "from");
577-
assert_eq!(args[0].type_, type_names.pascal_case);
579+
assert_eq!(args[0].type_, type_names.json_builtin_name);
578580
} else {
579581
panic!(
580582
"type {}: no constructor args found for copy constructor",
581-
type_names.pascal_case
583+
type_names.json_builtin_name
582584
);
583585
}
584586

@@ -734,7 +736,7 @@ fn format_load_error(ident: &impl std::fmt::Display) -> String {
734736
fn is_trivial(type_names: &TypeNames) -> bool {
735737
let list = ["bool", "int", "float"];
736738

737-
list.contains(&type_names.pascal_case.as_str())
739+
list.contains(&type_names.json_builtin_name.as_str())
738740
}
739741

740742
fn shout_to_pascal(shout_case: &str) -> String {

0 commit comments

Comments
 (0)