diff --git a/Cargo.toml b/Cargo.toml index 78505f4..081e820 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,8 +8,6 @@ license = "MIT" [lib] -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] actix-web = "4.1.0" serde_json = { version = "1" } diff --git a/src/dtos/message.rs b/src/dtos/message.rs index c5fa75c..29ba731 100644 --- a/src/dtos/message.rs +++ b/src/dtos/message.rs @@ -1,5 +1,10 @@ use serde::{Serialize, Deserialize}; +/// This is for sending errors back from requests. +/// This struct contains an optional key just in +/// case you want to deal with internationalization +/// It was left as optional just in case you don't +/// Have the time to yet... #[derive(Serialize, Deserialize, Debug)] pub struct MessageResource{ pub key: Option, @@ -7,9 +12,6 @@ pub struct MessageResource{ } impl MessageResource{ - pub fn new_from_str_with_type(msg: &str) -> MessageResource{ - MessageResource { key: None, message: String::from(msg) } - } pub fn new_empty() -> MessageResource{ MessageResource { key: None, message: String::from("") } } diff --git a/src/extensions/typed_response.rs b/src/extensions/typed_response.rs index 57b5891..bb37a3d 100644 --- a/src/extensions/typed_response.rs +++ b/src/extensions/typed_response.rs @@ -3,12 +3,27 @@ use serde::Serialize; use crate::dtos::message::MessageResource; - -pub struct TypedHttpResponse { +/// +/// ``` +/// +/// use actix_web::{web::{Path}, http::StatusCode}; +/// use actix_web_utils::extensions::typed_response::TypedHttpResponse; +/// use actix_web_utils::dtos::message::MessageResource; +/// +/// //Sample route +/// pub async fn testroute(number: Path) -> TypedHttpResponse { +/// if(*number > 0){ +/// return TypedHttpResponse::return_standard_response(StatusCode::OK, String::from("This is my test response!")); +/// } +/// TypedHttpResponse::return_empty_response(StatusCode::BAD_REQUEST); +/// } +/// +/// ``` +pub struct TypedHttpResponse { pub response: HttpResponse>>>>, } impl TypedHttpResponse { - /// Returns a response with the json struct inside + Status code + /// Returns a response with the json struct you define inside + Status code pub fn return_standard_response(status_code: StatusCode, body: B) -> TypedHttpResponse{ TypedHttpResponse { response: HttpResponse::with_body(StatusCode::from_u16(u16::from(status_code)).unwrap(), Some(web::Json(Ok(body)))) @@ -20,18 +35,22 @@ impl TypedHttpResponse { response: HttpResponse::with_body(StatusCode::from_u16(u16::from(status_code)).unwrap(), Some(web::Json(Err(body)))) } } + /// This is to return a MessageResource wrapped inside an HttpResponse pub fn return_standard_error(status_code: StatusCode, body: MessageResource) -> TypedHttpResponse{ TypedHttpResponse { response: HttpResponse::with_body(StatusCode::from_u16(u16::from(status_code)).unwrap(), Some(web::Json(Err(vec![body])))) } } - /// Returns an empty response with status code + /// Returns an empty http response with a status code + /// This is a bad practice, but I still left it here + /// as it is useful for debugging & specific cases pub fn return_empty_response(status_code: StatusCode) -> TypedHttpResponse{ TypedHttpResponse { response: HttpResponse::with_body(StatusCode::from_u16(u16::from(status_code)).unwrap(), None) } } } + impl Responder for TypedHttpResponse { type Body = BoxBody; @@ -46,4 +65,5 @@ impl Responder for TypedHttpResponse None => {builder.finish()} } } -} \ No newline at end of file +} + diff --git a/src/lib.rs b/src/lib.rs index 2746446..db510a3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,2 @@ pub mod extensions; -pub mod dtos; \ No newline at end of file +pub mod dtos;