This commit is contained in:
vcoppe committed 2026-10-14 19:15:02 +02:00
1 parent 2e1ca00afa
commit 1ba5436af3
29 files changed
+3348 -190

No files matched your search

+1 -1
View File
@@ -9,7 +9,7 @@ pub struct Load<'a> {
}
impl Load<'_> {
fn parse(self, categories: &mut TrackpointCategories) -> Result<File, CommandError> {
pub(crate) fn parse(self, categories: &mut TrackpointCategories) -> Result<File, CommandError> {
let mut file = parse(self.data, categories)
.map_err(|err| CommandError::InvalidData(err.to_string()))?;
if file.info.name.trim().is_empty() {
+2
View File
@@ -6,6 +6,7 @@ mod load;
mod move_elements;
mod new;
mod paste;
mod sync;
mod transfer;
pub use delete::*;
@@ -16,4 +17,5 @@ pub use load::*;
pub use move_elements::*;
pub use new::*;
pub use paste::*;
pub use sync::*;
pub use transfer::*;
+148
View File
@@ -0,0 +1,148 @@
use std::rc::Rc;
use crate::{Apply, CommandError, FileId, Load, State};
/// A file that comes from the outside (the file system), under the identity it has here.
#[derive(Debug)]
pub struct ExternalFile<'a> {
pub id: FileId,
pub data: &'a [u8],
/// Name of the file when the data has none (see [`Load`]).
pub name: &'a str,
}
/// Brings changes of the files from the outside in, as a single command (one undo step): the files
/// that are given replace the ones of the same id (which stay where they are in the order, and keep
/// their id), or are added at the end if there are none; the ones to `remove` are deleted.
///
/// Unlike [`crate::LoadFiles`], the selection is left alone (what it holds of a file that was
/// replaced or deleted is dropped by the engine afterwards, as with any edit).
///
/// The files that cannot be read are skipped, and the files that are here stay as they are. It is an
/// error when nothing could be done.
#[derive(Debug, Default)]
pub struct SyncFiles<'a> {
pub files: Vec<ExternalFile<'a>>,
pub remove: Vec<FileId>,
}
impl Apply for SyncFiles<'_> {
fn apply(self, state: &mut State) -> Result<(), CommandError> {
let mut changed = false;
let mut first_error = None;
for external in self.files {
let load = Load {
data: external.data,
name: external.name,
};
match load.parse(state.categories) {
Ok(mut file) => {
file.id = external.id;
if state.files.insert(external.id, Rc::new(file)).is_none() {
state.order.0.push(external.id);
}
changed = true;
}
Err(error) => {
first_error.get_or_insert(error);
}
}
}
for id in self.remove {
if state.files.remove(&id).is_some() {
state.order.0.retain(|other| *other != id);
changed = true;
}
}
if changed {
Ok(())
} else {
Err(first_error.unwrap_or(CommandError::NothingToDo))
}
}
}
#[cfg(test)]
mod tests {
use crate::engine::command::fixture::Fixture;
use crate::{New, Selection};
use super::*;
const GPX: &[u8] = br#"<gpx version="1.1"><metadata><name>from disk</name></metadata></gpx>"#;
fn new_file(fx: &mut Fixture, name: &str) -> FileId {
New {
name,
trackpoint: None,
}
.apply(&mut fx.state())
.unwrap();
*fx.selected_files().iter().next().unwrap()
}
fn external(id: FileId) -> ExternalFile<'static> {
ExternalFile {
id,
data: GPX,
name: "name",
}
}
#[test]
fn test_sync_replaces_a_file_in_place() {
let mut fx = Fixture::default();
let first = new_file(&mut fx, "first");
let second = new_file(&mut fx, "second");
SyncFiles {
files: vec![external(first)],
remove: vec![],
}
.apply(&mut fx.state())
.unwrap();
assert_eq!(fx.order.0, [first, second]);
assert_eq!(fx.files[&first].info.name, "from disk");
assert_eq!(fx.files[&first].id, first);
// the selection is not changed
assert_eq!(fx.selected_files(), [second].into());
}
#[test]
fn test_sync_adds_and_removes_files() {
let mut fx = Fixture::default();
let first = new_file(&mut fx, "first");
let added = FileId::default();
SyncFiles {
files: vec![external(added)],
remove: vec![first],
}
.apply(&mut fx.state())
.unwrap();
assert_eq!(fx.order.0, [added]);
assert!(!fx.files.contains_key(&first));
// what was selected is gone
assert!(matches!(fx.selection, Selection::File { .. }));
}
#[test]
fn test_sync_with_nothing_to_do_or_invalid_data_is_an_error() {
let mut fx = Fixture::default();
assert_eq!(
SyncFiles::default().apply(&mut fx.state()),
Err(CommandError::NothingToDo)
);
let invalid = SyncFiles {
files: vec![ExternalFile {
id: FileId::default(),
data: b"<gpx><trk></gpx>",
name: "x",
}],
remove: vec![],
};
assert!(matches!(
invalid.apply(&mut fx.state()),
Err(CommandError::InvalidData(_))
));
assert!(fx.files.is_empty());
}
}
+2
View File
@@ -24,6 +24,7 @@ pub enum Command<'a> {
New(New<'a>),
Load(Load<'a>),
LoadFiles(LoadFiles<'a>),
SyncFiles(SyncFiles<'a>),
Duplicate(Duplicate),
Delete(Delete),
Paste(Paste),
@@ -58,6 +59,7 @@ impl Apply for Command<'_> {
Command::New(c) => c.apply(state),
Command::Load(c) => c.apply(state),
Command::LoadFiles(c) => c.apply(state),
Command::SyncFiles(c) => c.apply(state),
Command::Duplicate(c) => c.apply(state),
Command::Delete(c) => c.apply(state),
Command::Paste(c) => c.apply(state),