Minor refactor

This commit is contained in:
Stephen Chung 2020-10-20 09:21:41 +08:00
parent f2b6d58122
commit 643ecc86a3
9 changed files with 31 additions and 28 deletions

View File

@ -1,4 +1,7 @@
use syn::{parse::ParseStream, parse::Parser, spanned::Spanned}; use syn::{
parse::{ParseStream, Parser},
spanned::Spanned,
};
#[derive(Debug)] #[derive(Debug)]
pub enum ExportScope { pub enum ExportScope {

View File

@ -13,7 +13,10 @@ use std::format;
use std::borrow::Cow; use std::borrow::Cow;
use quote::{quote, quote_spanned}; use quote::{quote, quote_spanned};
use syn::{parse::Parse, parse::ParseStream, parse::Parser, spanned::Spanned}; use syn::{
parse::{Parse, ParseStream, Parser},
spanned::Spanned,
};
use crate::attrs::{ExportInfo, ExportScope, ExportedParams}; use crate::attrs::{ExportInfo, ExportScope, ExportedParams};
@ -360,7 +363,7 @@ impl Parse for ExportedFn {
pass_context, pass_context,
return_dynamic, return_dynamic,
mut_receiver, mut_receiver,
params: ExportedFnParams::default(), params: Default::default(),
}) })
} }
} }
@ -466,13 +469,10 @@ impl ExportedFn {
// //
// 1. Do not allow non-returning raw functions. // 1. Do not allow non-returning raw functions.
// //
if params.return_raw if params.return_raw && self.return_type().is_none() {
&& mem::discriminant(&self.signature.output)
== mem::discriminant(&syn::ReturnType::Default)
{
return Err(syn::Error::new( return Err(syn::Error::new(
self.signature.span(), self.signature.span(),
"return_raw functions must return Result<T, Box<EvalAltResult>>", "functions marked with 'return_raw' must return Result<Dynamic, Box<EvalAltResult>>",
)); ));
} }
@ -481,7 +481,7 @@ impl ExportedFn {
FnSpecialAccess::Property(Property::Get(_)) if self.arg_count() != 1 => { FnSpecialAccess::Property(Property::Get(_)) if self.arg_count() != 1 => {
return Err(syn::Error::new( return Err(syn::Error::new(
self.signature.span(), self.signature.span(),
"property getter requires exactly 1 argument", "property getter requires exactly 1 parameter",
)) ))
} }
// 2b. Property getters must return a value. // 2b. Property getters must return a value.
@ -495,7 +495,7 @@ impl ExportedFn {
FnSpecialAccess::Property(Property::Set(_)) if self.arg_count() != 2 => { FnSpecialAccess::Property(Property::Set(_)) if self.arg_count() != 2 => {
return Err(syn::Error::new( return Err(syn::Error::new(
self.signature.span(), self.signature.span(),
"property setter requires exactly 2 arguments", "property setter requires exactly 2 parameters",
)) ))
} }
// 3b. Property setters must return nothing. // 3b. Property setters must return nothing.
@ -509,7 +509,7 @@ impl ExportedFn {
FnSpecialAccess::Index(Index::Get) if self.arg_count() != 2 => { FnSpecialAccess::Index(Index::Get) if self.arg_count() != 2 => {
return Err(syn::Error::new( return Err(syn::Error::new(
self.signature.span(), self.signature.span(),
"index getter requires exactly 2 arguments", "index getter requires exactly 2 parameters",
)) ))
} }
// 4b. Index getters must return a value. // 4b. Index getters must return a value.
@ -523,7 +523,7 @@ impl ExportedFn {
FnSpecialAccess::Index(Index::Set) if self.arg_count() != 3 => { FnSpecialAccess::Index(Index::Set) if self.arg_count() != 3 => {
return Err(syn::Error::new( return Err(syn::Error::new(
self.signature.span(), self.signature.span(),
"index setter requires exactly 3 arguments", "index setter requires exactly 3 parameters",
)) ))
} }
// 5b. Index setters must return nothing. // 5b. Index setters must return nothing.
@ -593,19 +593,19 @@ impl ExportedFn {
if self.params.return_raw { if self.params.return_raw {
quote_spanned! { return_span=> quote_spanned! { return_span=>
pub #dynamic_signature { pub #dynamic_signature {
super::#name(#(#arguments),*) #name(#(#arguments),*)
} }
} }
} else if self.return_dynamic { } else if self.return_dynamic {
quote_spanned! { return_span=> quote_spanned! { return_span=>
pub #dynamic_signature { pub #dynamic_signature {
Ok(super::#name(#(#arguments),*)) Ok(#name(#(#arguments),*))
} }
} }
} else { } else {
quote_spanned! { return_span=> quote_spanned! { return_span=>
pub #dynamic_signature { pub #dynamic_signature {
Ok(Dynamic::from(super::#name(#(#arguments),*))) Ok(Dynamic::from(#name(#(#arguments),*)))
} }
} }
} }

View File

@ -297,7 +297,7 @@ mod generate_tests {
Token().input_types() Token().input_types()
} }
pub fn dynamic_result_fn() -> Result<Dynamic, Box<EvalAltResult> > { pub fn dynamic_result_fn() -> Result<Dynamic, Box<EvalAltResult> > {
Ok(Dynamic::from(super::do_nothing())) Ok(Dynamic::from(do_nothing()))
} }
} }
}; };
@ -339,7 +339,7 @@ mod generate_tests {
Token().input_types() Token().input_types()
} }
pub fn dynamic_result_fn(x: usize) -> Result<Dynamic, Box<EvalAltResult> > { pub fn dynamic_result_fn(x: usize) -> Result<Dynamic, Box<EvalAltResult> > {
Ok(Dynamic::from(super::do_something(x))) Ok(Dynamic::from(do_something(x)))
} }
} }
}; };
@ -381,7 +381,7 @@ mod generate_tests {
Token().input_types() Token().input_types()
} }
pub fn dynamic_result_fn(context: NativeCallContext, x: usize) -> Result<Dynamic, Box<EvalAltResult> > { pub fn dynamic_result_fn(context: NativeCallContext, x: usize) -> Result<Dynamic, Box<EvalAltResult> > {
Ok(Dynamic::from(super::do_something(context, x))) Ok(Dynamic::from(do_something(context, x)))
} }
} }
}; };
@ -425,7 +425,7 @@ mod generate_tests {
Token().input_types() Token().input_types()
} }
pub fn dynamic_result_fn() -> Result<Dynamic, Box<EvalAltResult> > { pub fn dynamic_result_fn() -> Result<Dynamic, Box<EvalAltResult> > {
Ok(super::return_dynamic()) Ok(return_dynamic())
} }
} }
}; };
@ -497,7 +497,7 @@ mod generate_tests {
Token().input_types() Token().input_types()
} }
pub fn dynamic_result_fn(x: usize, y: usize) -> Result<Dynamic, Box<EvalAltResult> > { pub fn dynamic_result_fn(x: usize, y: usize) -> Result<Dynamic, Box<EvalAltResult> > {
Ok(Dynamic::from(super::add_together(x, y))) Ok(Dynamic::from(add_together(x, y)))
} }
} }
}; };
@ -541,7 +541,7 @@ mod generate_tests {
Token().input_types() Token().input_types()
} }
pub fn dynamic_result_fn(x: &mut usize, y: usize) -> Result<Dynamic, Box<EvalAltResult> > { pub fn dynamic_result_fn(x: &mut usize, y: usize) -> Result<Dynamic, Box<EvalAltResult> > {
Ok(Dynamic::from(super::increment(x, y))) Ok(Dynamic::from(increment(x, y)))
} }
} }
}; };
@ -584,7 +584,7 @@ mod generate_tests {
Token().input_types() Token().input_types()
} }
pub fn dynamic_result_fn(message: &str) -> Result<Dynamic, Box<EvalAltResult> > { pub fn dynamic_result_fn(message: &str) -> Result<Dynamic, Box<EvalAltResult> > {
Ok(Dynamic::from(super::special_print(message))) Ok(Dynamic::from(special_print(message)))
} }
} }
}; };

View File

@ -1,4 +1,4 @@
error: return_raw functions must return Result<T, Box<EvalAltResult>> error: functions marked with 'return_raw' must return Result<Dynamic, Box<EvalAltResult>>
--> $DIR/export_fn_raw_noreturn.rs:10:5 --> $DIR/export_fn_raw_noreturn.rs:10:5
| |
10 | pub fn test_fn(input: &mut Point) { 10 | pub fn test_fn(input: &mut Point) {

View File

@ -1,4 +1,4 @@
error: return_raw functions must return Result<T, Box<EvalAltResult>> error: functions marked with 'return_raw' must return Result<Dynamic, Box<EvalAltResult>>
--> $DIR/export_mod_raw_noreturn.rs:12:5 --> $DIR/export_mod_raw_noreturn.rs:12:5
| |
12 | pub fn test_fn(input: &mut Point) { 12 | pub fn test_fn(input: &mut Point) {

View File

@ -1,4 +1,4 @@
error: property getter requires exactly 1 argument error: property getter requires exactly 1 parameter
--> $DIR/rhai_fn_getter_signature.rs:13:9 --> $DIR/rhai_fn_getter_signature.rs:13:9
| |
13 | pub fn test_fn(input: Point, value: bool) -> bool { 13 | pub fn test_fn(input: Point, value: bool) -> bool {

View File

@ -1,4 +1,4 @@
error: index getter requires exactly 2 arguments error: index getter requires exactly 2 parameters
--> $DIR/rhai_fn_index_getter_signature.rs:13:9 --> $DIR/rhai_fn_index_getter_signature.rs:13:9
| |
13 | pub fn test_fn(input: Point) -> bool { 13 | pub fn test_fn(input: Point) -> bool {

View File

@ -1,4 +1,4 @@
error: index setter requires exactly 3 arguments error: index setter requires exactly 3 parameters
--> $DIR/rhai_fn_setter_index_signature.rs:13:9 --> $DIR/rhai_fn_setter_index_signature.rs:13:9
| |
13 | pub fn test_fn(input: Point) -> bool { 13 | pub fn test_fn(input: Point) -> bool {

View File

@ -1,4 +1,4 @@
error: property setter requires exactly 2 arguments error: property setter requires exactly 2 parameters
--> $DIR/rhai_fn_setter_signature.rs:13:9 --> $DIR/rhai_fn_setter_signature.rs:13:9
| |
13 | pub fn test_fn(input: Point) -> bool { 13 | pub fn test_fn(input: Point) -> bool {