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
#[cfg(feature = "serde")]
use serde::ser::{Serialize, SerializeStruct, Serializer};

use super::{Money, Tax};

/// Describes one particular product.
/// Amount is handled by `BillItem`
///
/// You can write your own product, just implement `BillProduct`
#[derive(Clone, Copy, Debug)]
pub struct Product<'a> {
    pub name: &'a str,
    pub price: Money,
    pub tax: Tax,
}

#[cfg(feature = "serde")]
impl<'a> Serialize for Product<'a> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut s = serializer.serialize_struct("Product", 3)?;
        s.serialize_field("name", &self.name)?;
        s.serialize_field("price", &self.price.as_float())?;
        s.serialize_field("tax", &self.tax)?;
        s.end()
    }
}

impl<'a> Product<'a> {
    /// Instantiates a new Product.
    pub fn new(name: &'a str, price: Money, tax: f64) -> Self {
        Product {
            name,
            price,
            tax: Tax::new(tax),
        }
    }
}

/// Describes one particular product.
/// Amount is handled by `BillItem`
pub trait BillProduct {
    /// Price in Money
    fn price(&self) -> Money;

    /// A [name](https://en.wikipedia.org/wiki/Name) is a term used for identification.
    fn name(&self) -> String;

    /// Tax
    ///
    ///
    /// > A [tax](https://en.wikipedia.org/wiki/Tax) (from the Latin [taxo](https://en.wiktionary.org/wiki/en:taxo#Latin)) is a financial charge or other levy imposed upon a taxpayer (an individual or legal entity) by a state or the functional equivalent of a state to fund various public expenditures.
    ///
    fn tax(&self) -> Tax;
}

impl<'a> BillProduct for Product<'a> {
    fn price(&self) -> Money {
        self.price
    }
    fn name(&self) -> String {
        self.name.to_owned()
    }
    fn tax(&self) -> Tax {
        self.tax
    }
}