From c10105ad750853fc41cdc967a88625566acfa961 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Tue, 25 Feb 2025 09:10:48 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/asset.rs | 17 ++++---- src/lib.rs | 84 ++++++++++++++++++++++++++++++++++++++ src/request/api.rs | 2 + src/ui/components/modal.rs | 45 ++++++++++---------- src/ui/components/shell.rs | 11 +++-- src/ui/primitives/grid.rs | 34 ++++++++------- 6 files changed, 146 insertions(+), 47 deletions(-) diff --git a/src/asset.rs b/src/asset.rs index 0239eda..2a929f9 100644 --- a/src/asset.rs +++ b/src/asset.rs @@ -52,12 +52,15 @@ pub trait AssetRoutes { impl AssetRoutes for rocket::Rocket { fn mount_assets(self) -> Self { - self.mount("/", routes![ - crate::asset::htmx_script_route, - crate::asset::flowbite_css, - crate::asset::flowbite_js, - crate::asset::material_css, - crate::asset::material_font - ]) + self.mount( + "/", + routes![ + crate::asset::htmx_script_route, + crate::asset::flowbite_css, + crate::asset::flowbite_js, + crate::asset::material_css, + crate::asset::material_font + ], + ) } } diff --git a/src/lib.rs b/src/lib.rs index 6255764..659dfcc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -81,6 +81,13 @@ fn transpose(matrix: Vec>) -> Vec> { pub enum DatabaseType { TEXT(String), INTEGER(i32), + BIGINT(i64), + REAL(f32), + DOUBLE(f64), + BOOLEAN(bool), + BYTEA(Vec), + UUID(uuid::Uuid), + JSON(serde_json::Value), } impl DatabaseType { @@ -88,6 +95,62 @@ impl DatabaseType { match self { Self::TEXT(_) => "TEXT", Self::INTEGER(_) => "INTEGER", + Self::BIGINT(_) => "BIGINT", + Self::REAL(_) => "REAL", + Self::DOUBLE(_) => "DOUBLE PRECISION", + Self::BOOLEAN(_) => "BOOLEAN", + Self::BYTEA(_) => "BYTEA", + Self::UUID(_) => "UUID", + Self::JSON(_) => "JSONB", + } + } + + pub fn as_json(self) -> serde_json::Value { + match self { + DatabaseType::JSON(result) => return result, + _ => panic!["Wrong DB type"], + } + } + + pub fn as_uuid(self) -> uuid::Uuid { + match self { + DatabaseType::UUID(result) => return result, + _ => panic!["Wrong DB type"], + } + } + + pub fn as_vec(self) -> Vec { + match self { + DatabaseType::BYTEA(result) => return result, + _ => panic!["Wrong DB type"], + } + } + + pub fn as_bool(self) -> bool { + match self { + DatabaseType::BOOLEAN(result) => return result, + _ => panic!["Wrong DB type"], + } + } + + pub fn as_f64(self) -> f64 { + match self { + DatabaseType::DOUBLE(result) => return result, + _ => panic!["Wrong DB type"], + } + } + + pub fn as_f32(self) -> f32 { + match self { + DatabaseType::REAL(result) => return result, + _ => panic!["Wrong DB type"], + } + } + + pub fn as_big_int(self) -> i64 { + match self { + DatabaseType::BIGINT(result) => return result, + _ => panic!["Wrong DB type"], } } @@ -141,6 +204,27 @@ pub async fn batch_insert(table: &str, values: &[String], entries: Vec { query = query.bind(e.into_iter().map(|x| x.as_integer()).collect::>()); } + DatabaseType::BIGINT(_) => { + query = query.bind(e.into_iter().map(|x| x.as_big_int()).collect::>()); + } + DatabaseType::REAL(_) => { + query = query.bind(e.into_iter().map(|x| x.as_f32()).collect::>()); + } + DatabaseType::DOUBLE(_) => { + query = query.bind(e.into_iter().map(|x| x.as_f64()).collect::>()); + } + DatabaseType::BOOLEAN(_) => { + query = query.bind(e.into_iter().map(|x| x.as_bool()).collect::>()); + } + DatabaseType::BYTEA(_) => { + query = query.bind(e.into_iter().map(|x| x.as_vec()).collect::>()); + } + DatabaseType::UUID(_) => { + query = query.bind(e.into_iter().map(|x| x.as_uuid()).collect::>()); + } + DatabaseType::JSON(_) => { + query = query.bind(e.into_iter().map(|x| x.as_json()).collect::>()); + } } } diff --git a/src/request/api.rs b/src/request/api.rs index 4ecee27..4f28036 100644 --- a/src/request/api.rs +++ b/src/request/api.rs @@ -41,6 +41,8 @@ pub async fn vec_to_api(items: &[impl ToAPI]) -> Vec { ret } +// TODO : more compact uuids -> base representation (example: yt video ids) + /// Converts a string into a `Uuid`. /// /// This function attempts to parse a string as a UUID. If the parsing fails, diff --git a/src/ui/components/modal.rs b/src/ui/components/modal.rs index a248df5..e062afd 100644 --- a/src/ui/components/modal.rs +++ b/src/ui/components/modal.rs @@ -29,29 +29,32 @@ pub fn Modal ) -> (String, PreEscaped) { let id = uuid::Uuid::new_v4().to_string(); - (format!("modal-{id}"), html! { - div id=(format!("modal-{id}")) tabindex="-1" aria-hidden="true" class="hidden overflow-y-auto overflow-x-hidden fixed top-0 right-0 left-0 z-50 justify-center items-center w-full md:inset-0 h-[calc(100%-1rem)] max-h-full" { - div class="relative p-4 w-full max-w-2xl max-h-full" { + ( + format!("modal-{id}"), + html! { + div id=(format!("modal-{id}")) tabindex="-1" aria-hidden="true" class="hidden overflow-y-auto overflow-x-hidden fixed top-0 right-0 left-0 z-50 justify-center items-center w-full md:inset-0 h-[calc(100%-1rem)] max-h-full" { + div class="relative p-4 w-full max-w-2xl max-h-full" { - div class="relative bg-white rounded-lg shadow dark:bg-gray-700" { - div class="flex items-center justify-between p-4 md:p-5 border-b rounded-t dark:border-gray-600" { - h3 class="text-xl font-semibold text-gray-900 dark:text-white" { (title) } - button type="button" class="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm w-8 h-8 ms-auto inline-flex justify-center items-center dark:hover:bg-gray-600 dark:hover:text-white" data-modal-hide=(format!("modal-{id}")) { - svg class="w-3 h-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 14 14" { - path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m1 1 6 6m0 0 6 6M7 7l6-6M7 7l-6 6" {}; - }; - span class="sr-only" { "Close modal" }; - } - }; + div class="relative bg-white rounded-lg shadow dark:bg-gray-700" { + div class="flex items-center justify-between p-4 md:p-5 border-b rounded-t dark:border-gray-600" { + h3 class="text-xl font-semibold text-gray-900 dark:text-white" { (title) } + button type="button" class="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm w-8 h-8 ms-auto inline-flex justify-center items-center dark:hover:bg-gray-600 dark:hover:text-white" data-modal-hide=(format!("modal-{id}")) { + svg class="w-3 h-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 14 14" { + path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m1 1 6 6m0 0 6 6M7 7l6-6M7 7l-6 6" {}; + }; + span class="sr-only" { "Close modal" }; + } + }; - div class="p-4 md:p-5 space-y-4" { - (body) - }; + div class="p-4 md:p-5 space-y-4" { + (body) + }; - div class="flex items-center p-4 md:p-5 border-t border-gray-200 rounded-b dark:border-gray-600" { - (footer(format!("modal-{id}"))) + div class="flex items-center p-4 md:p-5 border-t border-gray-200 rounded-b dark:border-gray-600" { + (footer(format!("modal-{id}"))) + }; }; - }; - }}; - }) + }}; + }, + ) } diff --git a/src/ui/components/shell.rs b/src/ui/components/shell.rs index aac9ab7..df00f44 100644 --- a/src/ui/components/shell.rs +++ b/src/ui/components/shell.rs @@ -287,10 +287,13 @@ pub fn BottomNavigationTile( ) -> ClassicWidget { Classic( "inline-flex flex-col items-center justify-center px-5 hover:bg-gray-50 dark:hover:bg-gray-800 group", - Link(reference, html! { - (icon.map(|x| x.render()).unwrap_or_default()); - span class="text-sm text-gray-500 dark:text-gray-400 group-hover:text-blue-600 dark:group-hover:text-blue-500" { (text) }; - }), + Link( + reference, + html! { + (icon.map(|x| x.render()).unwrap_or_default()); + span class="text-sm text-gray-500 dark:text-gray-400 group-hover:text-blue-600 dark:group-hover:text-blue-500" { (text) }; + }, + ), ) } diff --git a/src/ui/primitives/grid.rs b/src/ui/primitives/grid.rs index cd1e345..31d46c1 100644 --- a/src/ui/primitives/grid.rs +++ b/src/ui/primitives/grid.rs @@ -274,21 +274,25 @@ impl GridElement { } pub fn span(mut self, value: GridElementValue) -> Self { - self.1.push(format!("{}-span-{}", self.2, match value { - GridElementValue::_1 => "1", - GridElementValue::_2 => "2", - GridElementValue::_3 => "3", - GridElementValue::_4 => "4", - GridElementValue::_5 => "5", - GridElementValue::_6 => "6", - GridElementValue::_7 => "7", - GridElementValue::_8 => "8", - GridElementValue::_9 => "9", - GridElementValue::_10 => "10", - GridElementValue::_11 => "11", - GridElementValue::_12 => "12", - GridElementValue::Auto => "full", - })); + self.1.push(format!( + "{}-span-{}", + self.2, + match value { + GridElementValue::_1 => "1", + GridElementValue::_2 => "2", + GridElementValue::_3 => "3", + GridElementValue::_4 => "4", + GridElementValue::_5 => "5", + GridElementValue::_6 => "6", + GridElementValue::_7 => "7", + GridElementValue::_8 => "8", + GridElementValue::_9 => "9", + GridElementValue::_10 => "10", + GridElementValue::_11 => "11", + GridElementValue::_12 => "12", + GridElementValue::Auto => "full", + } + )); self }