ed_journals/modules/galaxy/models/
local_distance.rs1use serde::{Deserialize, Serialize};
2use std::fmt::Debug;
3
4#[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 pub fn from_m(m: f32) -> Self {
14 LocalDistance(m / LS_IN_M)
15 }
16
17 pub fn as_m(&self) -> f32 {
19 self.0 * LS_IN_M
20 }
21
22 pub fn from_ls(ls: f32) -> Self {
24 LocalDistance(ls)
25 }
26
27 pub fn as_ls(&self) -> f32 {
29 self.0
30 }
31
32 pub fn from_au(au: f32) -> Self {
34 LocalDistance(au * LS_IN_AU)
35 }
36
37 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}