Skip to content

Commit 6e2c008

Browse files
committed
docs(guide): add guide for IniBuilder
1 parent 03a7f2a commit 6e2c008

File tree

4 files changed

+151
-23
lines changed

4 files changed

+151
-23
lines changed

docsrs_bindings.rs

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,11 @@ pub type __uid_t = ::std::os::raw::c_uint;
204204
pub type __gid_t = ::std::os::raw::c_uint;
205205
pub type __ino_t = ::std::os::raw::c_ulong;
206206
pub type __mode_t = ::std::os::raw::c_uint;
207-
pub type __nlink_t = ::std::os::raw::c_ulong;
207+
pub type __nlink_t = ::std::os::raw::c_uint;
208208
pub type __off_t = ::std::os::raw::c_long;
209209
pub type __off64_t = ::std::os::raw::c_long;
210210
pub type __time_t = ::std::os::raw::c_long;
211-
pub type __blksize_t = ::std::os::raw::c_long;
211+
pub type __blksize_t = ::std::os::raw::c_int;
212212
pub type __blkcnt_t = ::std::os::raw::c_long;
213213
pub type __syscall_slong_t = ::std::os::raw::c_long;
214214
pub type gid_t = __gid_t;
@@ -768,19 +768,20 @@ pub type zend_class_arrayaccess_funcs = _zend_class_arrayaccess_funcs;
768768
pub struct stat {
769769
pub st_dev: __dev_t,
770770
pub st_ino: __ino_t,
771-
pub st_nlink: __nlink_t,
772771
pub st_mode: __mode_t,
772+
pub st_nlink: __nlink_t,
773773
pub st_uid: __uid_t,
774774
pub st_gid: __gid_t,
775-
pub __pad0: ::std::os::raw::c_int,
776775
pub st_rdev: __dev_t,
776+
pub __pad1: __dev_t,
777777
pub st_size: __off_t,
778778
pub st_blksize: __blksize_t,
779+
pub __pad2: ::std::os::raw::c_int,
779780
pub st_blocks: __blkcnt_t,
780781
pub st_atim: timespec,
781782
pub st_mtim: timespec,
782783
pub st_ctim: timespec,
783-
pub __glibc_reserved: [__syscall_slong_t; 3usize],
784+
pub __glibc_reserved: [::std::os::raw::c_int; 2usize],
784785
}
785786
pub type zend_stream_fsizer_t =
786787
::std::option::Option<unsafe extern "C" fn(handle: *mut ::std::os::raw::c_void) -> usize>;
@@ -1420,7 +1421,7 @@ pub struct _zend_execute_data {
14201421
pub run_time_cache: *mut *mut ::std::os::raw::c_void,
14211422
pub extra_named_params: *mut zend_array,
14221423
}
1423-
pub type __jmp_buf = [::std::os::raw::c_long; 8usize];
1424+
pub type __jmp_buf = [::std::os::raw::c_ulonglong; 22usize];
14241425
#[repr(C)]
14251426
#[derive(Debug, Copy, Clone)]
14261427
pub struct __jmp_buf_tag {
@@ -2732,3 +2733,42 @@ pub struct _sapi_post_entry {
27322733
),
27332734
>,
27342735
}
2736+
#[doc = " A class which helps with constructing INI entries from the command\n line."]
2737+
#[repr(C)]
2738+
#[derive(Debug, Copy, Clone)]
2739+
pub struct php_ini_builder {
2740+
pub value: *mut ::std::os::raw::c_char,
2741+
pub length: usize,
2742+
}
2743+
extern "C" {
2744+
#[doc = " Prepend a string.\n\n @param src the source string\n @param length the size of the source string"]
2745+
pub fn php_ini_builder_prepend(
2746+
b: *mut php_ini_builder,
2747+
src: *const ::std::os::raw::c_char,
2748+
length: usize,
2749+
);
2750+
}
2751+
extern "C" {
2752+
#[doc = " Append an unquoted name/value pair."]
2753+
pub fn php_ini_builder_unquoted(
2754+
b: *mut php_ini_builder,
2755+
name: *const ::std::os::raw::c_char,
2756+
name_length: usize,
2757+
value: *const ::std::os::raw::c_char,
2758+
value_length: usize,
2759+
);
2760+
}
2761+
extern "C" {
2762+
#[doc = " Append a quoted name/value pair."]
2763+
pub fn php_ini_builder_quoted(
2764+
b: *mut php_ini_builder,
2765+
name: *const ::std::os::raw::c_char,
2766+
name_length: usize,
2767+
value: *const ::std::os::raw::c_char,
2768+
value_length: usize,
2769+
);
2770+
}
2771+
extern "C" {
2772+
#[doc = " Parse an INI entry from the command-line option \"--define\"."]
2773+
pub fn php_ini_builder_define(b: *mut php_ini_builder, arg: *const ::std::os::raw::c_char);
2774+
}

guide/src/ini-builder.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# INI Builder
2+
3+
When configuring a SAPI you may use `IniBuilder` to load INI settings as text.
4+
This is useful for setting up configurations required by the SAPI capabilities.
5+
6+
INI settings applied to a SAPI through `sapi.ini_entries` will be immutable,
7+
meaning they cannot be changed at runtime. This is useful for applying settings
8+
to match hard requirements of the way your SAPI works.
9+
10+
To apply _configurable_ defaults it is recommended to use a `sapi.ini_defaults`
11+
callback instead, which will allow settings to be changed at runtime.
12+
13+
```rust,no_run
14+
use ext_php_rs::builder::{IniBuilder, SapiBuilder};
15+
16+
# fn main() {
17+
// Create a new IniBuilder instance.
18+
let mut builder = IniBuilder::new();
19+
20+
// Append a single key/value pair to the INIT buffer with an unquoted value.
21+
builder.unquoted("log_errors", "1");
22+
23+
// Append a single key/value pair to the INI buffer with a quoted value.
24+
builder.quoted("default_mimetype", "text/html");
25+
26+
// Append INI line text as-is. A line break will be automatically appended.
27+
builder.define("memory_limit=128MB");
28+
29+
// Prepend INI line text as-is. No line break insertion will occur.
30+
builder.prepend("error_reporting=0\ndisplay_errors=1\n");
31+
32+
// Construct a SAPI.
33+
let mut sapi = SapiBuilder::new("name", "pretty_name").build()
34+
.expect("should build SAPI");
35+
36+
// Dump INI entries from the builder into the SAPI.
37+
sapi.ini_entries = builder.finish();
38+
# }
39+
# main();
40+
```

src/builders/ini.rs

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
use std::ops::Deref;
2-
use std::ffi::{c_char, CStr, CString, NulError};
31
use 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.
129
struct CStringScope(*mut c_char);
@@ -28,13 +25,19 @@ impl Deref for CStringScope {
2825
impl 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.
3633
pub type IniBuilder = php_ini_builder;
3734

35+
impl Default for IniBuilder {
36+
fn default() -> Self {
37+
Self::new()
38+
}
39+
}
40+
3841
impl 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
}

src/builders/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
44
mod class;
55
mod function;
6+
#[cfg(php82)]
67
mod ini;
78
mod module;
89
#[cfg(feature = "embed")]
910
mod sapi;
1011

1112
pub use class::ClassBuilder;
1213
pub use function::FunctionBuilder;
14+
#[cfg(php82)]
1315
pub use ini::IniBuilder;
1416
pub use module::{ModuleBuilder, ModuleStartup};
1517
#[cfg(feature = "embed")]

0 commit comments

Comments
 (0)