Skip to content

Commit 479ae41

Browse files
committed
Change Throwable macro attribute, and Val drop_value method.
1 parent f14ca9f commit 479ae41

File tree

12 files changed

+42
-73
lines changed

12 files changed

+42
-73
lines changed

examples/http-client/src/client.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::{
2-
errors::HttpClientError, replace_and_get, replace_and_set, request::REQUEST_BUILDER_CLASS_NAME,
2+
errors::HttpClientError,
3+
request::REQUEST_BUILDER_CLASS_NAME,
4+
utils::{replace_and_get, replace_and_set},
35
};
46
use phper::{
57
classes::{ClassEntry, DynamicClass, Visibility},

examples/http-client/src/errors.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ use phper::{
55

66
const EXCEPTION_CLASS_NAME: &'static str = "HttpClient\\HttpClientException";
77

8-
#[derive(thiserror::Error, Debug)]
8+
#[derive(Debug, thiserror::Error, phper::Throwable)]
9+
#[throwable_class(EXCEPTION_CLASS_NAME)]
910
pub enum HttpClientError {
1011
#[error(transparent)]
12+
#[throwable(transparent)]
1113
Phper(#[from] phper::Error),
1214

1315
#[error(transparent)]
@@ -20,15 +22,6 @@ pub enum HttpClientError {
2022
ResponseHadRead,
2123
}
2224

23-
impl Throwable for HttpClientError {
24-
fn class_entry(&self) -> &StatelessClassEntry {
25-
match self {
26-
HttpClientError::Phper(e) => e.class_entry(),
27-
_ => ClassEntry::from_globals(EXCEPTION_CLASS_NAME).unwrap(),
28-
}
29-
}
30-
}
31-
3225
pub fn make_exception_class() -> DynamicClass<()> {
3326
let mut exception_class = DynamicClass::new(EXCEPTION_CLASS_NAME);
3427
exception_class.extends("Exception");

examples/http-client/src/lib.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use crate::{
55
response::make_response_class,
66
};
77
use phper::{modules::Module, php_get_module};
8-
use std::mem::replace;
98

109
pub mod client;
1110
pub mod errors;
1211
pub mod request;
1312
pub mod response;
13+
pub mod utils;
1414

1515
#[php_get_module]
1616
pub fn get_module() -> Module {
@@ -28,12 +28,3 @@ pub fn get_module() -> Module {
2828

2929
module
3030
}
31-
32-
fn replace_and_set<T>(t: &mut T, init: T, f: impl FnOnce(T) -> T) {
33-
let x = f(replace(t, init));
34-
let _ = replace(t, x);
35-
}
36-
37-
fn replace_and_get<T, R>(t: &mut T, init: T, f: impl FnOnce(T) -> R) -> R {
38-
f(replace(t, init))
39-
}

examples/http-client/src/request.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{errors::HttpClientError, replace_and_get, response::RESPONSE_CLASS_NAME};
1+
use crate::{errors::HttpClientError, response::RESPONSE_CLASS_NAME, utils::replace_and_get};
22
use phper::{
33
classes::{ClassEntry, DynamicClass, Visibility},
44
objects::Object,

examples/http-client/src/response.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{errors::HttpClientError, replace_and_get};
1+
use crate::{errors::HttpClientError, utils::replace_and_get};
22
use indexmap::map::IndexMap;
33
use phper::{
44
classes::{DynamicClass, Visibility},

examples/http-client/src/utils.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use std::mem::replace;
2+
3+
pub fn replace_and_set<T>(t: &mut T, init: T, f: impl FnOnce(T) -> T) {
4+
let x = f(replace(t, init));
5+
let _ = replace(t, x);
6+
}
7+
8+
pub fn replace_and_get<T, R>(t: &mut T, init: T, f: impl FnOnce(T) -> R) -> R {
9+
f(replace(t, init))
10+
}

phper-macros/src/derives.rs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use proc_macro::TokenStream;
22
use proc_macro2::TokenStream as TokenStream2;
33
use quote::quote;
4-
use syn::{Attribute, Data, DeriveInput, Fields, Meta};
4+
use syn::{Attribute, Data, DeriveInput, Expr, Fields, Meta, MetaNameValue};
55

66
pub(crate) fn derive_throwable(input: DeriveInput) -> syn::Result<TokenStream> {
77
let crate_ident = parse_throwable_crate_ident(&input);
@@ -20,27 +20,9 @@ fn parse_throwable_crate_ident(input: &DeriveInput) -> TokenStream2 {
2020
}
2121

2222
fn parse_throwable_attrs(input: &DeriveInput) -> syn::Result<TokenStream2> {
23-
let attr = attributes_find_ident(&input.attrs, "throwable");
24-
attr.map(|attr| {
25-
attr.parse_args::<Meta>().and_then(|meta| match meta {
26-
Meta::NameValue(name_value) => {
27-
if !name_value.path.is_ident("class") {
28-
Err(syn::Error::new_spanned(
29-
&attr,
30-
"now only support #[throwable(error = ?)] for enum",
31-
))
32-
} else {
33-
let lit = name_value.lit;
34-
Ok(quote! { #lit })
35-
}
36-
}
37-
_ => Err(syn::Error::new_spanned(
38-
&attr,
39-
"now only support #[throwable(error = ?)] for enum",
40-
)),
41-
})
42-
})
43-
.unwrap_or_else(|| Ok(quote! { "Exception" }))
23+
let attr = attributes_find_ident(&input.attrs, "throwable_class");
24+
attr.map(|attr| attr.parse_args::<Expr>().map(|expr| quote! { #expr }))
25+
.unwrap_or_else(|| Ok(quote! { "Exception" }))
4426
}
4527

4628
fn parse_throwable_input(

phper-macros/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub fn php_get_module(attr: TokenStream, input: TokenStream) -> TokenStream {
9191
/// ```
9292
///
9393
/// TODO Support attribute `throwable` with `code` and `message`, integration tests.
94-
#[proc_macro_derive(Throwable, attributes(throwable, throwable_crate))]
94+
#[proc_macro_derive(Throwable, attributes(throwable, throwable_class, throwable_crate))]
9595
pub fn derive_throwable(input: TokenStream) -> TokenStream {
9696
let input = parse_macro_input!(input as DeriveInput);
9797
derives::derive_throwable(input).unwrap_or_else(|e| e.into_compile_error().into())

phper-sys/php_wrapper.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,7 @@ bool phper_zend_hash_str_exists(const HashTable *ht, const char *str, size_t len
160160
bool phper_zend_hash_index_exists(const HashTable *ht, zend_ulong h) {
161161
return zend_hash_index_exists(ht, h) != 0;
162162
}
163+
164+
void phper_zval_dtor(zval *zval_ptr) {
165+
return zval_dtor(zval_ptr);
166+
}

phper-sys/php_wrapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,6 @@ bool phper_call_user_function(HashTable *function_table, zval *object, zval *fun
5656
bool phper_zend_hash_str_exists(const HashTable *ht, const char *str, size_t len);
5757
bool phper_zend_hash_index_exists(const HashTable *ht, zend_ulong h);
5858

59+
void phper_zval_dtor(zval *zval_ptr);
60+
5961
#endif //PHPER_PHP_WRAPPER_H

0 commit comments

Comments
 (0)