Skip to main content

ed_journals/modules/galaxy/models/
local_distance.rs

1use serde::{Deserialize, Serialize};
2use std::fmt::Debug;
3
4/// Model for working with local (system) distances. Expects the value to be in LS.
5#[derive(Serialize, Deserialize, Clone, PartialEq)]
6pub struct LocalDistance(pub f32);
7
8pub const LS_IN_M: f32 = 299792458.0;
9pub const LS_IN_AU: f32 = 500.0;
10
11impl LocalDistance {
12    /// Creates a new distance from the given amount of meters.
13    pub fn from_m(m: f32) -> Self {
14        LocalDistance(m / LS_IN_M)
15    }
16
17    /// Returns the distance in meters.
18    pub fn as_m(&self) -> f32 {
19        self.0 * LS_IN_M
20    }
21
22    /// Creates a new distance from the given amount of light seconds.
23    pub fn from_ls(ls: f32) -> Self {
24        LocalDistance(ls)
25    }
26
27    /// Returns the distance in light seconds.
28    pub fn as_ls(&self) -> f32 {
29        self.0
30    }
31
32    /// Creates a new distance from the given amount of astronomical units.
33    pub fn from_au(au: f32) -> Self {
34        LocalDistance(au * LS_IN_AU)
35    }
36
37    /// Returns the distance in astronomical units.
38    pub fn as_au(&self) -> f32 {
39        self.0 / LS_IN_AU
40    }
41}
42
43impl Debug for LocalDistance {
44    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45        write!(f, "{} ls ({} au / {} m)", self.0, self.as_au(), self.as_m())
46    }
47}