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