Skip to content

Commit

Permalink
馃毃 chore: make the clippy checks happy (#12)
Browse files Browse the repository at this point in the history
- add the missing documentation for the code
- add type annonation for the `parse_url` function
  • Loading branch information
neon-mmd authored and Brian3647 committed Dec 26, 2023
1 parent 7b30b59 commit a7d4c96
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! A module that handles and provides easy to use macros for the user.

/// A quick way to create responses.
///
/// Usage:
Expand Down
13 changes: 12 additions & 1 deletion src/request.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! A module that provides code to handle https/http requests.

use std::net::SocketAddr;
use std::{borrow::Cow, collections::HashMap};

Expand Down Expand Up @@ -67,6 +69,15 @@ impl Request {
})
}

/// A function that parses the header form the raw http request headers.
///
/// # Arguments
///
/// * `line` - It takes a http request header line as a byte slice.
///
/// # Returns
///
/// It returns an option type of tuple string containing the parsed header key value pairs.
fn parse_header(line: &[u8]) -> Option<(String, String)> {
let pos = line.iter().position(|&byte| byte == b':')?;
let (key, rest) = line.split_at(pos);
Expand Down Expand Up @@ -160,7 +171,7 @@ impl Request {

/// Get a parsed version of the URL.
/// See [Url]
pub fn parse_url(&self) -> Url {
pub fn parse_url(&self) -> Url<'_> {
self.url.as_str().into()
}

Expand Down
3 changes: 3 additions & 0 deletions src/response/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//! A module that provides code and other modules to serialize/deserialize response into appropriate
//! data types.

mod response_types;
mod responselike;

Expand Down
9 changes: 6 additions & 3 deletions src/response/response_types.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//! A module that provides code to provide different data types which are available for parsing
//! responses into the appropriate available data type.

use crate::{Headers, HttpVersion, Response};

// Macro rule used to create response types during compile time.
// We don't want every function to have documentation for it,
// since it would bloat the documentation, so we hide it.
/// Macro rule used to create response types during compile time.
/// We don't want every function to have documentation for it,
/// since it would bloat the documentation, so we hide it.
macro_rules! create_response_types {
($($name:ident, $code:expr, $text:expr);*) => {
type OptHeaders = Option<Headers>;
Expand Down
3 changes: 3 additions & 0 deletions src/response/responselike.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//! A module that provides and handles traits which can help in serializing and deserializing
//! response into different data types.

use super::Response;

/// A trait for everything that can be converted into a Response.
Expand Down
26 changes: 26 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! A module that provides server implementation for the library.

use crate::Request;
use crate::ResponseLike;

Expand Down Expand Up @@ -29,12 +31,17 @@ use std::future::Future;

/// Single threaded listener made for simpler servers.
pub struct Server {
/// It stores the TcpListener struct.
acceptor: TcpListener,
/// It stores the buffer size for the Tcp requests.
buffer_size: usize,
/// It stores the default HTTP/HTTPS request headers.
insert_default_headers: bool,
/// It stores the TlsAcceptor struct when the tls feature is enabled.
#[cfg(feature = "tls")]
tls_acceptor: TlsAcceptor,
#[cfg(feature = "websocket")]
/// It stores the WebSocket configuration for the HTTP/HTTPS server.
ws_handler: Option<(&'static str, fn(WebSocket<&mut Stream>))>,
}

Expand Down Expand Up @@ -221,6 +228,12 @@ impl Server {

#[cfg(not(feature = "tls"))]
#[inline]
/// A helper function which handles the requests done from the client.
///
/// # Error
///
/// Returns a tuple containing the stream and Client request on success otherwise returns an io
/// error on failure.
fn try_accept_inner(&self) -> io::Result<(Stream, Request)> {
let (stream, ip) = self.acceptor.accept()?;
self.handle_request(stream, ip)
Expand Down Expand Up @@ -253,6 +266,19 @@ impl Server {
}
}

/// A helper function which handles request by checking whether the request has an appropriate
/// buffer size by checking if it is too large or zero (in other words empty response). Also it
/// checks whether the request contains a valid input.
///
/// # Arguments
///
/// * `stream` - It takes the stream implementing read and write as an argument.
/// * `ip` - It takes the ip address as a SocketAddr type.
///
/// # Error
///
/// Returns a tuple containing stream implementing write and read traits and Request struct on
/// success otherwise returns an io error on failure.
fn handle_request<T: io::Write + io::Read>(
&self,
mut stream: T,
Expand Down
2 changes: 2 additions & 0 deletions src/url.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! A module that provides code to handle the parsing of the URL of the server.

use std::{collections::HashMap, fmt::Display};

/// A parsed URL.
Expand Down
2 changes: 2 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! A module that provides code to handle the HTTP/HTTPS header method types.

use std::{fmt::Display, net::SocketAddr};

/// Any valid HTTP method.
Expand Down
2 changes: 2 additions & 0 deletions src/ws.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! A module that provides code to handle the websocketing funtionality of the server-client.

use std::{collections::HashMap, io};

use crate::{headers, Request};
Expand Down

0 comments on commit a7d4c96

Please sign in to comment.