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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
use arrayvec::ArrayString;
use rust_decimal::prelude::ToPrimitive;
use rust_decimal::prelude::Zero;
use rust_decimal::Decimal;
#[cfg(feature = "serde-support")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
use std::str::FromStr;
use thiserror::Error;

/// The length of the [CurrencyCodeArray](CurrencyCodeArray) type,
/// used to store the code/id for a given [Currency](Currency) in
/// [CurrencyCode](CurrencyCode).
pub const CURRENCY_CODE_LENGTH: usize = 8;

/// The type used to store the value of a [CurrencyCode](CurrencyCode).
/// This array backed string has a fixed maximum size
/// of [CURRENCY_CODE_LENGTH](CURRENCY_CODE_LENGTH).
type CurrencyCodeArray = ArrayString<[u8; CURRENCY_CODE_LENGTH]>;

/// An error associated with functionality in the [commodity](./index.html) module.
#[derive(Error, Debug, PartialEq)]
pub enum CommodityError {
    #[error("This commodity {this_commodity:?} is incompatible with {other_commodity:?} because {reason:?}")]
    IncompatableCommodity {
        this_commodity: Commodity,
        other_commodity: Commodity,
        reason: String,
    },
    #[error(
        "The currency code {0} is too long. Maximum of {} characters allowed.",
        CURRENCY_CODE_LENGTH
    )]
    TooLongCurrencyCode(String),
    #[error("The provided alpha3 code {0} doesn't match any in the iso4217 database")]
    InvalidISO4217Alpha3(String),
    #[error("The provided string {0} is invalid, it should be a decimal followed by a currency. e.g. 1.234 USD")]
    InvalidCommodityString(String),
}

/// Represents a the type of currency held in a
/// [Commodity](Commodity). See [CurrencyCode](CurrencyCode) for the
/// primative which is genarally stored and used to refer to a given
/// [Currency](Currency).
#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct Currency {
    /// Stores the code/id of this currency in a fixed length
    /// [ArrayString](ArrayString), with a maximum length of
    /// [CURRENCY_CODE_LENGTH](CURRENCY_CODE_LENGTH).
    pub code: CurrencyCode,
    /// The human readable name of this currency.
    pub name: Option<String>,
}

impl Currency {
    /// Create a new [Currency](Currency)
    ///
    /// # Example
    /// ```
    /// # use commodity::{Currency, CurrencyCode};
    /// use std::str::FromStr;
    ///
    /// let code = CurrencyCode::from_str("AUD").unwrap();
    /// let currency = Currency::new(
    ///     code,
    ///     Some(String::from("Australian Dollar"))
    /// );
    ///
    /// assert_eq!(code, currency.code);
    /// assert_eq!(Some(String::from("Australian Dollar")), currency.name);
    /// ```
    pub fn new(code: CurrencyCode, name: Option<String>) -> Currency {
        Currency { code, name }
    }

    /// Create a [Currency](Currency) from strings, usually for
    /// debugging, or unit testing purposes.
    ///
    /// `code` is an array backed string that has a fixed maximum size
    /// of [CURRENCY_CODE_LENGTH](CURRENCY_CODE_LENGTH). The supplied
    /// string must not exeed this, or a
    /// [CommodityError::TooLongCurrencyCode](CommodityError::TooLongCurrencyCode)
    /// will be returned.
    ///
    /// # Example
    /// ```
    /// # use commodity::{Currency, CurrencyCode};
    /// use std::str::FromStr;
    ///
    /// let currency = Currency::from_str("AUD", "Australian dollar").unwrap();
    ///
    /// assert_eq!(CurrencyCode::from_str("AUD").unwrap(), currency.code);
    /// assert_eq!("Australian dollar", currency.name.unwrap());
    /// ```
    pub fn from_str(code: &str, name: &str) -> Result<Currency, CommodityError> {
        let code = CurrencyCode::from_str(code)?;

        let name = if name.len() == 0 {
            None
        } else {
            Some(String::from(name))
        };

        Ok(Currency::new(code, name))
    }

    /// Construct a [Currency](Currency) by looking it up in the iso4217
    /// currency database.
    ///
    /// # Example
    /// ```
    /// # use commodity::Currency;
    ///
    /// let currency = Currency::from_alpha3("AUD").unwrap();
    /// assert_eq!("AUD", currency.code);
    /// assert_eq!(Some(String::from("Australian dollar")), currency.name);
    /// ```
    pub fn from_alpha3(alpha3: &str) -> Result<Currency, CommodityError> {
        match iso4217::alpha3(alpha3) {
            Some(code) => Currency::from_str(alpha3, code.name),
            None => Err(CommodityError::InvalidISO4217Alpha3(String::from(alpha3))),
        }
    }
}

/// Return a vector of all iso4217 currencies
pub fn all_iso4217_currencies() -> Vec<Currency> {
    let mut currencies = Vec::new();
    for iso_currency in iso4217::all() {
        currencies.push(Currency::from_str(iso_currency.alpha3, iso_currency.name).unwrap());
    }

    return currencies;
}

/// The code/id of a [Currency](Currency).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct CurrencyCode {
    /// This is a fixed length array of characters of length [CURRENCY_CODE_LENGTH](CURRENCY_CODE_LENGTH),
    /// with a backing implementation based on [ArrayString](ArrayString).
    code_array: CurrencyCodeArray,
}

impl CurrencyCode {
    /// Create a new [CurrencyCode](CurrencyCode).
    pub fn new(code_array: CurrencyCodeArray) -> CurrencyCode {
        CurrencyCode { code_array }
    }
}

impl FromStr for CurrencyCode {
    type Err = CommodityError;

    /// Create a new [Currency](Currency).
    ///
    /// `code` is an array backed string that has a fixed maximum size
    /// of [CURRENCY_CODE_LENGTH](CURRENCY_CODE_LENGTH). The supplied
    /// string must not exeed this, or a
    /// [CommodityError::TooLongCurrencyCode](CommodityError::TooLongCurrencyCode)
    /// will be returned.
    ///
    /// # Example
    /// ```
    /// # use commodity::CurrencyCode;
    /// use std::str::FromStr;
    /// let currency_code = CurrencyCode::from_str("AUD").unwrap();
    /// assert_eq!("AUD", currency_code);
    /// ```
    fn from_str(code: &str) -> Result<CurrencyCode, CommodityError> {
        if code.len() > CURRENCY_CODE_LENGTH {
            return Err(CommodityError::TooLongCurrencyCode(String::from(code)));
        }

        return Ok(CurrencyCode::new(CurrencyCodeArray::from(code).unwrap()));
    }
}

impl From<&Currency> for CurrencyCode {
    fn from(currency: &Currency) -> CurrencyCode {
        currency.code
    }
}

#[cfg(feature = "serde-support")]
impl<'de> Deserialize<'de> for CurrencyCode {
    fn deserialize<D>(deserializer: D) -> std::result::Result<CurrencyCode, D::Error>
    where
        D: Deserializer<'de>,
    {
        use serde::de::{self, Visitor};

        struct CurrencyCodeVisitor;

        impl<'de> Visitor<'de> for CurrencyCodeVisitor {
            type Value = CurrencyCode;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str(
                    format!(
                        "a string with a maximum of {} characters",
                        CURRENCY_CODE_LENGTH
                    )
                    .as_ref(),
                )
            }

            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                CurrencyCode::from_str(v).map_err(|e| {
                    E::custom(format!(
                        "there was an error ({}) parsing the currency code string",
                        e
                    ))
                })
            }
        }

        deserializer.deserialize_str(CurrencyCodeVisitor)
    }
}

#[cfg(feature = "serde-support")]
impl Serialize for CurrencyCode {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&self.code_array)
    }
}

impl PartialEq<CurrencyCode> for &str {
    fn eq(&self, other: &CurrencyCode) -> bool {
        match CurrencyCodeArray::from_str(self) {
            Ok(self_as_code) => self_as_code == other.code_array,
            Err(_) => false,
        }
    }
}

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

/// A commodity, which holds a value of a type of [Currrency](Currency)
#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Commodity {
    pub value: Decimal,
    pub currency_code: CurrencyCode,
}

/// Check whether the currencies of two commodities are compatible (the same),
/// if they aren't then return a [IncompatableCommodity](CurrencyError::IncompatableCommodity) error in the `Result`.
fn check_currency_compatible(
    this_commodity: &Commodity,
    other_commodity: &Commodity,
    reason: String,
) -> Result<(), CommodityError> {
    if !this_commodity.compatible_with(other_commodity) {
        return Err(CommodityError::IncompatableCommodity {
            this_commodity: this_commodity.clone(),
            other_commodity: other_commodity.clone(),
            reason,
        });
    }

    return Ok(());
}

impl Commodity {
    /// Create a new [Commodity](Commodity).
    ///
    /// # Example
    ///
    /// ```
    /// # use commodity::{Commodity};
    /// use commodity::CurrencyCode;
    /// use std::str::FromStr;
    /// use rust_decimal::Decimal;
    ///
    /// let currency_code = CurrencyCode::from_str("USD").unwrap();
    /// let commodity = Commodity::new(Decimal::new(202, 2), currency_code);
    ///
    /// assert_eq!(Decimal::from_str("2.02").unwrap(), commodity.value);
    /// assert_eq!(currency_code, commodity.currency_code)
    /// ```
    ///
    /// Using using the `Into` trait to accept `Currency` as the `currency_code`:
    /// ```
    /// # use commodity::{Commodity};
    /// use std::str::FromStr;
    /// use commodity::Currency;
    /// use rust_decimal::Decimal;
    ///
    /// let currency = Currency::from_alpha3("USD").unwrap();
    /// let commodity = Commodity::new(Decimal::new(202, 2), &currency);
    /// ```
    pub fn new<T: Into<CurrencyCode>>(value: Decimal, currency_code: T) -> Commodity {
        Commodity {
            currency_code: currency_code.into(),
            value,
        }
    }

    /// Create a commodity with a value of zero
    pub fn zero(currency_code: CurrencyCode) -> Commodity {
        Commodity::new(Decimal::zero(), currency_code)
    }

    /// Add the value of commodity `other` to `self`
    /// such that `result = self + other`.
    ///
    /// # Example
    /// ```
    /// # use commodity::{Commodity, CurrencyCode};
    /// use rust_decimal::Decimal;
    /// use std::str::FromStr;
    ///
    /// let currency_code = CurrencyCode::from_str("USD").unwrap();
    /// let commodity1 = Commodity::new(Decimal::new(400, 2), currency_code);
    /// let commodity2 = Commodity::new(Decimal::new(250, 2), currency_code);
    ///
    /// // perform the add
    /// let result = commodity1.add(&commodity2).unwrap();
    ///
    /// assert_eq!(Decimal::new(650, 2), result.value);
    /// assert_eq!(currency_code, result.currency_code);
    /// ```
    pub fn add(&self, other: &Commodity) -> Result<Commodity, CommodityError> {
        check_currency_compatible(
            self,
            other,
            String::from("cannot add commodities with different currencies"),
        )?;

        return Ok(Commodity::new(self.value + other.value, self.currency_code));
    }

    /// Subtract the value of commodity `other` from `self`
    /// such that `result = self - other`.
    ///
    /// # Example
    /// ```
    /// # use commodity::{Commodity, CurrencyCode};
    /// use rust_decimal::Decimal;
    /// use std::str::FromStr;
    ///
    /// let currency_code = CurrencyCode::from_str("USD").unwrap();
    /// let commodity1 = Commodity::new(Decimal::new(400, 2), currency_code);
    /// let commodity2 = Commodity::new(Decimal::new(250, 2), currency_code);
    ///
    /// // perform the subtraction
    /// let result = commodity1.sub(&commodity2).unwrap();
    ///
    /// assert_eq!(Decimal::new(150, 2), result.value);
    /// assert_eq!(currency_code, result.currency_code);
    /// ```
    pub fn sub(&self, other: &Commodity) -> Result<Commodity, CommodityError> {
        check_currency_compatible(
            self,
            other,
            String::from("cannot subtract commodities with different currencies"),
        )?;

        return Ok(Commodity::new(self.value - other.value, self.currency_code));
    }

    /// Negate the value of this commodity such that `result = -self`
    ///
    /// # Example
    /// ```
    /// # use commodity::{Commodity, CurrencyCode};
    /// # use std::str::FromStr;
    /// use rust_decimal::Decimal;
    ///
    /// let currency_code = CurrencyCode::from_str("USD").unwrap();
    /// let commodity = Commodity::new(Decimal::new(202, 2), currency_code);
    ///
    /// // perform the negation
    /// let result = commodity.neg();
    ///
    /// assert_eq!(Decimal::from_str("-2.02").unwrap(), result.value);
    /// assert_eq!(currency_code, result.currency_code)
    /// ```
    pub fn neg(&self) -> Commodity {
        Commodity::new(-self.value, self.currency_code)
    }

    /// Divide this commodity by the specified integer value
    ///
    /// # Example
    /// ```
    /// # use commodity::{Commodity};
    /// use rust_decimal::{Decimal};
    /// use std::str::FromStr;
    ///
    /// let commodity = Commodity::from_str("4.03 AUD").unwrap();
    /// let result = commodity.div_i64(4);
    /// assert_eq!(Decimal::new(10075, 4), result.value);
    /// ```
    pub fn div_i64(&self, i: i64) -> Commodity {
        let decimal = Decimal::new(i * 100, 2);
        Commodity::new(self.value / decimal, self.currency_code)
    }

    /// Divide this commodity by the specified integer value
    ///
    /// # Example
    /// ```
    /// # use commodity::{Commodity};
    /// use rust_decimal::{Decimal};
    /// use std::str::FromStr;
    ///
    /// let commodity = Commodity::from_str("4.03 AUD").unwrap();
    /// let results = commodity.divide_share(4, 2);
    ///
    /// assert_eq!(Decimal::new(101, 2), results.get(0).unwrap().value);
    /// assert_eq!(Decimal::new(101, 2), results.get(1).unwrap().value);
    /// assert_eq!(Decimal::new(101, 2), results.get(2).unwrap().value);
    /// assert_eq!(Decimal::new(100, 2), results.get(3).unwrap().value);
    /// ```
    pub fn divide_share(&self, i: i64, dp: u32) -> Vec<Commodity> {
        // TODO: rework this algorithm
        //
        // Consider the following idea:
        // Use the normal divide, then round it. Sum it up, and
        // subtract this from the original number, to get the
        // remainder. Add the remainder one digit at a time to the
        // resulting shares.

        let mut commodities: Vec<Commodity> = Vec::new();
        let divisor = Decimal::new(i * 10_i64.pow(dp), dp);
        let remainder = self.value % divisor;
        // = 0.03

        let divided = self.value / divisor;
        // 4.03 / 0.04 = 100.75
        // divided.set_scale(dp * 2).unwrap();
        // = 1.0075
        let truncated = divided.trunc();
        // = 1.00

        let dp_divisor = Decimal::new(1, dp);

        let remainder_bits = (remainder / dp_divisor).to_i64().unwrap();
        let remainder_bits_abs = remainder_bits.abs();
        let i_abs = i.abs();

        // dbg!(self.value);
        // dbg!(i);
        // dbg!(divided);
        // dbg!(truncated);
        // dbg!(remainder_bits);
        // dbg!(remainder);

        let sign = Decimal::new(remainder_bits.signum() * i.signum(), 0);

        for commodity_index in 1..=i_abs {
            let value = if commodity_index <= remainder_bits_abs {
                truncated + dp_divisor * sign
            } else {
                truncated
            };

            commodities.push(Commodity::new(value, self.currency_code))
        }

        dbg!(commodities.clone());

        return commodities;
    }

    /// Convert this commodity to a different currency using a conversion rate.
    ///
    /// # Example
    /// ```
    /// # use commodity::{Commodity, CurrencyCode};
    /// use rust_decimal::Decimal;
    /// use std::str::FromStr;
    ///
    /// let aud = Commodity::from_str("100.00 AUD").unwrap();
    /// let usd = aud.convert(CurrencyCode::from_str("USD").unwrap(), Decimal::from_str("0.01").unwrap());
    ///
    /// assert_eq!(Decimal::from_str("1.00").unwrap(), usd.value);
    /// assert_eq!("USD", usd.currency_code);
    /// ```
    pub fn convert(&self, currency_code: CurrencyCode, rate: Decimal) -> Commodity {
        Commodity::new(self.value * rate, currency_code)
    }

    /// Returns true if the currencies of both this commodity, and
    /// the `other` commodity are compatible for numeric operations.
    ///
    /// # Example
    /// ```
    /// # use commodity::{Commodity};
    /// use std::str::FromStr;
    ///
    /// let aud1 = Commodity::from_str("1.0 AUD").unwrap();
    /// let aud2 = Commodity::from_str("2.0 AUD").unwrap();
    /// let nzd = Commodity::from_str("1.0 NZD").unwrap();
    ///
    /// assert!(aud1.compatible_with(&aud2));
    /// assert!(!aud1.compatible_with(&nzd));
    /// ```
    pub fn compatible_with(&self, other: &Commodity) -> bool {
        return self.currency_code == other.currency_code;
    }

    /// Compare whether this commodity has a value less than another commodity.
    ///
    /// Will return an error if the commodities have incompatible currencies.
    ///
    /// # Example
    /// ```
    /// # use commodity::{Commodity};
    /// use std::str::FromStr;
    ///
    /// let aud1 = Commodity::from_str("1.0 AUD").unwrap();
    /// let aud2 = Commodity::from_str("2.0 AUD").unwrap();
    ///
    /// assert_eq!(true, aud1.lt(&aud2).unwrap());
    /// assert_eq!(false, aud2.lt(&aud1).unwrap());
    /// ```
    pub fn lt(&self, other: &Commodity) -> Result<bool, CommodityError> {
        check_currency_compatible(
            self,
            other,
            String::from("cannot compare commodities with different currencies"),
        )?;

        Ok(self.value < other.value)
    }

    /// Compare whether this commodity has a value greater than another commodity.
    ///
    /// Will return an error if the commodities have incompatible currencies.
    ///
    /// # Example
    /// ```
    /// # use commodity::{Commodity};
    /// use std::str::FromStr;
    ///
    /// let aud1 = Commodity::from_str("1.0 AUD").unwrap();
    /// let aud2 = Commodity::from_str("2.0 AUD").unwrap();
    ///
    /// assert_eq!(false, aud1.gt(&aud2).unwrap());
    /// assert_eq!(true, aud2.gt(&aud1).unwrap());
    /// ```
    pub fn gt(&self, other: &Commodity) -> Result<bool, CommodityError> {
        check_currency_compatible(
            self,
            other,
            String::from("cannot compare commodities with different currencies"),
        )?;

        Ok(self.value > other.value)
    }

    /// Return the absolute value of this commodity (if the value is
    /// negative, then make it positive).
    ///
    /// # Example
    /// ```
    /// # use commodity::{Commodity};
    /// use std::str::FromStr;
    ///
    /// let aud1 = Commodity::from_str("-1.0 AUD").unwrap();
    /// assert_eq!(Commodity::from_str("1.0 AUD").unwrap(), aud1.abs());
    ///
    /// let aud2 = Commodity::from_str("2.0 AUD").unwrap();
    /// assert_eq!(Commodity::from_str("2.0 AUD").unwrap(), aud2.abs());
    /// ```
    pub fn abs(&self) -> Commodity {
        return Commodity::new(self.value.abs(), self.currency_code);
    }

    /// The default epsilon to use for comparisons between different [Commodity](Commodity)s.
    pub fn default_epsilon() -> Decimal {
        Decimal::new(1, 6)
    }
    pub fn eq_approx(&self, other: Commodity, epsilon: Decimal) -> bool {
        if other.currency_code != self.currency_code {
            return false;
        }

        let diff = if self.value > other.value {
            self.value - other.value
        } else {
            other.value - self.value
        };

        return diff <= epsilon;
    }
}

impl FromStr for Commodity {
    type Err = CommodityError;
    /// Construct a [Commodity](Commodity) from a string
    ///
    /// # Example
    /// ```
    /// # use commodity::{Commodity, CurrencyCode};
    /// use std::str::FromStr;
    /// use rust_decimal::Decimal;
    ///
    /// let commodity = Commodity::from_str("1.234 USD").unwrap();
    ///
    /// assert_eq!(Decimal::from_str("1.234").unwrap(), commodity.value);
    /// assert_eq!(CurrencyCode::from_str("USD").unwrap(), commodity.currency_code);
    /// ```
    fn from_str(commodity_string: &str) -> Result<Commodity, CommodityError> {
        let elements: Vec<&str> = commodity_string.split_whitespace().collect();

        if elements.len() != 2 {
            return Err(CommodityError::InvalidCommodityString(String::from(
                commodity_string,
            )));
        }

        Ok(Commodity::new(
            Decimal::from_str(elements.get(0).unwrap()).unwrap(),
            CurrencyCode::from_str(elements.get(1).unwrap())?,
        ))
    }
}

impl PartialOrd for Commodity {
    fn partial_cmp(&self, other: &Commodity) -> Option<std::cmp::Ordering> {
        check_currency_compatible(
            self,
            other,
            String::from("cannot compare commodities with different currencies"),
        )
        .unwrap();

        self.value.partial_cmp(&other.value)
    }
}

impl Ord for Commodity {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        check_currency_compatible(
            self,
            other,
            String::from("cannot compare commodities with different currencies"),
        )
        .unwrap();

        self.value.cmp(&other.value)
    }
}

impl fmt::Display for Commodity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} {}", self.value, self.currency_code)
    }
}

#[cfg(test)]
mod tests {
    use super::{Commodity, CommodityError, CurrencyCode};
    use rust_decimal::Decimal;
    use std::str::FromStr;

    // #[test]
    // fn divide_larger() {
    //     let commodity = Commodity::from_str("4.25 AUD").unwrap();
    //     let results = commodity.divide_share(4, 2);

    //     assert_eq!(4, results.len());
    //     assert_eq!(Decimal::new(107, 2), results.get(0).unwrap().value);
    //     assert_eq!(Decimal::new(106, 2), results.get(1).unwrap().value);
    //     assert_eq!(Decimal::new(106, 2), results.get(2).unwrap().value);
    //     assert_eq!(Decimal::new(106, 2), results.get(3).unwrap().value);
    // }

    // #[test]
    // fn divide_share_negative_dividend() {
    //     let commodity = Commodity::from_str("-4.03 AUD").unwrap();
    //     let results = commodity.divide_share(4, 2);

    //     assert_eq!(4, results.len());
    //     assert_eq!(Decimal::new(-101, 2), results.get(0).unwrap().value);
    //     assert_eq!(Decimal::new(-101, 2), results.get(1).unwrap().value);
    //     assert_eq!(Decimal::new(-101, 2), results.get(2).unwrap().value);
    //     assert_eq!(Decimal::new(-100, 2), results.get(3).unwrap().value);
    // }

    // #[test]
    // fn divide_share_negative_divisor() {
    //     let commodity = Commodity::from_str("4.03 AUD").unwrap();
    //     let results = commodity.divide_share(-4, 2);

    //     assert_eq!(4, results.len());
    //     assert_eq!(Decimal::new(-101, 2), results.get(0).unwrap().value);
    //     assert_eq!(Decimal::new(-101, 2), results.get(1).unwrap().value);
    //     assert_eq!(Decimal::new(-101, 2), results.get(2).unwrap().value);
    //     assert_eq!(Decimal::new(-100, 2), results.get(3).unwrap().value);
    // }

    #[test]
    fn commodity_incompatible_currency() {
        let currency1 = CurrencyCode::from_str("USD").unwrap();
        let currency2 = CurrencyCode::from_str("AUD").unwrap();

        let commodity1 = Commodity::new(Decimal::new(400, 2), currency1);
        let commodity2 = Commodity::new(Decimal::new(250, 2), currency2);

        let error1 = commodity1.add(&commodity2).expect_err("expected an error");

        assert_eq!(
            CommodityError::IncompatableCommodity {
                this_commodity: commodity1.clone(),
                other_commodity: commodity2.clone(),
                reason: String::from("cannot add commodities with different currencies"),
            },
            error1
        );

        let error2 = commodity1.sub(&commodity2).expect_err("expected an error");

        assert_eq!(
            CommodityError::IncompatableCommodity {
                this_commodity: commodity1,
                other_commodity: commodity2,
                reason: String::from("cannot subtract commodities with different currencies"),
            },
            error2
        );
    }

    #[cfg(feature = "serde-support")]
    #[test]
    fn test_currency_code_json_serialization() {
        use serde_json;

        let original_data = "\"AUD\"";
        let currency_code: CurrencyCode = serde_json::from_str(original_data).unwrap();

        assert_eq!(CurrencyCode::from_str("AUD").unwrap(), currency_code);

        let serialized_data = serde_json::to_string(&currency_code).unwrap();
        assert_eq!(original_data, serialized_data);
    }

    #[cfg(feature = "serde-support")]
    #[test]
    fn test_commodity_json_serialization() {
        use serde_json;

        let original_data = "\"AUD\"";
        let currency_code: CurrencyCode = serde_json::from_str(original_data).unwrap();

        assert_eq!(CurrencyCode::from_str("AUD").unwrap(), currency_code);

        let serialized_data = serde_json::to_string(&currency_code).unwrap();
        assert_eq!(original_data, serialized_data);
    }
}