use crate::uuid;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use crate::definitions::misc_structures::GeoPoint;
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct DeliveryGoal {}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct VisitGoal
{
#[serde(flatten)]
pub position: GeoPoint,
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Goal
{
pub uuid: uuid::Uuid,
#[serde(default = "Default::default")]
pub deliver: BTreeMap<String, DeliveryGoal>,
#[serde(default = "Default::default")]
pub visit: BTreeMap<String, VisitGoal>,
}
impl Goal
{
pub fn to_json_string(&self) -> serde_json::Result<String>
{
serde_json::to_string(self)
}
pub fn from_json_string(def: impl AsRef<str>) -> serde_json::Result<Goal>
{
serde_json::from_str(def.as_ref())
}
}
#[cfg(test)]
mod tests
{
use super::*;
fn mgp(longitude: f64, latitude: f64, altitude: f64) -> GeoPoint
{
GeoPoint {
longitude,
latitude,
altitude,
}
}
#[test]
fn parse_test_goals()
{
let message = include_str!("../../../../data/test_goal.json");
let goal: super::Goal = serde_json::from_str(message).unwrap();
assert!(goal.deliver.is_empty());
assert_eq!(goal.visit.len(), 2);
assert_eq!(
goal.visit.get("loc1").unwrap().position,
mgp(16.679423811351608, 57.75966551794022, 0.0)
);
assert_eq!(
goal.visit.get("loc2").unwrap().position,
mgp(16.677721987885022, 57.75937792909685, 0.0)
);
}
}