routee_compass/plugin/input/default/load_balancer/
weight_heuristic.rs1use super::custom_weight_type::CustomWeightType;
2use crate::plugin::{input::InputJsonExtensions, input::InputPluginError};
3use routee_compass_core::util::geo::haversine;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Serialize, Deserialize, Clone)]
7#[serde(rename_all = "snake_case", tag = "type")]
8pub enum WeightHeuristic {
9 Haversine,
12 Custom {
15 custom_weight_type: CustomWeightType,
16 },
17}
18
19impl WeightHeuristic {
20 pub fn estimate_weight(&self, query: &serde_json::Value) -> Result<f64, InputPluginError> {
21 match self {
22 WeightHeuristic::Haversine => {
23 let o = query.get_origin_coordinate()?;
24 let d_option = query.get_destination_coordinate()?;
25 match d_option {
26 None => Err(InputPluginError::InputPluginFailed(String::from(
27 "cannot estimate search size without destination coordinate",
28 ))),
29 Some(d) => haversine::coord_distance(&o, &d)
30 .map(|d| d.get::<uom::si::length::kilometer>())
31 .map_err(|s| {
32 InputPluginError::InputPluginFailed(format!(
33 "failed calculating load balancing weight value due to {s}"
34 ))
35 }),
36 }
37 }
38 WeightHeuristic::Custom { custom_weight_type } => custom_weight_type.get_weight(query),
39 }
40 }
41}