Skip to content

Commit 7243e9f

Browse files
committed
chore: make code more readable using deref
1 parent 26f2b8b commit 7243e9f

File tree

9 files changed

+58
-25
lines changed

9 files changed

+58
-25
lines changed

Diff for: Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rsql"
3-
version = "1.0.0-beta.0"
3+
version = "1.0.0-beta.2"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Diff for: common/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "common"
3-
version = "1.0.0-beta.0"
3+
version = "1.0.0-beta.2"
44
edition = "2021"
55

66
[dependencies]

Diff for: package.json

-9
This file was deleted.

Diff for: src-tauri/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rsql_tauri"
3-
version = "1.0.0-beta.0"
3+
version = "1.0.0-beta.2"
44
description = "PostgreSQL GUI written in Rust"
55
authors = ["Daniel Boros"]
66
license = ""

Diff for: src/sidebar/index.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub fn Sidebar() -> impl IntoView {
4242
</button>
4343
</div>
4444
<For
45-
each=move || projects_store.0.get()
45+
each=move || projects_store.get()
4646
key=|(project, _)| project.clone()
4747
children=|(project_id, project_details)| {
4848
if project_details.contains(Drivers::PGSQL.as_ref()) {
@@ -58,7 +58,7 @@ pub fn Sidebar() -> impl IntoView {
5858
/>
5959

6060
</div>
61-
<Show when=move || !queries_store.0.get().is_empty() fallback=|| view! { <div></div> }>
61+
<Show when=move || !queries_store.get().is_empty() fallback=|| view! { <div></div> }>
6262
<div class="py-2">
6363
<p class="font-semibold text-lg">Saved Queries</p>
6464
<div class="text-sm">

Diff for: src/sidebar/queries.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ use super::query::Query;
77
pub fn Queries() -> impl IntoView {
88
let queries_store = expect_context::<QueriesStore>();
99
let _ = create_resource(
10-
move || queries_store.0.get(),
10+
move || queries_store.get(),
1111
move |_| async move {
1212
queries_store.load_queries().await;
1313
},
1414
);
1515

1616
view! {
1717
<For
18-
each=move || queries_store.0.get()
18+
each=move || queries_store.get()
1919
key=|(query_id, _)| query_id.clone()
2020
children=move |(query_id, sql)| view! { <Query query_id sql/> }
2121
/>

Diff for: src/store/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
use std::collections::BTreeMap;
2+
3+
use leptos::RwSignal;
4+
15
pub mod atoms;
26
pub mod projects;
37
pub mod queries;
48
pub mod tabs;
59

10+
pub type BTreeStore = RwSignal<BTreeMap<String, String>>;
11+

Diff for: src/store/projects.rs

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
1-
use std::collections::BTreeMap;
1+
use std::{
2+
collections::BTreeMap,
3+
ops::{Deref, DerefMut},
4+
};
25

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

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

12+
use super::BTreeStore;
13+
914
#[derive(Clone, Copy, Debug)]
10-
pub struct ProjectsStore(pub RwSignal<BTreeMap<String, String>>);
15+
pub struct ProjectsStore(pub BTreeStore);
1116

1217
impl Default for ProjectsStore {
1318
fn default() -> Self {
1419
Self::new()
1520
}
1621
}
1722

23+
impl Deref for ProjectsStore {
24+
type Target = BTreeStore;
25+
26+
fn deref(&self) -> &Self::Target {
27+
&self.0
28+
}
29+
}
30+
31+
impl DerefMut for ProjectsStore {
32+
fn deref_mut(&mut self) -> &mut Self::Target {
33+
&mut self.0
34+
}
35+
}
36+
1837
impl ProjectsStore {
1938
#[must_use]
2039
pub fn new() -> Self {
2140
Self(RwSignal::default())
2241
}
2342

2443
pub fn select_project_by_name(&self, project_id: &str) -> Option<String> {
25-
self.0.get().get(project_id).cloned()
44+
self.get().get(project_id).cloned()
2645
}
2746

2847
pub fn select_driver_by_project(&self, project_id: Option<&str>) -> Drivers {
@@ -44,7 +63,7 @@ impl ProjectsStore {
4463
let projects = invoke::<_, BTreeMap<String, String>>(Invoke::ProjectDbSelect.as_ref(), &())
4564
.await
4665
.unwrap();
47-
self.0.set(projects);
66+
self.set(projects);
4867
}
4968

5069
pub async fn insert_project(&self, project_id: &str, project_details: &str) {

Diff for: src/store/queries.rs

+22-5
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,50 @@
1-
use std::collections::BTreeMap;
1+
use std::{
2+
collections::BTreeMap,
3+
ops::{Deref, DerefMut},
4+
};
25

36
use common::enums::Drivers;
47
use leptos::*;
58
use tauri_sys::tauri::invoke;
69

710
use crate::invoke::{Invoke, InvokeQueryDbDeleteArgs, InvokeQueryDbInsertArgs};
811

9-
use super::tabs::TabsStore;
12+
use super::{tabs::TabsStore, BTreeStore};
1013

1114
#[derive(Clone, Copy, Debug)]
12-
pub struct QueriesStore(pub RwSignal<BTreeMap<String, String>>);
15+
pub struct QueriesStore(pub BTreeStore);
1316

1417
impl Default for QueriesStore {
1518
fn default() -> Self {
1619
Self::new()
1720
}
1821
}
1922

23+
impl Deref for QueriesStore {
24+
type Target = BTreeStore;
25+
26+
fn deref(&self) -> &Self::Target {
27+
&self.0
28+
}
29+
}
30+
31+
impl DerefMut for QueriesStore {
32+
fn deref_mut(&mut self) -> &mut Self::Target {
33+
&mut self.0
34+
}
35+
}
36+
2037
impl QueriesStore {
2138
#[must_use]
2239
pub fn new() -> Self {
23-
Self(create_rw_signal(BTreeMap::new()))
40+
Self(RwSignal::default())
2441
}
2542

2643
pub async fn load_queries(&self) {
2744
let saved_queries = invoke::<_, BTreeMap<String, String>>(Invoke::QueryDbSelect.as_ref(), &())
2845
.await
2946
.unwrap();
30-
self.0.update(|prev| {
47+
self.update(|prev| {
3148
*prev = saved_queries.into_iter().collect();
3249
});
3350
}

0 commit comments

Comments
 (0)