use crate::directions::error::Error as DirectionsError;
use crate::error::Error as GoogleMapsError;
use phf::phf_map;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(u8)]
pub enum DrivingManeuver {
Ferry = 0,
FerryTrain = 1,
ForkLeft = 2,
ForkRight = 3,
KeepLeft = 4,
KeepRight = 5,
Merge = 6,
Ramp = 7,
RampLeft = 8,
RampRight = 9,
RoundaboutLeft = 10,
RoundaboutRight = 11,
#[default]
Straight = 12,
TurnLeft = 13,
TurnRight = 14,
TurnSharpLeft = 15,
TurnSharpRight = 16,
TurnSlightLeft = 17,
TurnSlightRight = 18,
UturnLeft = 19,
UturnRight = 20,
}
impl<'de> Deserialize<'de> for DrivingManeuver {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let string = String::deserialize(deserializer)?;
match Self::try_from(string.as_str()) {
Ok(variant) => Ok(variant),
Err(error) => Err(serde::de::Error::custom(error.to_string())),
} } }
impl Serialize for DrivingManeuver {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(std::convert::Into::<&str>::into(self))
} }
impl std::convert::From<&DrivingManeuver> for &str {
fn from(maneuver_type: &DrivingManeuver) -> Self {
match maneuver_type {
DrivingManeuver::Ferry => "ferry",
DrivingManeuver::FerryTrain => "ferry-train",
DrivingManeuver::ForkLeft => "fork-left",
DrivingManeuver::ForkRight => "fork-right",
DrivingManeuver::KeepLeft => "keep-left",
DrivingManeuver::KeepRight => "keep-right",
DrivingManeuver::Merge => "merge",
DrivingManeuver::Ramp => "ramp",
DrivingManeuver::RampLeft => "ramp-left",
DrivingManeuver::RampRight => "ramp-right",
DrivingManeuver::RoundaboutLeft => "roundabout-left",
DrivingManeuver::RoundaboutRight => "roundabout-right",
DrivingManeuver::Straight => "straight",
DrivingManeuver::TurnLeft => "turn-left",
DrivingManeuver::TurnRight => "turn-right",
DrivingManeuver::TurnSharpLeft => "turn-sharp-left",
DrivingManeuver::TurnSharpRight => "turn-sharp-right",
DrivingManeuver::TurnSlightLeft => "turn-slight-left",
DrivingManeuver::TurnSlightRight => "turn-slight-right",
DrivingManeuver::UturnLeft => "uturn-left",
DrivingManeuver::UturnRight => "uturn-right",
} } }
impl std::fmt::Display for DrivingManeuver {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", std::convert::Into::<&str>::into(self))
} }
impl std::convert::From<&DrivingManeuver> for String {
fn from(driving_maneuver: &DrivingManeuver) -> Self {
std::convert::Into::<&str>::into(driving_maneuver).to_string()
} }
static DRIVING_MANEUVERS_BY_CODE: phf::Map<&'static str, DrivingManeuver> = phf_map! {
"ferry" => DrivingManeuver::Ferry,
"ferry-train" => DrivingManeuver::FerryTrain,
"fork-left" => DrivingManeuver::ForkLeft,
"fork-right" => DrivingManeuver::ForkRight,
"keep-left" => DrivingManeuver::KeepLeft,
"keep-right" => DrivingManeuver::KeepRight,
"merge" => DrivingManeuver::Merge,
"ramp" => DrivingManeuver::Ramp,
"ramp-left" => DrivingManeuver::RampLeft,
"ramp-right" => DrivingManeuver::RampRight,
"roundabout-left" => DrivingManeuver::RoundaboutLeft,
"roundabout-right" => DrivingManeuver::RoundaboutRight,
"straight" => DrivingManeuver::Straight,
"turn-left" => DrivingManeuver::TurnLeft,
"turn-right" => DrivingManeuver::TurnRight,
"turn-sharp-left" => DrivingManeuver::TurnSharpLeft,
"turn-sharp-right" => DrivingManeuver::TurnSharpRight,
"turn-slight-left" => DrivingManeuver::TurnSlightLeft,
"turn-slight-right" => DrivingManeuver::TurnSlightRight,
"uturn-left" => DrivingManeuver::UturnLeft,
"uturn-right" => DrivingManeuver::UturnRight,
};
impl std::convert::TryFrom<&str> for DrivingManeuver {
type Error = GoogleMapsError;
fn try_from(driving_maneuver_type_code: &str) -> Result<Self, Self::Error> {
Ok(DRIVING_MANEUVERS_BY_CODE
.get(driving_maneuver_type_code)
.cloned()
.ok_or_else(|| {
DirectionsError::InvalidDrivingManeuverCode(driving_maneuver_type_code.to_string())
})?)
} }
impl std::str::FromStr for DrivingManeuver {
type Err = GoogleMapsError;
fn from_str(driving_maneuver_type_code: &str) -> Result<Self, Self::Err> {
Ok(DRIVING_MANEUVERS_BY_CODE
.get(driving_maneuver_type_code)
.cloned()
.ok_or_else(|| {
DirectionsError::InvalidDrivingManeuverCode(driving_maneuver_type_code.to_string())
})?)
} }
impl DrivingManeuver {
#[must_use]
pub const fn display(&self) -> &str {
match self {
Self::Ferry => "Ferry",
Self::FerryTrain => "Ferry Train",
Self::ForkLeft => "Fork Left",
Self::ForkRight => "Fork Right",
Self::KeepLeft => "Keep Left",
Self::KeepRight => "Keep Right",
Self::Merge => "Merge",
Self::Ramp => "Ramp",
Self::RampLeft => "Ramp Left",
Self::RampRight => "Ramp Right",
Self::RoundaboutLeft => "Roundabout Left",
Self::RoundaboutRight => "Roundabout Right",
Self::Straight => "Straight",
Self::TurnLeft => "Turn Left",
Self::TurnRight => "Turn Right",
Self::TurnSharpLeft => "Turn Sharp Left",
Self::TurnSharpRight => "Turn Sharp Right",
Self::TurnSlightLeft => "Turn Slight Left",
Self::TurnSlightRight => "Turn Slight Right",
Self::UturnLeft => "U-turn Left",
Self::UturnRight => "U-turn Right",
} } }