index : license-api-rs.git

ascending towards madness

author holly sparkles <sparkles@holly.sh> 2023-10-03 19:38:31.0 +00:00:00
committer holly sparkles <sparkles@holly.sh> 2023-10-03 19:38:31.0 +00:00:00
commit
1e43f7cfe8eca1f10cea7e68e112864940f533bb [patch]
tree
57968b3b3c9f95e2446c017563b352a509ebe080
parent
9fd75dae1cbf7d3803991dafdc412301fae62d3a
download
1e43f7cfe8eca1f10cea7e68e112864940f533bb.tar.gz

refactor: move database queries to data module



Diff

 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<String>;
	fn get_detailed_licenses(self: &Self) -> Option<String>;
	fn get_license(self: &Self, id: &str) -> Option<License>;
}

impl Queryable for LicenseDatabase {
	fn get_licenses(self: &Self, chunk_size: usize) -> Option<String> {
		let result = self
			.licenses
			.iter()
			.map(|license| license.id.clone())
			.collect::<Vec<String>>()
			.chunks(chunk_size)
			.map(|chunk| chunk.join(", "))
			.collect::<Vec<String>>()
			.join("\n");
		
		if result.is_empty() {
			log::warn!("License database not found!");
			None
		}
		else {
			Some(result)
		}
	}
	
	fn get_detailed_licenses(self: &Self) -> Option<String> {
		const PADDING: usize = 20;

		let result: String = self
			.licenses
			.iter()
			.map(|license| {
				format!(
					"{:padding$}{}",
					license.id,
					license.description,
					padding = PADDING
				)
			})
			.collect::<Vec<String>>()
			.join("\n");

		if result.is_empty() {
			log::warn!("License database not found!");
			None
		}
		else {
			Some(result)
		}
	}

	fn get_license(self: &Self, id: &str) -> Option<License> {
		let query: Option<License> = 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<Response> {
async fn get_license_list(req: tide::Request<()>) -> tide::Result<Response> {
    log::info!("{:?}", req);

    let licenses: String = DATABASE
        .licenses
        .iter()
        .map(|license| license.id.clone())
        .collect::<Vec<String>>()
        .chunks(5)
        .map(|chunk| chunk.join(", "))
        .collect::<Vec<String>>()
        .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<Response> {
    log::info!("{:?}", req);

    const PADDING: usize = 20;

    let licenses: String = DATABASE
        .licenses
        .iter()
        .map(|license| {
            format!(
                "{:padding$}{}",
                license.id,
                license.description,
                padding = PADDING
            )
        })
        .collect::<Vec<String>>()
        .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<Response> {