Skip to content

Commit ca175d2

Browse files
committed
fix: use single state object
except in the panic hook
1 parent 98eeff2 commit ca175d2

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ use crate::process::kill_process_group;
1919

2020
const BUILD_DIR: &str = ".flatplay";
2121

22-
pub struct FlatpakManager {
23-
state: State,
22+
pub struct FlatpakManager<'a> {
23+
state: &'a mut State,
2424
manifest: Option<Manifest>,
2525
}
2626

2727
use std::path::PathBuf;
2828

29-
impl FlatpakManager {
29+
impl<'a> FlatpakManager<'a> {
3030
fn find_manifests(&self) -> Result<Vec<PathBuf>> {
3131
let mut manifests = vec![];
3232
for entry in WalkDir::new(".")
@@ -70,7 +70,7 @@ impl FlatpakManager {
7070
Ok(())
7171
}
7272

73-
pub fn new(state: State) -> Result<Self> {
73+
pub fn new(state: &'a mut State) -> Result<Self> {
7474
let manifest = if let Some(path) = &state.active_manifest {
7575
Some(Manifest::from_file(path)?)
7676
} else {
@@ -318,7 +318,7 @@ impl FlatpakManager {
318318
}
319319

320320
pub fn stop(&mut self) -> Result<()> {
321-
kill_process_group(&mut self.state)
321+
kill_process_group(self.state)
322322
}
323323

324324
pub fn run(&self) -> Result<()> {

src/main.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ fn main() {
5858
let cli = Cli::parse();
5959
let mut state = State::load().unwrap();
6060

61-
// Special handling for the 'stop' command, which doesn't need the full manager setup.
6261
if let Some(Commands::Stop) = cli.command {
6362
handle_command!(kill_process_group(&mut state));
6463
return;
@@ -95,13 +94,16 @@ fn main() {
9594
// Handle unclean ends where possible.
9695
let original_hook = panic::take_hook();
9796
panic::set_hook(Box::new(move |panic_info| {
98-
let mut state = State::load().unwrap();
99-
state.process_group_id = None;
100-
state.save().unwrap();
97+
if let Ok(mut state) = State::load() {
98+
state.process_group_id = None;
99+
if let Err(e) = state.save() {
100+
eprintln!("Failed to save state in panic hook: {e}");
101+
}
102+
}
101103
original_hook(panic_info);
102104
}));
103105

104-
let mut flatpak_manager = FlatpakManager::new(state).unwrap();
106+
let mut flatpak_manager = FlatpakManager::new(&mut state).unwrap();
105107
match &cli.command {
106108
Some(Commands::Completions { shell }) => {
107109
use clap_complete::generate;
@@ -128,7 +130,6 @@ fn main() {
128130
}
129131

130132
// Clean up pgid in the state file on normal exit.
131-
let mut state = State::load().unwrap();
132133
state.process_group_id = None;
133134
state.save().unwrap();
134135
}

0 commit comments

Comments
 (0)