Code cleanup.

This commit is contained in:
Stephen Chung 2021-02-21 14:11:19 +08:00
parent fc7697d504
commit dd3b16403f
6 changed files with 79 additions and 78 deletions

View File

@ -101,7 +101,7 @@ pub fn parse_punctuated_items(
})
}
pub(crate) fn outer_item_attributes<T: ExportedParams>(
pub fn outer_item_attributes<T: ExportedParams>(
args: proc_macro2::TokenStream,
_attr_name: &str,
) -> syn::Result<T> {
@ -116,7 +116,7 @@ pub(crate) fn outer_item_attributes<T: ExportedParams>(
T::from_info(export_info)
}
pub(crate) fn inner_item_attributes<T: ExportedParams>(
pub fn inner_item_attributes<T: ExportedParams>(
attrs: &mut Vec<syn::Attribute>,
attr_name: &str,
) -> syn::Result<T> {
@ -132,7 +132,7 @@ pub(crate) fn inner_item_attributes<T: ExportedParams>(
}
}
pub(crate) fn deny_cfg_attr(attrs: &Vec<syn::Attribute>) -> syn::Result<()> {
pub fn deny_cfg_attr(attrs: &Vec<syn::Attribute>) -> syn::Result<()> {
if let Some(cfg_attr) = attrs
.iter()
.find(|a| a.path.get_ident().map(|i| *i == "cfg").unwrap_or(false))

View File

@ -1,10 +1,3 @@
#![allow(unused)]
#[cfg(no_std)]
use core::mem;
#[cfg(not(no_std))]
use std::mem;
#[cfg(no_std)]
use alloc::format;
#[cfg(not(no_std))]
@ -14,9 +7,8 @@ use std::borrow::Cow;
use quote::{quote, quote_spanned, ToTokens};
use syn::{
parse::{Parse, ParseStream, Parser},
parse::{Parse, ParseStream},
spanned::Spanned,
VisPublic, Visibility,
};
use crate::attrs::{ExportInfo, ExportScope, ExportedParams};
@ -83,7 +75,7 @@ impl FnSpecialAccess {
}
}
pub(crate) fn flatten_type_groups(ty: &syn::Type) -> &syn::Type {
pub fn flatten_type_groups(ty: &syn::Type) -> &syn::Type {
match ty {
syn::Type::Group(syn::TypeGroup { ref elem, .. })
| syn::Type::Paren(syn::TypeParen { ref elem, .. }) => flatten_type_groups(elem.as_ref()),
@ -91,7 +83,7 @@ pub(crate) fn flatten_type_groups(ty: &syn::Type) -> &syn::Type {
}
}
pub(crate) fn print_type(ty: &syn::Type) -> String {
pub fn print_type(ty: &syn::Type) -> String {
ty.to_token_stream()
.to_string()
.replace(" , ", ", ")
@ -104,7 +96,7 @@ pub(crate) fn print_type(ty: &syn::Type) -> String {
}
#[derive(Debug, Default)]
pub(crate) struct ExportedFnParams {
pub struct ExportedFnParams {
pub name: Vec<String>,
pub return_raw: bool,
pub pure: bool,
@ -191,53 +183,20 @@ impl ExportedParams for ExportedFnParams {
))
}
("name", Some(s)) => name.push(s.value()),
("set", Some(s)) => {
special = match special {
FnSpecialAccess::None => FnSpecialAccess::Property(Property::Set(
syn::Ident::new(&s.value(), s.span()),
)),
_ => return Err(syn::Error::new(item_span.span(), "conflicting setter")),
}
}
("get", Some(s)) => {
special = match special {
FnSpecialAccess::None => FnSpecialAccess::Property(Property::Get(
syn::Ident::new(&s.value(), s.span()),
)),
_ => return Err(syn::Error::new(item_span.span(), "conflicting getter")),
}
}
("index_get", None) => {
special = match special {
FnSpecialAccess::None => FnSpecialAccess::Index(Index::Get),
_ => {
return Err(syn::Error::new(item_span.span(), "conflicting index_get"))
}
}
("index_get", Some(s))
| ("index_set", Some(s))
| ("return_raw", Some(s))
| ("pure", Some(s))
| ("skip", Some(s))
| ("global", Some(s))
| ("internal", Some(s)) => {
return Err(syn::Error::new(s.span(), "extraneous value"))
}
("index_set", None) => {
special = match special {
FnSpecialAccess::None => FnSpecialAccess::Index(Index::Set),
_ => {
return Err(syn::Error::new(item_span.span(), "conflicting index_set"))
}
}
}
("index_get", Some(s)) | ("index_set", Some(s)) | ("return_raw", Some(s)) => {
return Err(syn::Error::new(s.span(), "extraneous value"))
}
("pure", None) => pure = true,
("pure", Some(s)) => return Err(syn::Error::new(s.span(), "extraneous value")),
("return_raw", None) => return_raw = true,
("return_raw", Some(s)) => {
return Err(syn::Error::new(s.span(), "extraneous value"))
}
("skip", None) => skip = true,
("skip", Some(s)) => return Err(syn::Error::new(s.span(), "extraneous value")),
("global", Some(s)) | ("internal", Some(s)) => {
return Err(syn::Error::new(s.span(), "extraneous value"))
}
("global", None) => match namespace {
FnNamespaceAccess::Unset => namespace = FnNamespaceAccess::Global,
FnNamespaceAccess::Global => (),
@ -248,6 +207,40 @@ impl ExportedParams for ExportedFnParams {
FnNamespaceAccess::Internal => (),
_ => return Err(syn::Error::new(key.span(), "conflicting namespace")),
},
("get", Some(s)) => {
special = match special {
FnSpecialAccess::None => FnSpecialAccess::Property(Property::Get(
syn::Ident::new(&s.value(), s.span()),
)),
_ => return Err(syn::Error::new(item_span.span(), "conflicting getter")),
}
}
("set", Some(s)) => {
special = match special {
FnSpecialAccess::None => FnSpecialAccess::Property(Property::Set(
syn::Ident::new(&s.value(), s.span()),
)),
_ => return Err(syn::Error::new(item_span.span(), "conflicting setter")),
}
}
("index_get", None) => {
special = match special {
FnSpecialAccess::None => FnSpecialAccess::Index(Index::Get),
_ => {
return Err(syn::Error::new(item_span.span(), "conflicting index_get"))
}
}
}
("index_set", None) => {
special = match special {
FnSpecialAccess::None => FnSpecialAccess::Index(Index::Set),
_ => {
return Err(syn::Error::new(item_span.span(), "conflicting index_set"))
}
}
}
(attr, _) => {
return Err(syn::Error::new(
key.span(),
@ -271,7 +264,7 @@ impl ExportedParams for ExportedFnParams {
}
#[derive(Debug)]
pub(crate) struct ExportedFn {
pub struct ExportedFn {
entire_span: proc_macro2::Span,
signature: syn::Signature,
visibility: syn::Visibility,
@ -418,11 +411,11 @@ impl Parse for ExportedFn {
}
impl ExportedFn {
pub(crate) fn params(&self) -> &ExportedFnParams {
pub fn params(&self) -> &ExportedFnParams {
&self.params
}
pub(crate) fn update_scope(&mut self, parent_scope: &ExportScope) {
pub fn update_scope(&mut self, parent_scope: &ExportScope) {
let keep = match (self.params.skip, parent_scope) {
(true, _) => false,
(_, ExportScope::PubOnly) => self.is_public(),
@ -432,35 +425,35 @@ impl ExportedFn {
self.params.skip = !keep;
}
pub(crate) fn skipped(&self) -> bool {
pub fn skipped(&self) -> bool {
self.params.skip
}
pub(crate) fn pass_context(&self) -> bool {
pub fn pass_context(&self) -> bool {
self.pass_context
}
pub(crate) fn signature(&self) -> &syn::Signature {
pub fn signature(&self) -> &syn::Signature {
&self.signature
}
pub(crate) fn mutable_receiver(&self) -> bool {
pub fn mutable_receiver(&self) -> bool {
self.mut_receiver
}
pub(crate) fn is_public(&self) -> bool {
pub fn is_public(&self) -> bool {
!matches!(self.visibility, syn::Visibility::Inherited)
}
pub(crate) fn span(&self) -> &proc_macro2::Span {
pub fn span(&self) -> &proc_macro2::Span {
&self.entire_span
}
pub(crate) fn name(&self) -> &syn::Ident {
pub fn name(&self) -> &syn::Ident {
&self.signature.ident
}
pub(crate) fn exported_names(&self) -> Vec<syn::LitStr> {
pub fn exported_names(&self) -> Vec<syn::LitStr> {
let mut literals: Vec<_> = self
.params
.name
@ -482,24 +475,24 @@ impl ExportedFn {
literals
}
pub(crate) fn exported_name<'n>(&'n self) -> Cow<'n, str> {
pub fn exported_name<'n>(&'n self) -> Cow<'n, str> {
self.params.name.last().map_or_else(
|| self.signature.ident.to_string().into(),
|s| s.as_str().into(),
)
}
pub(crate) fn arg_list(&self) -> impl Iterator<Item = &syn::FnArg> {
pub fn arg_list(&self) -> impl Iterator<Item = &syn::FnArg> {
let skip = if self.pass_context { 1 } else { 0 };
self.signature.inputs.iter().skip(skip)
}
pub(crate) fn arg_count(&self) -> usize {
pub fn arg_count(&self) -> usize {
let skip = if self.pass_context { 1 } else { 0 };
self.signature.inputs.len() - skip
}
pub(crate) fn return_type(&self) -> Option<&syn::Type> {
pub fn return_type(&self) -> Option<&syn::Type> {
if let syn::ReturnType::Type(_, ref ret_type) = self.signature.output {
Some(flatten_type_groups(ret_type))
} else {
@ -604,6 +597,7 @@ impl ExportedFn {
#input_names_block
#input_types_block
#return_type_block
#[allow(unused)]
#dyn_result_fn_block
}
}

View File

@ -20,7 +20,7 @@ use crate::attrs::{AttrItem, ExportInfo, ExportScope, ExportedParams};
use crate::function::ExportedFnParams;
#[derive(Debug, Clone, Eq, PartialEq, Hash, Default)]
pub(crate) struct ExportedModParams {
pub struct ExportedModParams {
pub name: String,
skip: bool,
pub scope: ExportScope,
@ -97,7 +97,7 @@ impl ExportedParams for ExportedModParams {
}
#[derive(Debug)]
pub(crate) struct Module {
pub struct Module {
mod_all: syn::ItemMod,
fns: Vec<ExportedFn>,
consts: Vec<ExportedConst>,

View File

@ -1,7 +1,7 @@
use quote::{quote, quote_spanned};
use syn::{parse::Parser, spanned::Spanned};
pub(crate) fn generated_module_path(
pub fn generated_module_path(
fn_path: &syn::Path,
) -> syn::punctuated::Punctuated<syn::PathSegment, syn::Token![::]> {
let mut g = fn_path.clone().segments;

View File

@ -9,9 +9,9 @@ use crate::function::{
};
use crate::module::Module;
pub(crate) type ExportedConst = (String, Box<syn::Type>, syn::Expr);
pub type ExportedConst = (String, Box<syn::Type>, syn::Expr);
pub(crate) fn generate_body(
pub fn generate_body(
fns: &mut [ExportedFn],
consts: &[ExportedConst],
sub_modules: &mut [Module],
@ -224,7 +224,7 @@ pub(crate) fn generate_body(
}
}
pub(crate) fn check_rename_collisions(fns: &Vec<ExportedFn>) -> Result<(), syn::Error> {
pub fn check_rename_collisions(fns: &Vec<ExportedFn>) -> Result<(), syn::Error> {
fn make_key(name: impl ToString, item_fn: &ExportedFn) -> String {
item_fn
.arg_list()

View File

@ -308,6 +308,7 @@ mod generate_tests {
pub fn token_return_type() -> &'static str {
Token().return_type()
}
#[allow(unused)]
pub fn dynamic_result_fn() -> Result<Dynamic, Box<EvalAltResult> > {
Ok(Dynamic::from(do_nothing()))
}
@ -362,6 +363,7 @@ mod generate_tests {
pub fn token_return_type() -> &'static str {
Token().return_type()
}
#[allow(unused)]
pub fn dynamic_result_fn(x: usize) -> Result<Dynamic, Box<EvalAltResult> > {
Ok(Dynamic::from(do_something(x)))
}
@ -416,6 +418,7 @@ mod generate_tests {
pub fn token_return_type() -> &'static str {
Token().return_type()
}
#[allow(unused)]
pub fn dynamic_result_fn(context: NativeCallContext, x: usize) -> Result<Dynamic, Box<EvalAltResult> > {
Ok(Dynamic::from(do_something(context, x)))
}
@ -472,6 +475,7 @@ mod generate_tests {
pub fn token_return_type() -> &'static str {
Token().return_type()
}
#[allow(unused)]
pub fn dynamic_result_fn() -> Result<Dynamic, Box<EvalAltResult> > {
Ok(return_dynamic())
}
@ -562,6 +566,7 @@ mod generate_tests {
pub fn token_return_type() -> &'static str {
Token().return_type()
}
#[allow(unused)]
pub fn dynamic_result_fn(x: usize, y: usize) -> Result<Dynamic, Box<EvalAltResult> > {
Ok(Dynamic::from(add_together(x, y)))
}
@ -623,6 +628,7 @@ mod generate_tests {
pub fn token_return_type() -> &'static str {
Token().return_type()
}
#[allow(unused)]
pub fn dynamic_result_fn(x: &mut usize, y: usize) -> Result<Dynamic, Box<EvalAltResult> > {
Ok(Dynamic::from(increment(x, y)))
}
@ -678,6 +684,7 @@ mod generate_tests {
pub fn token_return_type() -> &'static str {
Token().return_type()
}
#[allow(unused)]
pub fn dynamic_result_fn(message: &str) -> Result<Dynamic, Box<EvalAltResult> > {
Ok(Dynamic::from(special_print(message)))
}