Skip to content

Commit dfee61f

Browse files
committed
Run macro doctests, use readme in docs
1 parent 46de219 commit dfee61f

File tree

2 files changed

+53
-29
lines changed

2 files changed

+53
-29
lines changed

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ Calling the function from PHP:
3030
var_dump(hello_world("David")); // string(13) "Hello, David!"
3131
```
3232

33-
For more information read the [guide](https://davidcole1340.github.io/ext-php-rs/guide).
33+
For more examples read the library
34+
[guide](https://davidcole1340.github.io/ext-php-rs/guide).
3435

3536
## Features
3637

@@ -57,7 +58,8 @@ Our main goal is to **make extension development easier.**
5758

5859
## Documentation
5960

60-
The library guide can be read [here](https://davidcole1340.github.io/ext-php-rs/guide).
61+
The library guide can be read
62+
[here](https://davidcole1340.github.io/ext-php-rs/guide).
6163

6264
The project is documented in-line, so viewing the `cargo` documentation is the
6365
best resource at the moment.
@@ -122,8 +124,11 @@ dual licensed as above, without any additional terms or conditions.
122124

123125
Licensed under either of
124126

125-
- Apache License, Version 2.0 ([LICENSE_APACHE](LICENSE_APACHE) or
126-
http://www.apache.org/licenses/LICENSE-2.0)
127-
- MIT license ([LICENSE_MIT](LICENSE_MIT) or http://opensource.org/licenses/MIT)
127+
- Apache License, Version 2.0 ([LICENSE_APACHE] or
128+
<http://www.apache.org/licenses/LICENSE-2.0>)
129+
- MIT license ([LICENSE_MIT] or <http://opensource.org/licenses/MIT>)
128130

129131
at your option.
132+
133+
[LICENSE_APACHE]: https://github.com/davidcole1340/ext-php-rs/blob/master/LICENSE_APACHE
134+
[LICENSE_MIT]: https://github.com/davidcole1340/ext-php-rs/blob/master/LICENSE_MIT

src/lib.rs

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
//! Bindings and abstractions for the Zend API to build PHP extensions natively in Rust.
2-
//!
3-
//! The library guide can be read [here.](https://davidcole1340.github.io/ext-php-rs/guide)
4-
1+
#![doc = include_str!("../README.md")]
52
#![deny(clippy::unwrap_used)]
63
#![allow(non_upper_case_globals)]
74
#![allow(non_camel_case_types)]
@@ -23,12 +20,17 @@ pub mod php;
2320
///
2421
/// # Example
2522
///
26-
/// ```ignore
23+
/// ```
24+
/// # use ext_php_rs::prelude::*;
2725
/// #[php_const]
2826
/// const TEST_CONSTANT: i32 = 100;
2927
///
3028
/// #[php_const]
3129
/// const ANOTHER_CONST: &str = "Hello, world!";
30+
/// # #[php_module]
31+
/// # pub fn module(module: ModuleBuilder) -> ModuleBuilder {
32+
/// # module
33+
/// # }
3234
/// ```
3335
pub use ext_php_rs_derive::php_const;
3436

@@ -58,16 +60,22 @@ pub use ext_php_rs_derive::php_const;
5860
/// can take either [`String`] or [`&str`], the optional parameter `offset` is an [`Option<i64>`],
5961
/// and the return value is a [`Zval`] as the return type is an integer-boolean union.
6062
///
61-
/// ```ignore
63+
/// ```
64+
/// # use ext_php_rs::prelude::*;
65+
/// # use ext_php_rs::php::types::zval::Zval;
6266
/// #[php_extern]
6367
/// extern "C" {
6468
/// fn strpos(haystack: &str, needle: &str, offset: Option<i64>) -> Zval;
6569
/// }
6670
///
6771
/// #[php_function]
6872
/// pub fn my_strpos() {
69-
/// assert_eq!(unsafe { strpos("Hello", "e", None) }, 1);
73+
/// assert_eq!(unsafe { strpos("Hello", "e", None) }.long(), Some(1));
7074
/// }
75+
/// # #[php_module]
76+
/// # pub fn module(module: ModuleBuilder) -> ModuleBuilder {
77+
/// # module
78+
/// # }
7179
/// ```
7280
///
7381
/// [`strpos`]: https://www.php.net/manual/en/function.strpos.php
@@ -153,18 +161,24 @@ pub use ext_php_rs_derive::php_extern;
153161
/// Creating a simple function which will return a string. The function still must be declared in
154162
/// the PHP module to be able to call.
155163
///
156-
/// ```ignore
164+
/// ```
165+
/// # use ext_php_rs::prelude::*;
157166
/// #[php_function]
158167
/// pub fn hello(name: String) -> String {
159168
/// format!("Hello, {}!", name)
160169
/// }
170+
/// # #[php_module]
171+
/// # pub fn module(module: ModuleBuilder) -> ModuleBuilder {
172+
/// # module
173+
/// # }
161174
/// ```
162175
///
163176
/// Parameters can also be deemed optional by passing the parameter name in the attribute options.
164177
/// This function takes one required parameter (`hello`) and two optional parameters (`description`
165178
/// and `age`).
166179
///
167-
/// ```ignore
180+
/// ```
181+
/// # use ext_php_rs::prelude::*;
168182
/// #[php_function(optional = "description")]
169183
/// pub fn hello(name: String, description: Option<String>, age: Option<i32>) -> String {
170184
/// let mut response = format!("Hello, {}!", name);
@@ -179,26 +193,25 @@ pub use ext_php_rs_derive::php_extern;
179193
///
180194
/// response
181195
/// }
196+
/// # #[php_module]
197+
/// # pub fn module(module: ModuleBuilder) -> ModuleBuilder {
198+
/// # module
199+
/// # }
182200
/// ```
183201
///
184202
/// Defaults can also be given in a similar fashion. For example, the above function could have
185203
/// default values for `description` and `age` by changing the attribute to the following:
186204
///
187-
/// ```ignore
205+
/// ```
206+
/// # use ext_php_rs::prelude::*;
188207
/// #[php_function(optional = "description", defaults(description = "David", age = 10))]
189208
/// pub fn hello(name: String, description: String, age: i32) -> String {
190-
/// let mut response = format!("Hello, {}!", name);
191-
///
192-
/// if let Some(description) = description {
193-
/// response.push_str(format!(" {}.", description).as_ref());
194-
/// }
195-
///
196-
/// if let Some(age) = age {
197-
/// response.push_str(format!(" I am {} year(s) old.", age).as_ref());
198-
/// }
199-
///
200-
/// response
209+
/// format!("Hello, {}! {}. I am {} year(s) old.", name, description, age)
201210
/// }
211+
/// # #[php_module]
212+
/// # pub fn module(module: ModuleBuilder) -> ModuleBuilder {
213+
/// # module
214+
/// # }
202215
/// ```
203216
///
204217
/// [`Result<T, E>`]: std::result::Result
@@ -245,7 +258,7 @@ pub use ext_php_rs_derive::php_function;
245258
///
246259
/// ```ignore
247260
/// #[derive(Debug, Default, ZendObjectHandler)]
248-
/// struct Human {
261+
/// pub struct Human {
249262
/// name: String,
250263
/// age: i32,
251264
/// }
@@ -302,7 +315,8 @@ pub use ext_php_rs_derive::php_impl;
302315
/// since the function is declared above the module it will automatically be registered when the
303316
/// module attribute is called.
304317
///
305-
/// ```ignore
318+
/// ```
319+
/// # use ext_php_rs::prelude::*;
306320
/// #[php_function]
307321
/// pub fn hello(name: String) -> String {
308322
/// format!("Hello, {}!", name)
@@ -330,11 +344,16 @@ pub use ext_php_rs_derive::php_module;
330344
///
331345
/// # Example
332346
///
333-
/// ```ignore
347+
/// ```
348+
/// # use ext_php_rs::prelude::*;
334349
/// #[php_startup]
335350
/// pub fn startup_function() {
336351
/// // do whatever you need to do...
337352
/// }
353+
/// # #[php_module]
354+
/// # pub fn module(module: ModuleBuilder) -> ModuleBuilder {
355+
/// # module
356+
/// # }
338357
/// ```
339358
pub use ext_php_rs_derive::php_startup;
340359

0 commit comments

Comments
 (0)