Use a type alias instead of struct for Keys
This commit is contained in:
parent
b2e8141676
commit
4b86d2a6c1
2 changed files with 16 additions and 24 deletions
14
src/main.rs
14
src/main.rs
|
@ -42,9 +42,7 @@ struct Ldap0rConfig {
|
|||
|
||||
const BASE62: &[u8] = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
struct Keys {
|
||||
keys: Arc<Mutex<HashMap<String, String>>>,
|
||||
}
|
||||
type Keys = Arc<Mutex<HashMap<String, String>>>;
|
||||
|
||||
fn reset_prepare(config: &Ldap0rConfig, keys: &Keys, email_address: &str) -> Result<()> {
|
||||
// ldap lookup
|
||||
|
@ -70,7 +68,7 @@ fn reset_prepare(config: &Ldap0rConfig, keys: &Keys, email_address: &str) -> Res
|
|||
.collect();
|
||||
|
||||
// store key with id
|
||||
let keys = Arc::clone(&keys.keys);
|
||||
let keys = Arc::clone(&keys);
|
||||
let mut keys = keys
|
||||
.lock()
|
||||
.map_err(|e| anyhow!("Could not aquire lock for keys: {}", e))?;
|
||||
|
@ -117,7 +115,7 @@ fn set_password(
|
|||
}
|
||||
|
||||
// key lookup
|
||||
let keys = Arc::clone(&keys.keys);
|
||||
let keys = Arc::clone(&keys);
|
||||
let mut keys = match keys.lock() {
|
||||
Ok(keys) => keys,
|
||||
Err(e) => {
|
||||
|
@ -226,7 +224,7 @@ fn reset_email(
|
|||
|
||||
#[get("/reset/<key>")]
|
||||
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() {
|
||||
let mut context = HashMap::new();
|
||||
|
||||
|
@ -310,8 +308,6 @@ fn main() {
|
|||
Ok(rocket.manage(ldap0r_config))
|
||||
}))
|
||||
.attach(Template::fairing())
|
||||
.manage(Keys {
|
||||
keys: Arc::new(Mutex::new(HashMap::new())),
|
||||
})
|
||||
.manage(Keys::new(Mutex::new(HashMap::new())))
|
||||
.launch();
|
||||
}
|
||||
|
|
26
src/tests.rs
26
src/tests.rs
|
@ -33,7 +33,7 @@ fn reset() {
|
|||
fn reset_valid_url() {
|
||||
use super::Keys;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::sync::Mutex;
|
||||
|
||||
let mut context = HashMap::new();
|
||||
context.insert(
|
||||
|
@ -44,16 +44,14 @@ fn reset_valid_url() {
|
|||
let rocket = rocket::ignite()
|
||||
.mount("/", routes![super::reset_key])
|
||||
.attach(Template::fairing())
|
||||
.manage(Keys {
|
||||
keys: Arc::new(Mutex::new(
|
||||
Some((
|
||||
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".to_string(),
|
||||
"foobar".to_string(),
|
||||
))
|
||||
.into_iter()
|
||||
.collect(),
|
||||
)),
|
||||
});
|
||||
.manage(Keys::new(Mutex::new(
|
||||
Some((
|
||||
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".to_string(),
|
||||
"foobar".to_string(),
|
||||
))
|
||||
.into_iter()
|
||||
.collect(),
|
||||
)));
|
||||
let client = Client::new(rocket).unwrap();
|
||||
let mut response = client
|
||||
.get("/reset/0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
|
||||
|
@ -67,13 +65,11 @@ fn reset_valid_url() {
|
|||
fn reset_invalid_url() {
|
||||
use super::Keys;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::sync::Mutex;
|
||||
|
||||
let rocket = rocket::ignite()
|
||||
.mount("/", routes![super::reset, super::reset_key])
|
||||
.manage(Keys {
|
||||
keys: Arc::new(Mutex::new(HashMap::new())),
|
||||
});
|
||||
.manage(Keys::new(Mutex::new(HashMap::new())));
|
||||
let client = Client::new(rocket).unwrap();
|
||||
let response = client
|
||||
.get("/reset/0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
|
||||
|
|
Loading…
Reference in a new issue