vrp_pragmatic/
lib.rs

1//! Pragmatic crates aims to solve real world VRP variations allowing users to specify their problems
2//! via simple **pragmatic** json format.
3
4#![warn(missing_docs)]
5#![forbid(unsafe_code)]
6
7#[cfg(test)]
8#[path = "../tests/helpers/mod.rs"]
9#[macro_use]
10mod helpers;
11
12#[cfg(test)]
13#[path = "../tests/generator/mod.rs"]
14mod generator;
15
16#[cfg(test)]
17#[path = "../tests/features/mod.rs"]
18#[allow(clippy::needless_update)]
19mod features;
20
21#[cfg(test)]
22#[path = "../tests/discovery/mod.rs"]
23pub mod discovery;
24
25#[cfg(test)]
26#[path = "../tests/regression/mod.rs"]
27pub mod regression;
28
29pub use vrp_core as core;
30
31mod utils;
32
33pub mod checker;
34pub mod format;
35pub mod validation;
36
37use crate::format::problem::Problem;
38use crate::format::{CoordIndex, Location};
39use time::format_description::well_known::Rfc3339;
40use time::OffsetDateTime;
41use vrp_core::prelude::{Float, GenericError};
42
43/// Get lists of unique locations in the problem. Use it to request routing matrix from outside.
44/// NOTE: it includes all locations of all types, so you might need to filter it if types are mixed.
45pub fn get_unique_locations(problem: &Problem) -> Vec<Location> {
46    CoordIndex::new(problem).unique()
47}
48
49fn format_time(time: Float) -> String {
50    // TODO avoid using implicitly unwrap
51    OffsetDateTime::from_unix_timestamp(time as i64).map(|time| time.format(&Rfc3339).unwrap()).unwrap()
52}
53
54fn parse_time(time: &str) -> Float {
55    parse_time_safe(time).unwrap()
56}
57
58fn parse_time_safe(time: &str) -> Result<Float, GenericError> {
59    OffsetDateTime::parse(time, &Rfc3339)
60        .map(|time| time.unix_timestamp() as Float)
61        .map_err(|err| format!("cannot parse date: {err}").into())
62}