tg_flows/types/
label_price.rs

1use serde::{Deserialize, Serialize};
2
3/// This object represents a portion of the price for goods or services.
4///
5/// [The official docs](https://core.telegram.org/bots/api#labeledprice).
6#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
7pub struct LabeledPrice {
8    /// Portion label.
9    pub label: String,
10
11    /// Price of the product in the smallest units of the [currency] (integer,
12    /// **not** float/double). For example, for a price of `US$ 1.45` pass
13    /// `amount = 145`. See the exp parameter in [`currencies.json`], it shows
14    /// the number of digits past the decimal point for each currency (2
15    /// for the majority of currencies).
16    ///
17    /// [currency]: https://core.telegram.org/bots/payments#supported-currencies
18    /// [`currencies.json`]: https://core.telegram.org/bots/payments/currencies.json
19    pub amount: i32,
20}
21
22impl LabeledPrice {
23    pub fn new<S>(label: S, amount: i32) -> Self
24    where
25        S: Into<String>,
26    {
27        Self { label: label.into(), amount }
28    }
29
30    pub fn label<S>(mut self, val: S) -> Self
31    where
32        S: Into<String>,
33    {
34        self.label = val.into();
35        self
36    }
37
38    #[must_use]
39    pub fn amount(mut self, val: i32) -> Self {
40        self.amount = val;
41        self
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn serialize() {
51        let labeled_price = LabeledPrice { label: "Label".to_string(), amount: 60 };
52        let expected = r#"{"label":"Label","amount":60}"#;
53        let actual = serde_json::to_string(&labeled_price).unwrap();
54        assert_eq!(actual, expected);
55    }
56}