Skip to content

Commit

Permalink
Merge #201
Browse files Browse the repository at this point in the history
201: Implement FromStr for NodePath r=Bromeon a=juliohq

Implements `FromStr` for `NodePath`.

Co-authored-by: juliohq <[email protected]>
  • Loading branch information
bors[bot] and juliohq authored Mar 23, 2023
2 parents eb30a8b + 0556d52 commit afa2e59
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions godot-core/src/builtin/node_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

use std::convert::Infallible;
use std::fmt;
use std::str::FromStr;

use crate::builtin::GodotString;
use godot_ffi as sys;
Expand Down Expand Up @@ -60,6 +62,14 @@ impl From<&str> for NodePath {
}
}

impl FromStr for NodePath {
type Err = Infallible;

fn from_str(path: &str) -> Result<Self, Self::Err> {
Ok(Self::from(path))
}
}

impl fmt::Display for NodePath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let string = GodotString::from(self);
Expand Down
11 changes: 11 additions & 0 deletions itest/rust/src/node_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use godot::builtin::{NodePath, Variant};
use godot::engine::{global, node, Node, Node3D, NodeExt, PackedScene, SceneTree};
use godot::obj::Share;

use std::str::FromStr;

#[itest]
fn node_get_node() {
let mut child = Node3D::new_alloc();
Expand Down Expand Up @@ -54,6 +56,15 @@ fn node_get_node_fail() {
child.free();
}

#[itest]
fn node_path_from_str(ctx: &TestContext) {
let child = ctx.scene_tree.share();
assert_eq!(
child.get_path().to_string(),
NodePath::from_str("/root/TestRunner").unwrap().to_string()
);
}

#[itest(skip)]
fn node_scene_tree() {
let mut child = Node::new_alloc();
Expand Down

0 comments on commit afa2e59

Please sign in to comment.