From 7243e9fb10b554857b73eebee6e332aeeb79474a Mon Sep 17 00:00:00 2001 From: Daniel Boros Date: Mon, 20 May 2024 19:21:01 +0200 Subject: [PATCH] chore: make code more readable using deref --- Cargo.toml | 2 +- common/Cargo.toml | 2 +- package.json | 9 --------- src-tauri/Cargo.toml | 2 +- src/sidebar/index.rs | 4 ++-- src/sidebar/queries.rs | 4 ++-- src/store/mod.rs | 6 ++++++ src/store/projects.rs | 27 +++++++++++++++++++++++---- src/store/queries.rs | 27 ++++++++++++++++++++++----- 9 files changed, 58 insertions(+), 25 deletions(-) delete mode 100644 package.json diff --git a/Cargo.toml b/Cargo.toml index 4a40f20..4d97104 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rsql" -version = "1.0.0-beta.0" +version = "1.0.0-beta.2" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/common/Cargo.toml b/common/Cargo.toml index 22b4532..14b774a 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "common" -version = "1.0.0-beta.0" +version = "1.0.0-beta.2" edition = "2021" [dependencies] diff --git a/package.json b/package.json deleted file mode 100644 index de1b848..0000000 --- a/package.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "devDependencies": { - "tailwindcss": "^3.3.5" - }, - "dependencies": { - "monaco-editor": "^0.45.0", - "monaco-sql-languages": "^0.12.0-beta.10" - } -} diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index a570c3b..2addd52 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rsql_tauri" -version = "1.0.0-beta.0" +version = "1.0.0-beta.2" description = "PostgreSQL GUI written in Rust" authors = ["Daniel Boros"] license = "" diff --git a/src/sidebar/index.rs b/src/sidebar/index.rs index 09a0425..731a183 100644 --- a/src/sidebar/index.rs +++ b/src/sidebar/index.rs @@ -42,7 +42,7 @@ pub fn Sidebar() -> impl IntoView { impl IntoView { /> - }> + }>

Saved Queries

diff --git a/src/sidebar/queries.rs b/src/sidebar/queries.rs index b06d070..32e1e80 100644 --- a/src/sidebar/queries.rs +++ b/src/sidebar/queries.rs @@ -7,7 +7,7 @@ use super::query::Query; pub fn Queries() -> impl IntoView { let queries_store = expect_context::(); let _ = create_resource( - move || queries_store.0.get(), + move || queries_store.get(), move |_| async move { queries_store.load_queries().await; }, @@ -15,7 +15,7 @@ pub fn Queries() -> impl IntoView { view! { } /> diff --git a/src/store/mod.rs b/src/store/mod.rs index 1ee5f61..8fba340 100644 --- a/src/store/mod.rs +++ b/src/store/mod.rs @@ -1,5 +1,11 @@ +use std::collections::BTreeMap; + +use leptos::RwSignal; + pub mod atoms; pub mod projects; pub mod queries; pub mod tabs; +pub type BTreeStore = RwSignal>; + diff --git a/src/store/projects.rs b/src/store/projects.rs index 486090a..f45cfe0 100644 --- a/src/store/projects.rs +++ b/src/store/projects.rs @@ -1,4 +1,7 @@ -use std::collections::BTreeMap; +use std::{ + collections::BTreeMap, + ops::{Deref, DerefMut}, +}; use common::enums::Drivers; use leptos::{RwSignal, SignalGet, SignalSet}; @@ -6,8 +9,10 @@ use tauri_sys::tauri::invoke; use crate::invoke::{Invoke, InvokeProjectDbDeleteArgs, InvokeProjectDbInsertArgs}; +use super::BTreeStore; + #[derive(Clone, Copy, Debug)] -pub struct ProjectsStore(pub RwSignal>); +pub struct ProjectsStore(pub BTreeStore); impl Default for ProjectsStore { fn default() -> Self { @@ -15,6 +20,20 @@ impl Default for ProjectsStore { } } +impl Deref for ProjectsStore { + type Target = BTreeStore; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for ProjectsStore { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + impl ProjectsStore { #[must_use] pub fn new() -> Self { @@ -22,7 +41,7 @@ impl ProjectsStore { } pub fn select_project_by_name(&self, project_id: &str) -> Option { - self.0.get().get(project_id).cloned() + self.get().get(project_id).cloned() } pub fn select_driver_by_project(&self, project_id: Option<&str>) -> Drivers { @@ -44,7 +63,7 @@ impl ProjectsStore { let projects = invoke::<_, BTreeMap>(Invoke::ProjectDbSelect.as_ref(), &()) .await .unwrap(); - self.0.set(projects); + self.set(projects); } pub async fn insert_project(&self, project_id: &str, project_details: &str) { diff --git a/src/store/queries.rs b/src/store/queries.rs index 5a5229a..f4f469e 100644 --- a/src/store/queries.rs +++ b/src/store/queries.rs @@ -1,4 +1,7 @@ -use std::collections::BTreeMap; +use std::{ + collections::BTreeMap, + ops::{Deref, DerefMut}, +}; use common::enums::Drivers; use leptos::*; @@ -6,10 +9,10 @@ use tauri_sys::tauri::invoke; use crate::invoke::{Invoke, InvokeQueryDbDeleteArgs, InvokeQueryDbInsertArgs}; -use super::tabs::TabsStore; +use super::{tabs::TabsStore, BTreeStore}; #[derive(Clone, Copy, Debug)] -pub struct QueriesStore(pub RwSignal>); +pub struct QueriesStore(pub BTreeStore); impl Default for QueriesStore { fn default() -> Self { @@ -17,17 +20,31 @@ impl Default for QueriesStore { } } +impl Deref for QueriesStore { + type Target = BTreeStore; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for QueriesStore { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + impl QueriesStore { #[must_use] pub fn new() -> Self { - Self(create_rw_signal(BTreeMap::new())) + Self(RwSignal::default()) } pub async fn load_queries(&self) { let saved_queries = invoke::<_, BTreeMap>(Invoke::QueryDbSelect.as_ref(), &()) .await .unwrap(); - self.0.update(|prev| { + self.update(|prev| { *prev = saved_queries.into_iter().collect(); }); }