tutorial_1/product/
mod.rs

1// here we can use use category::Category; to shrink the code's representaiton
2// use category::Category;
3pub use category::Category;
4
5#[derive(PartialEq, Debug)]
6pub struct Product {
7    id: u64,
8    name: String,
9    price: f64,
10    category: Category,
11}
12
13// here in rust, a child module can get access to all structs and functions that define in parent modules
14// but, parent cannot get access to private child modules' function or structs
15// so we need to set enum Category the child module public so that it can be used in mod product correctly
16pub mod category;
17
18impl Product {
19    pub fn new(
20        id: u64,
21        name: String,
22        price: f64,
23        category: Category,
24    ) -> Self {
25        Product {
26            id,
27            name,
28            price,
29            category,
30        }
31    }
32
33    fn calculate_tax(&self) -> f64 {
34        self.price * 0.1
35    }
36
37    pub fn product_price(&self) -> f64 {
38        self.price + self.calculate_tax() as f64
39    }
40}