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
use super::{Money, Tax};
#[cfg(feature="serialization")]
use serde::ser::{Serialize, Serializer, SerializeStruct};
#[derive(Clone, Copy, Debug)]
pub struct Product<'a> {
pub name: &'a str,
pub price: Money,
pub tax: Tax,
}
#[cfg(feature="serialization")]
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> {
pub fn new(name: &'a str, price: Money, tax: f64) -> Self {
Product {
name: name,
price: price,
tax: Tax::new(tax),
}
}
}
pub trait BillProduct {
fn price(&self) -> Money;
fn name(&self) -> String;
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}
}