fix: remove_document() kind-check moved before mutable borrow to prevent silent error drop

This commit is contained in:
mow 2026-03-08 17:41:09 +01:00
parent a6c1863f57
commit 7fa218e68c

View file

@ -3,10 +3,10 @@
//
// Document Manager single entry point via SessionCommand.
use crate::document::command::SessionCommand;
use crate::document::loader::{load_document, scan_directory_into};
use crate::document::session::{CollectionKind, SessionData};
use crate::document::store;
use crate::document::session::command::SessionCommand;
use crate::document::session::data::{CollectionKind, SessionData};
use crate::document::session::store;
use crate::document::DocumentContent;
use crate::error::Error;
use log::warn;
@ -38,6 +38,7 @@ impl Default for DocumentManager {
}
impl DocumentManager {
#[must_use]
pub fn new() -> Self {
Self::default()
}
@ -87,11 +88,13 @@ impl DocumentManager {
}
/// Read-only access to a session by name.
#[must_use]
pub fn session(&self, name: &str) -> Option<&SessionData> {
self.sessions.iter().find(|s| s.name == name)
}
/// Read-only access to the active session.
#[must_use]
pub fn active_session(&self) -> Option<&SessionData> {
let name = self.active_session_name.as_deref()?;
self.sessions.iter().find(|s| s.name == name)
@ -118,7 +121,7 @@ impl DocumentManager {
fn open_session_file(&mut self, path: &Path) {
match store::load_sessions(path) {
Ok(mut loaded) => {
for session in loaded.iter_mut() {
for session in &mut loaded {
match session.kind {
CollectionKind::DirectoryBrowser => {
if let Some(dir) = session.path.clone() {
@ -214,29 +217,23 @@ impl DocumentManager {
source_index: usize,
target_index: usize,
) {
let item = match self.session(source_name) {
Some(s) => match s.items.get(source_index) {
let item = if let Some(s) = self.session(source_name) {
match s.items.get(source_index) {
Some(item) => item.clone(),
None => return,
},
None => {
self.state = DocumentState::Error(Error::Session(format!(
"Source session '{}' not found",
source_name
)));
return;
}
} else {
self.state = DocumentState::Error(Error::Session(format!(
"Source session '{source_name}' not found"
)));
return;
};
let target = match self.session(target_name) {
Some(s) => s,
None => {
self.state = DocumentState::Error(Error::Session(format!(
"Target session '{}' not found",
target_name
)));
return;
}
let Some(target) = self.session(target_name) else {
self.state = DocumentState::Error(Error::Session(format!(
"Target session '{target_name}' not found"
)));
return;
};
if target.kind != CollectionKind::DocumentCollection {
@ -262,18 +259,23 @@ impl DocumentManager {
}
fn remove_document(&mut self, index: usize) {
let session = match self.active_session_mut() {
Some(s) => s,
// Check kind before taking a mutable borrow on the session, so that
// self.state can be set without conflicting borrows.
match self.active_session().map(|s| s.kind.clone()) {
None => return,
};
if session.kind != CollectionKind::DocumentCollection {
self.state = DocumentState::Error(Error::Session(
"Can only remove from a DocumentCollection".to_string(),
));
return;
Some(CollectionKind::DirectoryBrowser) => {
self.state = DocumentState::Error(Error::Session(
"Can only remove from a DocumentCollection".to_string(),
));
return;
}
Some(CollectionKind::DocumentCollection) => {}
}
let Some(session) = self.active_session_mut() else {
return;
};
if index >= session.items.len() {
return;
}