Skip to main content

rialo_s_native_token/
lib.rs

1//! Definitions for the native RLO token and its fractional kelvins.
2
3#![allow(clippy::arithmetic_side_effects)]
4
5/// There are 10^9 kelvins in one RLO
6pub const KELVINS_PER_RLO: u64 = 1_000_000_000;
7
8/// Approximately convert fractional native tokens (kelvins) into native tokens (RLO)
9pub fn kelvins_to_rlo(kelvins: u64) -> f64 {
10    kelvins as f64 / KELVINS_PER_RLO as f64
11}
12
13/// Approximately convert native tokens (RLO) into fractional native tokens (kelvins)
14pub fn rlo_to_kelvins(rlo: f64) -> u64 {
15    (rlo * KELVINS_PER_RLO as f64) as u64
16}
17
18use std::fmt::{Debug, Display, Formatter, Result};
19pub struct Rlo(pub u64);
20
21impl Rlo {
22    fn write_in_rlo(&self, f: &mut Formatter<'_>) -> Result {
23        write!(
24            f,
25            "◎{}.{:09}",
26            self.0 / KELVINS_PER_RLO,
27            self.0 % KELVINS_PER_RLO
28        )
29    }
30}
31
32impl Display for Rlo {
33    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
34        self.write_in_rlo(f)
35    }
36}
37
38impl Debug for Rlo {
39    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
40        self.write_in_rlo(f)
41    }
42}