Skip to content

Commit 69f0d89

Browse files
committed
refactor(macro): change rename to change_case
`name` and `rename` were too similar, so `rename` has been changed to `change_case`. Also added a script to update the documentation of the macro crate. Refs: #466
1 parent 89aaab8 commit 69f0d89

File tree

9 files changed

+170
-93
lines changed

9 files changed

+170
-93
lines changed

crates/macros/src/impl_.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ enum MethodTy {
3030
#[darling(attributes(php), default)]
3131
pub struct PhpImpl {
3232
/// Rename methods to match the given rule.
33-
rename_methods: Option<RenameRule>,
33+
change_method_case: Option<RenameRule>,
3434
/// Rename constants to match the given rule.
35-
rename_constants: Option<RenameRule>,
35+
change_constant_case: Option<RenameRule>,
3636
}
3737

3838
pub fn parser(mut input: ItemImpl) -> Result<TokenStream> {
@@ -47,8 +47,9 @@ pub fn parser(mut input: ItemImpl) -> Result<TokenStream> {
4747

4848
let mut parsed = ParsedImpl::new(
4949
path,
50-
args.rename_methods.unwrap_or(RenameRule::Camel),
51-
args.rename_constants.unwrap_or(RenameRule::ScreamingSnake),
50+
args.change_method_case.unwrap_or(RenameRule::Camel),
51+
args.change_constant_case
52+
.unwrap_or(RenameRule::ScreamingSnake),
5253
);
5354
parsed.parse(input.items.iter_mut())?;
5455

@@ -116,8 +117,8 @@ impl MethodArgs {
116117
#[derive(Debug)]
117118
struct ParsedImpl<'a> {
118119
path: &'a syn::Path,
119-
rename_methods: RenameRule,
120-
rename_constants: RenameRule,
120+
change_method_case: RenameRule,
121+
change_constant_case: RenameRule,
121122
functions: Vec<FnBuilder>,
122123
constructor: Option<Function<'a>>,
123124
constants: Vec<Constant<'a>>,
@@ -165,12 +166,13 @@ impl<'a> ParsedImpl<'a> {
165166
/// # Parameters
166167
///
167168
/// * `path` - Path of the type the `impl` block is for.
168-
/// * `rename` - Rename rule for methods.
169+
/// * `rename_methods` - Rule to rename methods with.
170+
/// * `rename_constants` - Rule to rename constants with.
169171
fn new(path: &'a syn::Path, rename_methods: RenameRule, rename_constants: RenameRule) -> Self {
170172
Self {
171173
path,
172-
rename_methods,
173-
rename_constants,
174+
change_method_case: rename_methods,
175+
change_constant_case: rename_constants,
174176
functions: Vec::default(),
175177
constructor: Option::default(),
176178
constants: Vec::default(),
@@ -185,7 +187,7 @@ impl<'a> ParsedImpl<'a> {
185187
let attr = PhpConstAttribute::from_attributes(&c.attrs)?;
186188
let name = attr
187189
.rename
188-
.rename(c.ident.to_string(), self.rename_constants);
190+
.rename(c.ident.to_string(), self.change_constant_case);
189191
let docs = get_docs(&attr.attrs)?;
190192
c.attrs.retain(|attr| !attr.path().is_ident("php"));
191193

@@ -199,7 +201,7 @@ impl<'a> ParsedImpl<'a> {
199201
let attr = PhpFunctionImplAttribute::from_attributes(&method.attrs)?;
200202
let name = attr
201203
.rename
202-
.rename_method(method.sig.ident.to_string(), self.rename_methods);
204+
.rename_method(method.sig.ident.to_string(), self.change_method_case);
203205
let docs = get_docs(&attr.attrs)?;
204206
method.attrs.retain(|attr| !attr.path().is_ident("php"));
205207

crates/macros/src/lib.rs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use syn::{
1818

1919
extern crate proc_macro;
2020

21+
// BEGIN DOCS FROM classes.md
2122
/// # `#[php_class]` Attribute
2223
///
2324
/// Structs can be exported to PHP as classes with the `#[php_class]` attribute
@@ -33,7 +34,7 @@ extern crate proc_macro;
3334
/// - `name` - Changes the name of the class when exported to PHP. The Rust
3435
/// struct name is kept the same. If no name is given, the name of the struct
3536
/// is used. Useful for namespacing classes.
36-
/// - `rename` - Changes the case of the class name when exported to PHP.
37+
/// - `change_case` - Changes the case of the class name when exported to PHP.
3738
/// - `#[php(extends(ce = ce_fn, stub = "ParentClass"))]` - Sets the parent
3839
/// class of the class. Can only be used once. `ce_fn` must be a function with
3940
/// the signature `fn() -> &'static ClassEntry`.
@@ -50,8 +51,8 @@ extern crate proc_macro;
5051
///
5152
/// - `name` - Allows you to rename the property, e.g. `#[php(name =
5253
/// "new_name")]`
53-
/// - `rename` - Allows you to rename the property using rename rules, e.g.
54-
/// `#[php(rename = PascalCase)]`
54+
/// - `change_case` - Allows you to rename the property using rename rules, e.g.
55+
/// `#[php(change_case = PascalCase)]`
5556
///
5657
/// ## Restrictions
5758
///
@@ -199,6 +200,7 @@ extern crate proc_macro;
199200
/// }
200201
/// # fn main() {}
201202
/// ````
203+
// END DOCS FROM classes.md
202204
#[proc_macro_attribute]
203205
pub fn php_class(args: TokenStream, input: TokenStream) -> TokenStream {
204206
let input = parse_macro_input!(input as ItemStruct);
@@ -211,6 +213,7 @@ pub fn php_class(args: TokenStream, input: TokenStream) -> TokenStream {
211213
.into()
212214
}
213215

216+
// BEGIN DOCS FROM function.md
214217
/// # `#[php_function]` Attribute
215218
///
216219
/// Used to annotate functions which should be exported to PHP. Note that this
@@ -365,6 +368,7 @@ pub fn php_class(args: TokenStream, input: TokenStream) -> TokenStream {
365368
/// You can also return a `Result` from the function. The error variant will be
366369
/// translated into an exception and thrown. See the section on
367370
/// [exceptions](../exceptions.md) for more details.
371+
// END DOCS FROM function.md
368372
#[proc_macro_attribute]
369373
pub fn php_function(args: TokenStream, input: TokenStream) -> TokenStream {
370374
let input = parse_macro_input!(input as ItemFn);
@@ -377,6 +381,7 @@ pub fn php_function(args: TokenStream, input: TokenStream) -> TokenStream {
377381
.into()
378382
}
379383

384+
// BEGIN DOCS FROM constant.md
380385
/// # `#[php_const]` Attribute
381386
///
382387
/// Exports a Rust constant as a global PHP constant. The constant can be any
@@ -389,8 +394,8 @@ pub fn php_function(args: TokenStream, input: TokenStream) -> TokenStream {
389394
///
390395
/// - `name` - Allows you to rename the property, e.g. `#[php(name =
391396
/// "new_name")]`
392-
/// - `rename` - Allows you to rename the property using rename rules, e.g.
393-
/// `#[php(rename = PascalCase)]`
397+
/// - `change_case` - Allows you to rename the property using rename rules, e.g.
398+
/// `#[php(change_case = PascalCase)]`
394399
///
395400
/// ## Examples
396401
///
@@ -428,6 +433,7 @@ pub fn php_function(args: TokenStream, input: TokenStream) -> TokenStream {
428433
/// var_dump(I_AM_RENAMED); // int(42)
429434
/// var_dump(MANUAL_CONSTANT); // string(12) "Hello world!"
430435
/// ```
436+
// END DOCS FROM constant.md
431437
#[proc_macro_attribute]
432438
pub fn php_const(args: TokenStream, input: TokenStream) -> TokenStream {
433439
let input = parse_macro_input!(input as ItemConst);
@@ -440,6 +446,7 @@ pub fn php_const(args: TokenStream, input: TokenStream) -> TokenStream {
440446
.into()
441447
}
442448

449+
// BEGIN DOCS FROM module.md
443450
/// # `#[php_module]` Attribute
444451
///
445452
/// The module macro is used to annotate the `get_module` function, which is
@@ -506,6 +513,7 @@ pub fn php_const(args: TokenStream, input: TokenStream) -> TokenStream {
506513
/// }
507514
/// # fn main() {}
508515
/// ```
516+
// END DOCS FROM module.md
509517
#[proc_macro_attribute]
510518
pub fn php_module(args: TokenStream, input: TokenStream) -> TokenStream {
511519
let input = parse_macro_input!(input as ItemFn);
@@ -518,6 +526,7 @@ pub fn php_module(args: TokenStream, input: TokenStream) -> TokenStream {
518526
.into()
519527
}
520528

529+
// BEGIN DOCS FROM impl.md
521530
/// # `#[php_impl]` Attribute
522531
///
523532
/// You can export an entire `impl` block to PHP. This exports all methods as
@@ -532,6 +541,21 @@ pub fn php_module(args: TokenStream, input: TokenStream) -> TokenStream {
532541
/// If you want to use async Rust, use `#[php_async_impl]`, instead: see [here
533542
/// &raquo;](./async_impl.md) for more info.
534543
///
544+
/// ## Options
545+
///
546+
/// By default all constants are renamed to `UPPER_CASE` and all methods are
547+
/// renamed to camelCase. This can be changed by passing the
548+
/// `change_method_case` and `change_constant_case` as `#[php]` attributes on
549+
/// the `impl` block. The options are:
550+
///
551+
/// - `#[php(change_method_case = "snake_case")]` - Renames the method to snake
552+
/// case.
553+
/// - `#[php(change_constant_case = "snake_case")]` - Renames the constant to
554+
/// snake case.
555+
///
556+
/// See the [`name` and `change_case`](./php.md#name-and-change_case) section
557+
/// for a list of all available cases.
558+
///
535559
/// ## Methods
536560
///
537561
/// Methods basically follow the same rules as functions, so read about the
@@ -546,17 +570,6 @@ pub fn php_module(args: TokenStream, input: TokenStream) -> TokenStream {
546570
/// must be named `self_`. This can also be used to return a reference to
547571
/// `$this`.
548572
///
549-
/// By default, all methods are renamed in PHP to the camel-case variant of the
550-
/// Rust method name. This can be changed on the `#[php_impl]` attribute, by
551-
/// passing one of the following as the `rename_methods` option:
552-
///
553-
/// - `"none"` - does not rename the methods.
554-
/// - `"camelCase"` - renames all methods to camel case (default).
555-
/// - `"snake_case"` - renames all methods to snake case.
556-
///
557-
/// For example, to disable renaming, change the `#[php_impl]` attribute to
558-
/// `#[php_impl(rename_methods = "none")]`.
559-
///
560573
/// The rest of the options are passed as separate attributes:
561574
///
562575
/// - `#[php(defaults(i = 5, b = "hello")]` - Sets the default value for
@@ -602,8 +615,9 @@ pub fn php_module(args: TokenStream, input: TokenStream) -> TokenStream {
602615
/// attributes. By default, the `get_` or `set_` prefix is trimmed from the
603616
/// start of the function name, and the remainder is used as the property name.
604617
///
605-
/// If you want to use a different name for the property, you can pass a
606-
/// `rename` option to the attribute which will change the property name.
618+
/// If you want to use a different name for the property, you can pass a `name`
619+
/// or `change_case` option to the `#[php]` attribute which will change the
620+
/// property name.
607621
///
608622
/// Properties do not necessarily have to have both a getter and a setter, if
609623
/// the property is immutable the setter can be omitted, and vice versa for
@@ -696,6 +710,7 @@ pub fn php_module(args: TokenStream, input: TokenStream) -> TokenStream {
696710
/// ```
697711
///
698712
/// [`php_async_impl`]: ./async_impl.md
713+
// END DOCS FROM impl.md
699714
#[proc_macro_attribute]
700715
pub fn php_impl(args: TokenStream, input: TokenStream) -> TokenStream {
701716
let input = parse_macro_input!(input as ItemImpl);
@@ -708,6 +723,7 @@ pub fn php_impl(args: TokenStream, input: TokenStream) -> TokenStream {
708723
.into()
709724
}
710725

726+
// BEGIN DOCS FROM extern.md
711727
/// # `#[php_extern]` Attribute
712728
///
713729
/// Attribute used to annotate `extern` blocks which are deemed as PHP
@@ -771,6 +787,7 @@ pub fn php_impl(args: TokenStream, input: TokenStream) -> TokenStream {
771787
/// [`strpos`]: https://www.php.net/manual/en/function.strpos.php
772788
/// [`IntoZval`]: crate::convert::IntoZval
773789
/// [`Zval`]: crate::types::Zval
790+
// END DOCS FROM extern.md
774791
#[proc_macro_attribute]
775792
pub fn php_extern(_: TokenStream, input: TokenStream) -> TokenStream {
776793
let input = parse_macro_input!(input as ItemForeignMod);
@@ -780,6 +797,7 @@ pub fn php_extern(_: TokenStream, input: TokenStream) -> TokenStream {
780797
.into()
781798
}
782799

800+
// BEGIN DOCS FROM zval_convert.md
783801
/// # `ZvalConvert` Derive Macro
784802
///
785803
/// The `#[derive(ZvalConvert)]` macro derives the `FromZval` and `IntoZval`
@@ -932,6 +950,7 @@ pub fn php_extern(_: TokenStream, input: TokenStream) -> TokenStream {
932950
/// test_union(null); // UnionExample::None
933951
/// var_dump(give_union()); // int(5)
934952
/// ```
953+
// END DOCS FROM zval_convert.md
935954
#[proc_macro_derive(ZvalConvert)]
936955
pub fn zval_convert_derive(input: TokenStream) -> TokenStream {
937956
let input = parse_macro_input!(input as DeriveInput);

0 commit comments

Comments
 (0)