Skip to content

Commit 2b48a9a

Browse files
committed
Implemented a parse method for RPC mode,
add_method now calls with rpc_mode
1 parent 3c1531d commit 2b48a9a

File tree

2 files changed

+30
-51
lines changed

2 files changed

+30
-51
lines changed

gdnative-core/src/nativescript/init.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -294,15 +294,7 @@ impl<C: NativeClass> ClassBuilder<C> {
294294

295295
#[inline]
296296
pub fn add_method(&self, name: &str, method: ScriptMethodFn) {
297-
self.add_method_advanced(ScriptMethod {
298-
name,
299-
method_ptr: Some(method),
300-
attributes: ScriptMethodAttributes {
301-
rpc_mode: RpcMode::Disabled,
302-
},
303-
method_data: ptr::null_mut(),
304-
free_func: None,
305-
});
297+
self.add_method_with_rpc_mode(name, method, RpcMode::Disabled);
306298
}
307299

308300
/// Returns a `PropertyBuilder` which can be used to add a property to the class being

gdnative-derive/src/methods.rs

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ pub enum RpcMode {
1515
PuppetSync,
1616
}
1717

18+
impl RpcMode {
19+
fn parse(s: &str) -> Option<Self> {
20+
match s {
21+
"remote" => Some(RpcMode::Remote),
22+
"remote_sync" => Some(RpcMode::RemoteSync),
23+
"master" => Some(RpcMode::Master),
24+
"puppet" => Some(RpcMode::Puppet),
25+
"disabled" => Some(RpcMode::Disabled),
26+
"master_sync" => Some(RpcMode::MasterSync),
27+
"puppet_sync" => Some(RpcMode::PuppetSync),
28+
_ => None,
29+
}
30+
}
31+
}
32+
1833
impl Default for RpcMode {
1934
fn default() -> Self {
2035
RpcMode::Disabled
@@ -188,7 +203,7 @@ fn impl_gdnative_expose(ast: ItemImpl) -> (ItemImpl, ClassMethodExport) {
188203
let items = match func {
189204
ImplItem::Method(mut method) => {
190205
let mut export_args = None;
191-
let mut rpc = RpcMode::Disabled;
206+
let mut rpc = None;
192207

193208
let mut errors = vec![];
194209

@@ -253,12 +268,7 @@ fn impl_gdnative_expose(ast: ItemImpl) -> (ItemImpl, ClassMethodExport) {
253268
}
254269
};
255270

256-
for MetaNameValue {
257-
path,
258-
eq_token: _,
259-
lit,
260-
} in pairs
261-
{
271+
for MetaNameValue { path, lit, .. } in pairs {
262272
let last = match path.segments.last() {
263273
Some(val) => val,
264274
None => {
@@ -284,46 +294,23 @@ fn impl_gdnative_expose(ast: ItemImpl) -> (ItemImpl, ClassMethodExport) {
284294
return false;
285295
};
286296

287-
match value.as_str() {
288-
"remote" => {
289-
rpc = RpcMode::Remote;
290-
return false;
291-
}
292-
"remote_sync" => {
293-
rpc = RpcMode::RemoteSync;
294-
return false;
295-
}
296-
"master" => {
297-
rpc = RpcMode::Master;
298-
return false;
299-
}
300-
"puppet" => {
301-
rpc = RpcMode::Puppet;
302-
return false;
303-
}
304-
"disabled" => {
305-
rpc = RpcMode::Disabled;
306-
return false;
307-
}
308-
"master_sync" => {
309-
rpc = RpcMode::MasterSync;
310-
return false;
311-
}
312-
"puppet_sync" => {
313-
rpc = RpcMode::PuppetSync;
314-
return false;
315-
}
316-
_ => {
297+
if let Some(mode) = RpcMode::parse(value.as_str()) {
298+
if rpc.replace(mode).is_some() {
317299
errors.push(syn::Error::new(
318300
last.span(),
319-
format!(
320-
"unexpected value for rpc: {}",
321-
value
322-
),
301+
"rpc mode was set more than once",
323302
));
324303
return false;
325304
}
305+
} else {
306+
errors.push(syn::Error::new(
307+
last.span(),
308+
format!("unexpected value for rpc: {}", value),
309+
));
310+
return false;
326311
}
312+
313+
return false;
327314
}
328315
_ => (),
329316
}
@@ -380,7 +367,7 @@ fn impl_gdnative_expose(ast: ItemImpl) -> (ItemImpl, ClassMethodExport) {
380367
}
381368

382369
export_args.optional_args = optional_args;
383-
export_args.rpc_mode = rpc;
370+
export_args.rpc_mode = rpc.unwrap_or(RpcMode::Disabled);
384371

385372
methods_to_export.push(ExportMethod {
386373
sig: method.sig.clone(),

0 commit comments

Comments
 (0)