tg_flows/types/
shipping_option.rs

1use serde::{Deserialize, Serialize};
2
3use crate::types::LabeledPrice;
4
5/// This object represents one shipping option.
6///
7/// [The official docs](https://core.telegram.org/bots/api#shippingoption).
8#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
9pub struct ShippingOption {
10    /// Shipping option identifier.
11    pub id: String,
12
13    /// Option title.
14    pub title: String,
15
16    /// List of price portions.
17    pub prices: Vec<LabeledPrice>,
18}
19
20impl ShippingOption {
21    pub fn new<S1, S2, P>(id: S1, title: S2, prices: P) -> Self
22    where
23        S1: Into<String>,
24        S2: Into<String>,
25        P: IntoIterator<Item = LabeledPrice>,
26    {
27        Self { id: id.into(), title: title.into(), prices: prices.into_iter().collect() }
28    }
29
30    pub fn id<S>(mut self, val: S) -> Self
31    where
32        S: Into<String>,
33    {
34        self.id = val.into();
35        self
36    }
37
38    pub fn title<S>(mut self, val: S) -> Self
39    where
40        S: Into<String>,
41    {
42        self.title = val.into();
43        self
44    }
45
46    pub fn prices<P>(mut self, val: P) -> Self
47    where
48        P: IntoIterator<Item = LabeledPrice>,
49    {
50        self.prices = val.into_iter().collect();
51        self
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn serialize() {
61        let shipping_option = ShippingOption {
62            id: "0".to_string(),
63            title: "Option".to_string(),
64            prices: vec![LabeledPrice { label: "Label".to_string(), amount: 60 }],
65        };
66        let expected = r#"{"id":"0","title":"Option","prices":[{"label":"Label","amount":60}]}"#;
67        let actual = serde_json::to_string(&shipping_option).unwrap();
68        assert_eq!(actual, expected);
69    }
70}