Skip to content

Commit

Permalink
Update request and response to move locals to response, Fix doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
emrecancorapci committed Jul 15, 2024
1 parent 65022a2 commit 2735473
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 39 deletions.
4 changes: 3 additions & 1 deletion src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ pub use serde_json::to_string as struct_to_string;
/// # Example
///
/// ```rust
/// use krustie::{ Request, Response, StatusCode, request::RequestBody, json::{ get_string_from_json, json } };
///
/// fn post_req(req: &Request, res: &mut Response) {
/// match req.get_body() {
/// RequestBody::Json(body) => {
/// let server_key_option = body.get("server");
///
/// match get_string_from_json(server_key_option).unwrap() {
/// match get_string_from_json(server_key_option).unwrap().as_str() {
/// "Krustie" => {
/// res.status(StatusCode::Ok).body_json(body.clone());
/// },
Expand Down
37 changes: 1 addition & 36 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub struct Request {
request: RequestLine,
headers: HashMap<String, String>,
body: RequestBody,
locals: HashMap<String, String>,
peer_addr: SocketAddr,
}

Expand Down Expand Up @@ -88,40 +87,6 @@ impl Request {
&self.body
}

/// Adds a local variable to the http request
///
/// `Local` variables can be used to store data that can be defined in a *middleware* and accessed in the *controller*
///
/// # Example
///
/// ```rust
/// use krustie::{ Request, Response };
///
/// fn get(request: &Request, response: &mut Response) {
/// request.add_local("user_id", "123");
/// }
/// ```
pub fn add_local(&mut self, key: &str, value: &str) {
self.locals.insert(key.to_string(), value.to_string());
}

/// Returns the value of the local variable
///
/// `Local` variables can be used to store data that can be defined in a *middleware* and accessed in the *controller*
///
/// # Example
///
/// ```rust
/// use krustie::{ Request, Response };
///
/// fn get(request: &Request, response: &mut Response) {
/// let user_id = request.get_local("user_id");
/// }
/// ```
pub fn get_local(&self, key: &str) -> Option<&String> {
self.locals.get(key)
}

/// Returns the peer address of the HTTP request
///
/// The peer address is the address of the client that made the request
Expand All @@ -130,6 +95,7 @@ impl Request {
///
/// ```rust
/// use krustie::{ Request, Response };
/// use std::net::SocketAddr;
///
/// fn get(request: &Request, response: &mut Response) {
/// let peer_addr: &SocketAddr = request.get_peer_addr();
Expand Down Expand Up @@ -158,7 +124,6 @@ impl Default for Request {
),
headers: HashMap::new(),
body: RequestBody::None,
locals: HashMap::new(),
peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0),
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/request/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ impl Request {
headers,
peer_addr,
body: RequestBody::None,
locals: HashMap::new(),
});
}

Expand All @@ -63,7 +62,6 @@ impl Request {
headers,
peer_addr,
body,
locals: HashMap::new(),
})
}

Expand Down
2 changes: 2 additions & 0 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub struct Response {
http_version: String,
status_code: StatusCode,
headers: HashMap<String, String>,
locals: HashMap<String, String>,
body: Vec<u8>,
}

Expand Down Expand Up @@ -167,6 +168,7 @@ impl Default for Response {
status_code: StatusCode::NotFound,
headers: HashMap::new(),
body: Vec::new(),
locals: HashMap::new(),
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions src/response/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,38 @@ impl Response {
self.body = body;
return Ok(());
}

/// Adds a local variable to the http request
///
/// `Local` variables can be used to store data that can be defined in a *middleware* and accessed in the *controller*
///
/// # Example
///
/// ```rust
/// use krustie::{ Request, Response };
///
/// fn get(request: &Request, response: &mut Response) {
/// response.add_local("user_id", "123");
/// }
/// ```
pub fn add_local(&mut self, key: &str, value: &str) {
self.locals.insert(key.to_string(), value.to_string());
}

/// Returns the value of the local variable
///
/// `Local` variables can be used to store data that can be defined in a *middleware* and accessed in the *controller*
///
/// # Example
///
/// ```rust
/// use krustie::{ Request, Response };
///
/// fn get(request: &Request, response: &mut Response) {
/// let user_id = response.get_local("user_id");
/// }
/// ```
pub fn get_local(&self, key: &str) -> Option<&String> {
self.locals.get(key)
}
}

0 comments on commit 2735473

Please sign in to comment.