agent_tk/definitions/
goal.rs

1//! This module allows to represent goals.
2
3use crate::uuid;
4use serde::{Deserialize, Serialize};
5use std::collections::BTreeMap;
6
7use crate::definitions::misc_structures::GeoPoint;
8
9/// Represent a delivery goal
10#[derive(Serialize, Deserialize, Debug, Clone, Default)]
11pub struct DeliveryGoal {}
12
13/// Represent a visitation goal
14#[derive(Serialize, Deserialize, Debug, Clone, Default)]
15pub struct VisitGoal
16{
17  /// Position of the goal to visit
18  #[serde(flatten)]
19  pub position: GeoPoint,
20}
21
22/// Represent a set of goals
23#[derive(Serialize, Deserialize, Debug, Clone, Default)]
24pub struct Goal
25{
26  /// Unique identifier for the goal
27  pub uuid: uuid::Uuid,
28  #[serde(default = "Default::default")]
29  /// Delivery goals
30  pub deliver: BTreeMap<String, DeliveryGoal>,
31  /// Visit goals
32  #[serde(default = "Default::default")]
33  pub visit: BTreeMap<String, VisitGoal>,
34}
35
36impl Goal
37{
38  /// Convenient function to convert a node to a JSON string
39  pub fn to_json_string(&self) -> serde_json::Result<String>
40  {
41    serde_json::to_string(self)
42  }
43  /// Convenient function to convert a JSON string to a node
44  pub fn from_json_string(def: impl AsRef<str>) -> serde_json::Result<Goal>
45  {
46    serde_json::from_str(def.as_ref())
47  }
48}
49
50#[cfg(test)]
51mod tests
52{
53  use super::*;
54  fn mgp(longitude: f64, latitude: f64, altitude: f64) -> GeoPoint
55  {
56    GeoPoint {
57      longitude,
58      latitude,
59      altitude,
60    }
61  }
62  #[test]
63  fn parse_test_goals()
64  {
65    let message = include_str!("../../../../data/test_goal.json");
66    let goal: super::Goal = serde_json::from_str(message).unwrap();
67
68    assert!(goal.deliver.is_empty());
69    assert_eq!(goal.visit.len(), 2);
70    assert_eq!(
71      goal.visit.get("loc1").unwrap().position,
72      mgp(16.679423811351608, 57.75966551794022, 0.0)
73    );
74    assert_eq!(
75      goal.visit.get("loc2").unwrap().position,
76      mgp(16.677721987885022, 57.75937792909685, 0.0)
77    );
78  }
79}