simbld_http/utils/populate_metadata.rs
1use std::collections::HashMap;
2
3/// fills metadata according to the HTTP status code, the description and metadata of the request.
4///
5/// # arguments
6///
7/// * `code` - the HTTP status code.
8/// * `Description` - The description associated with the status code.
9/// * `Request_Metadata` - Optional metadata of the request in the form of a hashmap`.
10///
11/// # returns
12///
13/// a hashmap 'containing the metadata, including the description, if it is a mistake, and the family of status.
14///
15/// # Example
16///
17/// ```rust
18/// use std::collections::HashMap;
19/// use simbld_http::utils::populate_metadata::populate_metadata;
20///
21/// let code = 404;
22/// let description = "Not Found";
23/// let request_metadata = Some(HashMap::from([("method", "GET"), ("url", "https://example.com")]));
24/// let metadata = populate_metadata(code, description, request_metadata);
25/// println!("{:?}", metadata);
26/// ```
27pub fn populate_metadata(
28 code: u16,
29 description: &str,
30 request_metadata: Option<HashMap<&str, &str>>,
31) -> HashMap<String, String> {
32 let mut metadata = HashMap::new();
33
34 metadata.insert("description".to_string(), description.to_string());
35 metadata.insert("is_error".to_string(), (code >= 400).to_string());
36 metadata.insert(
37 "status_family".to_string(),
38 match code {
39 100..=199 => "Informational",
40 200..=299 => "Success",
41 300..=399 => "Redirection",
42 400..=499 => "Client Error",
43 500..=599 => "Server Error",
44 600..=699 => "Service Error",
45 700..=799 => "Crawler Error",
46 900..=999 => "Local API Error",
47 _ => "Unknown",
48 }
49 .to_string(),
50 );
51
52 if let Some(req_meta) = request_metadata {
53 for (key, value) in req_meta {
54 metadata.insert((*key).to_string(), (*value).to_string());
55 }
56 }
57
58 metadata
59}