Skip to content

Commit ac4e7d1

Browse files
committed
refactor: simplify close behavior io helpers
1 parent 99a4845 commit ac4e7d1

1 file changed

Lines changed: 22 additions & 15 deletions

File tree

src-tauri/src/close_behavior.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use std::{fs, io::Write, path::Path};
55
pub(crate) const CLOSE_ACTION_TRAY: &str = "tray";
66
pub(crate) const CLOSE_ACTION_EXIT: &str = "exit";
77

8-
type Logger<'a> = dyn Fn(&str) + 'a;
9-
108
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
119
#[serde(rename_all = "lowercase")]
1210
pub(crate) enum CloseAction {
@@ -54,7 +52,10 @@ pub(crate) fn parse_close_action(raw: &str) -> Option<CloseAction> {
5452
}
5553
}
5654

57-
fn load_desktop_state(raw: &str, log_subject: &str, log: &Logger<'_>) -> DesktopState {
55+
fn load_desktop_state<F>(raw: &str, log_subject: &str, log: &F) -> DesktopState
56+
where
57+
F: Fn(&str),
58+
{
5859
match serde_json::from_str::<DesktopState>(raw) {
5960
Ok(state) => state,
6061
Err(error) => {
@@ -77,25 +78,28 @@ where
7778
read_cached_close_action_at_path(&state_path, &log)
7879
}
7980

80-
fn read_cached_close_action_at_path(state_path: &Path, log: &Logger<'_>) -> Option<CloseAction> {
81+
fn read_cached_close_action_at_path<F>(state_path: &Path, log: &F) -> Option<CloseAction>
82+
where
83+
F: Fn(&str),
84+
{
8185
let raw = fs::read_to_string(state_path).ok()?;
8286
let state = load_desktop_state(&raw, "desktop close behavior state", log);
8387
state.close_action
8488
}
8589

86-
fn atomic_write_json(path: &Path, value: &impl Serialize, what: &str) -> Result<(), String> {
90+
fn atomic_write_desktop_state(path: &Path, state: &DesktopState) -> Result<(), String> {
8791
if let Some(parent_dir) = path.parent() {
8892
fs::create_dir_all(parent_dir).map_err(|error| {
8993
format!(
90-
"Failed to create {what} directory {}: {}",
94+
"Failed to create desktop state directory {}: {}",
9195
parent_dir.display(),
9296
error
9397
)
9498
})?;
9599
}
96100

97-
let serialized = serde_json::to_string_pretty(value)
98-
.map_err(|error| format!("Failed to serialize {what}: {error}"))?;
101+
let serialized = serde_json::to_string_pretty(state)
102+
.map_err(|error| format!("Failed to serialize desktop state: {error}"))?;
99103
let tmp_name = format!(
100104
"{}.tmp",
101105
path.file_name()
@@ -106,7 +110,7 @@ fn atomic_write_json(path: &Path, value: &impl Serialize, what: &str) -> Result<
106110

107111
let mut file = fs::File::create(&tmp_path).map_err(|error| {
108112
format!(
109-
"Failed to create temporary {what} file {}: {}",
113+
"Failed to create temporary desktop state file {}: {}",
110114
tmp_path.display(),
111115
error
112116
)
@@ -115,22 +119,22 @@ fn atomic_write_json(path: &Path, value: &impl Serialize, what: &str) -> Result<
115119
.and_then(|_| file.sync_all())
116120
.map_err(|error| {
117121
format!(
118-
"Failed to write temporary {what} file {}: {}",
122+
"Failed to write temporary desktop state file {}: {}",
119123
tmp_path.display(),
120124
error
121125
)
122126
})?;
123127
fs::rename(&tmp_path, path).map_err(|error| {
124128
format!(
125-
"Failed to atomically replace {what} file {}: {}",
129+
"Failed to atomically replace desktop state file {}: {}",
126130
path.display(),
127131
error
128132
)
129133
})
130134
}
131135

132136
fn save_desktop_state(path: &Path, state: &DesktopState) -> Result<(), String> {
133-
atomic_write_json(path, state, "close behavior state")
137+
atomic_write_desktop_state(path, state)
134138
}
135139

136140
pub(crate) fn write_cached_close_action<F>(
@@ -150,11 +154,14 @@ where
150154
write_cached_close_action_at_path(action, &state_path, &log)
151155
}
152156

153-
fn write_cached_close_action_at_path(
157+
fn write_cached_close_action_at_path<F>(
154158
action: Option<CloseAction>,
155159
state_path: &Path,
156-
log: &Logger<'_>,
157-
) -> Result<(), String> {
160+
log: &F,
161+
) -> Result<(), String>
162+
where
163+
F: Fn(&str),
164+
{
158165
let mut state = match fs::read_to_string(state_path) {
159166
Ok(raw) => load_desktop_state(
160167
&raw,

0 commit comments

Comments
 (0)