Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Display and Debug for Builtins #553

Merged
merged 1 commit into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions godot-core/src/builtin/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,27 @@ impl<T: GodotType> fmt::Debug for Array<T> {
}
}

impl<T: GodotType + fmt::Display> fmt::Display for Array<T> {
/// Formats `Array` to match Godot's string representation.
///
/// Example:
/// ```no_run
/// # use godot::prelude::*;
/// let a = array![1,2,3,4];
/// assert_eq!(format!("{a}"), "[1, 2, 3, 4]");
/// ```
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[")?;
for (count, v) in self.iter_shared().enumerate() {
if count != 0 {
write!(f, ", ")?;
}
write!(f, "{v}")?;
}
write!(f, "]")
}
}

/// Creates a new reference to the data in this array. Changes to the original array will be
/// reflected in the copy and vice versa.
///
Expand Down
13 changes: 13 additions & 0 deletions godot-core/src/builtin/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,19 @@ impl fmt::Debug for Dictionary {
write!(f, "{:?}", self.to_variant().stringify())
}
}
impl fmt::Display for Dictionary {
/// Formats `Dictionary` to match Godot's string representation.
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{{ ")?;
for (count, (key, value)) in self.iter_shared().enumerate() {
if count != 0 {
write!(f, ", ")?;
}
write!(f, "{key}: {value}")?;
}
write!(f, " }}")
}
}

/// Creates a new reference to the data in this dictionary. Changes to the original dictionary will be
/// reflected in the copy and vice versa.
Expand Down
2 changes: 1 addition & 1 deletion godot-core/src/builtin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub(crate) fn u8_to_bool(u: u8) -> bool {
///
/// _Godot equivalent: `@GlobalScope.Side`_
#[doc(alias = "Side")]
#[derive(Copy, Clone)]
#[derive(Copy, Clone, Debug)]
#[repr(C)]
pub enum RectSide {
Left = 0,
Expand Down
14 changes: 14 additions & 0 deletions godot-core/src/builtin/packed_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,20 @@ macro_rules! impl_packed_array {
}
}

impl fmt::Display for $PackedArray {
/// Formats `PackedArray` to match Godot's string representation.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[")?;
for i in 0..self.len() {
if i != 0 {
write!(f, ", ")?;
}
write!(f, "{}", self.get(i))?;
}
write!(f, "]")
}
}

unsafe impl GodotFfi for $PackedArray {
fn variant_type() -> sys::VariantType {
sys::VariantType::$PackedArray
Expand Down
9 changes: 9 additions & 0 deletions itest/rust/src/builtin_tests/containers/array_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,15 @@ fn untyped_array_try_from_typed() {
node.free();
}

#[itest]
fn array_should_format_with_display() {
let a = array![1, 2, 3, 4];
assert_eq!(format!("{a}"), "[1, 2, 3, 4]");

let a = Array::<real>::new();
assert_eq!(format!("{a}"), "[]");
}

#[derive(GodotClass, Debug)]
#[class(init, base=RefCounted)]
struct ArrayTest;
Expand Down
13 changes: 13 additions & 0 deletions itest/rust/src/builtin_tests/containers/dictionary_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,3 +615,16 @@ fn dictionary_iter_erase() {
```
*/
}

#[itest]
fn dictionary_should_format_with_display() {
let d = Dictionary::new();
assert_eq!(format!("{d}"), "{ }");

let d = dict! {
"one": 1,
"two": true,
"three": Variant::nil()
};
assert_eq!(format!("{d}"), "{ one: 1, two: true, three: <null> }")
}
9 changes: 9 additions & 0 deletions itest/rust/src/builtin_tests/containers/packed_array_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,12 @@ fn packed_array_reverse() {
array.reverse();
assert_eq!(array.to_vec(), vec![2, 1]);
}

#[itest]
fn packed_array_format() {
let a = PackedByteArray::from(&[2, 1]);
assert_eq!(format!("{a}"), "[2, 1]");

let a = PackedByteArray::new();
assert_eq!(format!("{a}"), "[]");
}
Loading