Skip to content

Commit 26ade87

Browse files
committed
simplify with_default_value
1 parent 0c92afe commit 26ade87

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

phper-doc/doc/_06_module/_02_register_functions/index.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ a nullable class (in this case, an interface), and a string with a default value
8989
```rust,no_run
9090
use phper::{modules::Module, php_get_module, functions::Argument, echo};
9191
use phper::types::ArgumentTypeHint;
92-
use std::ffi::CString;
9392
9493
#[php_get_module]
9594
pub fn get_module() -> Module {
@@ -103,7 +102,7 @@ pub fn get_module() -> Module {
103102
Ok(())
104103
})
105104
.argument(Argument::by_val("a_class").with_type_hint(ArgumentTypeHint::ClassEntry(String::from(r"\MyNamespace\MyInterface"))).allow_null())
106-
.argument(Argument::by_val("name").with_type_hint(ArgumentTypeHint::String).with_default_value(CString::new("'my_default'").unwrap()))
105+
.argument(Argument::by_val("name").with_type_hint(ArgumentTypeHint::String).with_default_value("'my_default'"))
107106
.argument(Argument::by_val("optional_bool").with_type_hint(ArgumentTypeHint::Bool).optional());
108107
109108
module

phper/src/functions.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,8 @@ impl Argument {
596596

597597
/// Argument default value. Example: "'a-string'", "A_CONST", "42",
598598
/// "[0=>'zero']"
599-
pub fn with_default_value(mut self, default_value: CString) -> Self {
599+
pub fn with_default_value(mut self, default_value: impl Into<String>) -> Self {
600+
let default_value = ensure_end_with_zero(default_value);
600601
self.default_value = Some(default_value);
601602
self.required = false; // arg with default value does not count towards required arg count
602603
self

tests/integration/src/typehints.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use phper::{
1515
types::{ArgumentTypeHint, ReturnTypeHint},
1616
values::ZVal,
1717
};
18-
use std::ffi::CString;
1918

2019
const I_FOO: &str = r"IntegrationTest\TypeHints\IFoo";
2120

@@ -32,32 +31,32 @@ pub fn integrate(module: &mut Module) {
3231
.argument(
3332
Argument::by_val("s")
3433
.with_type_hint(ArgumentTypeHint::String)
35-
.with_default_value(CString::new("'foobarbaz'").unwrap()),
34+
.with_default_value("'foobarbaz'"),
3635
)
3736
.argument(
3837
Argument::by_val("i")
3938
.with_type_hint(ArgumentTypeHint::Int)
40-
.with_default_value(CString::new("42").unwrap()),
39+
.with_default_value("42"),
4140
)
4241
.argument(
4342
Argument::by_val("f")
4443
.with_type_hint(ArgumentTypeHint::Float)
45-
.with_default_value(CString::new("7.89").unwrap()),
44+
.with_default_value("7.89"),
4645
)
4746
.argument(
4847
Argument::by_val("b")
4948
.with_type_hint(ArgumentTypeHint::Bool)
50-
.with_default_value(CString::new("true").unwrap()),
49+
.with_default_value("true"),
5150
)
5251
.argument(
5352
Argument::by_val("a")
5453
.with_type_hint(ArgumentTypeHint::Array)
55-
.with_default_value(CString::new("['a'=>'b']").unwrap()),
54+
.with_default_value("['a'=>'b']"),
5655
)
5756
.argument(
5857
Argument::by_val("m")
5958
.with_type_hint(ArgumentTypeHint::Mixed)
60-
.with_default_value(CString::new("1.23").unwrap()),
59+
.with_default_value("1.23"),
6160
)
6261
.return_type(ReturnType::by_val(ReturnTypeHint::Void));
6362
}
@@ -590,7 +589,7 @@ fn make_arg_default_value_class() -> ClassEntity<()> {
590589
.argument(
591590
Argument::by_val("string_value")
592591
.with_type_hint(ArgumentTypeHint::String)
593-
.with_default_value(CString::new("'foobarbaz'").unwrap()),
592+
.with_default_value("'foobarbaz'"),
594593
); //NB single quotes!
595594

596595
class
@@ -600,7 +599,7 @@ fn make_arg_default_value_class() -> ClassEntity<()> {
600599
.argument(
601600
Argument::by_val("const_value")
602601
.with_type_hint(ArgumentTypeHint::String)
603-
.with_default_value(CString::new("PHP_VERSION").unwrap()),
602+
.with_default_value("PHP_VERSION"),
604603
);
605604

606605
class
@@ -610,7 +609,7 @@ fn make_arg_default_value_class() -> ClassEntity<()> {
610609
.argument(
611610
Argument::by_val("bool_value")
612611
.with_type_hint(ArgumentTypeHint::Bool)
613-
.with_default_value(CString::new("true").unwrap()),
612+
.with_default_value("true"),
614613
);
615614

616615
class
@@ -620,15 +619,15 @@ fn make_arg_default_value_class() -> ClassEntity<()> {
620619
.argument(
621620
Argument::by_val("bool_value")
622621
.with_type_hint(ArgumentTypeHint::Bool)
623-
.with_default_value(CString::new("false").unwrap()),
622+
.with_default_value("false"),
624623
);
625624

626625
class
627626
.add_method("intDefault", Visibility::Public, move |_, _| phper::ok(()))
628627
.argument(
629628
Argument::by_val("int_value")
630629
.with_type_hint(ArgumentTypeHint::Int)
631-
.with_default_value(CString::new("42").unwrap()),
630+
.with_default_value("42"),
632631
);
633632

634633
class
@@ -640,7 +639,7 @@ fn make_arg_default_value_class() -> ClassEntity<()> {
640639
.argument(
641640
Argument::by_val("float_value")
642641
.with_type_hint(ArgumentTypeHint::Float)
643-
.with_default_value(CString::new("3.14159").unwrap()),
642+
.with_default_value("3.14159"),
644643
);
645644

646645
class
@@ -652,7 +651,7 @@ fn make_arg_default_value_class() -> ClassEntity<()> {
652651
.argument(
653652
Argument::by_val("array_value")
654653
.with_type_hint(ArgumentTypeHint::Array)
655-
.with_default_value(CString::new("['a' => 'b']").unwrap()),
654+
.with_default_value("['a' => 'b']"),
656655
);
657656

658657
class
@@ -662,7 +661,7 @@ fn make_arg_default_value_class() -> ClassEntity<()> {
662661
.argument(
663662
Argument::by_val("iterable_value")
664663
.with_type_hint(ArgumentTypeHint::Iterable)
665-
.with_default_value(CString::new("[0 => 1]").unwrap()),
664+
.with_default_value("[0 => 1]"),
666665
);
667666

668667
class
@@ -674,7 +673,7 @@ fn make_arg_default_value_class() -> ClassEntity<()> {
674673
.argument(
675674
Argument::by_val("mixed_value")
676675
.with_type_hint(ArgumentTypeHint::Mixed)
677-
.with_default_value(CString::new("999").unwrap()),
676+
.with_default_value("999"),
678677
);
679678

680679
class

0 commit comments

Comments
 (0)