From 39768de59f0f0df801db73943b3726598031ad8a Mon Sep 17 00:00:00 2001 From: Baitinq Date: Tue, 25 Oct 2022 02:06:32 +0200 Subject: Indexer: Add "correct" error handling --- indexer/src/main.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/indexer/src/main.rs b/indexer/src/main.rs index 43a5f7f..4d88f0a 100644 --- a/indexer/src/main.rs +++ b/indexer/src/main.rs @@ -21,7 +21,7 @@ async fn serve_http_endpoint(address: &str, port: u16) -> std::io::Result<()> { HttpServer::new(move || { App::new() .app_data(shared_state.clone()) - .service(greet) + .service(search) .service(add_resource) }) .bind((address, port))? @@ -67,17 +67,17 @@ async fn add_resource(data: web::Data, resource: web::Json) } #[get("/search/{term}")] -async fn greet(data: web::Data, term: web::Path) -> impl Responder { +async fn search(data: web::Data, term: web::Path) -> impl Responder { let query: Vec<&str> = term.split(' ').collect(); let database = data.database.lock().unwrap(); let mut valid_results: Option> = None; for w in query { - let curr_word_results = database.get(w); - if curr_word_results.is_none() { - return format!("No results found for {:?}!", w); - } - let curr_word_results = curr_word_results.unwrap(); + let curr_word_results = match database.get(w) { + None => return format!("No results found for {:?}!", w), + Some(results) => results, + }; + match valid_results { None => { valid_results = Some(curr_word_results.clone()); -- cgit 1.4.1