Files
gpx.studio/gpx-rs/src/engine/derived/file_structure.rs
T

186 lines
5.4 KiB
Rust
Raw Normal View History

2026-10-03 19:17:58 +02:00
use crate::{
2026-10-26 18:51:56 +01:00
File, FileId, FileWaypointsRevisionId, RouteId, TrackId, TrackInfo, TrackSegment,
TrackSegmentId, TrackSegmentRevisionId, WaypointId,
2026-10-03 19:17:58 +02:00
};
2026-10-03 19:08:56 +02:00
2026-10-26 18:51:56 +01:00
/// What the UI needs to display a file: its name and the structure of its routes, tracks and
/// waypoints,
2026-10-03 19:08:56 +02:00
/// with the ids that reference every element. Coordinates are not part of it, they are read
/// from the buffers of the [`CoordinatesCache`](crate::CoordinatesCache) (the `i`-th waypoint
/// of a file is the `i`-th coordinates pair of its waypoint buffer, same for the trackpoints of
/// a segment).
#[derive(Debug, Clone, PartialEq)]
pub struct FileStructure {
pub id: FileId,
pub name: String,
2026-10-04 11:00:46 +02:00
pub desc: Option<String>,
2026-10-26 18:51:56 +01:00
pub routes: Vec<RouteNode>,
2026-10-03 19:08:56 +02:00
pub tracks: Vec<TrackNode>,
pub waypoints: Vec<WaypointNode>,
/// Changes whenever the waypoints of the file (hence their buffer) change.
2026-10-03 19:17:58 +02:00
pub wpt_rev_id: FileWaypointsRevisionId,
2026-10-03 19:08:56 +02:00
}
2026-10-26 18:51:56 +01:00
/// The name, description and style of a route or a track (the style only when the file defines
/// it).
2026-10-03 19:08:56 +02:00
#[derive(Debug, Clone, PartialEq)]
2026-10-26 18:51:56 +01:00
pub struct PathInfo {
2026-10-03 19:08:56 +02:00
pub name: Option<String>,
2026-10-04 11:00:46 +02:00
pub desc: Option<String>,
2026-10-04 08:53:00 +02:00
pub color: Option<String>,
pub opacity: Option<f64>,
pub width: Option<f64>,
2026-10-26 18:51:56 +01:00
}
impl From<&TrackInfo> for PathInfo {
fn from(info: &TrackInfo) -> Self {
Self {
name: info.name.clone(),
desc: info.desc.clone(),
color: info.color.clone(),
opacity: info.opacity,
width: info.width,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct RouteNode {
pub id: RouteId,
pub info: PathInfo,
pub segment: SegmentNode,
}
#[derive(Debug, Clone, PartialEq)]
pub struct TrackNode {
pub id: TrackId,
pub info: PathInfo,
2026-10-03 19:08:56 +02:00
pub segments: Vec<SegmentNode>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct SegmentNode {
pub id: TrackSegmentId,
/// Changes whenever the trackpoints of the segment (hence their buffer) change.
pub rev_id: TrackSegmentRevisionId,
pub len: usize,
}
#[derive(Debug, Clone, PartialEq)]
pub struct WaypointNode {
pub id: WaypointId,
pub name: Option<String>,
2026-10-04 08:53:00 +02:00
pub sym: Option<String>,
2026-10-03 19:08:56 +02:00
}
2026-10-26 18:51:56 +01:00
impl From<&TrackSegment> for SegmentNode {
fn from(seg: &TrackSegment) -> Self {
Self {
id: seg.id,
rev_id: seg.rev_id,
len: seg.len(),
}
}
}
2026-10-03 19:08:56 +02:00
impl FileStructure {
pub fn new(file: &File) -> Self {
Self {
id: file.id,
name: file.info.name.clone(),
2026-10-04 11:00:46 +02:00
desc: file.info.desc.clone(),
2026-10-26 18:51:56 +01:00
routes: file
.rte
.iter()
.map(|rte| RouteNode {
id: rte.id,
info: (&rte.info).into(),
segment: (&rte.seg).into(),
})
.collect(),
2026-10-03 19:08:56 +02:00
tracks: file
.trk
.iter()
.map(|trk| TrackNode {
id: trk.id,
2026-10-26 18:51:56 +01:00
info: (&trk.info).into(),
segments: trk.trkseg.iter().map(Into::into).collect(),
2026-10-03 19:08:56 +02:00
})
.collect(),
waypoints: file
.wpt
.iter()
.map(|wpt| WaypointNode {
id: wpt.id,
name: wpt.name.clone(),
2026-10-04 08:53:00 +02:00
sym: wpt.sym.clone(),
2026-10-03 19:08:56 +02:00
})
.collect(),
2026-10-07 18:22:30 +02:00
wpt_rev_id: file.wpt.rev_id,
2026-10-03 19:08:56 +02:00
}
}
}
#[cfg(test)]
mod tests {
use crate::{Apply, Load, Waypoint, WaypointChunk, engine::command::fixture::Fixture};
use super::*;
#[test]
fn test_structure_of_a_file() {
let mut fx = Fixture::default();
let data = std::fs::read("data/with_tracks_and_segments.gpx").unwrap();
2026-10-04 11:07:12 +02:00
Load {
data: &data,
name: "file",
}
.apply(&mut fx.state())
.unwrap();
2026-10-03 19:08:56 +02:00
let file = &fx.files[&fx.order.0[0]];
let node = FileStructure::new(file);
2026-10-04 11:00:46 +02:00
assert_eq!(
(node.id, &node.name, &node.desc),
(file.id, &file.info.name, &file.info.desc)
);
2026-10-03 19:08:56 +02:00
assert_eq!(node.tracks.len(), file.trk.len());
for (n, t) in node.tracks.iter().zip(&file.trk) {
2026-10-26 18:51:56 +01:00
assert_eq!(n.id, t.id);
assert_eq!(n.info, PathInfo::from(&t.info));
2026-10-03 19:08:56 +02:00
assert_eq!(n.segments.len(), t.trkseg.len());
for (n, s) in n.segments.iter().zip(&t.trkseg) {
assert_eq!((n.id, n.rev_id, n.len), (s.id, s.rev_id, s.len()));
}
}
}
#[test]
fn test_waypoints_in_buffer_order_and_revision() {
let mut file = crate::File::default();
let wpt = |n: &str| Waypoint {
name: Some(n.to_string()),
..Default::default()
};
2026-10-07 18:22:30 +02:00
let before = file.wpt.rev_id;
file.wpt = crate::Waypoints::new([
WaypointChunk {
2026-10-03 19:08:56 +02:00
wpt: vec![wpt("a"), wpt("b")],
..Default::default()
2026-10-07 18:22:30 +02:00
},
WaypointChunk {
2026-10-03 19:08:56 +02:00
wpt: vec![wpt("c")],
..Default::default()
2026-10-07 18:22:30 +02:00
},
]);
2026-10-03 19:08:56 +02:00
let node = FileStructure::new(&file);
let names: Vec<_> = node
.waypoints
.iter()
.map(|w| w.name.clone().unwrap())
.collect();
assert_eq!(names, ["a", "b", "c"]);
2026-10-03 19:17:58 +02:00
assert_ne!(node.wpt_rev_id, before);
2026-10-07 18:22:30 +02:00
assert_eq!(node.wpt_rev_id, file.wpt.rev_id);
2026-10-03 19:08:56 +02:00
}
}