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

+21
View File
@@ -178,6 +178,27 @@ pub fn load_files(
edit(engine, Command::LoadFiles(engine::LoadFiles { files }))
}
/// Brings changes of the files from the outside in (see [`engine::SyncFiles`]): each file is
/// `(id, data, name)`, and replaces the file of that id, or is added if there is none. The files to
/// `remove` are deleted. The selection is left alone.
pub fn sync_files(engine: &mut Engine, files: &[(Uuid, &[u8], &str)], remove: &[Uuid]) -> Outcome {
let files = files
.iter()
.map(|(id, data, name)| engine::ExternalFile {
id: engine::FileId(*id),
data,
name,
})
.collect();
edit(
engine,
Command::SyncFiles(engine::SyncFiles {
files,
remove: file_ids(remove),
}),
)
}
pub fn duplicate(engine: &mut Engine) -> Outcome {
edit(engine, Command::Duplicate(engine::Duplicate))
}
+56 -1
View File
@@ -17,7 +17,7 @@ pub fn ids(items: &[FileId]) -> Vec<String> {
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Outcome {
changed: bool,
pub changed: bool,
order_changed: bool,
selection_changed: bool,
clipboard_changed: bool,
@@ -47,11 +47,66 @@ impl Outcome {
}
}
/// The outcome of a frontend that knows nothing of the engine: all the files are added, and the
/// order, the selection and the clipboard are to be read.
pub fn everything(engine: &engine::Engine) -> Self {
Self {
changed: true,
order_changed: true,
selection_changed: true,
clipboard_changed: true,
added: ids(engine.order()),
modified: vec![],
removed: vec![],
error: None,
saves: false,
}
}
/// The outcome of a call that did nothing and has nothing to say.
pub fn failed_none() -> Self {
Self::new(&engine::Outcome::default(), None)
}
/// Whether the call did nothing and says why.
pub fn has_error(&self) -> bool {
self.error.is_some()
}
/// What two calls that were made one after the other did together.
pub fn merge(mut self, other: Self) -> Self {
fn union(into: &mut Vec<String>, from: Vec<String>) {
for id in from {
if !into.contains(&id) {
into.push(id);
}
}
}
self.changed |= other.changed;
self.order_changed |= other.order_changed;
self.selection_changed |= other.selection_changed;
self.clipboard_changed |= other.clipboard_changed;
self.saves |= other.saves;
// a file that was added and then modified is still an added one, and one that was added
// and then removed is not there at all
let added_before = self.added.clone();
union(&mut self.added, other.added);
for id in other.modified {
if !self.added.contains(&id) && !self.modified.contains(&id) {
self.modified.push(id);
}
}
for id in other.removed {
self.added.retain(|added| *added != id);
self.modified.retain(|modified| *modified != id);
if !added_before.contains(&id) && !self.removed.contains(&id) {
self.removed.push(id);
}
}
self.error = self.error.or(other.error);
self
}
/// What a call that does nothing returns.
pub fn failed(message: impl Into<String>) -> Self {
Self::new(&engine::Outcome::default(), Some(message.into()))
+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),