From bf6c4bd75168671a5a0e53f7390ebd4796a6bd57 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sun, 14 Mar 2021 15:39:00 +0100 Subject: chore: add helpers module --- src/helpers.rs | 9 +++++++++ src/main.rs | 1 + src/repository/routes.rs | 6 ++---- 3 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 src/helpers.rs diff --git a/src/helpers.rs b/src/helpers.rs new file mode 100644 index 0000000..d915a50 --- /dev/null +++ b/src/helpers.rs @@ -0,0 +1,9 @@ +use uuid::Uuid; + +/// Returns a valid Uuid if `id` is not a valid Uuid +pub fn uuid_from_string(id: &String) -> Uuid { + return match Uuid::parse_str(&id) { + Ok(x) => x, + Err(_) => Uuid::parse_str("00000000000000000000000000000000").unwrap(), + }; +} diff --git a/src/main.rs b/src/main.rs index 176dae1..2c5532a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ mod config; mod db; mod errors; +mod helpers; mod repository; diff --git a/src/repository/routes.rs b/src/repository/routes.rs index 56e42ab..c13af7f 100644 --- a/src/repository/routes.rs +++ b/src/repository/routes.rs @@ -1,5 +1,6 @@ use crate::config::AppState; use crate::errors::AppErrorResponse; +use crate::helpers::uuid_from_string; use crate::repository::models::Repository; use actix_web::{web, HttpResponse, Responder}; use slog::info; @@ -29,10 +30,7 @@ async fn get_repo( // I have to match the &id.0 because if it's not a valid Uuid, the server // must response "Repository not found". // If I pass a not valid Uuid to Repository::find() it raises an error. - let uuid: Uuid = match Uuid::parse_str(&id.0) { - Ok(x) => x, - Err(_) => Uuid::parse_str("00000000000000000000000000000000").unwrap(), - }; + let uuid: Uuid = uuid_from_string(&id.0); let result = Repository::find(state.pool.clone(), &uuid).await; info!(state.log, "GET /repo/{}/", id.0); -- cgit v1.2.3-18-g5258