From 0556d5228745abb1540f1d967eb6baf7603b7e2e Mon Sep 17 00:00:00 2001 From: juliohq Date: Thu, 23 Mar 2023 11:36:57 -0300 Subject: [PATCH] Implement FromStr for NodePath --- godot-core/src/builtin/node_path.rs | 10 ++++++++++ itest/rust/src/node_test.rs | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/godot-core/src/builtin/node_path.rs b/godot-core/src/builtin/node_path.rs index 8bfbbe603..191bb13b5 100644 --- a/godot-core/src/builtin/node_path.rs +++ b/godot-core/src/builtin/node_path.rs @@ -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; @@ -60,6 +62,14 @@ impl From<&str> for NodePath { } } +impl FromStr for NodePath { + type Err = Infallible; + + fn from_str(path: &str) -> Result { + Ok(Self::from(path)) + } +} + impl fmt::Display for NodePath { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let string = GodotString::from(self); diff --git a/itest/rust/src/node_test.rs b/itest/rust/src/node_test.rs index 440725df5..5099ed647 100644 --- a/itest/rust/src/node_test.rs +++ b/itest/rust/src/node_test.rs @@ -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(); @@ -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();