Skip to content

Commit

Permalink
chore: clippy lint -Dwarnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Yacobucci authored and ivanitskiy committed Aug 25, 2023
1 parent 57c68ff commit a1e55cd
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 17 deletions.
20 changes: 5 additions & 15 deletions nginx-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@
//! ```rust,no_run
//! use nginx_sys::nginx_version;
//!
//! fn main() {
//! let version = unsafe { nginx_version() };
//! println!("Nginx version: {}", version);
//! }
//! let version = unsafe { nginx_version() };
//! println!("Nginx version: {}", version);
//! ```
//!
#![warn(missing_docs)]
Expand All @@ -45,7 +43,7 @@ mod bindings {
#![allow(dead_code)]
#![allow(clippy::all)]
#![allow(improper_ctypes)]
#[allow(rustdoc::broken_intra_doc_links)]
#![allow(rustdoc::broken_intra_doc_links)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}
#[doc(no_inline)]
Expand Down Expand Up @@ -89,19 +87,11 @@ impl ngx_str_t {
/// A string slice (`&str`) representing the nginx string.
pub fn to_str(&self) -> &str {
unsafe {
let slice = slice::from_raw_parts(self.data, self.len as usize);
let slice = slice::from_raw_parts(self.data, self.len);
return std::str::from_utf8(slice).unwrap();
}
}

/// Convert the nginx string to a `String` by copying its contents.
///
/// # Returns
/// A new `String` containing the contents of the nginx string.
pub fn to_string(&self) -> String {
return String::from(self.to_str());
}

/// Create an `ngx_str_t` instance from a `String`.
///
/// # Arguments
Expand Down Expand Up @@ -140,7 +130,7 @@ impl From<ngx_str_t> for &[u8] {
if s.len == 0 || s.data.is_null() {
return Default::default();
}
unsafe { slice::from_raw_parts(s.data, s.len as usize) }
unsafe { slice::from_raw_parts(s.data, s.len) }
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/core/status.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use crate::ffi::*;
use std::fmt;

/// Status
///
/// Rust native wrapper for NGINX status codes.
#[derive(Ord, PartialOrd, Eq, PartialEq)]
pub struct Status(pub ngx_int_t);

impl Status {
/// Is this Status equivalent to NGX_OK?
pub fn is_ok(&self) -> bool {
self == &Status::NGX_OK
}
Expand Down Expand Up @@ -40,13 +44,23 @@ macro_rules! ngx_codes {
}

ngx_codes! {
/// NGX_OK - Operation succeeded.
(NGX_OK);
/// NGX_ERROR - Operation failed.
(NGX_ERROR);
/// NGX_AGAIN - Operation incomplete; call the function again.
(NGX_AGAIN);
/// NGX_BUSY - Resource is not available.
(NGX_BUSY);
/// NGX_DONE - Operation complete or continued elsewhere. Also used as an alternative success code.
(NGX_DONE);
/// NGX_DECLINED - Operation rejected, for example, because it is disabled in the configuration.
/// This is never an error.
(NGX_DECLINED);
/// NGX_ABORT - Function was aborted. Also used as an alternative error code.
(NGX_ABORT);
}

/// NGX_CONF_ERROR - An error occurred while parsing and validating configuration.
pub const NGX_CONF_ERROR: *const () = -1isize as *const ();
// pub const CONF_OK: Status = Status(NGX_CONF_OK as ngx_int_t);
18 changes: 18 additions & 0 deletions src/http/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::ffi::*;
use core::ptr;
use std::os::raw::{c_char, c_void};

/// MergeConfigError - configuration cannot be merged with levels above.
#[derive(Debug)]
pub enum MergeConfigError {
/// No value provided for configuration argument
Expand All @@ -21,7 +22,15 @@ impl std::fmt::Display for MergeConfigError {
}
}

/// The `Merge` trait provides a method for merging configuration down through each level.
///
/// A module configuration should implement this trait for setting its configuration throughout
/// each level.
pub trait Merge {
/// Module merge function.
///
/// # Returns
/// Result, Ok on success or MergeConfigError on failure.
fn merge(&mut self, prev: &Self) -> Result<(), MergeConfigError>;
}

Expand All @@ -31,9 +40,18 @@ impl Merge for () {
}
}

/// The `HTTPModule` trait provides the NGINX configuration stage interface.
///
/// These functions allocate structures, initialize them, and merge through the configuration
/// layers.
///
/// See https://nginx.org/en/docs/dev/development_guide.html#adding_new_modules for details.
pub trait HTTPModule {
/// Configuration in the `http` block.
type MainConf: Merge + Default;
/// Configuration in a `server` block within the `http` block.
type SrvConf: Merge + Default;
/// Configuration in a `location` block within the `http` block.
type LocConf: Merge + Default;

/// # Safety
Expand Down
13 changes: 13 additions & 0 deletions src/http/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ impl Request {
Some(co)
}

/// Sets the value as the module's context.
///
/// See https://nginx.org/en/docs/dev/development_guide.html#http_request
pub fn set_module_ctx(&self, value: *mut c_void, module: &ngx_module_t) {
unsafe {
*self.0.ctx.add(module.ctx_index) = value;
Expand Down Expand Up @@ -191,11 +194,17 @@ impl Request {
self.0.headers_out.status = status.into();
}

/// Add header to the `headers_in` object.
///
/// See https://nginx.org/en/docs/dev/development_guide.html#http_request
pub fn add_header_in(&mut self, key: &str, value: &str) -> Option<()> {
let table: *mut ngx_table_elt_t = unsafe { ngx_list_push(&mut self.0.headers_in.headers) as _ };
add_to_ngx_table(table, self.0.pool, key, value)
}

/// Add header to the `headers_out` object.
///
/// See https://nginx.org/en/docs/dev/development_guide.html#http_request
pub fn add_header_out(&mut self, key: &str, value: &str) -> Option<()> {
let table: *mut ngx_table_elt_t = unsafe { ngx_list_push(&mut self.0.headers_out.headers) as _ };
add_to_ngx_table(table, self.0.pool, key, value)
Expand Down Expand Up @@ -340,6 +349,9 @@ impl fmt::Debug for Request {
}
}

/// Iterator for `ngx_list_t` types.
///
/// Implementes the std::iter::Iterator trait.
pub struct NgxListIterator {
done: bool,
part: *const ngx_list_part_t,
Expand Down Expand Up @@ -459,6 +471,7 @@ impl Method {
/// CONNECT
pub const CONNECT: Method = Method(MethodInner::Connect);

/// Convert a Method to a &str.
#[inline]
pub fn as_str(&self) -> &str {
match self.0 {
Expand Down
3 changes: 2 additions & 1 deletion src/http/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl fmt::Debug for HTTPStatus {
}

impl HTTPStatus {
/// Convets a u16 to a status code.
#[inline]
pub fn from_u16(src: u16) -> Result<HTTPStatus, InvalidHTTPStatusCode> {
if !(100..600).contains(&src) {
Expand All @@ -58,7 +59,7 @@ impl HTTPStatus {
Ok(HTTPStatus(src.into()))
}

/// Converts a &[u8] to a status code
/// Converts a &[u8] to a status code.
pub fn from_bytes(src: &[u8]) -> Result<HTTPStatus, InvalidHTTPStatusCode> {
if src.len() != 3 {
return Err(InvalidHTTPStatusCode::new());
Expand Down
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,27 @@
//! ```
#![warn(missing_docs)]
/// The core module.
///
/// This module provides fundamental utilities needed to interface with many NGINX primitives.
/// String conversions, the pool (memory interface) object, and buffer APIs are covered here. These
/// utilities will generally align with the NGINX 'core' files and APIs.
pub mod core;

/// The ffi module.
///
/// This module provides scoped FFI bindings for NGINX symbols.
pub mod ffi;

/// The http module.
///
/// This modules provides wrappers and utilities to NGINX http APIs, such as requests,
/// configuration access, and statuses.
pub mod http;

/// The log module.
///
/// This module provides an interface into the NGINX logger framework.
pub mod log;

/// Define modules exported by this library.
Expand Down
2 changes: 1 addition & 1 deletion tests/log_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Nginx {
// make sure we stop existing nginx and start new master process
// intentinally ignore failure in stop
pub fn restart(&mut self) -> Result<Output> {
self.stop();
let _ = self.stop();
self.start()
}

Expand Down

0 comments on commit a1e55cd

Please sign in to comment.