diff --git a/src/endpoints.rs b/src/endpoints.rs index eb91b02..4d2c444 100644 --- a/src/endpoints.rs +++ b/src/endpoints.rs @@ -31,14 +31,12 @@ pub async fn handler(state: &rocket::State, client_ip: ClientIp, s { let rate_limit = &state.rate_limit; - if client_ip.0.is_none() { - return Err((Status::InternalServerError, Json(Response { + let client_ip = client_ip.0.ok_or( + (Status::InternalServerError, Json(Response { status: "error", message: Some("Could not identify client IP".to_string()) - }))) - } - - let client_ip = client_ip.0.unwrap(); + })) + )?; let rate_limit_outcome = rate_limit.check_key(&client_ip); @@ -59,14 +57,12 @@ pub async fn handler(state: &rocket::State, client_ip: ClientIp, s form.slug == slug }); - if form_conf.is_none() { - return Err((Status::NotFound, Json(Response { + let form_conf = form_conf.ok_or( + (Status::NotFound, Json(Response { status: "notfound", message: Some(format!("Form ID {} not found", slug)) - }))); - } - - let form_conf = form_conf.unwrap(); + })) + )?; let mut message_text = format!( "You have recieved a message on {}: diff --git a/src/main.rs b/src/main.rs index 7c5d7e7..92e12e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,11 +19,11 @@ pub struct State { fn rocket() -> _ { let args: Vec = std::env::args().collect(); - if args.len() - 1 != 1 { + let [_exe, path] = args.as_slice() else { panic!("Expected 1 argument, got {}", args.len() - 1) - } + }; - let path = Path::new(&args[1]); + let path = Path::new(path); let config = crate::config::Config::parse(path).unwrap(); let smtp_builder = SmtpClientBuilder::new(config.mailer.host.clone(), config.mailer.port) @@ -38,7 +38,7 @@ fn rocket() -> _ { Duration::from_secs(config.rate_limit.replenish_seconds.into()) ).unwrap().allow_burst(config.rate_limit.burst_max); - let rate_limit : RateLimiter = RateLimiter::dashmap(quota); + let rate_limit = RateLimiter::dashmap(quota); let state = State { config, @@ -49,7 +49,7 @@ fn rocket() -> _ { let rocket_conf = rocket::config::Config { limits: Limits::default() .limit("json", 2.kibibytes()), - ..rocket::config::Config::default() + ..Default::default() }; rocket::custom(rocket_conf)