1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
use std::fmt::{self, Display, Formatter};

#[derive(Debug, Clone, Copy, PartialEq)] pub struct Meters(pub f32);
impl Meters {
    pub fn as_f32(&self) -> f32 {
        self.0
    }
    pub fn as_f64(&self) -> f64 {
        self.0 as f64
    }
}
impl Display for Meters {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{}\u{202f}m", self.0)
    }
}
#[derive(Debug, Clone, Copy, PartialEq)] pub struct MetersPerSecond(pub f32);
impl MetersPerSecond {
    pub fn as_f32(&self) -> f32 {
        self.0
    }
    pub fn as_f64(&self) -> f64 {
        self.0 as f64
    }
}
impl Display for MetersPerSecond {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{}\u{202f}m/s", self.0)
    }
}
#[derive(Debug, Clone, Copy, PartialEq)] pub struct Radians(pub f32);
impl Radians {
    pub fn as_f32(&self) -> f32 {
        self.0
    }
    pub fn as_f64(&self) -> f64 {
        self.0 as f64
    }
}
impl Display for Radians {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}
#[derive(Debug, Clone, Copy, PartialEq)] pub struct Celsius(pub f32);
impl Celsius {
    pub fn as_f32(&self) -> f32 {
        self.0
    }
    pub fn as_f64(&self) -> f64 {
        self.0 as f64
    }
}
impl Display for Celsius {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{}°C", self.0)
    }
}


#[derive(Debug, Clone, Copy, PartialEq)] pub struct Feet(pub f32);
impl Feet {
    pub fn as_f32(&self) -> f32 {
        self.0
    }
    pub fn as_f64(&self) -> f64 {
        self.0 as f64
    }
    #[deprecated(since="0.2.0", note="use `Feet::from(Meters(v))` instead")]
    pub fn from_meters(v : f32) -> Feet {
        Feet::from(Meters(v))
    }
}
impl Display for Feet {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{}\u{202f}ft", self.0)
    }
}
const FEET_IN_METER : f32 = 3.28084;
impl From<Meters> for Feet {
    fn from(v : Meters) -> Self {
        Feet(v.0 * FEET_IN_METER)
    }
}
impl From<Feet> for Meters {
    fn from(v : Feet) -> Self {
        Meters(v.0 / FEET_IN_METER)
    }
}

#[derive(Debug, Clone, Copy, PartialEq)] pub struct Knots(pub f32);
impl Knots {
    pub fn as_f32(&self) -> f32 {
        self.0
    }
    pub fn as_f64(&self) -> f64 {
        self.0 as f64
    }
    #[deprecated(since="0.2.0", note="use `Knots::from(KilometersPerHour(v))` instead")]
    pub fn from_kmh(v : f32) -> Knots {
        Knots(0.539957*v)
    }
}
impl Display for Knots {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{}\u{202f}kn", self.0)
    }
}
const KNOTS_IN_MPS : f32 = 1.94384;
impl From<MetersPerSecond> for Knots {
    fn from(v : MetersPerSecond) -> Self {
        Knots(v.0 * KNOTS_IN_MPS)
    }
}
impl From<KilometersPerHour> for Knots {
    fn from(v : KilometersPerHour) -> Self {
        Knots::from(MetersPerSecond::from(KilometersPerHour(v.0)))
    }
}
impl From<Knots> for MetersPerSecond {
    fn from(v : Knots) -> Self {
        MetersPerSecond(v.0 / KNOTS_IN_MPS)
    }
}

#[derive(Debug, Clone, Copy, PartialEq)] pub struct MilesPerHour(pub f32);
impl MilesPerHour {
    pub fn as_f32(&self) -> f32 {
        self.0
    }
    pub fn as_f64(&self) -> f64 {
        self.0 as f64
    }
}
impl Display for MilesPerHour {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{}\u{202f}mph", self.0)
    }
}
const MPH_IN_MPS : f32 = 2.23694;
impl From<MetersPerSecond> for MilesPerHour {
    fn from(v : MetersPerSecond) -> Self {
        MilesPerHour(v.0 * MPH_IN_MPS)
    }
}
impl From<MilesPerHour> for MetersPerSecond {
    fn from(v : MilesPerHour) -> Self {
        MetersPerSecond(v.0 / MPH_IN_MPS)
    }
}

#[derive(Debug, Clone, Copy, PartialEq)] pub struct KilometersPerHour(pub f32);
impl KilometersPerHour {
    pub fn as_f32(&self) -> f32 {
        self.0
    }
    pub fn as_f64(&self) -> f64 {
        self.0 as f64
    }
}
impl Display for KilometersPerHour {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{}\u{202f}km/h", self.0)
    }
}
const KMH_IN_MPS : f32 = 3.6;
impl From<MetersPerSecond> for KilometersPerHour {
    fn from(v : MetersPerSecond) -> Self {
        KilometersPerHour(v.0 * KMH_IN_MPS)
    }
}
impl From<KilometersPerHour> for MetersPerSecond {
    fn from(v : KilometersPerHour) -> Self {
        MetersPerSecond(v.0 / KMH_IN_MPS)
    }
}

#[derive(Debug, Clone, Copy, PartialEq)] pub struct Degrees(pub f32);
impl Degrees {
    pub fn as_f32(&self) -> f32 {
        self.0
    }
    pub fn as_f64(&self) -> f64 {
        self.0 as f64
    }
}
impl Display for Degrees {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{}°", self.0)
    }
}
impl From<Radians> for Degrees {
    fn from(v : Radians) -> Self {
        Degrees(v.0.to_degrees())
    }
}
impl From<Degrees> for Radians {
    fn from(v : Degrees) -> Self {
        Radians(v.0.to_radians())
    }
}

#[derive(Debug, Clone, Copy, PartialEq)] pub struct Fahrenheits(pub f32);
impl Fahrenheits {
    pub fn as_f32(&self) -> f32 {
        self.0
    }
    pub fn as_f64(&self) -> f64 {
        self.0 as f64
    }
}
impl Display for Fahrenheits {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{}°F", self.0)
    }
}
impl From<Fahrenheits> for Celsius {
    fn from(v : Fahrenheits) -> Self {
        Celsius( 5.0*(v.0 - 32.0)/9.0 )
    }
}
impl From<Celsius> for Fahrenheits {
    fn from(v : Celsius) -> Self {
        Fahrenheits( 32.0 + 9.0*v.0/5.0 )
    }
}


#[cfg(test)]
mod tests {
    use super::{Meters, Feet, Knots, MetersPerSecond, KilometersPerHour, MilesPerHour, Celsius, Fahrenheits};
    const EPSILON : f32 = 0.0001;

    #[test]
    fn test_as_f64() {
         assert_eq!(Meters(1.0).as_f64(), 1.0);
         assert_eq!(Knots(1.0).as_f64(), 1.0);
    }

    #[test]
    fn test_feet_to_meters() {
         assert_abs_diff_eq!(Meters::from(Feet(100.0)).0, Meters(30.48).0, epsilon=EPSILON);
         assert_abs_diff_eq!(Feet::from(Meters(30.48)).0, Feet(100.0).0, epsilon=EPSILON);
    }

    #[test]
    fn test_knots_to_mps() {
         assert_abs_diff_eq!(MetersPerSecond::from(Knots(20.0)).0, MetersPerSecond(10.2889).0, epsilon=EPSILON);
         assert_abs_diff_eq!(Knots::from(MetersPerSecond(10.2889)).0, Knots(20.0).0, epsilon=EPSILON);
    }

    #[test]
    fn test_kmh_to_mps() {
         assert_abs_diff_eq!(MetersPerSecond::from(KilometersPerHour(90.0)).0, MetersPerSecond(25.0).0, epsilon=EPSILON);
         assert_abs_diff_eq!(KilometersPerHour::from(MetersPerSecond(25.0)).0, Knots(90.0).0, epsilon=EPSILON);
    }

    #[test]
    fn test_mph_to_mps() {
         assert_abs_diff_eq!(MetersPerSecond::from(MilesPerHour(55.9234)).0, MetersPerSecond(25.0).0, epsilon=EPSILON);
         assert_abs_diff_eq!(MilesPerHour::from(MetersPerSecond(25.0)).0, Knots(55.9234).0, epsilon=EPSILON);
    }

    #[test]
    fn test_mph_to_kmh() {
         assert_abs_diff_eq!(KilometersPerHour::from(MetersPerSecond::from(MilesPerHour(60.0))).0, KilometersPerHour(96.5605).0, epsilon=EPSILON);
         assert_abs_diff_eq!(MilesPerHour::from(MetersPerSecond::from(KilometersPerHour(96.5605))).0, MilesPerHour(60.0).0, epsilon=EPSILON);
    }

    #[test]
    fn test_kmh_to_knots() {
         //assert_abs_diff_eq!(KilometersPerHour::from(Knots(30.0)).0, KilometersPerHour(55.56).0, epsilon=EPSILON);
         assert_abs_diff_eq!(Knots::from(KilometersPerHour(55.56)).0, Knots(30.0).0, epsilon=EPSILON);
    }

    #[test]
    fn test_celsius_to_fahrenheits() {
         assert_abs_diff_eq!(Celsius::from(Fahrenheits(80.0)).0, Celsius(26.6667).0, epsilon=EPSILON);
         assert_abs_diff_eq!(Fahrenheits::from(Celsius(26.6667)).0, Fahrenheits(80.0).0, epsilon=EPSILON);
    }

    #[test]
    fn into_itself() {
        let x : Meters = Meters(5.0).into();
        assert_eq!(x.0, 5.0);
    }

    #[test]
    fn convert_into() {
        let x : Meters = Feet(100.0).into();
        assert_eq!(x.0, 30.48);

        let x : Feet = Meters(30.48).into();
        assert_eq!(x.0, 100.0);
    }

    #[test]
    fn test_debug_fmt() {
         assert_eq!(format!("{:?}", Meters(5.0)), "Meters(5.0)");
    }

    #[test]
    fn test_display() {
        assert_eq!(format!("{}", Meters(5.0)), "5 m");
        assert_eq!(format!("{}", Fahrenheits(5.0)), "5°F");
    }

    #[test] #[allow(deprecated)]
    fn test_deprecated() {
        assert_eq!(Feet::from_meters(100.0), Feet(328.08398));
    }

}