transdirect/product.rs
1/// Represents an item and its associated quantity
2///
3///
4use std::default::Default;
5use std::collections::HashMap;
6use num_traits::{Float,Unsigned};
7use serde_derive::{Serialize,Deserialize};
8use serde::ser;
9
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
11pub struct Product<T, U> where T: Unsigned, U: Float {
12 pub quantity: T,
13 pub weight: U, // Transdirect calculates weight in increments of 1kg
14 #[serde(flatten)]
15 pub dimensions: Dimensions<U>,
16 pub description: String,
17 pub id: Option<u32>, // Not necessary for Client to create
18}
19
20impl<T, U> Product<T, U>
21where T: Unsigned + ser::Serialize + Default, U: Float + ser::Serialize + Default {
22 /// Creates a new empty Product instance
23 ///
24 /// This is a convenience function to create a valid Product fast
25 ///
26 /// # Examples
27 ///
28 /// For example, there is an opaque function that modifies the product
29 ///
30 /// ```
31 /// // use transdirect::prelude::Product;
32 /// //
33 /// // fn deliver_extra(prod: &mut Product) -> Result<(), String> {
34 /// //
35 /// // }
36 /// // let m = Product::new();
37 ///
38 ///
39 pub fn new() -> Self {
40 Default::default()
41 }
42}
43
44#[derive(Debug, Copy, Clone, PartialEq, Eq, Deserialize, Serialize, Default)]
45pub struct Dimensions<T> where T: Float {
46 pub length: T,
47 pub width: T,
48 pub height: T,
49}
50
51impl<T> Dimensions<T> where T: Float + Default {
52 pub fn new() -> Self {
53 Default::default()
54 }
55
56 pub fn from_lwh(length: T, width: T, height: T) -> Self {
57 Dimensions {
58 length,
59 width,
60 height,
61 }
62 }
63}
64
65// impl<T> ser::Serialize for Dimensions<T> where T: Float + ser::Serialize {
66// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
67// where S: ser::Serializer {
68// let mut seq = serializer.serialize_seq(Some(self.len()))?;
69// for e in self {
70// seq.serialize_element(e)?
71// }
72// seq.end()
73// }
74// }
75
76/// A service provided by one of the companies listed by Transdirect.
77/// It is put in the products file because it is a product provided by
78/// external companies.
79#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
80pub struct Service<T> where T: Float {
81 pub total: T,
82 pub price_insurance_ex: T,
83 pub fee: T,
84 pub insured_amount: T,
85 pub service: String,
86 pub transit_time: String,
87 pub pickup_dates: Vec<String>,
88 pub pickup_time: HashMap<String, String>,
89}