Skip to content

Commit f744dcc

Browse files
committed
gstring concatenation macro
1 parent a752851 commit f744dcc

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

godot-core/src/global/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
2525
mod print;
2626

27-
pub use crate::{godot_error, godot_print, godot_print_rich, godot_script_error, godot_warn};
27+
pub use crate::{
28+
godot_error, godot_print, godot_print_rich, godot_script_error, godot_str, godot_warn,
29+
};
2830

2931
// Some enums are directly re-exported from crate::builtin.
3032
pub use crate::gen::central::global_enums::*;

godot-core/src/global/print.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,15 @@ macro_rules! godot_print_rich {
105105
])
106106
};
107107
}
108+
109+
/// Concatenates format-style into a `GString`.
110+
#[macro_export]
111+
macro_rules! godot_str {
112+
($fmt:literal $(, $args:expr)* $(,)?) => {
113+
$crate::global::str(&[
114+
$crate::builtin::Variant::from(
115+
format!($fmt $(, $args)*)
116+
)
117+
])
118+
};
119+
}

itest/rust/src/engine_tests/utilities_test.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,22 @@ fn utilities_sign() {
3333

3434
#[itest]
3535
fn utilities_str() {
36+
let a = 12;
37+
let b = " is a ";
38+
let c = true;
39+
let d = " number";
3640
let concat = str(&[
37-
Variant::from(12),
38-
Variant::from(" is a "),
39-
Variant::from(true),
40-
Variant::from(" number"),
41+
Variant::from(a),
42+
Variant::from(b),
43+
Variant::from(c),
44+
Variant::from(d),
4145
]);
4246

4347
let empty = str(&[]);
4448

4549
// TODO: implement GString==&str operator. Then look for "...".into() patterns and replace them.
4650
assert_eq!(concat, "12 is a true number".into());
51+
assert_eq!(concat, godot_str!("{a}{b}{c}{d}"));
4752
assert_eq!(empty, GString::new());
4853
}
4954

0 commit comments

Comments
 (0)