Skip to main content

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, Default)]
17pub enum LengthUnit {
18    Micrometer,
19    Millimeter,
20    Meter,
21    #[default]
22    Kilometer,
23    Megameter,
24}
25
26impl LengthUnit {
27    /// Returns the conversion factor of this distance unit to meters.
28    /// E.g. To convert Self::Kilometers into Self::Meters, multiply by 1e-3.
29    #[must_use]
30    pub const fn to_meters(&self) -> f64 {
31        match self {
32            Self::Micrometer => 1e6,
33            Self::Millimeter => 1e3,
34            Self::Meter => 1.0,
35            Self::Kilometer => 1e-3,
36            Self::Megameter => 1e-6,
37        }
38    }
39
40    /// Returns the conversion factor of this distance unit from meters.
41    /// E.g. To convert Self::Kilometers into Self::Meters, multiply by 1e3.
42    #[must_use]
43    pub const fn from_meters(&self) -> f64 {
44        match self {
45            Self::Micrometer => 1e-6,
46            Self::Millimeter => 1e-3,
47            Self::Meter => 1.0,
48            Self::Kilometer => 1e3,
49            Self::Megameter => 1e6,
50        }
51    }
52}
53
54impl Display for LengthUnit {
55    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56        match self {
57            Self::Micrometer => write!(f, "um"),
58            Self::Millimeter => write!(f, "mm"),
59            Self::Meter => write!(f, "m"),
60            Self::Kilometer => write!(f, "km"),
61            Self::Megameter => write!(f, "Mm"),
62        }
63    }
64}