Skip to content

Commit 52b21b7

Browse files
Merge pull request #63 from Reim-developer/dev
Major: Change the entire project as follows: + Previously, it was necessary to wrap multipe layers of abstraction and namespaces to be able to call an FFI function. Now we will do it like C. + The Rust side functions have also had their entire function signatures and enums changed. + Finally, the enum merging scheme has been carefully handled. And in `error_types.hxx`, we will remove the redundant `pair`.
2 parents 363a73c + 3ba2351 commit 52b21b7

38 files changed

Lines changed: 585 additions & 402 deletions

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.h linguist-language=C

src/back_end/Cargo.lock

Lines changed: 135 additions & 0 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ crate-type = ["staticlib", "lib"]
1010
panic = "abort"
1111

1212
[dependencies]
13+
chrono = "0.4.41"
1314
dirs = "6.0.0"
1415
hex = "0.4.3"
1516
rusqlite = { version = "0.37.0", features = ["bundled"] }
Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::raw_config::constant::{
1+
use crate::config::constant::{
22
APP_GUI_SETTINGS, APP_SETTINGS, BACKGROUND_COLOR, BACKGROUND_COLOR_BUTTON,
33
BACKGROUND_COLOR_TABLE_HEADER, FALLBACK_BG_BUTTON_COLOR, FALLBACK_BG_COLOR,
44
FALLBACK_BG_TABLE_HEADER_COLOR, FALLBACK_FG_BUTTON_COLOR,
@@ -13,13 +13,13 @@ use std::{
1313
use toml::Value;
1414

1515
#[repr(C)]
16-
pub struct RawAppSettings {
16+
pub struct AppSettings {
1717
pub hide_when_closed: bool,
1818
pub notification: bool,
1919
}
2020

2121
#[repr(C)]
22-
pub struct RawAppGuiSettings {
22+
pub struct AppGuiSettings {
2323
pub background_color: *mut c_char,
2424
pub foreground_color: *mut c_char,
2525
pub background_button_color: *mut c_char,
@@ -29,13 +29,13 @@ pub struct RawAppGuiSettings {
2929
}
3030

3131
#[repr(C)]
32-
pub struct RawAppConfig {
33-
pub app_settings: RawAppSettings,
34-
pub app_gui_settings: RawAppGuiSettings,
32+
pub struct AppConfig {
33+
pub app_settings: AppSettings,
34+
pub app_gui_settings: AppGuiSettings,
3535
}
3636

3737
#[repr(C)]
38-
pub enum RawReadAppConfigStatus {
38+
pub enum ReadConfigResult {
3939
Ok,
4040
ReadFileFailed,
4141
Utf8Error,
@@ -58,8 +58,8 @@ fn color_by_settings(
5858
color.to_string()
5959
}
6060

61-
fn raw_app_settings(toml_value: &Value) -> RawAppSettings {
62-
RawAppSettings {
61+
fn app_settings(toml_value: &Value) -> AppSettings {
62+
AppSettings {
6363
hide_when_closed: toml_value
6464
.get(APP_SETTINGS)
6565
.and_then(|value| value.get(HIDE_WHEN_CLOSED))
@@ -77,13 +77,13 @@ fn raw_app_settings(toml_value: &Value) -> RawAppSettings {
7777
/// # Safety
7878
/// Careful with raw pointers.
7979
#[unsafe(no_mangle)]
80-
pub unsafe extern "C" fn raw_exists_config(
81-
raw_file_path: *const c_char,
82-
raw_cfg_out: *mut RawAppConfig,
83-
) -> RawReadAppConfigStatus {
84-
use RawReadAppConfigStatus as Status;
80+
pub unsafe extern "C" fn read_exists_config(
81+
file_path: *const c_char,
82+
out: *mut AppConfig,
83+
) -> ReadConfigResult {
84+
use ReadConfigResult as Status;
8585

86-
let file_path_str = unsafe { CStr::from_ptr(raw_file_path).to_str() };
86+
let file_path_str = unsafe { CStr::from_ptr(file_path).to_str() };
8787
let Ok(file_path) = file_path_str else {
8888
return Status::Utf8Error;
8989
};
@@ -96,7 +96,7 @@ pub unsafe extern "C" fn raw_exists_config(
9696
return Status::ParseTomlFailed;
9797
};
9898

99-
let app_settings = raw_app_settings(&toml_value);
99+
let app_settings = app_settings(&toml_value);
100100

101101
let bg_color =
102102
color_by_settings(&toml_value, BACKGROUND_COLOR, FALLBACK_BG_COLOR);
@@ -147,7 +147,7 @@ pub unsafe extern "C" fn raw_exists_config(
147147
return Status::ConvertToCStringFailed;
148148
};
149149

150-
if let Some(config) = unsafe { raw_cfg_out.as_mut() } {
150+
if let Some(config) = unsafe { out.as_mut() } {
151151
config.app_settings.hide_when_closed = app_settings.hide_when_closed;
152152
config.app_settings.notification = app_settings.notification;
153153
config.app_gui_settings.background_color = bg_cstr.into_raw();
@@ -161,16 +161,16 @@ pub unsafe extern "C" fn raw_exists_config(
161161
config.app_gui_settings.foreground_header_table_color =
162162
fg_table_header_cstr.into_raw();
163163

164-
return RawReadAppConfigStatus::Ok;
164+
return ReadConfigResult::Ok;
165165
}
166166

167-
RawReadAppConfigStatus::ConvertToMutFailed
167+
ReadConfigResult::ConvertToMutFailed
168168
}
169169

170170
#[unsafe(no_mangle)]
171171
/// # Safety
172172
/// Be careful with raw pointers & double-free.
173-
pub unsafe extern "C" fn raw_free_cstr_app_config(config: *mut RawAppConfig) {
173+
pub unsafe extern "C" fn free_app_config(config: *mut AppConfig) {
174174
unsafe {
175175
if let Some(cfg) = config.as_mut() {
176176
let _ = CString::from_raw(cfg.app_gui_settings.background_color);

src/back_end/src/core/result_enum.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,11 @@ pub enum AllocResult {
66
NULL_DEFERENCE_ERR,
77
C_STRING_CONVERT_ERR,
88
}
9+
10+
#[derive(PartialEq, Eq, Debug)]
11+
#[repr(C)]
12+
#[allow(non_camel_case_types)]
13+
pub enum ResultContext {
14+
OK,
15+
FAILED,
16+
}

src/back_end/src/core/sqlite.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const INIT_QUERY: &str = r"--sql
2525
#[unsafe(no_mangle)]
2626
/// # Safety
2727
/// Careful with unsafe context & raw pointers.
28-
pub unsafe extern "C" fn raw_init_clipboard_cache(
28+
pub unsafe extern "C" fn init_clipboard_cache(
2929
file_path: *const c_char,
3030
) -> QueryResult {
3131
unsafe {
@@ -61,7 +61,7 @@ pub struct TextClipboard {
6161
/// Careful with raw pointers & memory leaks.
6262
#[must_use]
6363
#[unsafe(no_mangle)]
64-
pub unsafe extern "C" fn raw_add_text_clipboard(
64+
pub unsafe extern "C" fn add_text_clipboard(
6565
db_path: *const c_char,
6666
text_clipboard: TextClipboard,
6767
) -> QueryResult {

src/back_end/src/core/time.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use std::ffi::{CString, c_schar};
2+
3+
use chrono::{DateTime, Utc};
4+
5+
use crate::{core::result_enum::AllocResult, ensure};
6+
7+
/// # Safety
8+
/// Careful with raw pointers and memory leaks.
9+
#[unsafe(no_mangle)]
10+
pub unsafe extern "C" fn time_now(out: *mut *mut c_schar) -> AllocResult {
11+
unsafe {
12+
use AllocResult as R;
13+
14+
ensure!(!out.is_null(), R::NULL_DEFERENCE_ERR);
15+
16+
let now_utc: DateTime<Utc> = Utc::now();
17+
let fmt_utc = now_utc.format("%Y-%m-%d %H:%M:%S").to_string();
18+
19+
let Ok(fmt_utc_cstr) = CString::new(fmt_utc) else {
20+
return R::C_STRING_CONVERT_ERR;
21+
};
22+
23+
*out = fmt_utc_cstr.into_raw();
24+
25+
R::OK
26+
}
27+
}

src/back_end/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ pub mod core {
1111
pub mod result_enum;
1212
pub mod sha;
1313
pub mod sqlite;
14+
pub mod time;
1415
pub mod utf8;
1516
}
1617

1718
pub mod internal {
1819
pub mod app_config;
1920
}
2021

21-
pub mod raw_config {
22+
pub mod config {
2223
pub mod constant;
23-
pub mod raw_toml;
24+
pub mod toml;
2425
}

0 commit comments

Comments
 (0)