1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use crate::core_types::GodotString;
use crate::object::NewRef;
use crate::private::get_api;
use crate::sys;
use std::fmt;
pub struct NodePath(pub(crate) sys::godot_node_path);
impl NodePath {
#[inline]
#[allow(clippy::should_implement_trait)]
pub fn from_str(path: &str) -> Self {
unsafe {
let mut dest = sys::godot_node_path::default();
let api = get_api();
let mut from = (api.godot_string_chars_to_utf8_with_len)(
path.as_ptr() as *const _,
path.len() as _,
);
(api.godot_node_path_new)(&mut dest, &from);
(api.godot_string_destroy)(&mut from);
NodePath(dest)
}
}
#[inline]
pub fn new(path: &GodotString) -> Self {
unsafe {
let mut dest = sys::godot_node_path::default();
(get_api().godot_node_path_new)(&mut dest, &path.0);
NodePath(dest)
}
}
#[inline]
pub fn is_empty(&self) -> bool {
unsafe { (get_api().godot_node_path_is_empty)(&self.0) }
}
#[inline]
pub fn is_absolute(&self) -> bool {
unsafe { (get_api().godot_node_path_is_absolute)(&self.0) }
}
#[inline]
pub fn name_count(&mut self) -> i32 {
unsafe { (get_api().godot_node_path_get_name_count)(&mut self.0) }
}
#[inline]
pub fn get_subname(&self, idx: i32) -> GodotString {
unsafe { GodotString((get_api().godot_node_path_get_subname)(&self.0, idx)) }
}
#[inline]
pub fn get_subname_count(&self) -> i32 {
unsafe { (get_api().godot_node_path_get_subname_count)(&self.0) }
}
#[inline]
pub fn get_concatenated_subnames(&self) -> GodotString {
unsafe {
GodotString((get_api().godot_node_path_get_concatenated_subnames)(
&self.0,
))
}
}
#[inline]
pub fn to_godot_string(&self) -> GodotString {
unsafe { GodotString((get_api().godot_node_path_as_string)(&self.0)) }
}
#[doc(hidden)]
#[inline]
pub fn sys(&self) -> *const sys::godot_node_path {
&self.0
}
#[doc(hidden)]
#[inline]
pub fn sys_mut(&mut self) -> *mut sys::godot_node_path {
&mut self.0
}
#[doc(hidden)]
#[inline]
pub fn from_sys(sys: sys::godot_node_path) -> Self {
NodePath(sys)
}
}
impl Default for NodePath {
#[inline]
fn default() -> Self {
NodePath::new(&GodotString::default())
}
}
impl ToString for NodePath {
#[inline]
fn to_string(&self) -> String {
self.to_godot_string().to_string()
}
}
impl<S> From<S> for NodePath
where
S: AsRef<str>,
{
#[inline]
fn from(s: S) -> NodePath {
NodePath::from_str(s.as_ref())
}
}
impl From<NodePath> for String {
#[inline]
fn from(p: NodePath) -> String {
p.to_string()
}
}
impl From<GodotString> for NodePath {
#[inline]
fn from(s: GodotString) -> NodePath {
NodePath::new(&s)
}
}
impl From<NodePath> for GodotString {
#[inline]
fn from(p: NodePath) -> GodotString {
p.to_godot_string()
}
}
impl_basic_traits_as_sys!(
for NodePath as godot_node_path {
Drop => godot_node_path_destroy;
Eq => godot_node_path_operator_equal;
NewRef => godot_node_path_new_copy;
}
);
impl fmt::Debug for NodePath {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "NodePath({})", self.to_string())
}
}
#[cfg(feature = "serde")]
mod serialize {
use super::*;
use serde::{
de::{Error, Visitor},
Deserialize, Deserializer, Serialize, Serializer,
};
use std::fmt::Formatter;
impl Serialize for NodePath {
#[inline]
fn serialize<S>(&self, ser: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where
S: Serializer,
{
ser.serialize_newtype_struct("NodePath", &*self.to_string())
}
}
impl<'de> Deserialize<'de> for NodePath {
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct NodePathVisitor;
impl<'de> Visitor<'de> for NodePathVisitor {
type Value = NodePath;
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
formatter.write_str("a NodePath")
}
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: Error,
{
Ok(NodePath::from_str(s))
}
fn visit_newtype_struct<D>(
self,
deserializer: D,
) -> Result<Self::Value, <D as Deserializer<'de>>::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_str(self)
}
}
deserializer.deserialize_newtype_struct("NodePath", NodePathVisitor)
}
}
}