libmedium/units/native/
angular_velocity.rs

1use crate::units::{Error as UnitError, Raw, Result as UnitResult};
2
3use std::borrow::Cow;
4use std::fmt;
5use std::ops::{Add, Div, Mul};
6
7/// Struct that represents an angular velocity.
8#[derive(Debug, Clone, Copy, PartialOrd, PartialEq, Eq, Ord, Hash)]
9pub struct AngularVelocity(u32);
10
11impl AngularVelocity {
12    /// Creates an `AngularVelocity` struct from a value measuring revolutions per minute.
13    pub fn from_rpm(rpm: impl Into<u32>) -> Self {
14        AngularVelocity(rpm.into())
15    }
16
17    /// Returns the struct's value in revolutions per minute.
18    pub fn as_rpm(self) -> u32 {
19        self.0
20    }
21}
22
23impl Raw for AngularVelocity {
24    fn from_raw(raw: &str) -> UnitResult<Self> {
25        raw.trim()
26            .parse::<u32>()
27            .map(AngularVelocity::from_rpm)
28            .map_err(UnitError::parsing)
29    }
30
31    fn to_raw(&self) -> Cow<'_, str> {
32        Cow::Owned(self.as_rpm().to_string())
33    }
34}
35
36impl fmt::Display for AngularVelocity {
37    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
38        write!(f, "{}rpm", self.as_rpm())
39    }
40}
41
42impl Add for AngularVelocity {
43    type Output = Self;
44
45    fn add(self, other: Self) -> Self {
46        AngularVelocity(self.0 + other.0)
47    }
48}
49
50impl<T: Into<u32>> Mul<T> for AngularVelocity {
51    type Output = Self;
52
53    fn mul(self, other: T) -> AngularVelocity {
54        AngularVelocity(self.0 * other.into())
55    }
56}
57
58impl<T: Into<u32>> Div<T> for AngularVelocity {
59    type Output = Self;
60
61    fn div(self, other: T) -> AngularVelocity {
62        AngularVelocity(self.0 / other.into())
63    }
64}