mirror of
https://github.com/gpxstudio/gpx.studio.git
synced 2026-11-15 07:07:07 +00:00
Migration to new Rust-based engine (#401)
* wasm first steps * parsing start * parsing continued * progress * progress * progress * progress * progress * progress * progress * progress * fixes * reorganize * change type * fix * progress * refactoring * progress * progress * progress * keep style info * progress * progress * remove (copy) in name * progress * merge methods into 1 * splice * progress * progress * progress * cleaning * progress * progress * progress * visibility * progress * progress * progress * progress * progress * fixes * progress * fix markers * fix * progress * progress * progress * progress * progress * progress * small fix * use engine info * fix popups * small fix * progress * progress * progress * clean tool * handle self closing elements * reduce data exchanges across boundary * copy on wasm side to avoid any data corruption * speed fix * routing * refactoring * reverse * reverse * crop * split * merge * progress * time tool * fix bounds for waypoints * reduce * fix reactivity * handle corrupt time data * export * progress * remove gpx js package * remove todo * update readme * cleaning * progress * fix link fields * remove unused fields * refactoring and tests * import and export of routes, ref #192 * per-chunk categories storage * cross-tab sync * outdated state * refactoring * reorder methods * refactoring * fix * improve flow * renaming * progress * progress * fix * progress * per file history * keep original flag * use history variant * fix * progress * file explorer * progress * fix * sticky folders * improved sticky folders * fix sortable * fix sortable * fix * fix tab size * no file delete on desktop * fix routing on other file * fix sorting * remember language * open links in browser * commit missing changes * avoid selecting on resize * clarify filename * explorer actions * fix folder styling * drop anywhere in folder * cleaning * fixes * remove focus-ring * full screen * small fix * consistent highlight color * improved preview * performance improvements * sparser data transfers * sparser data transfers * show popup once data is loaded * fix routing control size * fix * sparse desktop build * remove custom ign styles * use new endpoint for openhikingmap * strip native menu * reorder context menu items * route full support * route - track conversion * create route file action * route export, filter out anchors at high zoom level * clarify name handling * move hide & center to view * routes continued * update docs * fix power format, closes #378 * lint fixes * fix display of additional data with gaps, closes #361 * fix linting issues * fix import modules * fix multidrag * change base to resolve * fix type errors * fix layer typings * fix last type error * each keys * improve typings * deploy to cloudflare --------- Co-authored-by: vcoppe <vcoppe@hexaly.com>
This commit is contained in:
1 parent
6a0d434371
commit
8fe7d8d64d
484 files changed
+53949
-41152
No files matched your search
@@ -0,0 +1,151 @@
|
||||
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 (the name of the file on the disk, without its
|
||||
/// extension).
|
||||
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,
|
||||
route: false,
|
||||
}
|
||||
.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]);
|
||||
// named as the data says, the name outside is only for a file that has none
|
||||
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());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user