1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use std::{
    fmt::Display,
    ops::{Add, AddAssign, Mul, Sub},
};

use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};

use super::number::Number;

/// A value of kilo watt hours.
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone, Copy, PartialOrd, Ord, Default)]
#[serde(transparent)]
pub struct Kwh(Number);

impl Kwh {
    pub(crate) fn zero() -> Self {
        Self(Number::default())
    }

    pub(crate) fn watt_hours(self) -> Number {
        self.0 * Number::from(dec!(1000.0))
    }

    pub(crate) fn from_watt_hours(num: Number) -> Self {
        Self(num / dec!(1000.0).into())
    }

    /// Round this number to the OCPI specified amount of decimals.
    pub fn with_scale(self) -> Self {
        Self(self.0.with_scale())
    }
}

impl From<Kwh> for Number {
    fn from(value: Kwh) -> Self {
        value.0
    }
}

impl Display for Kwh {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:.4}", self.0)
    }
}

impl Add for Kwh {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Self(self.0 + rhs.0)
    }
}

impl AddAssign for Kwh {
    fn add_assign(&mut self, rhs: Self) {
        self.0 = self.0 + rhs.0;
    }
}

impl Sub for Kwh {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        Self(self.0 - rhs.0)
    }
}

impl Mul<Number> for Kwh {
    type Output = Self;

    fn mul(self, rhs: Number) -> Self::Output {
        Self(self.0 * rhs)
    }
}

/// A value of kilo watts.
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone, Copy, PartialOrd, Ord)]
#[serde(transparent)]
pub struct Kw(Number);

/// A value of amperes.
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone, Copy, PartialOrd, Ord)]
#[serde(transparent)]
pub struct Ampere(Number);

impl From<Number> for Ampere {
    fn from(value: Number) -> Self {
        Self(value)
    }
}