Skip to content

Commit 278b44a

Browse files
Merge pull request #60 from Reim-developer/dev
Improve `SQLite` & error enum naming
2 parents 6096387 + 42a44f9 commit 278b44a

14 files changed

Lines changed: 308 additions & 120 deletions

File tree

src/back_end/Cargo.lock

Lines changed: 62 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/back_end/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ panic = "abort"
1111

1212
[dependencies]
1313
dirs = "6.0.0"
14+
rusqlite = { version = "0.37.0", features = ["bundled"] }
1415
serde = { version = "1.0.219", features = ["derive"] }
15-
sqlite = "0.37.0"
1616
toml = "0.9.5"
1717
webbrowser = "1.0.5"

src/back_end/src/core/sqlite.rs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
use rusqlite::Connection;
2+
use std::ffi::{CStr, c_char};
3+
4+
#[derive(PartialEq, Eq, Debug)]
5+
#[repr(C)]
6+
#[allow(non_camel_case_types)]
7+
pub enum QueryResult {
8+
OK,
9+
OPEN_DATABASE_ERR,
10+
C_STR_CONVERT_ERR,
11+
EXECUTE_SQL_ERR,
12+
}
13+
14+
const INIT_QUERY: &str = r"--sql
15+
CREATE TABLE IF NOT EXISTS clipboard_cache (
16+
id INTEGER PRIMARY KEY AUTOINCREMENT,
17+
content_hash TEXT NOT NULL UNIQUE,
18+
time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
19+
content TEXT NOT NULL,
20+
content_type TEXT NOT NULL,
21+
is_pinned BOOLEAN NOT NULL DEFAULT FALSE
22+
);
23+
";
24+
25+
#[unsafe(no_mangle)]
26+
/// # Safety
27+
/// Careful with unsafe context & raw pointers.
28+
pub unsafe extern "C" fn raw_init_clipboard_cache(
29+
file_path: *const c_char,
30+
) -> QueryResult {
31+
unsafe {
32+
use QueryResult as R;
33+
34+
let Ok(file_path_cstr) = CStr::from_ptr(file_path).to_str() else {
35+
return R::C_STR_CONVERT_ERR;
36+
};
37+
38+
let Ok(sql) = Connection::open(file_path_cstr) else {
39+
return R::OPEN_DATABASE_ERR;
40+
};
41+
42+
let is_err = sql.execute(INIT_QUERY, ()).is_err();
43+
44+
if is_err {
45+
return R::EXECUTE_SQL_ERR;
46+
}
47+
48+
R::OK
49+
}
50+
}
51+
52+
#[repr(C)]
53+
pub struct TextClipboard {
54+
pub content: *const c_char,
55+
pub content_hash: *const c_char,
56+
pub content_type: *const c_char,
57+
pub is_pinned: bool,
58+
}
59+
60+
/// # Safety
61+
/// Careful with raw pointers & memory leaks.
62+
#[must_use]
63+
#[unsafe(no_mangle)]
64+
pub unsafe extern "C" fn raw_add_text_clipboard(
65+
db_path: *const c_char,
66+
text_clipboard: TextClipboard,
67+
) -> QueryResult {
68+
use QueryResult as R;
69+
70+
unsafe {
71+
let Ok(file_path_cstr) = CStr::from_ptr(db_path).to_str() else {
72+
return R::C_STR_CONVERT_ERR;
73+
};
74+
75+
let Ok(sql) = Connection::open(file_path_cstr) else {
76+
return R::OPEN_DATABASE_ERR;
77+
};
78+
79+
let Ok(content_str) = CStr::from_ptr(text_clipboard.content).to_str()
80+
else {
81+
return R::C_STR_CONVERT_ERR;
82+
};
83+
84+
let Ok(content_hash_str) =
85+
CStr::from_ptr(text_clipboard.content_hash).to_str()
86+
else {
87+
return R::C_STR_CONVERT_ERR;
88+
};
89+
90+
let Ok(content_type_str) =
91+
CStr::from_ptr(text_clipboard.content_type).to_str()
92+
else {
93+
return R::C_STR_CONVERT_ERR;
94+
};
95+
96+
let query = r"
97+
INSERT INTO clipboard_cache (content, content_hash, content_type, is_pinned)
98+
VALUES (?, ?, ?, ?)
99+
";
100+
101+
let binds = (
102+
content_str,
103+
content_hash_str,
104+
content_type_str,
105+
text_clipboard.is_pinned,
106+
);
107+
let is_err = sql.execute(query, binds).is_err();
108+
109+
/* For panic testing only. Don't remove it. */
110+
/* sql.execute(query, binds).unwrap(); */
111+
112+
if is_err {
113+
return R::EXECUTE_SQL_ERR;
114+
}
115+
116+
R::OK
117+
}
118+
}

src/back_end/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ pub mod utils {
44
pub mod browser;
55
pub mod fs_utils;
66
pub mod memory;
7+
}
8+
9+
pub mod core {
710
pub mod sqlite;
811
}
912

src/back_end/src/utils/sqlite.rs

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/back_end/tests/sqlite.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#[test]
2+
fn test_add_text_clipboard() {
3+
use back_end::core::sqlite::{
4+
QueryResult, TextClipboard, raw_add_text_clipboard,
5+
raw_init_clipboard_cache,
6+
};
7+
use std::ffi::CString;
8+
use std::fs;
9+
10+
use QueryResult as R;
11+
use TextClipboard as T;
12+
13+
let content = CString::new("my clipboard content").unwrap();
14+
let content_hash = CString::new("my hash abc xyz ").unwrap();
15+
let content_type = CString::new("text").unwrap();
16+
let db_path = CString::new("test.db").unwrap();
17+
18+
let _ = fs::remove_file(db_path.to_str().unwrap());
19+
20+
let text_clipboard = T {
21+
content: content.as_ptr(),
22+
content_hash: content_hash.as_ptr(),
23+
content_type: content_type.as_ptr(),
24+
is_pinned: false,
25+
};
26+
27+
unsafe {
28+
let init_result = raw_init_clipboard_cache(db_path.as_ptr());
29+
let result = raw_add_text_clipboard(db_path.as_ptr(), text_clipboard);
30+
31+
assert_ne!(init_result, R::C_STR_CONVERT_ERR);
32+
assert_ne!(init_result, R::EXECUTE_SQL_ERR);
33+
assert_ne!(init_result, R::OPEN_DATABASE_ERR);
34+
assert_eq!(init_result, R::OK);
35+
36+
assert_ne!(result, R::C_STR_CONVERT_ERR);
37+
assert_ne!(result, R::EXECUTE_SQL_ERR);
38+
assert_ne!(result, R::OPEN_DATABASE_ERR);
39+
assert_eq!(result, R::OK);
40+
}
41+
}

src/ffi/namespace/include/sqlite.hxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "../../raw/sqlite.hxx"
44

55
namespace Lazyboard::ffi {
6-
inline auto init_clipboard_cache(const char *path) -> InitDatabaseStatus {
6+
inline auto init_clipboard_cache(const char *path) -> QueryResult {
77
auto status = raw_init_clipboard_cache(path);
88

99
return status;

src/ffi/raw/sqlite.hxx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ extern "C" {
88
#include <cstdint>
99
using std::uint8_t;
1010

11-
enum class InitDatabaseStatus : uint8_t {
11+
enum class QueryResult : uint8_t {
1212
OK,
13-
CREATE_DATABASE_FAILED,
14-
C_STR_CONVERT_FAILED,
15-
EXECUTE_SQL_FAILED
13+
OPEN_DATABASE_ERR,
14+
C_STR_CONVERT_ERR,
15+
EXECUTE_SQL_ERR
1616
};
1717

18-
auto raw_init_clipboard_cache(const char *path) -> InitDatabaseStatus;
18+
auto raw_init_clipboard_cache(const char *path) -> QueryResult;
1919

2020
#if defined(__cplusplus)
2121
}

0 commit comments

Comments
 (0)