Skip to content

Commit 639e055

Browse files
committed
Unify file/dir export functions
1 parent fd61780 commit 639e055

File tree

3 files changed

+29
-52
lines changed

3 files changed

+29
-52
lines changed

godot-core/src/registry/property.rs

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -410,58 +410,32 @@ pub mod export_info_functions {
410410
}
411411
}
412412

413-
/// Equivalent to `@export_file` in Godot.
414-
///
415-
/// Pass an empty string to have no filter.
416-
pub fn export_file<T: Export>(filter: impl AsRef<str>) -> PropertyHintInfo {
417-
//T::var_hint()
418-
export_file_inner::<T>(false, filter)
419-
}
420-
421-
/// Equivalent to `@export_dir` in Godot.
422-
///
423-
/// Pass an empty string to have no filter.
424-
pub fn export_dir<T: Export>() -> PropertyHintInfo {
425-
PropertyHintInfo {
426-
hint: PropertyHint::DIR,
427-
hint_string: GString::new(),
428-
}
429-
}
430-
431-
/// Equivalent to `@export_global_dir` in Godot.
432-
///
433-
/// Pass an empty string to have no filter.
434-
pub fn export_global_dir<T: Export>() -> PropertyHintInfo {
435-
PropertyHintInfo {
436-
hint: PropertyHint::GLOBAL_DIR,
437-
hint_string: GString::new(),
438-
}
439-
}
440-
441-
/// Equivalent to `@export_global_file` in Godot.
442-
///
443-
/// Pass an empty string to have no filter.
444-
pub fn export_global_file<T: Export>(filter: impl AsRef<str>) -> PropertyHintInfo {
445-
export_file_inner::<T>(true, filter)
446-
}
447-
448-
pub fn export_file_inner<T: Export>(global: bool, filter: impl AsRef<str>) -> PropertyHintInfo {
449-
let hint = if global {
450-
PropertyHint::GLOBAL_FILE
451-
} else {
452-
PropertyHint::FILE
413+
/// Handles `@export_file`, `@export_global_file`, `@export_dir` and `@export_global_dir`.
414+
pub fn export_file_or_dir<T: Export>(
415+
is_file: bool,
416+
is_global: bool,
417+
filter: impl AsRef<str>,
418+
) -> PropertyHintInfo {
419+
let filter = filter.as_ref();
420+
debug_assert!(is_file || filter.is_empty()); // Dir never has filter.
421+
422+
let hint = match (is_file, is_global) {
423+
(true, true) => PropertyHint::GLOBAL_FILE,
424+
(true, false) => PropertyHint::FILE,
425+
(false, true) => PropertyHint::GLOBAL_DIR,
426+
(false, false) => PropertyHint::DIR,
453427
};
454428

455429
PropertyHintInfo {
456430
hint,
457-
hint_string: filter.as_ref().into(),
431+
hint_string: GString::from(filter),
458432
}
459433
}
460434

461435
pub fn export_placeholder<S: AsRef<str>>(placeholder: S) -> PropertyHintInfo {
462436
PropertyHintInfo {
463437
hint: PropertyHint::PLACEHOLDER_TEXT,
464-
hint_string: placeholder.as_ref().into(),
438+
hint_string: GString::from(placeholder.as_ref()),
465439
}
466440
}
467441

godot-macros/src/class/data_models/field_export.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -495,29 +495,27 @@ impl ExportType {
495495
} => quote_export_func! { export_flags_3d_navigation() },
496496

497497
Self::File {
498-
global: false,
499-
kind: FileKind::Dir,
500-
} => quote_export_func! { export_dir<T>() },
501-
502-
Self::File {
503-
global: true,
498+
global,
504499
kind: FileKind::Dir,
505-
} => quote_export_func! { export_global_dir<T>() },
500+
} => {
501+
let filter = quote! { "" };
502+
quote_export_func! { export_file_or_dir<T>(false, #global, #filter) }
503+
}
506504

507505
Self::File {
508506
global,
509507
kind: FileKind::File { filter },
510508
} => {
511509
let filter = filter.clone().unwrap_or(quote! { "" });
512-
513-
quote_export_func! { export_file_inner<T>(#global, #filter) }
510+
quote_export_func! { export_file_or_dir<T>(true, #global, #filter) }
514511
}
515512

516513
Self::Multiline => quote_export_func! { export_multiline() },
517514

518515
Self::PlaceholderText { placeholder } => quote_export_func! {
519516
export_placeholder(#placeholder)
520517
},
518+
521519
Self::ColorNoAlpha => quote_export_func! { export_color_no_alpha() },
522520
}
523521
}

itest/rust/src/object_tests/property_template_test.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,12 @@ fn property_template_test(ctx: &TestContext) {
102102
"not all properties were matched, missing: {properties:?}"
103103
);
104104

105-
assert!(errors.is_empty(), "{}", errors.join("\n"));
105+
assert!(
106+
errors.is_empty(),
107+
"Encountered {} mismatches between GDScript and Rust:\n{}",
108+
errors.len(),
109+
errors.join("\n")
110+
);
106111

107112
rust_properties.free();
108113
}

0 commit comments

Comments
 (0)