|
| 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 | +} |
0 commit comments