Skip to content

Commit

Permalink
move the ParamMap type to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
lovasoa committed May 30, 2024
1 parent eb0557a commit cef4066
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
30 changes: 4 additions & 26 deletions src/webserver/http_request_info.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use super::http::SingleOrVec;
use crate::AppState;
use actix_multipart::form::bytes::Bytes;
use actix_multipart::form::tempfile::TempFile;
Expand All @@ -16,13 +15,15 @@ use actix_web_httpauth::headers::authorization::Authorization;
use actix_web_httpauth::headers::authorization::Basic;
use anyhow::anyhow;
use anyhow::Context;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::net::IpAddr;
use std::rc::Rc;
use std::sync::Arc;
use tokio_stream::StreamExt;

use super::request_variables::param_map;
use super::request_variables::ParamMap;

#[derive(Debug)]
pub struct RequestInfo {
pub method: actix_web::http::Method,
Expand Down Expand Up @@ -210,32 +211,9 @@ async fn extract_file(
Ok(file)
}

pub type ParamMap = HashMap<String, SingleOrVec>;

fn param_map<PAIRS: IntoIterator<Item = (String, String)>>(values: PAIRS) -> ParamMap {
values
.into_iter()
.fold(HashMap::new(), |mut map, (mut k, v)| {
let entry = if k.ends_with("[]") {
k.replace_range(k.len() - 2.., "");
SingleOrVec::Vec(vec![v])
} else {
SingleOrVec::Single(v)
};
match map.entry(k) {
Entry::Occupied(mut s) => {
SingleOrVec::merge(s.get_mut(), entry);
}
Entry::Vacant(v) => {
v.insert(entry);
}
}
map
})
}

#[cfg(test)]
mod test {
use super::super::http::SingleOrVec;
use super::*;
use crate::app_config::AppConfig;
use actix_web::{http::header::ContentType, test::TestRequest};
Expand Down
1 change: 1 addition & 0 deletions src/webserver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod error_with_status;
pub mod http;
pub mod http_request_info;
mod https;
pub mod request_variables;

pub use database::Database;
pub use error_with_status::ErrorWithStatus;
Expand Down
27 changes: 27 additions & 0 deletions src/webserver/request_variables.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::collections::{hash_map::Entry, HashMap};

use super::http::SingleOrVec;

pub type ParamMap = HashMap<String, SingleOrVec>;

pub fn param_map<PAIRS: IntoIterator<Item = (String, String)>>(values: PAIRS) -> ParamMap {
values
.into_iter()
.fold(HashMap::new(), |mut map, (mut k, v)| {
let entry = if k.ends_with("[]") {
k.replace_range(k.len() - 2.., "");
SingleOrVec::Vec(vec![v])
} else {
SingleOrVec::Single(v)
};
match map.entry(k) {
Entry::Occupied(mut s) => {
SingleOrVec::merge(s.get_mut(), entry);
}
Entry::Vacant(v) => {
v.insert(entry);
}
}
map
})
}

0 comments on commit cef4066

Please sign in to comment.