Skip to content

Commit

Permalink
Clippy pedantic
Browse files Browse the repository at this point in the history
  • Loading branch information
fredclausen committed Jan 25, 2024
1 parent 1a75173 commit 4031a60
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/decoders/json_types/speed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use std::fmt;
#[derive(Deserialize, Debug, Clone, PartialEq, PartialOrd, Default)]
#[serde(from = "f32")]
pub enum Speed {
Knots(f32),
KnotsAsF32(f32),
KnotsAsF64(f64),
#[default]
None,
}
Expand All @@ -19,7 +20,8 @@ impl Speed {
#[must_use]
pub fn get_speed(&self) -> f64 {
match self {
Self::Knots(speed) => f64::from(*speed),
Self::KnotsAsF32(speed) => f64::from(*speed),
Self::KnotsAsF64(speed) => *speed,
Self::None => 0.0,
}
}
Expand All @@ -31,53 +33,58 @@ impl Serialize for Speed {
S: serde::Serializer,
{
match *self {
Speed::Knots(speed) => serializer.serialize_f32(speed),
Speed::KnotsAsF32(speed) => serializer.serialize_f32(speed),
Speed::KnotsAsF64(speed) => serializer.serialize_f64(speed),
Speed::None => serializer.serialize_none(),
}
}
}

impl From<f64> for Speed {
fn from(speed: f64) -> Self {
Self::Knots(speed as f32)
Self::KnotsAsF64(speed)
}
}

impl From<f32> for Speed {
fn from(speed: f32) -> Self {
Self::Knots(speed)
Self::KnotsAsF32(speed)
}
}

impl Speed {
#[must_use]
pub fn as_meters(&self) -> f32 {
pub fn as_meters(&self) -> f64 {
match self {
Speed::Knots(speed) => *speed * 0.514_444,
Speed::KnotsAsF32(speed) => f64::from(*speed) * 0.514_444,
Speed::KnotsAsF64(speed) => *speed * 0.514_444,
Speed::None => 0.0,
}
}

#[must_use]
pub fn as_knots(&self) -> f32 {
pub fn as_knots(&self) -> f64 {
match self {
Speed::Knots(speed) => *speed,
Speed::KnotsAsF32(speed) => f64::from(*speed),
Speed::KnotsAsF64(speed) => *speed,
Speed::None => 0.0,
}
}

#[must_use]
pub fn display_as_knots(&self) -> String {
match self {
Speed::Knots(speed) => format!("{speed} knots"),
Speed::KnotsAsF32(speed) => format!("{speed} knots"),
Speed::KnotsAsF64(speed) => format!("{speed} knots"),
Speed::None => "None".to_string(),
}
}

#[must_use]
pub fn display_as_meters(&self) -> String {
match self {
Speed::Knots(speed) => format!("{} m/min", speed * 0.514_444),
Speed::KnotsAsF32(speed) => format!("{} m/min", speed * 0.514_444),
Speed::KnotsAsF64(speed) => format!("{} m/min", speed * 0.514_444),
Speed::None => "None".to_string(),
}
}
Expand All @@ -87,7 +94,8 @@ impl fmt::Display for Speed {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
// cast to u32 to remove the decimal
Speed::Knots(speed) => write!(f, "{} knots", *speed),
Speed::KnotsAsF32(speed) => write!(f, "{} knots", *speed),
Speed::KnotsAsF64(speed) => write!(f, "{} knots", *speed),
Speed::None => write!(f, "None"),
}
}
Expand Down

0 comments on commit 4031a60

Please sign in to comment.