create route file action

This commit is contained in:
vcoppe committed 2026-10-26 19:22:14 +01:00
1 parent 764d0b233a
commit 0f957fb1f2
60 files changed
+211 -85

No files matched your search

+12 -3
View File
@@ -148,21 +148,30 @@ pub fn redo<H: History>(engine: &mut Engine<H>) -> Outcome {
// Files
/// Creates a file and selects it. With `lng` and `lat`, it starts with a track and a segment that
/// hold that trackpoint (at `ele`, 0 by default).
/// Creates a file and selects it. With `route`, it starts with a route. With `lng` and `lat`, it
/// starts with a segment that holds that trackpoint (at `ele`, 0 by default): the one of its route
/// with `route`, of a track otherwise.
pub fn new_file<H: History>(
engine: &mut Engine<H>,
name: &str,
lng: Option<f64>,
lat: Option<f64>,
ele: Option<f64>,
route: bool,
) -> Outcome {
let trackpoint = lng.zip(lat).map(|(lng, lat)| engine::NewTrackpoint {
lng,
lat,
ele: ele.unwrap_or_default(),
});
edit(engine, Command::New(engine::New { name, trackpoint }))
edit(
engine,
Command::New(engine::New {
name,
trackpoint,
route,
}),
)
}
/// Loads files as a single command (one undo step). The files are in `data`, one after the other,
@@ -59,12 +59,14 @@ mod tests {
New {
name: "first",
trackpoint: None,
route: false,
}
.apply(&mut fx.state())
.unwrap();
New {
name: "second",
trackpoint: None,
route: false,
}
.apply(&mut fx.state())
.unwrap(); // now selected
@@ -122,6 +124,7 @@ mod tests {
New {
name: "file",
trackpoint: None,
route: false,
}
.apply(&mut fx.state())
.unwrap();
@@ -36,6 +36,7 @@ mod tests {
New {
name: "file",
trackpoint: None,
route: false,
}
.apply(&mut fx.state())
.unwrap();
@@ -61,6 +61,7 @@ mod tests {
New {
name: "file",
trackpoint: None,
route: false,
}
.apply(&mut fx.state())
.unwrap();
@@ -77,6 +78,7 @@ mod tests {
New {
name: "file",
trackpoint: None,
route: false,
}
.apply(&mut fx.state())
.unwrap();
@@ -29,6 +29,7 @@ mod tests {
New {
name: "file",
trackpoint: None,
route: false,
}
.apply(&mut fx.state())
.unwrap();
+2
View File
@@ -146,6 +146,7 @@ mod tests {
crate::New {
name: "keep",
trackpoint: None,
route: false,
}
.apply(&mut fx.state())
.unwrap();
@@ -280,6 +281,7 @@ mod tests {
crate::New {
name: "keep",
trackpoint: None,
route: false,
}
.apply(&mut fx.state())
.unwrap();
@@ -31,12 +31,14 @@ mod tests {
crate::New {
name: "a",
trackpoint: None,
route: false,
}
.apply(&mut fx.state())
.unwrap();
crate::New {
name: "b",
trackpoint: None,
route: false,
}
.apply(&mut fx.state())
.unwrap();
@@ -217,6 +217,7 @@ mod tests {
crate::New {
name,
trackpoint: None,
route: false,
}
.apply(&mut fx.state())
.unwrap();
+3
View File
@@ -85,6 +85,7 @@ mod tests {
crate::New {
name: "keep",
trackpoint: None,
route: false,
}
.apply(&mut fx.state())
.unwrap();
@@ -140,6 +141,7 @@ mod tests {
crate::New {
name: "before",
trackpoint: None,
route: false,
}
.apply(&mut fx.state())
.unwrap();
@@ -189,6 +191,7 @@ mod tests {
crate::New {
name: "keep",
trackpoint: None,
route: false,
}
.apply(&mut fx.state())
.unwrap();
+53 -6
View File
@@ -1,12 +1,16 @@
use crate::{Apply, CommandError, File, LngLat, State, Track, TrackSegment, Trackpoint, produce};
use crate::{
Apply, CommandError, File, LngLat, Route, State, Track, TrackSegment, Trackpoint, produce,
};
/// Creates an empty file and selects it. With a `trackpoint`, the file starts with a track and a
/// segment that hold it: it is how the routing tool starts a route on an empty map, in a single
/// undo step.
/// Creates an empty file and selects it. With `route`, the file starts with a route, which is
/// empty. With a `trackpoint`, the file starts with a segment that holds it, the one of its route
/// with `route` or of a track otherwise: it is how the routing tool starts a route on an empty map,
/// in a single undo step.
#[derive(Debug)]
pub struct New<'a> {
pub name: &'a str,
pub trackpoint: Option<NewTrackpoint>,
pub route: bool,
}
#[derive(Debug, Clone, Copy)]
@@ -21,8 +25,8 @@ impl Apply for New<'_> {
produce(state, |_| {
let mut file = File::default();
file.info.name = self.name.to_owned();
let mut segment = TrackSegment::default();
if let Some(point) = self.trackpoint {
let mut segment = TrackSegment::default();
// the ends of a segment are anchors of the routing tool
segment.splice(
0,
@@ -36,6 +40,13 @@ impl Apply for New<'_> {
..Default::default()
}],
);
}
if self.route {
file.rte.push(Route {
seg: segment,
..Default::default()
});
} else if self.trackpoint.is_some() {
file.trk.push(Track {
trkseg: vec![segment],
..Default::default()
@@ -59,7 +70,8 @@ mod tests {
assert!(
New {
name: "new",
trackpoint: None
trackpoint: None,
route: false,
}
.apply(&mut fx.state())
.is_ok()
@@ -83,6 +95,7 @@ mod tests {
New {
name: "new",
trackpoint: Some(point),
route: false,
}
.apply(&mut fx.state())
.unwrap();
@@ -98,4 +111,38 @@ mod tests {
assert_eq!(segment[0].anchor, Some(0));
assert_eq!(fx.selected_files(), [file.id].into());
}
#[test]
fn test_new_with_a_route() {
let mut fx = Fixture::default();
New {
name: "new",
trackpoint: None,
route: true,
}
.apply(&mut fx.state())
.unwrap();
let file = fx.files.values().next().unwrap();
assert_eq!((file.rte.len(), file.trk.len()), (1, 0));
assert!(file.rte[0].seg.is_empty());
assert_eq!(fx.selected_files(), [file.id].into());
// the trackpoint goes to the route
let mut fx = Fixture::default();
New {
name: "new",
trackpoint: Some(NewTrackpoint {
lng: 1.0,
lat: 2.0,
ele: 3.0,
}),
route: true,
}
.apply(&mut fx.state())
.unwrap();
let file = fx.files.values().next().unwrap();
assert_eq!((file.rte.len(), file.trk.len()), (1, 0));
assert_eq!(file.rte[0].seg.len(), 1);
assert_eq!(file.rte[0].seg[0].anchor, Some(0));
}
}
+1
View File
@@ -80,6 +80,7 @@ mod tests {
New {
name,
trackpoint: None,
route: false,
}
.apply(&mut fx.state())
.unwrap();
@@ -119,12 +119,14 @@ mod tests {
New {
name: "a",
trackpoint: None,
route: false,
}
.apply(&mut fx.state())
.unwrap();
New {
name: "b",
trackpoint: None,
route: false,
}
.apply(&mut fx.state())
.unwrap();
@@ -170,6 +172,7 @@ mod tests {
New {
name: "a",
trackpoint: None,
route: false,
}
.apply(&mut fx.state())
.unwrap();
@@ -197,6 +200,7 @@ mod tests {
New {
name,
trackpoint: None,
route: false,
}
.apply(&mut fx.state())
.unwrap();
@@ -235,6 +239,7 @@ mod tests {
New {
name: "a",
trackpoint: None,
route: false,
}
.apply(&mut fx.state())
.unwrap();
@@ -251,6 +256,7 @@ mod tests {
New {
name: "a",
trackpoint: None,
route: false,
}
.apply(&mut fx.state())
.unwrap();
+2
View File
@@ -87,6 +87,7 @@ fn new(engine: &mut TestEngine, name: &str) -> bool {
Command::New(New {
name,
trackpoint: None,
route: false,
}),
)
}
@@ -1489,6 +1490,7 @@ mod per_file_history {
.execute(Action::Edit(Command::New(New {
name,
trackpoint: None,
route: false,
})))
.unwrap();
*engine.order().last().unwrap()