use crate::LaneId;
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Along<T> {
pub lane: LaneId,
pub from: f32,
pub to: f32,
pub value: T,
}
pub(crate) fn at<T>(list: &[Along<T>], lane: LaneId, s: f32) -> Option<&T> {
let end = list.partition_point(|a| (a.lane.0, a.from) <= (lane.0, s));
let found = &list[..end].last()?;
(found.lane == lane && s <= found.to).then_some(&found.value)
}
pub(crate) fn sorted<T>(mut list: Vec<Along<T>>) -> Vec<Along<T>> {
list.retain(|a| !a.from.is_nan() && !a.to.is_nan());
list.sort_by(|a, b| a.lane.0.cmp(&b.lane.0).then(a.from.total_cmp(&b.from)));
list
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum SpeedLimit {
Max(f32),
Unlimited,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Access {
Allow(Vec<String>),
Deny(Vec<String>),
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Visibility {
pub forward: f32,
pub back: f32,
pub left: f32,
pub right: f32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum RoadType {
Unknown,
Rural,
Motorway,
Town,
LowSpeed,
Pedestrian,
Bicycle,
TownExpressway,
TownCollector,
TownArterial,
TownPrivate,
TownLocal,
TownPlayStreet,
}
impl RoadType {
pub fn as_str(&self) -> &'static str {
match self {
Self::Unknown => "unknown",
Self::Rural => "rural",
Self::Motorway => "motorway",
Self::Town => "town",
Self::LowSpeed => "low-speed",
Self::Pedestrian => "pedestrian",
Self::Bicycle => "bicycle",
Self::TownExpressway => "town-expressway",
Self::TownCollector => "town-collector",
Self::TownArterial => "town-arterial",
Self::TownPrivate => "town-private",
Self::TownLocal => "town-local",
Self::TownPlayStreet => "town-play-street",
}
}
}
impl std::fmt::Display for RoadType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn stretch(lane: usize, from: f32, to: f32, value: u8) -> Along<u8> {
Along {
lane: LaneId(lane),
from,
to,
value,
}
}
#[test]
fn at_finds_the_stretch_under_a_point() {
let list = sorted(vec![
stretch(2, 0.0, 5.0, 3),
stretch(1, 4.0, 10.0, 2),
stretch(1, 0.0, 4.0, 1),
]);
assert_eq!(at(&list, LaneId(1), 0.0), Some(&1));
assert_eq!(at(&list, LaneId(1), 4.0), Some(&2));
assert_eq!(at(&list, LaneId(1), 10.0), Some(&2));
assert_eq!(at(&list, LaneId(1), 10.5), None);
assert_eq!(at(&list, LaneId(2), 1.0), Some(&3));
assert_eq!(at(&list, LaneId(3), 1.0), None);
assert_eq!(at(&list, LaneId(0), 1.0), None);
}
}