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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use core::fmt::Debug;
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};

use crate::errors::RuntimeUnitError;
use crate::units_base::UnitDefinition;
use crate::Units;

pub(crate) trait IsQuantity
{
    fn value(&self) -> f64;
    fn definition(&self) -> UnitDefinition;
}

#[doc = "A quantity of a unit, supports converting from one unit to another." ]
#[derive(Copy, Clone)]
pub struct Quantity
{
    pub(crate) value: f64,
    pub(crate) unit: UnitDefinition
}
impl Quantity
{
    ///
    /// Create a new instance of `Quantity` with a given `value` and `unit` 
    ///
    pub fn new(value: f64, unit: UnitDefinition) -> Self
    {        
        Self { value, unit }
    }

    ///
    /// Amount of unit stored in quantity
    /// 
    #[inline]    
    pub fn value(&self) -> f64
    {
        self.value
    }

    ///
    /// Get mutable reference to the value for this quantity.
    /// 
    #[inline]    
    pub fn value_mut(&mut self) -> &mut f64
    {
        &mut self.value
    }

    ///
    /// Retrieve a unit with a corresponding multiplier
    /// 
    #[inline]
    pub fn definition(&self) -> UnitDefinition
    {
        self.unit
    }

    ///
    /// Get mutable reference to the definition for this quantity.
    /// 
    #[inline]
    pub fn definition_mut(&mut self) -> &mut UnitDefinition
    {
        &mut self.unit
    }
    ///
    /// Convert a quantity from one unit to another
    ///
    #[inline]
    pub fn convert(&self, unit: Units) -> Result<Quantity, RuntimeUnitError>
    {
        self.convert_unit(unit.into())
    }    
    #[inline]
    pub fn convert_unit(&self, unit: UnitDefinition) -> Result<Quantity, RuntimeUnitError>
    {
        if self.unit == unit
        {
            Ok(*self)
        }
        else
        {
            if self.unit.is_convertible(unit)
            {
                Ok(Self { value: self.value * self.unit.multiplier / unit.multiplier(), unit })
            }
            else
            {
                Err(RuntimeUnitError::IncompatibleUnitConversion(format!("Could not convert from base units of {} to {}", self.unit.unit_string(), unit.unit_string())))
            }
        }
    }   

    #[inline] 
    /// Convert from one unit to another (no check is made to ensure destination unit is valid).
    pub(crate) fn convert_unchecked(&self, unit: UnitDefinition) -> f64
    {
        if self.unit == unit
        {
            self.value
        }
        else
        {
            self.value * self.unit.multiplier / unit.multiplier()
        }
    }

}
impl Debug for Quantity
{
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result 
    {
        write!(f, "{} {:?}", self.value(), self.definition())
    }
}
impl Mul<f64> for Quantity
{
    type Output = Quantity;

    fn mul(self, rhs: f64) -> Self::Output {
        Self{ value: self.value*rhs, unit: self.unit }
    }
}

impl Div<f64> for Quantity
{
    type Output = Quantity;

    fn div(self, rhs: f64) -> Self::Output {
        Self{ value: self.value/rhs, unit: self.unit }
    }
}
impl Mul<Quantity> for Quantity
{
    type Output = Quantity;

    fn mul(self, rhs: Quantity) -> Self::Output {
        Self{ value: self.value*rhs.value, unit: self.unit*rhs.unit }
    }
}
impl Div<Quantity> for Quantity
{
    type Output = Quantity;

    fn div(self, rhs: Quantity) -> Self::Output {
        Self{ value: self.value/rhs.value, unit: self.unit/rhs.unit }
    }
}
impl Add<Quantity> for Quantity
{
    type Output=Quantity;

    fn add(self, rhs: Quantity) -> Self::Output {
        Self { value: self.value + rhs.value, unit: self.unit }
    }
}
impl Sub<Quantity> for Quantity
{
    type Output=Quantity;

    fn sub(self, rhs: Quantity) -> Self::Output {
        Self { value: self.value - rhs.value, unit: self.unit }
    }
}

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

impl SubAssign for Quantity
{
    fn sub_assign(&mut self, rhs: Self) {
        self.value -= rhs.value;
    }
}

impl MulAssign for Quantity
{
    fn mul_assign(&mut self, rhs: Self) {
        self.value *= rhs.value;
        self.unit *= rhs.unit;
    }
}


impl DivAssign for Quantity
{
    fn div_assign(&mut self, rhs: Self) {
        self.value /= rhs.value;
        self.unit /= rhs.unit;
    }
}

impl<T:IsQuantity> Mul<T> for Quantity
{
    type Output = Quantity;
    fn mul(self, rhs: T) -> Quantity {
        Quantity{ value: self.value*rhs.value(), unit: self.definition()*rhs.definition() }
    }
}
impl<T:IsQuantity> Div<T> for Quantity
{
    type Output = Quantity;

    fn div(self, rhs: T) -> Quantity {
        Quantity{ value: self.value/rhs.value(), unit: self.definition()/rhs.definition() }
    }
}
///
/// This only compares magnitudes...
/// 
impl PartialOrd<Quantity> for Quantity
{
    fn partial_cmp(&self, other: &Quantity) -> Option<core::cmp::Ordering> {
        self.convert_unchecked(other.unit).partial_cmp(&other.value)
    }
}

impl PartialEq for Quantity
{
    fn eq(&self, other: &Self) -> bool {
        self.unit.base == other.unit.base && self.convert_unchecked(other.unit) == other.value
    }
}