Skip to content
Merged
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
9 changes: 4 additions & 5 deletions crates/macros/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use darling::{FromAttributes, ToTokens};
use proc_macro2::{Ident, Span, TokenStream};
use quote::{format_ident, quote};
use syn::spanned::Spanned as _;
use syn::PatType;
use syn::{FnArg, GenericArgument, ItemFn, Lit, PathArguments, Type, TypePath};
use syn::{Expr, FnArg, GenericArgument, ItemFn, PatType, PathArguments, Type, TypePath};

use crate::helpers::get_docs;
use crate::parsing::{PhpRename, Visibility};
Expand All @@ -28,7 +27,7 @@ pub fn wrap(input: &syn::Path) -> Result<TokenStream> {
struct PhpFunctionAttribute {
#[darling(flatten)]
rename: PhpRename,
defaults: HashMap<Ident, Lit>,
defaults: HashMap<Ident, Expr>,
optional: Option<Ident>,
vis: Option<Visibility>,
attrs: Vec<syn::Attribute>,
Expand Down Expand Up @@ -381,7 +380,7 @@ pub struct TypedArg<'a> {
pub name: &'a Ident,
pub ty: Type,
pub nullable: bool,
pub default: Option<Lit>,
pub default: Option<Expr>,
pub as_ref: bool,
pub variadic: bool,
}
Expand All @@ -395,7 +394,7 @@ pub struct Args<'a> {
impl<'a> Args<'a> {
pub fn parse_from_fnargs(
args: impl Iterator<Item = &'a FnArg>,
mut defaults: HashMap<Ident, Lit>,
mut defaults: HashMap<Ident, Expr>,
) -> Result<Self> {
let mut result = Self {
receiver: None,
Expand Down
6 changes: 3 additions & 3 deletions crates/macros/src/impl_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use darling::FromAttributes;
use proc_macro2::TokenStream;
use quote::quote;
use std::collections::{HashMap, HashSet};
use syn::{Ident, ItemImpl, Lit};
use syn::{Expr, Ident, ItemImpl};

use crate::constant::PhpConstAttribute;
use crate::function::{Args, CallType, Function, MethodReceiver};
Expand Down Expand Up @@ -68,7 +68,7 @@ struct MethodArgs {
/// The first optional argument of the function signature.
optional: Option<Ident>,
/// Default values for optional arguments.
defaults: HashMap<Ident, Lit>,
defaults: HashMap<Ident, Expr>,
/// Visibility of the method (public, protected, private).
vis: Visibility,
/// Method type.
Expand All @@ -80,7 +80,7 @@ struct MethodArgs {
pub struct PhpFunctionImplAttribute {
#[darling(flatten)]
rename: PhpRename,
defaults: HashMap<Ident, Lit>,
defaults: HashMap<Ident, Expr>,
optional: Option<Ident>,
vis: Option<Visibility>,
attrs: Vec<syn::Attribute>,
Expand Down
6 changes: 6 additions & 0 deletions tests/src/integration/defaults/defaults.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

assert(test_defaults_integer() === 42);
assert(test_defaults_integer(12) === 12);
assert(test_defaults_nullable_string() === null);
assert(test_defaults_nullable_string('test') === 'test');
27 changes: 27 additions & 0 deletions tests/src/integration/defaults/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use ext_php_rs::prelude::*;

#[php_function]
#[php(defaults(a = 42))]
pub fn test_defaults_integer(a: i32) -> i32 {
a
}

#[php_function]
#[php(defaults(a = None))]
pub fn test_defaults_nullable_string(a: Option<String>) -> Option<String> {
a
}

pub fn build_module(builder: ModuleBuilder) -> ModuleBuilder {
builder
.function(wrap_function!(test_defaults_integer))
.function(wrap_function!(test_defaults_nullable_string))
}

#[cfg(test)]
mod tests {
#[test]
fn defaults_works() {
assert!(crate::integration::test::run_php("defaults/defaults.php"));
}
}
1 change: 1 addition & 0 deletions tests/src/integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod bool;
pub mod callable;
pub mod class;
pub mod closure;
pub mod defaults;
pub mod globals;
pub mod iterator;
pub mod magic_method;
Expand Down
1 change: 1 addition & 0 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub fn build_module(module: ModuleBuilder) -> ModuleBuilder {
module = integration::callable::build_module(module);
module = integration::class::build_module(module);
module = integration::closure::build_module(module);
module = integration::defaults::build_module(module);
module = integration::globals::build_module(module);
module = integration::iterator::build_module(module);
module = integration::magic_method::build_module(module);
Expand Down