Skip to content

Commit 45c7242

Browse files
committed
Added parse_args! macro for parsing arguments
1 parent f1a63c3 commit 45c7242

File tree

2 files changed

+79
-7
lines changed

2 files changed

+79
-7
lines changed

example/skel/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use ext_php_rs::{
2-
call_user_func, info_table_end, info_table_row, info_table_start,
2+
call_user_func, info_table_end, info_table_row, info_table_start, parse_args,
33
php::{
44
args::{Arg, ArgParser},
55
class::ClassBuilder,
@@ -161,11 +161,11 @@ pub extern "C" fn skeleton_version(execute_data: &mut ExecutionData, _retval: &m
161161
#[no_mangle]
162162
pub extern "C" fn skeleton_array(execute_data: &mut ExecutionData, _retval: &mut Zval) {
163163
let mut arr = Arg::new("arr", DataType::Array);
164+
let mut x = Arg::new("x", DataType::Long);
165+
let mut y = Arg::new("y", DataType::Double);
166+
let mut z = Arg::new("z", DataType::Double);
164167

165-
let result = ArgParser::new(execute_data).arg(&mut arr).parse();
166-
if result.is_err() {
167-
return;
168-
}
168+
parse_args!(execute_data, arr, x, y; z);
169169

170170
let ht: ZendHashTable = arr.val().unwrap();
171171

src/macros.rs

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,83 @@ macro_rules! _info_table_row {
5151
///
5252
/// # Parameters
5353
///
54-
/// * `$fn` - The 'function' to call. Can be an [`Arg`] or a [`Zval`].
55-
/// * ...`$param` - The parameters to pass to the function. Must be able to be converted into a [`Zval`].
54+
/// * `$fn` - The 'function' to call. Can be an [Arg](crate::php::args::Arg) or a
55+
/// [Zval](crate::php::types::zval::Zval).
56+
/// * ...`$param` - The parameters to pass to the function. Must be able to be converted into a
57+
/// (crate::php::types::zval::Zval).
5658
#[macro_export]
5759
macro_rules! call_user_func {
5860
($fn: expr, $($param: expr),*) => {
5961
$fn.try_call(vec![$($param.into()),*])
6062
};
6163
}
64+
65+
/// Parses a given list of arguments using the [ArgParser](crate::php::args::ArgParser) class.
66+
///
67+
/// # Examples
68+
///
69+
/// This example parses all of the arguments. If one is invalid, execution of the function will
70+
/// stop at the `parse_args!` macro invocation. The user is notified via PHP's argument parsing
71+
/// system.
72+
///
73+
/// In this case, all of the arguments are required.
74+
///
75+
/// ```
76+
/// # #[macro_use] extern crate ext_php_rs;
77+
/// use ext_php_rs::{
78+
/// parse_args,
79+
/// php::{args::Arg, enums::DataType, execution_data::ExecutionData, types::zval::Zval},
80+
/// };
81+
///
82+
/// pub extern "C" fn example_fn(execute_data: &mut ExecutionData, _: &mut Zval) {
83+
/// let mut x = Arg::new("x", DataType::Long);
84+
/// let mut y = Arg::new("y", DataType::Long);
85+
/// let mut z = Arg::new("z", DataType::Long);
86+
///
87+
/// parse_args!(execute_data, x, y, z);
88+
/// }
89+
/// ```
90+
///
91+
/// This example is similar to the one above, apart from the fact that the `z` argument is not
92+
/// required. Note the semicolon seperating the first two arguments from the second.
93+
///
94+
/// ```
95+
/// use ext_php_rs::{
96+
/// parse_args,
97+
/// php::{args::Arg, enums::DataType, execution_data::ExecutionData, types::zval::Zval},
98+
/// };
99+
///
100+
/// pub extern "C" fn example_fn(execute_data: &mut ExecutionData, _: &mut Zval) {
101+
/// let mut x = Arg::new("x", DataType::Long);
102+
/// let mut y = Arg::new("y", DataType::Long);
103+
/// let mut z = Arg::new("z", DataType::Long);
104+
///
105+
/// parse_args!(execute_data, x, y; z);
106+
/// }
107+
/// ```
108+
#[macro_export]
109+
macro_rules! parse_args {
110+
($ed: expr, $($arg: expr),*) => {{
111+
use $crate::php::args::ArgParser;
112+
113+
let parser = ArgParser::new($ed)
114+
$(.arg(&mut $arg))*
115+
.parse();
116+
if parser.is_err() {
117+
return;
118+
}
119+
}};
120+
121+
($ed: expr, $($arg: expr),* ; $($opt: expr),*) => {{
122+
use $crate::php::args::ArgParser;
123+
124+
let parser = ArgParser::new($ed)
125+
$(.arg(&mut $arg))*
126+
.not_required()
127+
$(.arg(&mut $opt))*
128+
.parse();
129+
if parser.is_err() {
130+
return;
131+
}
132+
}};
133+
}

0 commit comments

Comments
 (0)