summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-03-14 15:39:00 +0100
committerSanto Cariotti <santo@dcariotti.me>2021-03-14 15:39:00 +0100
commitbf6c4bd75168671a5a0e53f7390ebd4796a6bd57 (patch)
tree0e41323722317500f24d9504478b461a71fee453
parent6afd0e59f62889d44b90193f0d8581668eae9c28 (diff)
chore: add helpers module
-rw-r--r--src/helpers.rs9
-rw-r--r--src/main.rs1
-rw-r--r--src/repository/routes.rs6
3 files changed, 12 insertions, 4 deletions
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);