Skip to content

Commit 8746e20

Browse files
committed
Add some Val as_* apis.
1 parent 58d4fb7 commit 8746e20

File tree

12 files changed

+185
-29
lines changed

12 files changed

+185
-29
lines changed

examples/hello/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ fn module_init(_args: ModuleArgs) -> bool {
1313
true
1414
}
1515

16-
fn say_hello(arguments: &mut [Val]) -> impl SetVal {
17-
let name = arguments[0].as_string();
18-
format!("Hello, {}!\n", name)
16+
fn say_hello(arguments: &mut [Val]) -> phper::Result<String> {
17+
let name = arguments[0].as_string_value()?;
18+
Ok(format!("Hello, {}!\n", name))
1919
}
2020

2121
fn throw_exception(_: &mut [Val]) -> phper::Result<()> {
@@ -66,9 +66,9 @@ pub fn get_module() -> Module {
6666
foo_class.add_property("foo", 100);
6767
foo_class.add_method(
6868
"getFoo",
69-
|this: &mut Object, _: &mut [Val]| {
69+
|this: &mut Object, _: &mut [Val]| -> phper::Result<Val> {
7070
let prop = this.get_property("foo");
71-
Val::new(prop.as_string())
71+
Ok(Val::new(prop.as_string_value()?))
7272
},
7373
vec![],
7474
);

examples/http-client/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ crate-type = ["cdylib"]
1313

1414
[dependencies]
1515
phper = { version = "0.2.0-alpha.2", path = "../../phper" }
16+
reqwest = { version = "0.11.3", features = ["blocking"] }
1617

1718
[dev-dependencies]
1819
phper-test = { version = "0.2.0-alpha.2", path = "../../phper-test" }

examples/http-client/LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../LICENSE

examples/http-client/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# hello
2+
3+
Http client example.
4+
5+
## Environment
6+
7+
```bash
8+
# Specify if php isn't installed globally.
9+
export PHP_CONFIG = <Your path of php-config>
10+
```
11+
12+
## Build
13+
14+
```bash
15+
cargo build --release
16+
```
17+
18+
## Test
19+
20+
```bash
21+
cargo test --release
22+
```
23+
24+
## Install
25+
26+
```bash
27+
cargo run --release -- install
28+
```
29+
30+
## License
31+
32+
[Unlicense](https://github.com/jmjoy/phper/blob/master/LICENSE).
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use reqwest::blocking::{Client, ClientBuilder};
2+
use std::time::Duration;
3+
4+
pub struct HttpClient {
5+
client: Client,
6+
}
7+
8+
impl HttpClient {
9+
pub fn new() -> reqwest::Result<Self> {
10+
let client = ClientBuilder::new()
11+
.timeout(Duration::from_secs(15))
12+
.build()?;
13+
Ok(Self { client })
14+
}
15+
}

examples/http-client/src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use phper::{modules::Module, php_get_module};
1+
use crate::http_client::HttpClient;
2+
use phper::{classes::StdClass, modules::Module, php_get_module};
3+
4+
pub mod http_client;
25

36
#[php_get_module]
47
pub fn get_module() -> Module {
@@ -8,7 +11,9 @@ pub fn get_module() -> Module {
811
env!("CARGO_PKG_AUTHORS"),
912
);
1013

11-
// ...
14+
let client = HttpClient::new();
15+
let client_class = StdClass::new();
16+
module.add_class("HttpClient", client_class);
1217

1318
module
1419
}

examples/log/src/lib.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,50 @@ pub fn get_module() -> Module {
1313

1414
module.add_function(
1515
"log_say",
16-
|params: &mut [Val]| {
17-
let message = params[0].as_string();
16+
|params: &mut [Val]| -> phper::Result<()> {
17+
let message = params[0].as_string_value()?;
1818
echo!("Hello, {}!", message);
19+
Ok(())
1920
},
2021
vec![Argument::by_val("message")],
2122
);
2223

2324
module.add_function(
2425
"log_notice",
25-
|params: &mut [Val]| {
26-
let message = params[0].as_string();
26+
|params: &mut [Val]| -> phper::Result<()> {
27+
let message = params[0].as_string_value()?;
2728
notice!("Something happened: {}", message);
29+
Ok(())
2830
},
2931
vec![Argument::by_val("message")],
3032
);
3133

3234
module.add_function(
3335
"log_warning",
34-
|params: &mut [Val]| {
35-
let message = params[0].as_string();
36+
|params: &mut [Val]| -> phper::Result<()> {
37+
let message = params[0].as_string_value()?;
3638
warning!("Something warning: {}", message);
39+
Ok(())
3740
},
3841
vec![Argument::by_val("message")],
3942
);
4043

4144
module.add_function(
4245
"log_error",
43-
|params: &mut [Val]| {
44-
let message = params[0].as_string();
46+
|params: &mut [Val]| -> phper::Result<()> {
47+
let message = params[0].as_string_value()?;
4548
error!("Something gone failed: {}", message);
49+
Ok(())
4650
},
4751
vec![Argument::by_val("message")],
4852
);
4953

5054
module.add_function(
5155
"log_deprecated",
52-
|params: &mut [Val]| {
53-
let message = params[0].as_string();
56+
|params: &mut [Val]| -> phper::Result<()> {
57+
let message = params[0].as_string_value()?;
5458
deprecated!("Something deprecated: {}", message);
59+
Ok(())
5560
},
5661
vec![Argument::by_val("message")],
5762
);

phper-macros/src/inner.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub(crate) fn php_get_module(_attr: TokenStream, input: TokenStream) -> TokenStr
2222

2323
let result = quote! {
2424
#[no_mangle]
25+
#[doc(hidden)]
2526
#(#attrs)*
2627
#vis extern "C" fn #name() -> *const ::phper::sys::zend_module_entry {
2728
fn internal(#inputs) #ret {

phper/src/errors.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ pub enum Error {
1717
#[error(transparent)]
1818
FromBytesWithNul(#[from] FromBytesWithNulError),
1919

20+
#[error(transparent)]
21+
TypeError(#[from] TypeError),
22+
2023
#[error(transparent)]
2124
Other(#[from] anyhow::Error),
2225
}
@@ -29,6 +32,19 @@ impl Error {
2932
}
3033
}
3134

35+
/// PHP type error.
36+
#[derive(thiserror::Error, Debug)]
37+
#[error("{message}")]
38+
pub struct TypeError {
39+
message: String,
40+
}
41+
42+
impl TypeError {
43+
pub fn new(message: String) -> Self {
44+
Self { message }
45+
}
46+
}
47+
3248
/// PHP Throwable, can cause throwing an exception when setting to [crate::values::Val].
3349
pub trait Throwable: error::Error {
3450
fn class_entity(&self) -> *const ClassEntity;

phper/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ pub mod modules;
135135
pub mod objects;
136136
pub mod output;
137137
pub mod strings;
138+
pub mod types;
138139
mod utils;
139140
pub mod values;
140141

0 commit comments

Comments
 (0)