anise/math/
units.rs

1/*
2 * ANISE Toolkit
3 * Copyright (C) 2021-onward Christopher Rabotin <christopher.rabotin@gmail.com> et al. (cf. AUTHORS.md)
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7 *
8 * Documentation: https://nyxspace.com/
9 */
10use core::fmt::Display;
11
12/// Re-export hifitime's units as DurationUnit.
13pub use hifitime::Unit as TimeUnit;
14
15/// Defines the distance units supported by ANISE. This notably allows storing interpolation information from instruments to comets.
16#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord)]
17pub enum LengthUnit {
18    Micrometer,
19    Millimeter,
20    Meter,
21    Kilometer,
22    Megameter,
23}
24
25impl LengthUnit {
26    /// Returns the conversion factor of this distance unit to meters.
27    /// E.g. To convert Self::Kilometers into Self::Meters, multiply by 1e-3.
28    #[must_use]
29    pub const fn to_meters(&self) -> f64 {
30        match self {
31            Self::Micrometer => 1e6,
32            Self::Millimeter => 1e3,
33            Self::Meter => 1.0,
34            Self::Kilometer => 1e-3,
35            Self::Megameter => 1e-6,
36        }
37    }
38
39    /// Returns the conversion factor of this distance unit from meters.
40    /// E.g. To convert Self::Kilometers into Self::Meters, multiply by 1e3.
41    #[must_use]
42    pub const fn from_meters(&self) -> f64 {
43        match self {
44            Self::Micrometer => 1e-6,
45            Self::Millimeter => 1e-3,
46            Self::Meter => 1.0,
47            Self::Kilometer => 1e3,
48            Self::Megameter => 1e6,
49        }
50    }
51}
52
53impl Display for LengthUnit {
54    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55        match self {
56            Self::Micrometer => write!(f, "um"),
57            Self::Millimeter => write!(f, "mm"),
58            Self::Meter => write!(f, "m"),
59            Self::Kilometer => write!(f, "km"),
60            Self::Megameter => write!(f, "Mm"),
61        }
62    }
63}
64
65impl Default for LengthUnit {
66    fn default() -> Self {
67        Self::Kilometer
68    }
69}