Skip to content

Commit f239b26

Browse files
committed
refactor: reduce startup duplication and trim release builds
1 parent 833fa8c commit f239b26

3 files changed

Lines changed: 96 additions & 92 deletions

File tree

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,8 @@ winresource = "0.1.31"
3030

3131
[dev-dependencies]
3232
tempfile = "3.27.0"
33+
34+
[profile.release]
35+
codegen-units = 1
36+
lto = "thin"
37+
strip = "symbols"

src/app.rs

Lines changed: 67 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,33 @@ impl App {
172172
self.update_ui();
173173
}
174174

175+
fn reset_watchers(&mut self) {
176+
self.watcher = None;
177+
self.watcher_key = None;
178+
}
179+
180+
fn reset_pending_syncs(&mut self) {
181+
self.pending_pull_sync = false;
182+
self.pending_push_sync = false;
183+
}
184+
185+
fn reset_mount_state(&mut self) {
186+
self.has_auto_synced_this_mount = false;
187+
self.reset_pending_syncs();
188+
self.reset_watchers();
189+
}
190+
191+
fn run_open_action<F>(&mut self, failure_prefix: &str, action: F)
192+
where
193+
F: FnOnce() -> Result<()>,
194+
{
195+
if let Err(error) = action() {
196+
self.last_status = format!("{failure_prefix}: {error}");
197+
append_log(&self.paths, &self.last_status);
198+
self.update_ui();
199+
}
200+
}
201+
175202
fn reload_config(&mut self, force: bool) {
176203
let current_stamp = config_modified(&self.paths);
177204
if !force && current_stamp == self.config_stamp {
@@ -198,8 +225,7 @@ impl App {
198225
self.config = None;
199226
self.config_error = Some(message.clone());
200227
self.last_status = format!("Config error: {message}");
201-
self.watcher = None;
202-
self.watcher_key = None;
228+
self.reset_watchers();
203229
self.maybe_open_wizard_for_config_error(current_stamp, &message);
204230
}
205231
}
@@ -232,42 +258,38 @@ impl App {
232258
}
233259

234260
fn update_drive_presence(&mut self) {
235-
let Some(config) = self.config.as_ref() else {
261+
let Some((is_present, drive_label, clear_shadow_on_eject)) = self.config.as_ref().map(|config| {
262+
(
263+
platform::drive_present(&config.drive_root),
264+
config.drive_label.clone(),
265+
config.cache.shadow_copy && config.cache.clear_shadow_on_eject,
266+
)
267+
}) else {
236268
self.drive_present = false;
237-
self.has_auto_synced_this_mount = false;
238-
self.pending_pull_sync = false;
239-
self.pending_push_sync = false;
240-
self.watcher = None;
241-
self.watcher_key = None;
269+
self.reset_mount_state();
242270
return;
243271
};
244272

245273
let was_present = self.drive_present;
246-
let is_present = platform::drive_present(&config.drive_root);
247274
self.drive_present = is_present;
248275

249276
if is_present && !was_present {
250-
self.has_auto_synced_this_mount = false;
251-
self.pending_pull_sync = false;
252-
self.pending_push_sync = false;
277+
self.reset_mount_state();
253278
if !self.syncing {
254-
self.last_status = format!("Drive {} detected", config.drive_label);
279+
self.last_status = format!("Drive {drive_label} detected");
255280
}
256281
}
257282

258283
if !is_present && was_present {
259-
if config.cache.shadow_copy && config.cache.clear_shadow_on_eject {
260-
if let Err(error) = clear_shadow_cache(config) {
284+
if clear_shadow_on_eject
285+
&& let Some(config) = self.config.as_ref()
286+
&& let Err(error) = clear_shadow_cache(config)
287+
{
261288
append_log(&self.paths, format!("Cache cleanup error: {error}"));
262-
}
263289
}
264-
self.has_auto_synced_this_mount = false;
265-
self.pending_pull_sync = false;
266-
self.pending_push_sync = false;
267-
self.watcher = None;
268-
self.watcher_key = None;
290+
self.reset_mount_state();
269291
if !self.syncing {
270-
self.last_status = format!("Drive {} removed", config.drive_label);
292+
self.last_status = format!("Drive {drive_label} removed");
271293
}
272294
}
273295
}
@@ -372,8 +394,7 @@ impl App {
372394

373395
fn maybe_run_pending_syncs(&mut self) -> bool {
374396
let Some(config) = self.config.as_ref() else {
375-
self.pending_pull_sync = false;
376-
self.pending_push_sync = false;
397+
self.reset_pending_syncs();
377398
return false;
378399
};
379400
if self.syncing || !self.drive_present {
@@ -395,28 +416,24 @@ impl App {
395416

396417
fn refresh_watchers(&mut self) {
397418
if self.syncing {
398-
self.watcher = None;
399-
self.watcher_key = None;
419+
self.reset_watchers();
400420
return;
401421
}
402422

403423
let Some(config) = self.config.as_ref() else {
404-
self.watcher = None;
405-
self.watcher_key = None;
424+
self.reset_watchers();
406425
return;
407426
};
408427

409428
if !self.drive_present {
410-
self.watcher = None;
411-
self.watcher_key = None;
429+
self.reset_watchers();
412430
return;
413431
}
414432

415433
let watch_usb = config.app.sync_while_mounted;
416434
let watch_local = config.app.auto_sync_to_usb;
417435
if !watch_usb && !watch_local {
418-
self.watcher = None;
419-
self.watcher_key = None;
436+
self.reset_watchers();
420437
return;
421438
}
422439

@@ -442,8 +459,7 @@ impl App {
442459
}
443460
}
444461
Err(error) => {
445-
self.watcher = None;
446-
self.watcher_key = None;
462+
self.reset_watchers();
447463
let message = format!("Watch setup failed: {error}");
448464
append_log(&self.paths, &message);
449465
if !self.syncing {
@@ -525,11 +541,8 @@ impl App {
525541
}
526542

527543
fn open_config(&mut self) {
528-
if let Err(error) = platform::open_path(&self.paths.config_file) {
529-
self.last_status = format!("Open config failed: {error}");
530-
append_log(&self.paths, &self.last_status);
531-
self.update_ui();
532-
}
544+
let path = self.paths.config_file.clone();
545+
self.run_open_action("Open config failed", move || platform::open_path(&path));
533546
}
534547

535548
fn open_drive_root(&mut self) {
@@ -539,11 +552,10 @@ impl App {
539552
return;
540553
};
541554

542-
if let Err(error) = platform::open_in_file_manager(&config.drive_root) {
543-
self.last_status = format!("Open drive failed: {error}");
544-
append_log(&self.paths, &self.last_status);
545-
self.update_ui();
546-
}
555+
let drive_root = config.drive_root.clone();
556+
self.run_open_action("Open drive failed", move || {
557+
platform::open_in_file_manager(&drive_root)
558+
});
547559
}
548560

549561
fn open_shadow_cache(&mut self) {
@@ -553,19 +565,15 @@ impl App {
553565
return;
554566
};
555567

556-
if let Err(error) = platform::open_in_file_manager(&config.cache.shadow_root) {
557-
self.last_status = format!("Open shadow cache failed: {error}");
558-
append_log(&self.paths, &self.last_status);
559-
self.update_ui();
560-
}
568+
let shadow_root = config.cache.shadow_root.clone();
569+
self.run_open_action("Open shadow cache failed", move || {
570+
platform::open_in_file_manager(&shadow_root)
571+
});
561572
}
562573

563574
fn open_log(&mut self) {
564-
if let Err(error) = platform::open_path(&self.paths.log_file) {
565-
self.last_status = format!("Open log failed: {error}");
566-
append_log(&self.paths, &self.last_status);
567-
self.update_ui();
568-
}
575+
let path = self.paths.log_file.clone();
576+
self.run_open_action("Open log failed", move || platform::open_path(&path));
569577
}
570578

571579
fn open_setup_wizard(&mut self) {
@@ -577,11 +585,10 @@ impl App {
577585
}
578586

579587
fn open_app_folder(&mut self) {
580-
if let Err(error) = platform::open_in_file_manager(&self.paths.app_dir) {
581-
self.last_status = format!("Open app folder failed: {error}");
582-
append_log(&self.paths, &self.last_status);
583-
self.update_ui();
584-
}
588+
let path = self.paths.app_dir.clone();
589+
self.run_open_action("Open app folder failed", move || {
590+
platform::open_in_file_manager(&path)
591+
});
585592
}
586593

587594
fn handle_menu_event(&mut self, event_loop: &ActiveEventLoop, event: MenuEvent) {
@@ -715,13 +722,7 @@ impl App {
715722
direction: SyncDirection::FromUsb,
716723
trigger: SyncTrigger::Manual,
717724
});
718-
format!(
719-
"State: {}",
720-
match active_sync.direction {
721-
SyncDirection::FromUsb => "Syncing from USB",
722-
SyncDirection::ToUsb => "Syncing to USB",
723-
}
724-
)
725+
format!("State: {}", active_sync.direction.syncing_text())
725726
} else if !self.drive_present {
726727
"State: Waiting for USB".to_string()
727728
} else if self.last_status.starts_with("Watching ") {

src/config.rs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,40 +36,25 @@ impl AppPaths {
3636
}
3737

3838
pub fn ensure_layout(&self) -> Result<()> {
39-
fs::create_dir_all(&self.app_dir)
40-
.with_context(|| format!("failed to create {}", self.app_dir.display()))?;
41-
fs::create_dir_all(&self.shadow_root)
42-
.with_context(|| format!("failed to create {}", self.shadow_root.display()))?;
43-
44-
if !self.config_file.exists() {
45-
fs::write(&self.config_file, default_config_template())
46-
.with_context(|| format!("failed to create {}", self.config_file.display()))?;
47-
}
48-
49-
if !self.manifest_file.exists() {
50-
fs::write(&self.manifest_file, "{}")
51-
.with_context(|| format!("failed to create {}", self.manifest_file.display()))?;
52-
}
53-
54-
if !self.log_file.exists() {
55-
fs::write(&self.log_file, b"")
56-
.with_context(|| format!("failed to create {}", self.log_file.display()))?;
57-
}
39+
self.ensure_app_dir()?;
40+
ensure_seed_file(&self.shadow_root, None)?;
41+
ensure_seed_file(&self.config_file, Some(default_config_template().as_bytes()))?;
42+
ensure_seed_file(&self.manifest_file, Some(b"{}"))?;
43+
ensure_seed_file(&self.log_file, Some(b""))?;
5844

5945
Ok(())
6046
}
6147

6248
pub fn ensure_wizard_layout(&self) -> Result<()> {
63-
fs::create_dir_all(&self.app_dir)
64-
.with_context(|| format!("failed to create {}", self.app_dir.display()))?;
65-
66-
if !self.config_file.exists() {
67-
fs::write(&self.config_file, default_config_template())
68-
.with_context(|| format!("failed to create {}", self.config_file.display()))?;
69-
}
49+
self.ensure_app_dir()?;
50+
ensure_seed_file(&self.config_file, Some(default_config_template().as_bytes()))?;
7051

7152
Ok(())
7253
}
54+
55+
fn ensure_app_dir(&self) -> Result<()> {
56+
ensure_seed_file(&self.app_dir, None)
57+
}
7358
}
7459

7560
#[derive(Debug, Clone, Deserialize, Serialize)]
@@ -265,6 +250,19 @@ pub fn append_log(paths: &AppPaths, line: impl AsRef<str>) {
265250
.and_then(|mut file| std::io::Write::write_all(&mut file, entry.as_bytes()));
266251
}
267252

253+
fn ensure_seed_file(path: &Path, contents: Option<&[u8]>) -> Result<()> {
254+
if path.exists() {
255+
return Ok(());
256+
}
257+
258+
match contents {
259+
Some(contents) => fs::write(path, contents)
260+
.with_context(|| format!("failed to create {}", path.display())),
261+
None => fs::create_dir_all(path)
262+
.with_context(|| format!("failed to create {}", path.display())),
263+
}
264+
}
265+
268266
fn validate_config(config: AppConfig, paths: &AppPaths) -> Result<ResolvedConfig> {
269267
ensure!(
270268
!config.jobs.is_empty(),

0 commit comments

Comments
 (0)