Simplify rhai_fn attribute.
This commit is contained in:
parent
09b75ed1a3
commit
767c39d974
@ -13,15 +13,9 @@ use std::collections::HashMap;
|
|||||||
use quote::{quote, quote_spanned};
|
use quote::{quote, quote_spanned};
|
||||||
use syn::{parse::Parse, parse::ParseStream, parse::Parser, spanned::Spanned};
|
use syn::{parse::Parse, parse::ParseStream, parse::Parser, spanned::Spanned};
|
||||||
|
|
||||||
use super::rhai_module::get_register_name;
|
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub(crate) struct ExportedFnParams {
|
pub(crate) struct ExportedFnParams {
|
||||||
pub name: Option<String>,
|
pub name: Option<String>,
|
||||||
pub get: Option<String>,
|
|
||||||
pub set: Option<String>,
|
|
||||||
pub index_get: bool,
|
|
||||||
pub index_set: bool,
|
|
||||||
pub return_raw: bool,
|
pub return_raw: bool,
|
||||||
pub skip: bool,
|
pub skip: bool,
|
||||||
}
|
}
|
||||||
@ -34,6 +28,16 @@ impl ExportedFnParams {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const FN_IDX_GET: &str = "index$get$";
|
||||||
|
pub const FN_IDX_SET: &str = "index$set$";
|
||||||
|
|
||||||
|
pub fn make_getter(id: &str) -> String {
|
||||||
|
format!("get${}", id)
|
||||||
|
}
|
||||||
|
pub fn make_setter(id: &str) -> String {
|
||||||
|
format!("set${}", id)
|
||||||
|
}
|
||||||
|
|
||||||
impl Parse for ExportedFnParams {
|
impl Parse for ExportedFnParams {
|
||||||
fn parse(args: ParseStream) -> syn::Result<Self> {
|
fn parse(args: ParseStream) -> syn::Result<Self> {
|
||||||
if args.is_empty() {
|
if args.is_empty() {
|
||||||
@ -82,22 +86,18 @@ impl Parse for ExportedFnParams {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut name = None;
|
let mut name = None;
|
||||||
let mut get = None;
|
|
||||||
let mut set = None;
|
|
||||||
let mut index_get = false;
|
|
||||||
let mut index_set = false;
|
|
||||||
let mut return_raw = false;
|
let mut return_raw = false;
|
||||||
let mut skip = false;
|
let mut skip = false;
|
||||||
for (ident, value) in attrs.drain() {
|
for (ident, value) in attrs.drain() {
|
||||||
match (ident.to_string().as_ref(), value) {
|
match (ident.to_string().as_ref(), value) {
|
||||||
("name", Some(s)) => name = Some(s.value()),
|
("name", Some(s)) => name = Some(s.value()),
|
||||||
("get", Some(s)) => get = Some(s.value()),
|
("get", Some(s)) => name = Some(make_getter(&s.value())),
|
||||||
("set", Some(s)) => set = Some(s.value()),
|
("set", Some(s)) => name = Some(make_setter(&s.value())),
|
||||||
("get", None) | ("set", None) | ("name", None) => {
|
("get", None) | ("set", None) | ("name", None) => {
|
||||||
return Err(syn::Error::new(ident.span(), "requires value"))
|
return Err(syn::Error::new(ident.span(), "requires value"))
|
||||||
}
|
}
|
||||||
("index_get", None) => index_get = true,
|
("index_get", None) => name = Some(FN_IDX_GET.to_string()),
|
||||||
("index_set", None) => index_get = true,
|
("index_set", None) => name = Some(FN_IDX_SET.to_string()),
|
||||||
("return_raw", None) => return_raw = true,
|
("return_raw", None) => return_raw = true,
|
||||||
("index_get", Some(s)) | ("index_set", Some(s)) | ("return_raw", Some(s)) => {
|
("index_get", Some(s)) | ("index_set", Some(s)) | ("return_raw", Some(s)) => {
|
||||||
return Err(syn::Error::new(s.span(), "extraneous value"))
|
return Err(syn::Error::new(s.span(), "extraneous value"))
|
||||||
@ -115,10 +115,6 @@ impl Parse for ExportedFnParams {
|
|||||||
|
|
||||||
Ok(ExportedFnParams {
|
Ok(ExportedFnParams {
|
||||||
name,
|
name,
|
||||||
get,
|
|
||||||
set,
|
|
||||||
index_get,
|
|
||||||
index_set,
|
|
||||||
return_raw,
|
return_raw,
|
||||||
skip,
|
skip,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
@ -373,7 +369,11 @@ impl ExportedFn {
|
|||||||
|
|
||||||
pub fn generate_impl(&self, on_type_name: &str) -> proc_macro2::TokenStream {
|
pub fn generate_impl(&self, on_type_name: &str) -> proc_macro2::TokenStream {
|
||||||
let sig_name = self.name().clone();
|
let sig_name = self.name().clone();
|
||||||
let name = get_register_name(self);
|
let name = self
|
||||||
|
.params
|
||||||
|
.name
|
||||||
|
.clone()
|
||||||
|
.unwrap_or_else(|| self.name().to_string());
|
||||||
|
|
||||||
let arg_count = self.arg_count();
|
let arg_count = self.arg_count();
|
||||||
let is_method_call = self.mutable_receiver();
|
let is_method_call = self.mutable_receiver();
|
||||||
|
@ -4,32 +4,6 @@ use crate::function::ExportedFn;
|
|||||||
|
|
||||||
pub(crate) type ExportedConst = (String, syn::Expr);
|
pub(crate) type ExportedConst = (String, syn::Expr);
|
||||||
|
|
||||||
pub(crate) fn get_register_name(function: &ExportedFn) -> String {
|
|
||||||
pub const FN_IDX_GET: &str = "index$get$";
|
|
||||||
pub const FN_IDX_SET: &str = "index$set$";
|
|
||||||
|
|
||||||
pub fn make_getter(id: &str) -> String {
|
|
||||||
format!("get${}", id)
|
|
||||||
}
|
|
||||||
pub fn make_setter(id: &str) -> String {
|
|
||||||
format!("set${}", id)
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(ref name) = function.params.name {
|
|
||||||
name.clone()
|
|
||||||
} else if let Some(ref name) = function.params.get {
|
|
||||||
make_getter(name).clone()
|
|
||||||
} else if let Some(ref name) = function.params.set {
|
|
||||||
make_setter(name).clone()
|
|
||||||
} else if function.params.index_get {
|
|
||||||
FN_IDX_GET.to_string()
|
|
||||||
} else if function.params.index_set {
|
|
||||||
FN_IDX_SET.to_string()
|
|
||||||
} else {
|
|
||||||
function.name().to_string()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn generate_body(
|
pub(crate) fn generate_body(
|
||||||
fns: &Vec<ExportedFn>,
|
fns: &Vec<ExportedFn>,
|
||||||
consts: &Vec<ExportedConst>,
|
consts: &Vec<ExportedConst>,
|
||||||
@ -55,7 +29,11 @@ pub(crate) fn generate_body(
|
|||||||
&format!("{}_token", function.name().to_string()),
|
&format!("{}_token", function.name().to_string()),
|
||||||
function.name().span(),
|
function.name().span(),
|
||||||
);
|
);
|
||||||
let reg_name = get_register_name(function);
|
let reg_name = function
|
||||||
|
.params
|
||||||
|
.name
|
||||||
|
.clone()
|
||||||
|
.unwrap_or_else(|| function.name().to_string());
|
||||||
let fn_literal = syn::LitStr::new(®_name, proc_macro2::Span::call_site());
|
let fn_literal = syn::LitStr::new(®_name, proc_macro2::Span::call_site());
|
||||||
let fn_input_types: Vec<syn::Expr> = function
|
let fn_input_types: Vec<syn::Expr> = function
|
||||||
.arg_list()
|
.arg_list()
|
||||||
|
Loading…
Reference in New Issue
Block a user