use routee_compass_core::model::cost::TraversalCost;
use routee_compass_core::model::state::StateVariable;
use serde::Serialize;
#[derive(Debug, Clone, Serialize)]
pub struct MapMatchingResponse {
pub point_matches: Vec<PointMatchResponse>,
pub matched_path: serde_json::Value,
#[serde(skip_serializing_if = "Option::is_none")]
pub traversal_summary: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize)]
pub struct MatchedEdgeResponse {
pub edge_list_id: usize,
pub edge_id: u64,
#[serde(skip_serializing_if = "Option::is_none")]
pub geometry: Option<geo::LineString<f32>>,
pub cost: TraversalCost,
pub result_state: Vec<StateVariable>,
}
impl MatchedEdgeResponse {
pub fn new(
edge_list_id: usize,
edge_id: u64,
geometry: Option<geo::LineString<f32>>,
cost: TraversalCost,
result_state: Vec<StateVariable>,
) -> Self {
Self {
edge_list_id,
edge_id,
geometry,
cost,
result_state,
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct PointMatchResponse {
pub edge_list_id: usize,
pub edge_id: u64,
pub distance: f64,
}
impl MapMatchingResponse {
pub fn new(
point_matches: Vec<PointMatchResponse>,
matched_path: serde_json::Value,
traversal_summary: Option<serde_json::Value>,
) -> Self {
Self {
point_matches,
matched_path,
traversal_summary,
}
}
}
impl PointMatchResponse {
pub fn new(edge_list_id: usize, edge_id: u64, distance: f64) -> Self {
Self {
edge_list_id,
edge_id,
distance,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn test_serialize_response() {
let response = MapMatchingResponse {
point_matches: vec![
PointMatchResponse::new(0, 1, 5.5),
PointMatchResponse::new(0, 2, 3.2),
],
matched_path: json!([
MatchedEdgeResponse::new(0, 1, None, TraversalCost::empty(), vec![]),
MatchedEdgeResponse::new(0, 2, None, TraversalCost::empty(), vec![]),
]),
traversal_summary: None,
};
let json = serde_json::to_string(&response).unwrap();
assert!(json.contains("\"point_matches\""));
assert!(json.contains("\"matched_path\""));
assert!(!json.contains("\"geometry\""));
assert!(json.contains("\"cost\""));
assert!(json.contains("\"result_state\""));
}
}