Use a type alias instead of struct for Keys

This commit is contained in:
finga 2020-08-29 01:13:06 +02:00
parent b2e8141676
commit 4b86d2a6c1
2 changed files with 16 additions and 24 deletions

View file

@ -42,9 +42,7 @@ struct Ldap0rConfig {
const BASE62: &[u8] = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; const BASE62: &[u8] = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
struct Keys { type Keys = Arc<Mutex<HashMap<String, String>>>;
keys: Arc<Mutex<HashMap<String, String>>>,
}
fn reset_prepare(config: &Ldap0rConfig, keys: &Keys, email_address: &str) -> Result<()> { fn reset_prepare(config: &Ldap0rConfig, keys: &Keys, email_address: &str) -> Result<()> {
// ldap lookup // ldap lookup
@ -70,7 +68,7 @@ fn reset_prepare(config: &Ldap0rConfig, keys: &Keys, email_address: &str) -> Res
.collect(); .collect();
// store key with id // store key with id
let keys = Arc::clone(&keys.keys); let keys = Arc::clone(&keys);
let mut keys = keys let mut keys = keys
.lock() .lock()
.map_err(|e| anyhow!("Could not aquire lock for keys: {}", e))?; .map_err(|e| anyhow!("Could not aquire lock for keys: {}", e))?;
@ -117,7 +115,7 @@ fn set_password(
} }
// key lookup // key lookup
let keys = Arc::clone(&keys.keys); let keys = Arc::clone(&keys);
let mut keys = match keys.lock() { let mut keys = match keys.lock() {
Ok(keys) => keys, Ok(keys) => keys,
Err(e) => { Err(e) => {
@ -226,7 +224,7 @@ fn reset_email(
#[get("/reset/<key>")] #[get("/reset/<key>")]
fn reset_key(keys: State<Keys>, key: String, flash: Option<FlashMessage>) -> Option<Template> { fn reset_key(keys: State<Keys>, key: String, flash: Option<FlashMessage>) -> Option<Template> {
let keys = Arc::clone(&keys.keys); let keys = Arc::clone(&keys);
if let Ok(keys) = keys.lock() { if let Ok(keys) = keys.lock() {
let mut context = HashMap::new(); let mut context = HashMap::new();
@ -310,8 +308,6 @@ fn main() {
Ok(rocket.manage(ldap0r_config)) Ok(rocket.manage(ldap0r_config))
})) }))
.attach(Template::fairing()) .attach(Template::fairing())
.manage(Keys { .manage(Keys::new(Mutex::new(HashMap::new())))
keys: Arc::new(Mutex::new(HashMap::new())),
})
.launch(); .launch();
} }

View file

@ -33,7 +33,7 @@ fn reset() {
fn reset_valid_url() { fn reset_valid_url() {
use super::Keys; use super::Keys;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::{Arc, Mutex}; use std::sync::Mutex;
let mut context = HashMap::new(); let mut context = HashMap::new();
context.insert( context.insert(
@ -44,16 +44,14 @@ fn reset_valid_url() {
let rocket = rocket::ignite() let rocket = rocket::ignite()
.mount("/", routes![super::reset_key]) .mount("/", routes![super::reset_key])
.attach(Template::fairing()) .attach(Template::fairing())
.manage(Keys { .manage(Keys::new(Mutex::new(
keys: Arc::new(Mutex::new( Some((
Some(( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".to_string(),
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".to_string(), "foobar".to_string(),
"foobar".to_string(), ))
)) .into_iter()
.into_iter() .collect(),
.collect(), )));
)),
});
let client = Client::new(rocket).unwrap(); let client = Client::new(rocket).unwrap();
let mut response = client let mut response = client
.get("/reset/0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") .get("/reset/0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
@ -67,13 +65,11 @@ fn reset_valid_url() {
fn reset_invalid_url() { fn reset_invalid_url() {
use super::Keys; use super::Keys;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::{Arc, Mutex}; use std::sync::Mutex;
let rocket = rocket::ignite() let rocket = rocket::ignite()
.mount("/", routes![super::reset, super::reset_key]) .mount("/", routes![super::reset, super::reset_key])
.manage(Keys { .manage(Keys::new(Mutex::new(HashMap::new())));
keys: Arc::new(Mutex::new(HashMap::new())),
});
let client = Client::new(rocket).unwrap(); let client = Client::new(rocket).unwrap();
let response = client let response = client
.get("/reset/0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") .get("/reset/0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")