aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorkhai96_ <hvksmr1996@gmail.com>2019-05-05 14:21:34 +0000
committerkhai96_ <hvksmr1996@gmail.com>2019-05-05 14:21:34 +0000
commit28f35beb4104bf777f3e84e4fe102599ec405ed6 (patch)
treea1268ac73455c6b788f3b4bec0053c0437ef8a01 /src
parentAdd unit test for where it provides multiple valid auth strings (diff)
downloadminiserve-28f35beb4104bf777f3e84e4fe102599ec405ed6.tar.gz
miniserve-28f35beb4104bf777f3e84e4fe102599ec405ed6.zip
Refactoring: Use immutable HashMap
This should allow compiler to optimize further
Diffstat (limited to '')
-rw-r--r--src/args.rs25
1 files changed, 12 insertions, 13 deletions
diff --git a/src/args.rs b/src/args.rs
index 3d6cac4..4851b41 100644
--- a/src/args.rs
+++ b/src/args.rs
@@ -79,12 +79,13 @@ fn parse_interface(src: &str) -> Result<IpAddr, std::net::AddrParseError> {
/// Parse a string of multiple authentication requirements
fn parse_auth(src: &str) -> Result<auth::RequiredAuth, ContextualError> {
- let mut required_auth = auth::RequiredAuth::new();
-
- for pair in src.split_whitespace().map(parse_single_auth) {
- let (username, password) = pair?;
- required_auth.insert(username.to_owned(), password);
- }
+ let required_auth = src
+ .split_whitespace()
+ .map(parse_single_auth)
+ .collect::<Result<Vec<_>, ContextualError>>()?
+ .iter()
+ .cloned()
+ .collect::<auth::RequiredAuth>();
Ok(required_auth)
}
@@ -237,17 +238,15 @@ mod tests {
fn parse_multiple_auth_valid() -> Result<(), ContextualError> {
let hex2str = |x: &str| hex::decode(x).expect("Invalid hex code");
- let pairs = [
+ let expected: auth::RequiredAuth = [
("usr0", auth::RequiredAuthPassword::Plain("pwd0".to_owned())),
("usr1", auth::RequiredAuthPassword::Plain("pwd1".to_owned())),
("usr2", auth::RequiredAuthPassword::Sha256(hex2str("abcd"))),
("usr3", auth::RequiredAuthPassword::Sha512(hex2str("abcd"))),
- ];
-
- let mut expected = auth::RequiredAuth::new();
- for (username, password) in pairs.iter() {
- expected.insert(username.to_owned().to_string(), password.clone());
- }
+ ]
+ .iter()
+ .map(|(username, password)| (username.to_owned().to_string(), password.clone()))
+ .collect();
assert_eq!(
parse_auth("usr0:pwd0 usr1:pwd1 usr2:sha256:abcd usr3:sha512:abcd")?,