From 1e43f7cfe8eca1f10cea7e68e112864940f533bb Mon Sep 17 00:00:00 2001 From: holly sparkles Date: Tue, 3 Oct 2023 21:38:31 +0200 Subject: [PATCH] refactor: move database queries to data module --- src/data.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 49 +++++++++++++++---------------------------------- 2 files changed, 64 insertions(+), 34 deletions(-) diff --git a/src/data.rs b/src/data.rs index ef2ae7a..345d07d 100644 --- a/src/data.rs +++ b/src/data.rs @@ -1,6 +1,7 @@ use std::path::Path; use serde::Deserialize; +use tide::log; #[derive(Debug, Deserialize)] pub struct LicenseDatabase { @@ -8,10 +9,58 @@ pub struct LicenseDatabase { } pub trait Queryable { + fn get_licenses(self: &Self, chunk_size: usize) -> Option; + fn get_detailed_licenses(self: &Self) -> Option; fn get_license(self: &Self, id: &str) -> Option; } impl Queryable for LicenseDatabase { + fn get_licenses(self: &Self, chunk_size: usize) -> Option { + let result = self + .licenses + .iter() + .map(|license| license.id.clone()) + .collect::>() + .chunks(chunk_size) + .map(|chunk| chunk.join(", ")) + .collect::>() + .join("\n"); + + if result.is_empty() { + log::warn!("License database not found!"); + None + } + else { + Some(result) + } + } + + fn get_detailed_licenses(self: &Self) -> Option { + const PADDING: usize = 20; + + let result: String = self + .licenses + .iter() + .map(|license| { + format!( + "{:padding$}{}", + license.id, + license.description, + padding = PADDING + ) + }) + .collect::>() + .join("\n"); + + if result.is_empty() { + log::warn!("License database not found!"); + None + } + else { + Some(result) + } + } + fn get_license(self: &Self, id: &str) -> Option { let query: Option = self .licenses diff --git a/src/main.rs b/src/main.rs index 9998824..6fd0f5e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -111,45 +111,26 @@ async fn get_welcome_message(req: tide::Request<()>) -> tide::Result { async fn get_license_list(req: tide::Request<()>) -> tide::Result { log::info!("{:?}", req); - let licenses: String = DATABASE - .licenses - .iter() - .map(|license| license.id.clone()) - .collect::>() - .chunks(5) - .map(|chunk| chunk.join(", ")) - .collect::>() - .join("\n"); - - Ok(build_response( - StatusCode::Ok, - format!("{}", licenses).to_string(), - )) + // TODO: env var this + match DATABASE.get_licenses(5) { + Some(licenses) => Ok(build_response(StatusCode::Ok, format!("{}", licenses))), + None => Ok(build_response( + StatusCode::NotFound, + String::from("License not found"), + )), + } } async fn get_detailed_license_list(req: tide::Request<()>) -> tide::Result { log::info!("{:?}", req); - const PADDING: usize = 20; - - let licenses: String = DATABASE - .licenses - .iter() - .map(|license| { - format!( - "{:padding$}{}", - license.id, - license.description, - padding = PADDING - ) - }) - .collect::>() - .join("\n"); - - Ok(build_response( - StatusCode::Ok, - format!("{}", licenses).to_string(), - )) + match DATABASE.get_detailed_licenses() { + Some(licenses) => Ok(build_response(StatusCode::Ok, format!("{}", licenses))), + None => Ok(build_response( + StatusCode::NotFound, + String::from("No licenses found."), + )), + } } async fn get_requested_license(req: tide::Request<()>) -> tide::Result { -- libgit2 1.7.2