-
-
Notifications
You must be signed in to change notification settings - Fork 211
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
Conversation
API docs are being generated and will be shortly available at: https://godot-rust.github.io/docs/gdext/pr-553 |
ff2a6a8
to
0b38ae6
Compare
Somehow this picked up a bunch of other people's changes. I must've messed up the |
Cleaned this up, but now have a different commit from merging in master |
a645ca3
to
1808118
Compare
Got it working. 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot! 🙂
Small tip, if you change your initial post to write "Closes #110.", then GitHub will recognize the relation and automatically close the issue upon merging.
godot-core/src/builtin/array.rs
Outdated
/// Example: | ||
/// ```no_run | ||
/// # use godot::prelude::*; | ||
/// let array = array![1,2,3,4]; | ||
/// assert_eq!(format!("{}", array), "[1, 2, 3, 4]"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This misses closing code tags. "Example" could also be a section title like elsewhere.
I also made things a bit more compact and less ambiguous with "array" identifiers:
/// Example: | |
/// ```no_run | |
/// # use godot::prelude::*; | |
/// let array = array![1,2,3,4]; | |
/// assert_eq!(format!("{}", array), "[1, 2, 3, 4]"); | |
/// # Example | |
/// ```no_run | |
/// # use godot::prelude::*; | |
/// let a = array![1, 2, 3, 4]; | |
/// assert_eq!(format!("{a}"), "[1, 2, 3, 4]"); | |
/// ``` |
godot-core/src/builtin/array.rs
Outdated
if count != 0 { | ||
write!(f, ", ")?; | ||
} | ||
write!(f, "{}", v)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
write!(f, "{}", v)?; | |
write!(f, "{v}")?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw: is this ChatGPT code? 😁
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not at all 😂. Just followed the patterns of rust by example and the std library docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, then ChatGPT stole it from there 😅
godot-core/src/builtin/array.rs
Outdated
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Formats `Array` to match Godot's string representation | |
/// Formats `Array` to match Godot's string representation. |
(We should possibly change this elsewhere too, but we adopted this convention of full sentences only after some time).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updating all instances in my comments to have the full sentences paradigm.
#[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())); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use assert_eq!(actual, expected)
convention like existing tests? I.e. swap arguments.
Also don't hesitate to create some "actual" variables for readability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good
#[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> }" | ||
) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also this would benefit from extracting expressions into variables. Ideally the assert statements are single-line and not nested.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will Do.
godot-core/src/builtin/dictionary.rs
Outdated
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, " }}") | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are the spaces after {
and before }
consistent with the way Godot formats dictionaries?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, Its quite strange but it is how an all dictionaries are logged out. Empty -> { }
and others have spaces between the braces and the key/value pairs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Particularly strange because arrays don't do that.
1808118
to
d7f2abc
Compare
d7f2abc
to
b5fa2e3
Compare
Added
|
Thanks a lot! |
Closes #110.