From 0e616570907f3427be99f4bfc227bd57a252c8c1 Mon Sep 17 00:00:00 2001 From: rmanach Date: Thu, 16 Feb 2023 15:24:28 +0100 Subject: [PATCH] fix documentation + update README --- Cargo.toml | 2 +- README.md | 26 +++++++++++++++++++++++++- src/body.rs | 4 ++-- src/lib.rs | 12 +++++++----- src/message.rs | 4 ++-- src/request.rs | 6 +++--- src/response.rs | 1 + src/status.rs | 2 -- 8 files changed, 41 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f1ba79d..e910a70 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "http" -version = "0.1.5" +version = "0.1.6" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/README.md b/README.md index 6f615a0..2cf3258 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,27 @@ # http -A basic Rust lib to parse an HTTP request and build HTTP response. +A basic Rust library to parse an HTTP request and build HTTP response. + +**NOTE**: only few parts of the specification has been implemented and only JSON body are allowed. + +## Integration +Get the latest version: +```toml +http = { git = "https://gitea.thegux.fr/rmanach/http" } +``` + +Or get a specific version: +```toml +http = { git = "https://gitea.thegux.fr/rmanach/http", version = "0.1.2" } +``` + +## Documentation +```bash +cargo doc -r --no-deps --open +``` + +## Launch unit tests +```bash +cargo test +``` + diff --git a/src/body.rs b/src/body.rs index 69ec0f4..73f1070 100644 --- a/src/body.rs +++ b/src/body.rs @@ -7,8 +7,8 @@ pub struct HTTPBody { data: JsonValue, } -/// HTTPBody represents an HTTP request body -/// for simplicity, only JSON body is allowed +/// HTTPBody represents an HTTP request body. +/// For simplicity, only JSON body is allowed. impl HTTPBody { fn new(data: JsonValue) -> HTTPBody { HTTPBody { data } diff --git a/src/lib.rs b/src/lib.rs index 049852b..ebecabb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,12 @@ -//! http parses the request according to the HTTP message specifications -//! it also includes `HTTPResponse` to build an HTTPResponse +//! **http** library is a light HTTP parser and builder. //! -//! see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages -//! NOTE: only few parts of the specification has been implemented +//! It parses the request according to the HTTP message specifications and includes `HTTPResponse` to build an HTTP Response. //! -//! * Only json body allowed +//! See for more details. +//! +//! NOTE: only few parts of the specification has been implemented. +//! +//! * Only JSON body allowed //! * HTTP Headers not parsed mod body; diff --git a/src/message.rs b/src/message.rs index bd5edda..5b24a9a 100644 --- a/src/message.rs +++ b/src/message.rs @@ -36,7 +36,7 @@ impl JSONMessage { self.message.insert(key.to_string(), value.to_string()); } - /// associated function to build an HTTPMessage error + // associated function to build an HTTPMessage error pub fn error(message: &str) -> Option { let mut http_message = JSONMessage::default(); http_message.put("error", message); @@ -54,7 +54,7 @@ impl JSONMessage { } } - /// loops over all the HashMap keys, builds a JSON key value for each one and join them with `JSON_DELIMITER` + /// build_json loops over all the HashMap keys, builds a JSON key value for each one and join them with `JSON_DELIMITER` fn build_json(self) -> String { let unstruct: Vec = self .message diff --git a/src/request.rs b/src/request.rs index 8b63fa1..4e84ae0 100644 --- a/src/request.rs +++ b/src/request.rs @@ -16,14 +16,14 @@ pub struct HTTPRequest<'a> { impl<'a> HTTPRequest<'a> { /// get_request_parts splits correctly the incoming request in order to get: - /// * start_line + /// * start line /// * headers /// * data (if exists) fn get_request_parts(request: &str) -> Result { let mut request_parts: VecDeque<&str> = request.split(HTTP_REQUEST_SEPARATOR).collect(); if request_parts.len() < 3 { - return Err("request has no enough informations to be correctly parsed"); + return Err("request has not enough informations to be correctly parsed"); } let start_line = request_parts.pop_front().unwrap(); @@ -55,7 +55,7 @@ impl<'a> HTTPRequest<'a> { } } - /// get_bodyèvalue retrieves JSON value in `HTTPBody` + /// get_body_value retrieves JSON value in `HTTPBody` pub fn get_body_value(&self, key: &str) -> Option { match self.body { Some(ref b) => match &b.get_data() { diff --git a/src/response.rs b/src/response.rs index fee8ff6..a52feef 100644 --- a/src/response.rs +++ b/src/response.rs @@ -10,6 +10,7 @@ const BAD_REQUEST_ERROR: &'static str = r#"{"error":"bad request"}"#; const STATUS_OK: &'static str = r#"{"status":"ok"}"#; /// HTTPResponse represents an HTTP response (headers are not parsed) +/// /// NOTE: for simplicity, only JSON body are allowed pub struct HTTPResponse { status_line: HTTPStatusLine, diff --git a/src/status.rs b/src/status.rs index 03881e3..a441726 100644 --- a/src/status.rs +++ b/src/status.rs @@ -21,7 +21,6 @@ impl Into for HTTPStatusCode { } } - pub struct HTTPStatusLine { version: HTTPVersion, status_code: HTTPStatusCode, @@ -53,4 +52,3 @@ impl HTTPStatusLine { self.status_code = code; } } -