Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions crates/macros/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use quote::{TokenStreamExt, quote};
use syn::{Attribute, Expr, Fields, ItemStruct};

use crate::helpers::get_docs;
use crate::parsing::{PhpRename, RenameRule};
use crate::parsing::{PhpNameContext, PhpRename, RenameRule, ident_to_php_name, validate_php_name};
use crate::prelude::*;

#[derive(FromAttributes, Debug, Default)]
Expand Down Expand Up @@ -44,7 +44,10 @@ impl ToTokens for ClassEntryAttribute {
pub fn parser(mut input: ItemStruct) -> Result<TokenStream> {
let attr = StructAttributes::from_attributes(&input.attrs)?;
let ident = &input.ident;
let name = attr.rename.rename(ident.to_string(), RenameRule::Pascal);
let name = attr
.rename
.rename(ident_to_php_name(ident), RenameRule::Pascal);
validate_php_name(&name, PhpNameContext::Class);
let docs = get_docs(&attr.attrs)?;
input.attrs.retain(|attr| !attr.path().is_ident("php"));

Expand Down Expand Up @@ -110,9 +113,12 @@ struct Property<'a> {

impl Property<'_> {
pub fn name(&self) -> String {
self.attr
let name = self
.attr
.rename
.rename(self.ident.to_string(), RenameRule::Camel)
.rename(ident_to_php_name(self.ident), RenameRule::Camel);
validate_php_name(&name, PhpNameContext::Property);
name
}
}

Expand Down
5 changes: 3 additions & 2 deletions crates/macros/src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use quote::{format_ident, quote};
use syn::ItemConst;

use crate::helpers::get_docs;
use crate::parsing::{PhpRename, RenameRule};
use crate::parsing::{PhpNameContext, PhpRename, RenameRule, ident_to_php_name, validate_php_name};
use crate::prelude::*;

const INTERNAL_CONST_DOC_PREFIX: &str = "_internal_const_docs_";
Expand All @@ -25,7 +25,8 @@ pub fn parser(mut item: ItemConst) -> Result<TokenStream> {

let name = attr
.rename
.rename(item.ident.to_string(), RenameRule::ScreamingSnake);
.rename(ident_to_php_name(&item.ident), RenameRule::ScreamingSnake);
validate_php_name(&name, PhpNameContext::Constant);
let name_ident = format_ident!("{INTERNAL_CONST_NAME_PREFIX}{}", item.ident);

let docs = get_docs(&attr.attrs)?;
Expand Down
11 changes: 8 additions & 3 deletions crates/macros/src/enum_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use syn::{Fields, Ident, ItemEnum, Lit};

use crate::{
helpers::get_docs,
parsing::{PhpRename, RenameRule, Visibility},
parsing::{
PhpNameContext, PhpRename, RenameRule, Visibility, ident_to_php_name, validate_php_name,
},
prelude::*,
};

Expand Down Expand Up @@ -84,7 +86,7 @@ pub fn parser(mut input: ItemEnum) -> Result<TokenStream> {
cases.push(EnumCase {
ident: variant.ident.clone(),
name: variant_attr.rename.rename(
variant.ident.to_string(),
ident_to_php_name(&variant.ident),
php_attr.rename_cases.unwrap_or(RenameRule::Pascal),
),
attrs: variant_attr,
Expand Down Expand Up @@ -137,7 +139,10 @@ impl<'a> Enum<'a> {
flags: Option<String>,
discriminant_type: DiscriminantType,
) -> Self {
let name = attrs.rename.rename(ident.to_string(), RenameRule::Pascal);
let name = attrs
.rename
.rename(ident_to_php_name(ident), RenameRule::Pascal);
validate_php_name(&name, PhpNameContext::Enum);

Self {
ident,
Expand Down
3 changes: 2 additions & 1 deletion crates/macros/src/extern_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use syn::{
spanned::Spanned as _, token::Unsafe,
};

use crate::parsing::ident_to_php_name;
use crate::prelude::*;

pub fn parser(input: ItemForeignMod) -> Result<TokenStream> {
Expand All @@ -27,7 +28,7 @@ fn parse_function(mut func: ForeignItemFn) -> Result<TokenStream> {

let Signature { ident, .. } = &sig;

let name = ident.to_string();
let name = ident_to_php_name(ident);
let params = sig
.inputs
.iter()
Expand Down
20 changes: 9 additions & 11 deletions crates/macros/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use syn::spanned::Spanned as _;
use syn::{Expr, FnArg, GenericArgument, ItemFn, PatType, PathArguments, Type, TypePath};

use crate::helpers::get_docs;
use crate::parsing::{PhpRename, RenameRule, Visibility};
use crate::parsing::{
PhpNameContext, PhpRename, RenameRule, Visibility, ident_to_php_name, validate_php_name,
};
use crate::prelude::*;
use crate::syn_ext::DropLifetimes;

Expand Down Expand Up @@ -44,15 +46,11 @@ pub fn parser(mut input: ItemFn) -> Result<TokenStream> {

let docs = get_docs(&php_attr.attrs)?;

let func = Function::new(
&input.sig,
php_attr
.rename
.rename(input.sig.ident.to_string(), RenameRule::Snake),
args,
php_attr.optional,
docs,
);
let func_name = php_attr
.rename
.rename(ident_to_php_name(&input.sig.ident), RenameRule::Snake);
validate_php_name(&func_name, PhpNameContext::Function);
let func = Function::new(&input.sig, func_name, args, php_attr.optional, docs);
let function_impl = func.php_function_impl();

Ok(quote! {
Expand Down Expand Up @@ -625,7 +623,7 @@ impl TypedArg<'_> {
/// Returns a token stream containing the `Arg` definition to be passed to
/// `ext-php-rs`.
fn arg_builder(&self) -> TokenStream {
let name = self.name.to_string();
let name = ident_to_php_name(self.name);
let ty = self.clean_ty();
let null = if self.nullable {
Some(quote! { .allow_null() })
Expand Down
15 changes: 10 additions & 5 deletions crates/macros/src/impl_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use syn::{Expr, Ident, ItemImpl};
use crate::constant::PhpConstAttribute;
use crate::function::{Args, CallType, Function, MethodReceiver};
use crate::helpers::get_docs;
use crate::parsing::{PhpRename, RenameRule, Visibility};
use crate::parsing::{
PhpNameContext, PhpRename, RenameRule, Visibility, ident_to_php_name, validate_php_name,
};
use crate::prelude::*;

/// Method types.
Expand Down Expand Up @@ -187,7 +189,8 @@ impl<'a> ParsedImpl<'a> {
let attr = PhpConstAttribute::from_attributes(&c.attrs)?;
let name = attr
.rename
.rename(c.ident.to_string(), self.change_constant_case);
.rename(ident_to_php_name(&c.ident), self.change_constant_case);
validate_php_name(&name, PhpNameContext::Constant);
let docs = get_docs(&attr.attrs)?;
c.attrs.retain(|attr| !attr.path().is_ident("php"));

Expand All @@ -199,9 +202,11 @@ impl<'a> ParsedImpl<'a> {
}
syn::ImplItem::Fn(method) => {
let attr = PhpFunctionImplAttribute::from_attributes(&method.attrs)?;
let name = attr
.rename
.rename_method(method.sig.ident.to_string(), self.change_method_case);
let name = attr.rename.rename_method(
ident_to_php_name(&method.sig.ident),
self.change_method_case,
);
validate_php_name(&name, PhpNameContext::Method);
let docs = get_docs(&attr.attrs)?;
method.attrs.retain(|attr| !attr.path().is_ident("php"));

Expand Down
26 changes: 14 additions & 12 deletions crates/macros/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use quote::{ToTokens, format_ident, quote};
use syn::{Expr, Ident, ItemTrait, Path, TraitItem, TraitItemConst, TraitItemFn};

use crate::impl_::{FnBuilder, MethodModifier};
use crate::parsing::{PhpRename, RenameRule, Visibility};
use crate::parsing::{
PhpNameContext, PhpRename, RenameRule, Visibility, ident_to_php_name, validate_php_name,
};
use crate::prelude::*;

const INTERNAL_INTERFACE_NAME_PREFIX: &str = "PhpInterface";
Expand Down Expand Up @@ -196,7 +198,10 @@ impl<'a> Parse<'a, InterfaceData<'a>> for ItemTrait {
fn parse(&'a mut self) -> Result<InterfaceData<'a>> {
let attrs = TraitAttributes::from_attributes(&self.attrs)?;
let ident = &self.ident;
let name = attrs.rename.rename(ident.to_string(), RenameRule::Pascal);
let name = attrs
.rename
.rename(ident_to_php_name(ident), RenameRule::Pascal);
validate_php_name(&name, PhpNameContext::Interface);
let docs = get_docs(&attrs.attrs)?;
self.attrs.clean_php();
let interface_name = format_ident!("{INTERNAL_INTERFACE_NAME_PREFIX}{ident}");
Expand Down Expand Up @@ -277,16 +282,12 @@ fn parse_trait_item_fn(
modifiers.insert(MethodModifier::Static);
}

let f = Function::new(
&fn_item.sig,
php_attr.rename.rename(
fn_item.sig.ident.to_string(),
change_case.unwrap_or(RenameRule::Camel),
),
args,
php_attr.optional,
docs,
let method_name = php_attr.rename.rename(
ident_to_php_name(&fn_item.sig.ident),
change_case.unwrap_or(RenameRule::Camel),
);
validate_php_name(&method_name, PhpNameContext::Method);
let f = Function::new(&fn_item.sig, method_name, args, php_attr.optional, docs);

if php_attr.constructor.is_present() {
Ok(MethodKind::Constructor(f))
Expand Down Expand Up @@ -336,9 +337,10 @@ fn parse_trait_item_const(

let attr = PhpConstAttribute::from_attributes(&const_item.attrs)?;
let name = attr.rename.rename(
const_item.ident.to_string(),
ident_to_php_name(&const_item.ident),
change_case.unwrap_or(RenameRule::ScreamingSnake),
);
validate_php_name(&name, PhpNameContext::Constant);
let docs = get_docs(&attr.attrs)?;
const_item.attrs.clean_php();

Expand Down
Loading
Loading