Skip to content

Commit

Permalink
chore: make code more readable using deref
Browse files Browse the repository at this point in the history
  • Loading branch information
dancixx committed May 20, 2024
1 parent 26f2b8b commit 7243e9f
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "common"
version = "1.0.0-beta.0"
version = "1.0.0-beta.2"
edition = "2021"

[dependencies]
Expand Down
9 changes: 0 additions & 9 deletions package.json

This file was deleted.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 = ""
Expand Down
4 changes: 2 additions & 2 deletions src/sidebar/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn Sidebar() -> impl IntoView {
</button>
</div>
<For
each=move || projects_store.0.get()
each=move || projects_store.get()
key=|(project, _)| project.clone()
children=|(project_id, project_details)| {
if project_details.contains(Drivers::PGSQL.as_ref()) {
Expand All @@ -58,7 +58,7 @@ pub fn Sidebar() -> impl IntoView {
/>

</div>
<Show when=move || !queries_store.0.get().is_empty() fallback=|| view! { <div></div> }>
<Show when=move || !queries_store.get().is_empty() fallback=|| view! { <div></div> }>
<div class="py-2">
<p class="font-semibold text-lg">Saved Queries</p>
<div class="text-sm">
Expand Down
4 changes: 2 additions & 2 deletions src/sidebar/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ use super::query::Query;
pub fn Queries() -> impl IntoView {
let queries_store = expect_context::<QueriesStore>();
let _ = create_resource(
move || queries_store.0.get(),
move || queries_store.get(),
move |_| async move {
queries_store.load_queries().await;
},
);

view! {
<For
each=move || queries_store.0.get()
each=move || queries_store.get()
key=|(query_id, _)| query_id.clone()
children=move |(query_id, sql)| view! { <Query query_id sql/> }
/>
Expand Down
6 changes: 6 additions & 0 deletions src/store/mod.rs
Original file line number Diff line number Diff line change
@@ -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<BTreeMap<String, String>>;

27 changes: 23 additions & 4 deletions src/store/projects.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
use std::collections::BTreeMap;
use std::{
collections::BTreeMap,
ops::{Deref, DerefMut},
};

use common::enums::Drivers;
use leptos::{RwSignal, SignalGet, SignalSet};
use tauri_sys::tauri::invoke;

use crate::invoke::{Invoke, InvokeProjectDbDeleteArgs, InvokeProjectDbInsertArgs};

use super::BTreeStore;

#[derive(Clone, Copy, Debug)]
pub struct ProjectsStore(pub RwSignal<BTreeMap<String, String>>);
pub struct ProjectsStore(pub BTreeStore);

impl Default for ProjectsStore {
fn default() -> Self {
Self::new()
}
}

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 {
Self(RwSignal::default())
}

pub fn select_project_by_name(&self, project_id: &str) -> Option<String> {
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 {
Expand All @@ -44,7 +63,7 @@ impl ProjectsStore {
let projects = invoke::<_, BTreeMap<String, String>>(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) {
Expand Down
27 changes: 22 additions & 5 deletions src/store/queries.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,50 @@
use std::collections::BTreeMap;
use std::{
collections::BTreeMap,
ops::{Deref, DerefMut},
};

use common::enums::Drivers;
use leptos::*;
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<BTreeMap<String, String>>);
pub struct QueriesStore(pub BTreeStore);

impl Default for QueriesStore {
fn default() -> Self {
Self::new()
}
}

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<String, String>>(Invoke::QueryDbSelect.as_ref(), &())
.await
.unwrap();
self.0.update(|prev| {
self.update(|prev| {
*prev = saved_queries.into_iter().collect();
});
}
Expand Down

0 comments on commit 7243e9f

Please sign in to comment.