1- use std:: ops:: Deref ;
2- use std:: ffi:: { c_char, CStr , CString , NulError } ;
31use crate :: ffi:: {
4- php_ini_builder,
5- php_ini_builder_prepend,
2+ php_ini_builder, php_ini_builder_define, php_ini_builder_prepend, php_ini_builder_quoted,
63 php_ini_builder_unquoted,
7- php_ini_builder_quoted,
8- php_ini_builder_define
94} ;
5+ use std:: ffi:: { c_char, CStr , CString , NulError } ;
6+ use std:: ops:: Deref ;
107
118// Helpful for CString which only needs to live until immediately after C call.
129struct CStringScope ( * mut c_char ) ;
@@ -28,13 +25,19 @@ impl Deref for CStringScope {
2825impl Drop for CStringScope {
2926 fn drop ( & mut self ) {
3027 // Convert back to a CString to ensure it gets dropped
31- drop ( unsafe { CString :: from_raw ( self . 0 ) } )
28+ drop ( unsafe { CString :: from_raw ( self . 0 ) } ) ;
3229 }
3330}
3431
3532/// A builder for creating INI configurations.
3633pub type IniBuilder = php_ini_builder ;
3734
35+ impl Default for IniBuilder {
36+ fn default ( ) -> Self {
37+ Self :: new ( )
38+ }
39+ }
40+
3841impl IniBuilder {
3942 /// Creates a new INI builder.
4043 ///
@@ -44,8 +47,9 @@ impl IniBuilder {
4447 /// # use ext_php_rs::builders::IniBuilder;
4548 /// let mut builder = IniBuilder::new();
4649 /// ```
50+ #[ must_use]
4751 pub fn new ( ) -> IniBuilder {
48- IniBuilder {
52+ IniBuilder {
4953 value : std:: ptr:: null_mut ( ) ,
5054 length : 0 ,
5155 }
@@ -57,6 +61,10 @@ impl IniBuilder {
5761 ///
5862 /// * `value` - The value to append.
5963 ///
64+ /// # Errors
65+ ///
66+ /// Returns a `NulError` if the value contains a null byte.
67+ ///
6068 /// # Examples
6169 ///
6270 /// ```
@@ -82,6 +90,10 @@ impl IniBuilder {
8290 /// * `name` - The name of the pair.
8391 /// * `value` - The value of the pair.
8492 ///
93+ /// # Errors
94+ ///
95+ /// Returns a `NulError` if the value contains a null byte.
96+ ///
8597 /// # Examples
8698 ///
8799 /// ```
@@ -114,6 +126,10 @@ impl IniBuilder {
114126 /// * `name` - The name of the pair.
115127 /// * `value` - The value of the pair.
116128 ///
129+ /// # Errors
130+ ///
131+ /// Returns a `NulError` if the value contains a null byte.
132+ ///
117133 /// # Examples
118134 ///
119135 /// ```
@@ -145,6 +161,10 @@ impl IniBuilder {
145161 ///
146162 /// * `value` - The value to define.
147163 ///
164+ /// # Errors
165+ ///
166+ /// Returns a `NulError` if the value contains a null byte.
167+ ///
148168 /// # Examples
149169 ///
150170 /// ```
@@ -177,7 +197,7 @@ impl IniBuilder {
177197 return std:: ptr:: null_mut ( ) ;
178198 }
179199
180- unsafe { CStr :: from_ptr ( self . value ) } . as_ptr ( ) as * mut c_char
200+ unsafe { CStr :: from_ptr ( self . value ) } . as_ptr ( ) . cast_mut ( )
181201 }
182202}
183203
@@ -188,40 +208,66 @@ mod tests {
188208 #[ test]
189209 fn test_ini_builder_prepend ( ) {
190210 let mut builder = IniBuilder :: new ( ) ;
191- builder. prepend ( "foo=bar" ) . unwrap ( ) ;
211+ builder. prepend ( "foo=bar" ) . expect ( "should prepend value" ) ;
192212
193213 let ini = builder. finish ( ) ;
194214 assert ! ( !ini. is_null( ) ) ;
195- assert_eq ! ( unsafe { CStr :: from_ptr( ini) } . to_str( ) . unwrap( ) , "foo=bar" ) ;
215+ assert_eq ! (
216+ unsafe { CStr :: from_ptr( ini) }
217+ . to_str( )
218+ . expect( "should convert to str" ) ,
219+ "foo=bar"
220+ ) ;
196221 }
197222
198223 #[ test]
199224 fn test_ini_builder_unquoted ( ) {
200225 let mut builder = IniBuilder :: new ( ) ;
201- builder. unquoted ( "baz" , "qux" ) . unwrap ( ) ;
226+ builder
227+ . unquoted ( "baz" , "qux" )
228+ . expect ( "should add unquoted value" ) ;
202229
203230 let ini = builder. finish ( ) ;
204231 assert ! ( !ini. is_null( ) ) ;
205- assert_eq ! ( unsafe { CStr :: from_ptr( ini) } . to_str( ) . unwrap( ) , "baz=qux\n " ) ;
232+ assert_eq ! (
233+ unsafe { CStr :: from_ptr( ini) }
234+ . to_str( )
235+ . expect( "should convert to str" ) ,
236+ "baz=qux\n "
237+ ) ;
206238 }
207239
208240 #[ test]
209241 fn test_ini_builder_quoted ( ) {
210242 let mut builder = IniBuilder :: new ( ) ;
211- builder. quoted ( "quux" , "corge" ) . unwrap ( ) ;
243+ builder
244+ . quoted ( "quux" , "corge" )
245+ . expect ( "should add quoted value" ) ;
212246
213247 let ini = builder. finish ( ) ;
214248 assert ! ( !ini. is_null( ) ) ;
215- assert_eq ! ( unsafe { CStr :: from_ptr( ini) } . to_str( ) . unwrap( ) , "quux=\" corge\" \n " ) ;
249+ assert_eq ! (
250+ unsafe { CStr :: from_ptr( ini) }
251+ . to_str( )
252+ . expect( "should convert to str" ) ,
253+ "quux=\" corge\" \n "
254+ ) ;
216255 }
217256
218257 #[ test]
219258 fn test_ini_builder_define ( ) {
220259 let mut builder = IniBuilder :: new ( ) ;
221- builder. define ( "grault=garply" ) . unwrap ( ) ;
260+ builder
261+ . define ( "grault=garply" )
262+ . expect ( "should define value" ) ;
222263
223264 let ini = builder. finish ( ) ;
224265 assert ! ( !ini. is_null( ) ) ;
225- assert_eq ! ( unsafe { CStr :: from_ptr( ini) } . to_str( ) . unwrap( ) , "grault=garply\n " ) ;
266+ assert_eq ! (
267+ unsafe { CStr :: from_ptr( ini) }
268+ . to_str( )
269+ . expect( "should convert to str" ) ,
270+ "grault=garply\n "
271+ ) ;
226272 }
227273}
0 commit comments