Skip to content

Commit

Permalink
debug-display-for-builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
0awful committed Jan 5, 2024
1 parent 0b93fe1 commit 1808118
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 1 deletion.
20 changes: 20 additions & 0 deletions godot-core/src/builtin/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,26 @@ 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 array = array![1,2,3,4];
/// assert_eq!(format!("{}", array), "[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, entry) in self.iter_shared().enumerate() {
if count != 0 {
write!(f, ", ")?;
}
write!(f, "{}: {}", entry.0, entry.1)?;
}
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
13 changes: 13 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,19 @@ macro_rules! impl_packed_array {
}
}

impl fmt::Display for $PackedArray {
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
6 changes: 6 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,12 @@ fn untyped_array_try_from_typed() {
node.free();
}

#[itest]
fn array_should_format_with_display() {
assert_eq!("[1, 2, 3, 4]", format!("{}", array![1, 2, 3, 4]));
assert_eq!("[]", format!("{}", Array::<real>::new()));
}

#[derive(GodotClass, Debug)]
#[class(init, base=RefCounted)]
struct ArrayTest;
Expand Down
16 changes: 16 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,19 @@ fn dictionary_iter_erase() {
```
*/
}

#[itest]
fn dictionary_should_format_with_display() {
assert_eq!(format!("{}", Dictionary::new()), "{ }");
assert_eq!(
format!(
"{}",
dict! {
"one": 1,
"two": true,
"three": Variant::nil()
}
),
"{ one: 1, two: true, three: <null> }"
)
}
6 changes: 6 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,9 @@ fn packed_array_reverse() {
array.reverse();
assert_eq!(array.to_vec(), vec![2, 1]);
}

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

0 comments on commit 1808118

Please sign in to comment.