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
use super::iota_units::IotaUnits;

/// Converts an amount of iotas to a new unit
///
/// * `amount` - Amount to convert
/// * `from` - IotaUnit that `amount` is in
/// * `to` - Target IotaUnit
///```
/// extern crate iota_conversion;
/// use iota_conversion::{iota_units::IotaUnits, unit_converter};
///
/// let amount_in_new_unit = unit_converter::convert_units(1000, IotaUnits::TeraIota, IotaUnits::PetaIota);
/// assert_eq!(amount_in_new_unit, 1);
///```
pub fn convert_units(amount: u64, from: IotaUnits, to: IotaUnits) -> u64 {
    let amount_in_source = amount * 10_u64.pow(u32::from(from.value()));
    convert_units_helper(amount_in_source, to)
}

fn convert_units_helper(amount: u64, to: IotaUnits) -> u64 {
    amount / 10_u64.pow(u32::from(to.value()))
}

/// Converts an iota amount into the optimal unit for display
///
/// * `amount` - amount in base Iota unit
/// * `extended` - Whether to use two significant digests, or 15
///```
/// extern crate iota_conversion;
/// use iota_conversion::{iota_units::IotaUnits, unit_converter};
///
/// let s = unit_converter::convert_raw_iota_amount_to_display_text(1000000, false);
/// assert_eq!(s, "1.00 Mi");
///
/// let extended_s = unit_converter::convert_raw_iota_amount_to_display_text(1900000000000002, true);
/// assert_eq!(extended_s, "1.900000000000002 Pi");
///```
pub fn convert_raw_iota_amount_to_display_text(amount: u64, extended: bool) -> String {
    let unit = find_optimal_iota_unit_to_display(amount);
    let amount_in_display_unit = convert_amount_to(amount, unit);
    create_amount_with_unit_display_text(amount_in_display_unit, unit, extended)
}

fn create_amount_with_unit_display_text(amount: f64, unit: IotaUnits, extended: bool) -> String {
    if unit == IotaUnits::Iota {
        format!("{} {}", amount, unit.unit())
    } else if extended {
        format!("{:.15} {}", amount, unit.unit())
    } else {
        format!("{:.2} {}", amount, unit.unit())
    }
}

/// Converts an amount of iota to a unit
///
/// * `amount` - Amount in base Iota unit
/// * `target` - Target IotaUnit
///```
/// extern crate iota_conversion;
/// use iota_conversion::{iota_units::IotaUnits, unit_converter};
///
/// let unit = unit_converter::convert_amount_to(1000000, IotaUnits::GigaIota);
/// assert_eq!(unit, 0.001);
///```
pub fn convert_amount_to(amount: u64, target: IotaUnits) -> f64 {
    amount as f64 / 10_u64.pow(u32::from(target.value())) as f64
}

/// Finds the optimal unit for displaying an iota amount
///
/// * `amount` - Amount in base Iota unit
///```
/// extern crate iota_conversion;
/// use iota_conversion::{iota_units::IotaUnits, unit_converter};
///
/// let unit = unit_converter::find_optimal_iota_unit_to_display(1000000);
/// assert_eq!(unit, IotaUnits::MegaIota);
///```
pub fn find_optimal_iota_unit_to_display(amount: u64) -> IotaUnits {
    let length = amount.to_string().len();

    if length >= 1 && length <= 3 {
        IotaUnits::Iota
    } else if length > 3 && length <= 6 {
        IotaUnits::KiloIota
    } else if length > 6 && length <= 9 {
        IotaUnits::MegaIota
    } else if length > 9 && length <= 12 {
        IotaUnits::GigaIota
    } else if length > 12 && length <= 15 {
        IotaUnits::TeraIota
    } else if length > 15 && length <= 18 {
        IotaUnits::PetaIota
    } else {
        panic!("Invalid number")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_convert_unit_i_to_ki() {
        assert_eq!(1, convert_units(1000, IotaUnits::Iota, IotaUnits::KiloIota));
    }

    #[test]
    fn test_convert_unit_ki_to_mi() {
        assert_eq!(
            1,
            convert_units(1000, IotaUnits::KiloIota, IotaUnits::MegaIota)
        );
    }

    #[test]
    fn test_convert_unit_mi_to_gi() {
        assert_eq!(
            1,
            convert_units(1000, IotaUnits::MegaIota, IotaUnits::GigaIota)
        );
    }

    #[test]
    fn test_convert_unit_gi_to_ti() {
        assert_eq!(
            1,
            convert_units(1000, IotaUnits::GigaIota, IotaUnits::TeraIota)
        );
    }

    #[test]
    fn test_convert_unit_ti_to_pi() {
        assert_eq!(
            1,
            convert_units(1000, IotaUnits::TeraIota, IotaUnits::PetaIota)
        );
    }

    #[test]
    fn test_find_optimize_unit_to_display() {
        assert_eq!(find_optimal_iota_unit_to_display(1), IotaUnits::Iota);
        assert_eq!(find_optimal_iota_unit_to_display(1000), IotaUnits::KiloIota);
        assert_eq!(
            find_optimal_iota_unit_to_display(1000000),
            IotaUnits::MegaIota
        );
        assert_eq!(
            find_optimal_iota_unit_to_display(1000000000),
            IotaUnits::GigaIota
        );
        assert_eq!(
            find_optimal_iota_unit_to_display(1000000000000),
            IotaUnits::TeraIota
        );
        assert_eq!(
            find_optimal_iota_unit_to_display(1000000000000000),
            IotaUnits::PetaIota
        );
    }

    #[test]
    fn test_convert_raw_iota_amount_to_display_text() {
        assert_eq!(convert_raw_iota_amount_to_display_text(1, false), "1 i");
        assert_eq!(
            convert_raw_iota_amount_to_display_text(1000, false),
            "1.00 Ki"
        );
        assert_eq!(
            convert_raw_iota_amount_to_display_text(1000000, false),
            "1.00 Mi"
        );
        assert_eq!(
            convert_raw_iota_amount_to_display_text(1000000000, false),
            "1.00 Gi"
        );
        assert_eq!(
            convert_raw_iota_amount_to_display_text(1000000000000, false),
            "1.00 Ti"
        );
        assert_eq!(
            convert_raw_iota_amount_to_display_text(1000000000000000, false),
            "1.00 Pi"
        );
        assert_eq!(
            convert_raw_iota_amount_to_display_text(1900000000000002, true),
            "1.900000000000002 Pi"
        );
    }
}