Skip to content

Commit dfe1aaa

Browse files
authored
Make add_function, add_method more ergonomics. (#79)
1 parent 278722a commit dfe1aaa

File tree

21 files changed

+422
-465
lines changed

21 files changed

+422
-465
lines changed

examples/complex/src/lib.rs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,21 @@ pub fn get_module() -> Module {
5656
module.on_request_shutdown(|_| true);
5757

5858
// register functions
59-
module.add_function(
60-
"complex_say_hello",
61-
say_hello,
62-
vec![Argument::by_val("name")],
63-
);
64-
module.add_function("complex_throw_exception", throw_exception, vec![]);
65-
module.add_function(
66-
"complex_get_all_ini",
67-
|_: &mut [ZVal]| {
68-
let mut arr = ZArray::new();
59+
module
60+
.add_function("complex_say_hello", say_hello)
61+
.argument(Argument::by_val("name"));
62+
module.add_function("complex_throw_exception", throw_exception);
63+
module.add_function("complex_get_all_ini", |_: &mut [ZVal]| {
64+
let mut arr = ZArray::new();
6965

70-
let complex_enable = ZVal::from(ini_get::<bool>("complex.enable"));
71-
arr.insert("complex.enable", complex_enable);
66+
let complex_enable = ZVal::from(ini_get::<bool>("complex.enable"));
67+
arr.insert("complex.enable", complex_enable);
7268

73-
let complex_description = ZVal::from(ini_get::<Option<&CStr>>("complex.description"));
74-
arr.insert("complex.description", complex_description);
69+
let complex_description = ZVal::from(ini_get::<Option<&CStr>>("complex.description"));
70+
arr.insert("complex.description", complex_description);
7571

76-
arr
77-
},
78-
vec![],
79-
);
72+
arr
73+
});
8074

8175
// register classes
8276
let mut foo_class = StatefulClass::new("FooClass");
@@ -88,17 +82,17 @@ pub fn get_module() -> Module {
8882
let prop = this.get_property("foo");
8983
Ok::<_, phper::Error>(prop.clone())
9084
},
91-
vec![],
92-
);
93-
foo_class.add_method(
94-
"setFoo",
95-
Visibility::Public,
96-
|this: &mut StatefulObj<()>, arguments: &mut [ZVal]| -> phper::Result<()> {
97-
this.set_property("foo", arguments[0].clone());
98-
Ok(())
99-
},
100-
vec![Argument::by_val("foo")],
10185
);
86+
foo_class
87+
.add_method(
88+
"setFoo",
89+
Visibility::Public,
90+
|this: &mut StatefulObj<()>, arguments: &mut [ZVal]| -> phper::Result<()> {
91+
this.set_property("foo", arguments[0].clone());
92+
Ok(())
93+
},
94+
)
95+
.argument(Argument::by_val("foo"));
10296
module.add_class(foo_class);
10397

10498
module

examples/hello/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ pub fn get_module() -> Module {
3434
);
3535

3636
// Register function `say_hello`, with one argument `name`.
37-
module.add_function("say_hello", say_hello, vec![Argument::by_val("name")]);
37+
module
38+
.add_function("say_hello", say_hello)
39+
.argument(Argument::by_val("name"));
3840

3941
module
4042
}

examples/http-client/src/client.rs

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -26,62 +26,49 @@ pub fn make_client_builder_class() -> StatefulClass<ClientBuilder> {
2626
let mut class = StatefulClass::new_with_default_state(HTTP_CLIENT_BUILDER_CLASS_NAME);
2727

2828
// Inner call the `ClientBuilder::timeout`.
29-
class.add_method(
30-
"timeout",
31-
Visibility::Public,
32-
|this, arguments| {
29+
class
30+
.add_method("timeout", Visibility::Public, |this, arguments| {
3331
let ms = arguments[0].expect_long()?;
3432
let state = this.as_mut_state();
3533
let builder: ClientBuilder = take(state);
3634
*state = builder.timeout(Duration::from_millis(ms as u64));
3735
Ok::<_, HttpClientError>(this.to_ref_owned())
38-
},
39-
vec![Argument::by_val("ms")],
40-
);
36+
})
37+
.argument(Argument::by_val("ms"));
4138

4239
// Inner call the `ClientBuilder::cookie_store`.
43-
class.add_method(
44-
"cookie_store",
45-
Visibility::Public,
46-
|this, arguments| {
40+
class
41+
.add_method("cookie_store", Visibility::Public, |this, arguments| {
4742
let enable = arguments[0].expect_bool()?;
4843
let state = this.as_mut_state();
4944
let builder: ClientBuilder = take(state);
5045
*state = builder.cookie_store(enable);
5146
Ok::<_, HttpClientError>(this.to_ref_owned())
52-
},
53-
vec![Argument::by_val("enable")],
54-
);
47+
})
48+
.argument(Argument::by_val("enable"));
5549

5650
// Inner call the `ClientBuilder::build`, and wrap the result `Client` in
5751
// Object.
58-
class.add_method(
59-
"build",
60-
Visibility::Public,
61-
|this, _arguments| {
62-
let state = take(this.as_mut_state());
63-
let client = ClientBuilder::build(state)?;
64-
let mut object = ClassEntry::from_globals(HTTP_CLIENT_CLASS_NAME)?.init_object()?;
65-
unsafe {
66-
*object.as_mut_state() = Some(client);
67-
}
68-
Ok::<_, HttpClientError>(object)
69-
},
70-
vec![],
71-
);
52+
class.add_method("build", Visibility::Public, |this, _arguments| {
53+
let state = take(this.as_mut_state());
54+
let client = ClientBuilder::build(state)?;
55+
let mut object = ClassEntry::from_globals(HTTP_CLIENT_CLASS_NAME)?.init_object()?;
56+
unsafe {
57+
*object.as_mut_state() = Some(client);
58+
}
59+
Ok::<_, HttpClientError>(object)
60+
});
7261

7362
class
7463
}
7564

7665
pub fn make_client_class() -> StatefulClass<Option<Client>> {
7766
let mut class = StatefulClass::<Option<Client>>::new_with_default_state(HTTP_CLIENT_CLASS_NAME);
7867

79-
class.add_method("__construct", Visibility::Private, |_, _| {}, vec![]);
68+
class.add_method("__construct", Visibility::Private, |_, _| {});
8069

81-
class.add_method(
82-
"get",
83-
Visibility::Public,
84-
|this, arguments| {
70+
class
71+
.add_method("get", Visibility::Public, |this, arguments| {
8572
let url = arguments[0].expect_z_str()?.to_str().unwrap();
8673
let client = this.as_state().as_ref().unwrap();
8774
let request_builder = client.get(url);
@@ -90,14 +77,11 @@ pub fn make_client_class() -> StatefulClass<Option<Client>> {
9077
*object.as_mut_state() = Some(request_builder);
9178
}
9279
Ok::<_, HttpClientError>(object)
93-
},
94-
vec![Argument::by_val("url")],
95-
);
80+
})
81+
.argument(Argument::by_val("url"));
9682

97-
class.add_method(
98-
"post",
99-
Visibility::Public,
100-
|this, arguments| {
83+
class
84+
.add_method("post", Visibility::Public, |this, arguments| {
10185
let url = arguments[0].expect_z_str()?.to_str().unwrap();
10286
let client = this.as_state().as_ref().unwrap();
10387
let request_builder = client.post(url);
@@ -106,9 +90,8 @@ pub fn make_client_class() -> StatefulClass<Option<Client>> {
10690
*object.as_mut_state() = Some(request_builder);
10791
}
10892
Ok::<_, HttpClientError>(object)
109-
},
110-
vec![Argument::by_val("url")],
111-
);
93+
})
94+
.argument(Argument::by_val("url"));
11295

11396
class
11497
}

examples/http-client/src/request.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,17 @@ pub fn make_request_builder_class() -> StatefulClass<Option<RequestBuilder>> {
1919
let mut class =
2020
StatefulClass::<Option<RequestBuilder>>::new_with_default_state(REQUEST_BUILDER_CLASS_NAME);
2121

22-
class.add_method("__construct", Visibility::Private, |_, _| {}, vec![]);
22+
class.add_method("__construct", Visibility::Private, |_, _| {});
2323

24-
class.add_method(
25-
"send",
26-
Visibility::Public,
27-
|this, _arguments| {
28-
let state = take(this.as_mut_state());
29-
let response = state.unwrap().send()?;
30-
let mut object = ClassEntry::from_globals(RESPONSE_CLASS_NAME)?.new_object([])?;
31-
unsafe {
32-
*object.as_mut_state() = Some(response);
33-
}
34-
Ok::<_, HttpClientError>(object)
35-
},
36-
vec![],
37-
);
24+
class.add_method("send", Visibility::Public, |this, _arguments| {
25+
let state = take(this.as_mut_state());
26+
let response = state.unwrap().send()?;
27+
let mut object = ClassEntry::from_globals(RESPONSE_CLASS_NAME)?.new_object([])?;
28+
unsafe {
29+
*object.as_mut_state() = Some(response);
30+
}
31+
Ok::<_, HttpClientError>(object)
32+
});
3833

3934
class
4035
}

examples/http-client/src/response.rs

Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -22,60 +22,44 @@ pub const RESPONSE_CLASS_NAME: &str = "HttpClient\\Response";
2222
pub fn make_response_class() -> StatefulClass<Option<Response>> {
2323
let mut class = StatefulClass::<Option<Response>>::new_with_default_state(RESPONSE_CLASS_NAME);
2424

25-
class.add_method(
26-
"body",
27-
Visibility::Public,
28-
|this, _arguments| {
29-
let response = take(this.as_mut_state());
30-
let body = response
31-
.ok_or(HttpClientError::ResponseHadRead)
32-
.and_then(|response| response.bytes().map_err(Into::into))?;
33-
Ok::<_, HttpClientError>(body.to_vec())
34-
},
35-
vec![],
36-
);
25+
class.add_method("body", Visibility::Public, |this, _arguments| {
26+
let response = take(this.as_mut_state());
27+
let body = response
28+
.ok_or(HttpClientError::ResponseHadRead)
29+
.and_then(|response| response.bytes().map_err(Into::into))?;
30+
Ok::<_, HttpClientError>(body.to_vec())
31+
});
3732

38-
class.add_method(
39-
"status",
40-
Visibility::Public,
41-
|this, _arguments| {
42-
let response =
43-
this.as_state()
44-
.as_ref()
45-
.ok_or_else(|| HttpClientError::ResponseAfterRead {
46-
method_name: "status".to_owned(),
47-
})?;
33+
class.add_method("status", Visibility::Public, |this, _arguments| {
34+
let response =
35+
this.as_state()
36+
.as_ref()
37+
.ok_or_else(|| HttpClientError::ResponseAfterRead {
38+
method_name: "status".to_owned(),
39+
})?;
4840

49-
Ok::<_, HttpClientError>(response.status().as_u16() as i64)
50-
},
51-
vec![],
52-
);
41+
Ok::<_, HttpClientError>(response.status().as_u16() as i64)
42+
});
5343

54-
class.add_method(
55-
"headers",
56-
Visibility::Public,
57-
|this, _arguments| {
58-
let response =
59-
this.as_state()
60-
.as_ref()
61-
.ok_or_else(|| HttpClientError::ResponseAfterRead {
62-
method_name: "headers".to_owned(),
63-
})?;
64-
let headers_map =
65-
response
66-
.headers()
67-
.iter()
68-
.fold(ZArray::new(), |mut acc, (key, value)| {
69-
let arr = acc.entry(key.as_str()).or_insert(ZVal::from(ZArray::new()));
70-
arr.as_mut_z_arr()
71-
.unwrap()
72-
.insert(InsertKey::NextIndex, ZVal::from(value.as_bytes()));
73-
acc
74-
});
75-
Ok::<_, HttpClientError>(headers_map)
76-
},
77-
vec![],
78-
);
44+
class.add_method("headers", Visibility::Public, |this, _arguments| {
45+
let response =
46+
this.as_state()
47+
.as_ref()
48+
.ok_or_else(|| HttpClientError::ResponseAfterRead {
49+
method_name: "headers".to_owned(),
50+
})?;
51+
let headers_map = response
52+
.headers()
53+
.iter()
54+
.fold(ZArray::new(), |mut acc, (key, value)| {
55+
let arr = acc.entry(key.as_str()).or_insert(ZVal::from(ZArray::new()));
56+
arr.as_mut_z_arr()
57+
.unwrap()
58+
.insert(InsertKey::NextIndex, ZVal::from(value.as_bytes()));
59+
acc
60+
});
61+
Ok::<_, HttpClientError>(headers_map)
62+
});
7963

8064
class
8165
}

examples/http-server/src/response.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,24 @@ pub const HTTP_RESPONSE_CLASS_NAME: &str = "HttpServer\\HttpResponse";
2020
pub fn make_response_class() -> StatefulClass<Response<Body>> {
2121
let mut class = StatefulClass::new_with_default_state(HTTP_RESPONSE_CLASS_NAME);
2222

23-
class.add_method(
24-
"header",
25-
Visibility::Public,
26-
|this, arguments| {
23+
class
24+
.add_method("header", Visibility::Public, |this, arguments| {
2725
let response: &mut Response<Body> = this.as_mut_state();
2826
response.headers_mut().insert(
2927
HeaderName::from_bytes(arguments[0].as_z_str().unwrap().to_bytes())?,
3028
HeaderValue::from_bytes(arguments[1].as_z_str().unwrap().to_bytes())?,
3129
);
3230
Ok::<_, HttpServerError>(())
33-
},
34-
vec![Argument::by_val("data")],
35-
);
31+
})
32+
.argument(Argument::by_val("data"));
3633

37-
class.add_method(
38-
"end",
39-
Visibility::Public,
40-
|this, arguments| {
34+
class
35+
.add_method("end", Visibility::Public, |this, arguments| {
4136
let response: &mut Response<Body> = this.as_mut_state();
4237
*response.body_mut() = arguments[0].as_z_str().unwrap().to_bytes().to_vec().into();
4338
Ok::<_, phper::Error>(())
44-
},
45-
vec![Argument::by_val("data")],
46-
);
39+
})
40+
.argument(Argument::by_val("data"));
4741

4842
class
4943
}

0 commit comments

Comments
 (0)