daedalus_data/
units.rs

1//! Units and scaling helpers.
2
3use serde::{Deserialize, Serialize};
4
5/// Supported units for typed metadata.
6///
7/// ```
8/// use daedalus_data::units::Unit;
9/// let unit = Unit::Seconds;
10/// assert_eq!(unit, Unit::Seconds);
11/// ```
12#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
13pub enum Unit {
14    Unitless,
15    Seconds,
16    Meters,
17    Bytes,
18    Celsius,
19    Fahrenheit,
20}
21
22/// Unit + scale pair (e.g. milliseconds as `Seconds` with `0.001` scale).
23///
24/// ```
25/// use daedalus_data::units::{Unit, UnitValue};
26/// let value = UnitValue::new(Unit::Seconds, 0.5);
27/// assert_eq!(value.scale, 0.5);
28/// ```
29#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
30pub struct UnitValue {
31    pub unit: Unit,
32    pub scale: f64,
33}
34
35impl UnitValue {
36    /// Construct a new unit value.
37    pub fn new(unit: Unit, scale: f64) -> Self {
38        Self { unit, scale }
39    }
40}