Skip to content

Commit 4f9f47f

Browse files
authored
Merge pull request #729 from godot-rust/qol/streamlined-modules
Flatten modules - part I: merge to `godot::global`
2 parents 4c06b58 + 6e5ad8d commit 4f9f47f

File tree

34 files changed

+382
-419
lines changed

34 files changed

+382
-419
lines changed

godot-codegen/src/generator/central_files.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub fn make_core_central_code(api: &ExtensionApi, ctx: &mut Context) -> TokenStr
5959
..
6060
} = make_variant_enums(api, ctx);
6161

62-
let global_enum_defs = make_global_enums(api);
62+
let (global_enum_defs, global_reexported_enum_defs) = make_global_enums(api);
6363
let variant_type_traits = make_variant_type_enum(api, false).0;
6464

6565
// TODO impl Clone, Debug, PartialEq, PartialOrd, Hash for VariantDispatch
@@ -107,16 +107,16 @@ pub fn make_core_central_code(api: &ExtensionApi, ctx: &mut Context) -> TokenStr
107107
}
108108
}
109109

110-
/// Global enums and constants.
111-
///
112-
/// A list of global-scope enumerated constants.
113-
/// For global built-in functions, check out the [`utilities` module][crate::engine::utilities].
114-
///
115-
/// See also [Godot docs for `@GlobalScope`](https://docs.godotengine.org/en/stable/classes/class_@globalscope.html#enumerations).
116-
pub mod global {
110+
/// Global enums and constants, generated by Godot.
111+
pub mod global_enums {
117112
use crate::sys;
118113
#( #global_enum_defs )*
119114
}
115+
116+
pub mod global_reexported_enums {
117+
use crate::sys;
118+
#( #global_reexported_enum_defs )*
119+
}
120120
}
121121
}
122122

@@ -183,8 +183,9 @@ fn make_variant_enums(api: &ExtensionApi, ctx: &mut Context) -> VariantEnums {
183183
result
184184
}
185185

186-
fn make_global_enums(api: &ExtensionApi) -> Vec<TokenStream> {
186+
fn make_global_enums(api: &ExtensionApi) -> (Vec<TokenStream>, Vec<TokenStream>) {
187187
let mut global_enum_defs = vec![];
188+
let mut global_reexported_enum_defs = vec![];
188189

189190
for enum_ in api.global_enums.iter() {
190191
// Skip VariantType, which is already defined in godot-ffi.
@@ -193,10 +194,15 @@ fn make_global_enums(api: &ExtensionApi) -> Vec<TokenStream> {
193194
}
194195

195196
let def = enums::make_enum_definition(enum_);
196-
global_enum_defs.push(def);
197+
198+
if enum_.is_private {
199+
global_reexported_enum_defs.push(def);
200+
} else {
201+
global_enum_defs.push(def);
202+
}
197203
}
198204

199-
global_enum_defs
205+
(global_enum_defs, global_reexported_enum_defs)
200206
}
201207

202208
fn make_variant_type_enum(api: &ExtensionApi, is_definition: bool) -> (TokenStream, TokenStream) {

godot-codegen/src/generator/enums.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub fn make_enum_definition_with(
7272
// Non-exhaustive enums are declared as newtype structs with associated constants.
7373
else {
7474
// Workaround because traits are defined in separate crate, but need access to field `ord`.
75-
let vis = (!define_traits).then(|| {
75+
let ord_vis = (!define_traits).then(|| {
7676
quote! { #[doc(hidden)] pub }
7777
});
7878

@@ -82,7 +82,7 @@ pub fn make_enum_definition_with(
8282
#[derive( #( #derives ),* )]
8383
#( #[doc = #enum_doc] )*
8484
pub struct #name {
85-
#vis ord: #ord_type
85+
#ord_vis ord: #ord_type
8686
}
8787

8888
impl #name {

godot-codegen/src/generator/utility_functions.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ pub(crate) fn generate_utilities_file(
2727
let imports = util::make_imports();
2828

2929
let tokens = quote! {
30-
//! Global utility functions.
31-
//!
32-
//! A list of global-scope built-in functions.
33-
//! For global enums and constants, check out the [`global` module][crate::engine::global].
34-
//!
35-
//! See also [Godot docs for `@GlobalScope`](https://docs.godotengine.org/en/stable/classes/class_@globalscope.html#methods).
36-
3730
#imports
3831

3932
#(#utility_fn_defs)*

godot-codegen/src/models/domain/enums.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub struct Enum {
1818
pub name: Ident,
1919
pub godot_name: String,
2020
pub is_bitfield: bool,
21+
pub is_private: bool,
2122
pub is_exhaustive: bool,
2223
pub enumerators: Vec<Enumerator>,
2324
}

godot-codegen/src/models/domain_mapping.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,15 @@ impl UtilityFunction {
486486
return None;
487487
}
488488

489+
// Some vararg functions like print() or str() are declared with a single argument "arg1: Variant", but that seems
490+
// to be a mistake. We change their parameter list by removing that.
491+
let args = option_as_slice(&function.arguments);
492+
let parameters = if function.is_vararg && args.len() == 1 && args[0].name == "arg1" {
493+
vec![]
494+
} else {
495+
FnParam::new_range(&function.arguments, ctx)
496+
};
497+
489498
let godot_method_name = function.name.clone();
490499
let rust_method_name = godot_method_name.clone(); // No change for now.
491500

@@ -498,7 +507,7 @@ impl UtilityFunction {
498507
common: FunctionCommon {
499508
name: rust_method_name,
500509
godot_name: godot_method_name,
501-
parameters: FnParam::new_range(&function.arguments, ctx),
510+
parameters,
502511
return_value: FnReturn::new(&return_value, ctx),
503512
is_vararg: function.is_vararg,
504513
is_private: false,
@@ -517,6 +526,7 @@ impl Enum {
517526
pub fn from_json(json_enum: &JsonEnum, surrounding_class: Option<&TyName>) -> Self {
518527
let godot_name = &json_enum.name;
519528
let is_bitfield = json_enum.is_bitfield;
529+
let is_private = special_cases::is_enum_private(surrounding_class, godot_name);
520530
let is_exhaustive = special_cases::is_enum_exhaustive(surrounding_class, godot_name);
521531

522532
let rust_enum_name = conv::make_enum_name_str(godot_name);
@@ -551,6 +561,7 @@ impl Enum {
551561
name: ident(&rust_enum_name),
552562
godot_name: godot_name.clone(),
553563
is_bitfield,
564+
is_private,
554565
is_exhaustive,
555566
enumerators,
556567
}

godot-codegen/src/special_cases/special_cases.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub fn is_class_method_deleted(class_name: &TyName, method: &JsonClassMethod, ct
3737
}
3838

3939
match (class_name.godot_ty.as_str(), method.name.as_str()) {
40-
// Already covered by manual APIs
40+
// Already covered by manual APIs.
4141
//| ("Object", "to_string")
4242
| ("Object", "get_instance_id")
4343

@@ -280,8 +280,15 @@ pub fn is_builtin_type_scalar(name: &str) -> bool {
280280
name.chars().next().unwrap().is_ascii_lowercase()
281281
}
282282

283+
#[rustfmt::skip]
283284
pub fn is_utility_function_deleted(function: &JsonUtilityFunction, ctx: &mut Context) -> bool {
284-
codegen_special_cases::is_utility_function_excluded(function, ctx)
285+
/*let hardcoded = match function.name.as_str() {
286+
| "..."
287+
288+
=> true, _ => false
289+
};
290+
291+
hardcoded ||*/ codegen_special_cases::is_utility_function_excluded(function, ctx)
285292
}
286293

287294
pub fn maybe_rename_class_method<'m>(class_name: &TyName, godot_method_name: &'m str) -> &'m str {
@@ -355,6 +362,21 @@ pub fn is_class_level_server(class_name: &str) -> bool {
355362
}
356363
}
357364

365+
/// Whether a generated enum is `pub(crate)`; useful for manual re-exports.
366+
#[rustfmt::skip]
367+
pub fn is_enum_private(class_name: Option<&TyName>, enum_name: &str) -> bool {
368+
match (class_name, enum_name) {
369+
// Re-exported to godot::builtin.
370+
| (None, "Corner")
371+
| (None, "EulerOrder")
372+
| (None, "Side")
373+
| (None, "Variant.Operator")
374+
| (None, "Variant.Type")
375+
376+
=> true, _ => false
377+
}
378+
}
379+
358380
/// Certain enums that are extremely unlikely to get new identifiers in the future.
359381
///
360382
/// `class_name` = None for global enums.

godot-core/src/builtin/color.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,21 @@
55
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
66
*/
77

8+
use crate::builtin::color_hsv::rgba_to_hsva;
89
use crate::builtin::inner::InnerColor;
910
use crate::builtin::math::ApproxEq;
10-
use crate::builtin::GString;
11-
11+
use crate::builtin::meta::impl_godot_as_self;
12+
use crate::builtin::{ColorHsv, GString};
1213
use godot_ffi as sys;
13-
use sys::{ffi_methods, GodotFfi};
14-
1514
use std::ops;
16-
17-
use super::meta::impl_godot_as_self;
18-
use super::{rgba_to_hsva, ColorHsv};
15+
use sys::{ffi_methods, GodotFfi};
1916

2017
/// Color built-in type, in floating-point RGBA format.
2118
///
2219
/// Channel values are _typically_ in the range of 0 to 1, but this is not a requirement, and
2320
/// values outside this range are explicitly allowed for e.g. High Dynamic Range (HDR).
2421
///
25-
/// To access its [**HSVA**](super::ColorHsv) representation, use [`Color::to_hsv`].
22+
/// To access its [**HSVA**](ColorHsv) representation, use [`Color::to_hsv`].
2623
#[repr(C)]
2724
#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
2825
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
@@ -100,8 +97,8 @@ impl Color {
10097
/// Constructs a `Color` from a string, which can be either:
10198
///
10299
/// - An HTML color code as accepted by [`Color::from_html`].
103-
/// - The name of a built-in color constant, such as `BLUE` or `lawn-green`. Matching is case
104-
/// insensitive and hyphens can be used interchangeably with underscores. See the [list of
100+
/// - The name of a built-in color constant, such as `BLUE` or `lawn-green`. Matching is case-insensitive
101+
/// and hyphens can be used interchangeably with underscores. See the [list of
105102
/// color constants][color_constants] in the Godot API documentation, or the visual [cheat
106103
/// sheet][cheat_sheet] for the full list.
107104
///
@@ -365,6 +362,7 @@ impl ApproxEq for Color {
365362
}
366363
}
367364

365+
/// Defines how individual color channels are laid out in memory.
368366
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
369367
pub enum ColorChannelOrder {
370368
/// RGBA channel order. Godot's default.
@@ -566,7 +564,7 @@ impl std::fmt::Display for Color {
566564
/// # Example
567565
/// ```
568566
/// use godot::prelude::*;
569-
/// let color = Color::from_rgba(1.0,1.0,1.0,1.0);
567+
/// let color = Color::from_rgba(1.0, 1.0, 1.0, 1.0);
570568
/// assert_eq!(format!("{}", color), "(1, 1, 1, 1)");
571569
/// ```
572570
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {

godot-core/src/builtin/color_constants.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use crate::builtin::Color;
99

10-
/// Godot's predefined colors
10+
/// Godot's predefined colors.
1111
///
1212
/// This [visual cheat sheet](https://raw.githubusercontent.com/godotengine/godot-docs/master/img/color_constants.png)
1313
/// shows how the colors look.

godot-core/src/builtin/meta/godot_convert/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@ pub trait ToGodot: Sized + GodotConvert {
5757

5858
/// Defines the canonical conversion from Godot for a type.
5959
///
60-
/// It is assumed that all the methods return equal values given equal inputs. Additionally it is assumed
60+
/// It is assumed that all the methods return equal values given equal inputs. Additionally, it is assumed
6161
/// that if [`ToGodot`] is implemented, converting to Godot and back again will return a value equal to the
6262
/// starting value.
6363
///
6464
/// Violating these assumptions is safe but will give unexpected results.
6565
pub trait FromGodot: Sized + GodotConvert {
66-
/// Performs the conversion.
66+
/// Converts the Godot representation to this type, returning `Err` on failure.
6767
fn try_from_godot(via: Self::Via) -> Result<Self, ConvertError>;
6868

69-
/// ⚠️ Performs the conversion.
69+
/// ⚠️ Converts the Godot representation to this type.
7070
///
7171
/// # Panics
7272
/// If the conversion fails.
@@ -75,7 +75,7 @@ pub trait FromGodot: Sized + GodotConvert {
7575
.unwrap_or_else(|err| panic!("FromGodot::from_godot() failed: {err}"))
7676
}
7777

78-
/// Performs the conversion from a [`Variant`].
78+
/// Performs the conversion from a [`Variant`], returning `Err` on failure.
7979
fn try_from_variant(variant: &Variant) -> Result<Self, ConvertError> {
8080
let ffi = <Self::Via as GodotType>::Ffi::ffi_from_variant(variant)?;
8181

godot-core/src/builtin/meta/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub use signature::*;
2121
pub(crate) use godot_convert::convert_error::*;
2222

2323
use crate::builtin::*;
24-
use crate::engine::global::{self, PropertyHint, PropertyUsageFlags};
24+
use crate::global::{self, PropertyHint, PropertyUsageFlags};
2525
use crate::property::Var;
2626
use crate::property::{Export, PropertyHintInfo};
2727
use godot_ffi as sys;

0 commit comments

Comments
 (0)