Skip to content

Commit 1405454

Browse files
committed
chore: prepare nginx-sys to be published to crates.io
1 parent f23e4b1 commit 1405454

File tree

3 files changed

+128
-15
lines changed

3 files changed

+128
-15
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ license = "Apache-2.0"
1414
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1515

1616
[dependencies]
17-
nginx-sys = { path = "nginx-sys"}
17+
nginx-sys = { path = "nginx-sys", version = "0.1"}
1818

1919
[build-dependencies]
2020
bindgen = "0.64.0"

nginx-sys/Cargo.toml

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22
name = "nginx-sys"
33
version = "0.1.0"
44
edition = "2021"
5+
categories = ["external-ffi-bindings"]
6+
description = "FFI bindings to NGINX"
7+
repository = "https://github.com/nginxinc/ngx-rust"
8+
homepage = "https://github.com/nginxinc/ngx-rust"
59
license = "Apache-2.0"
10+
keywords = ["nginx", "ffi", "sys"]
611

712
[lib]
8-
crate-type = ["staticlib","rlib"]
13+
crate-type = ["staticlib", "rlib"]
914

1015
[dependencies]
1116

1217
[build-dependencies]
13-
bindgen = "0.64.0"
18+
bindgen = "0.65.0"
1419
which = "4.4.0"
1520
duct = "0.13.6"
1621
ureq = { version = "2.6.2", features = ["tls"] }

nginx-sys/src/lib.rs

+120-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,75 @@
1-
#![allow(non_upper_case_globals)]
2-
#![allow(non_camel_case_types)]
3-
#![allow(non_snake_case)]
4-
#![allow(dead_code)]
5-
#![allow(clippy::all)]
6-
#![allow(improper_ctypes)]
7-
8-
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
1+
//! # nginx-sys
2+
//!
3+
//! The `nginx-sys` crate provides low-level bindings for the nginx C API, allowing Rust applications to interact with nginx servers and modules.
4+
//!
5+
//! ## Usage
6+
//!
7+
//! Add `nginx-sys` as a dependency in your `Cargo.toml`:
8+
//!
9+
//! ```toml
10+
//! [dependencies]
11+
//! nginx-sys = "0.1.0"
12+
//! ```
13+
//!
14+
//! ## Features
15+
//!
16+
//! - `build`: Enables the build scripts to compile and link against the nginx C library. This feature is enabled by default.
17+
//!
18+
//! ## Examples
19+
//!
20+
//! ### Get Nginx Version
21+
//!
22+
//! This example demonstrates how to retrieve the version of the nginx server.
23+
//!
24+
//! ```rust,no_run
25+
//! use nginx_sys::nginx_version;
26+
//!
27+
//! fn main() {
28+
//! let version = unsafe { nginx_version() };
29+
//! println!("Nginx version: {}", version);
30+
//! }
31+
//! ```
32+
//!
33+
#![warn(missing_docs)]
934

1035
use std::fmt;
1136
use std::ptr::copy_nonoverlapping;
1237
use std::slice;
1338

39+
#[doc(hidden)]
40+
mod bindings {
41+
#![allow(missing_docs)]
42+
#![allow(non_upper_case_globals)]
43+
#![allow(non_camel_case_types)]
44+
#![allow(non_snake_case)]
45+
#![allow(dead_code)]
46+
#![allow(clippy::all)]
47+
#![allow(improper_ctypes)]
48+
#[allow(rustdoc::broken_intra_doc_links)]
49+
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
50+
}
51+
#[doc(no_inline)]
52+
pub use bindings::*;
53+
54+
/// Convert a string slice (`&str`) to a raw pointer (`*mut u_char`) allocated in the given nginx memory pool.
55+
///
56+
/// # Arguments
57+
///
58+
/// * `pool` - A pointer to the nginx memory pool (`ngx_pool_t`).
59+
/// * `data` - The string slice to convert to a raw pointer.
60+
///
61+
/// # Safety
62+
/// This function is marked as unsafe because it involves raw pointer manipulation and direct memory allocation using `ngx_palloc`.
63+
///
64+
/// # Returns
65+
/// A raw pointer (`*mut u_char`) to the allocated memory containing the converted string data.
66+
///
67+
/// # Example
68+
/// ```rust
69+
/// let pool: *mut ngx_pool_t = ...; // Obtain a pointer to the nginx memory pool
70+
/// let data: &str = "example"; // The string to convert
71+
/// let ptr = str_to_uchar(pool, data);
72+
/// ```
1473
pub fn str_to_uchar(pool: *mut ngx_pool_t, data: &str) -> *mut u_char {
1574
let ptr: *mut u_char = unsafe { ngx_palloc(pool, data.len() as _) as _ };
1675
unsafe {
@@ -20,28 +79,54 @@ pub fn str_to_uchar(pool: *mut ngx_pool_t, data: &str) -> *mut u_char {
2079
}
2180

2281
impl ngx_str_t {
23-
// convert nginx string to str slice
82+
/// Convert the nginx string to a string slice (`&str`).
83+
///
84+
/// # Safety
85+
/// This function is marked as unsafe because it involves raw pointer manipulation.
86+
/// It assumes that the underlying `data` pointer is valid and points to a valid UTF-8 encoded string.
87+
///
88+
/// # Returns
89+
/// A string slice (`&str`) representing the nginx string.
2490
pub fn to_str(&self) -> &str {
2591
unsafe {
2692
let slice = slice::from_raw_parts(self.data, self.len as usize);
2793
return std::str::from_utf8(slice).unwrap();
2894
}
2995
}
3096

31-
// get string
97+
/// Convert the nginx string to a `String` by copying its contents.
98+
///
99+
/// # Returns
100+
/// A new `String` containing the contents of the nginx string.
32101
pub fn to_string(&self) -> String {
33102
return String::from(self.to_str());
34103
}
35104

36-
/// create from string
105+
/// Create an `ngx_str_t` instance from a `String`.
106+
///
107+
/// # Arguments
108+
///
109+
/// * `pool` - A pointer to the nginx memory pool (`ngx_pool_t`).
110+
/// * `data` - The `String` from which to create the nginx string.
111+
///
112+
/// # Returns
113+
/// An `ngx_str_t` instance representing the given `String`.
37114
pub fn from_string(pool: *mut ngx_pool_t, data: String) -> Self {
38115
ngx_str_t {
39116
data: str_to_uchar(pool, data.as_str()),
40117
len: data.len() as _,
41118
}
42119
}
43120

44-
/// create from string
121+
/// Create an `ngx_str_t` instance from a string slice (`&str`).
122+
///
123+
/// # Arguments
124+
///
125+
/// * `pool` - A pointer to the nginx memory pool (`ngx_pool_t`).
126+
/// * `data` - The string slice from which to create the nginx string.
127+
///
128+
/// # Returns
129+
/// An `ngx_str_t` instance representing the given string slice.
45130
pub fn from_str(pool: *mut ngx_pool_t, data: &str) -> Self {
46131
ngx_str_t {
47132
data: str_to_uchar(pool, data),
@@ -82,6 +167,29 @@ impl TryFrom<ngx_str_t> for &str {
82167
}
83168
}
84169

170+
/// Add a key-value pair to an nginx table entry (`ngx_table_elt_t`) in the given nginx memory pool.
171+
///
172+
/// # Arguments
173+
///
174+
/// * `table` - A pointer to the nginx table entry (`ngx_table_elt_t`) to modify.
175+
/// * `pool` - A pointer to the nginx memory pool (`ngx_pool_t`) for memory allocation.
176+
/// * `key` - The key string to add to the table entry.
177+
/// * `value` - The value string to add to the table entry.
178+
///
179+
/// # Safety
180+
/// This function is marked as unsafe because it involves raw pointer manipulation and direct memory allocation using `str_to_uchar`.
181+
///
182+
/// # Returns
183+
/// An `Option<()>` representing the result of the operation. `Some(())` indicates success, while `None` indicates a null table pointer.
184+
///
185+
/// # Example
186+
/// ```rust
187+
/// let table: *mut ngx_table_elt_t = ...; // Obtain a pointer to the nginx table entry
188+
/// let pool: *mut ngx_pool_t = ...; // Obtain a pointer to the nginx memory pool
189+
/// let key: &str = "key"; // The key to add
190+
/// let value: &str = "value"; // The value to add
191+
/// let result = add_to_ngx_table(table, pool, key, value);
192+
/// ```
85193
pub fn add_to_ngx_table(table: *mut ngx_table_elt_t, pool: *mut ngx_pool_t, key: &str, value: &str) -> Option<()> {
86194
if table.is_null() {
87195
return None;

0 commit comments

Comments
 (0)