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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use serde::{Deserialize, Serialize};
/// Describes a Telegram Star transaction. Note that if the buyer initiates a chargeback with the payment provider from whom they acquired Stars (e.g., Apple, Google) following this transaction, the refunded Stars will be deducted from the bot's balance. This is outside of Telegram's control.
/// Currently, it can be one of
/// - [`crate::types::StarTransactionIncoming`]
/// - [`crate::types::StarTransactionOutgoing`]
/// # Documentation
/// <https://core.telegram.org/bots/api#startransaction>
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum StarTransaction {
Incoming(crate::types::StarTransactionIncoming),
Outgoing(crate::types::StarTransactionOutgoing),
}
impl StarTransaction {
/// Helper method for field `amount`.
///
/// Integer amount of Telegram Stars transferred by the transaction
#[must_use]
pub fn amount(&self) -> i64 {
match self {
Self::Incoming(val) => val.amount,
Self::Outgoing(val) => val.amount,
}
}
/// Helper method for field `date`.
///
/// Date the transaction was created in Unix time
#[must_use]
pub fn date(&self) -> i64 {
match self {
Self::Incoming(val) => val.date,
Self::Outgoing(val) => val.date,
}
}
/// Helper method for field `id`.
///
/// Unique identifier of the transaction. Coincides with the identifier of the original transaction for refund transactions. Coincides with [`crate::types::SuccessfulPayment`].`telegram_payment_charge_id` for successful incoming payments from users.
#[must_use]
pub fn id(&self) -> &str {
match self {
Self::Incoming(val) => val.id.as_ref(),
Self::Outgoing(val) => val.id.as_ref(),
}
}
/// Helper method for field `nanostar_amount`.
///
/// The number of 1/1000000000 shares of Telegram Stars transferred by the transaction; from 0 to 999999999
#[must_use]
pub fn nanostar_amount(&self) -> Option<u32> {
match self {
Self::Incoming(val) => val.nanostar_amount,
Self::Outgoing(val) => val.nanostar_amount,
}
}
/// Helper method for field `receiver`.
///
/// Receiver of an outgoing transaction (e.g., a user for a purchase refund, Fragment for a withdrawal). Only for outgoing transactions
#[must_use]
pub fn receiver(&self) -> Option<&crate::types::TransactionPartner> {
match self {
Self::Outgoing(val) => Some(&val.receiver),
Self::Incoming(_) => None,
}
}
/// Helper method for field `source`.
///
/// Source of an incoming transaction (e.g., a user purchasing goods or services, Fragment refunding a failed withdrawal). Only for incoming transactions
#[must_use]
pub fn source(&self) -> Option<&crate::types::TransactionPartner> {
match self {
Self::Incoming(val) => Some(&val.source),
Self::Outgoing(_) => None,
}
}
/// Helper method for nested field `chat`.
#[must_use]
pub fn chat(&self) -> Option<&crate::types::Chat> {
match self {
Self::Outgoing(val) => {
let inner = &val.receiver;
crate::types::TransactionPartner::chat(inner)
}
Self::Incoming(val) => {
let inner = &val.source;
crate::types::TransactionPartner::chat(inner)
}
}
}
/// Helper method for nested field `commission_per_mille`.
#[must_use]
pub fn commission_per_mille(&self) -> Option<i64> {
match self {
Self::Outgoing(val) => {
let inner = &val.receiver;
crate::types::TransactionPartner::commission_per_mille(inner)
}
Self::Incoming(val) => {
let inner = &val.source;
crate::types::TransactionPartner::commission_per_mille(inner)
}
}
}
/// Helper method for nested field `gift`.
#[must_use]
pub fn gift(&self) -> Option<&crate::types::Gift> {
match self {
Self::Outgoing(val) => {
let inner = &val.receiver;
crate::types::TransactionPartner::gift(inner)
}
Self::Incoming(val) => {
let inner = &val.source;
crate::types::TransactionPartner::gift(inner)
}
}
}
/// Helper method for nested field `request_count`.
#[must_use]
pub fn request_count(&self) -> Option<i64> {
match self {
Self::Outgoing(val) => {
let inner = &val.receiver;
crate::types::TransactionPartner::request_count(inner)
}
Self::Incoming(val) => {
let inner = &val.source;
crate::types::TransactionPartner::request_count(inner)
}
}
}
/// Helper method for nested field `sponsor_user`.
#[must_use]
pub fn sponsor_user(&self) -> Option<&crate::types::User> {
match self {
Self::Outgoing(val) => {
let inner = &val.receiver;
crate::types::TransactionPartner::sponsor_user(inner)
}
Self::Incoming(val) => {
let inner = &val.source;
crate::types::TransactionPartner::sponsor_user(inner)
}
}
}
/// Helper method for nested field `withdrawal_state`.
#[must_use]
pub fn withdrawal_state(&self) -> Option<&crate::types::RevenueWithdrawalState> {
match self {
Self::Outgoing(val) => {
let inner = &val.receiver;
crate::types::TransactionPartner::withdrawal_state(inner)
}
Self::Incoming(val) => {
let inner = &val.source;
crate::types::TransactionPartner::withdrawal_state(inner)
}
}
}
}
impl From<crate::types::StarTransactionIncoming> for StarTransaction {
fn from(val: crate::types::StarTransactionIncoming) -> Self {
Self::Incoming(val)
}
}
impl TryFrom<StarTransaction> for crate::types::StarTransactionIncoming {
type Error = crate::errors::ConvertToTypeError;
fn try_from(val: StarTransaction) -> Result<Self, Self::Error> {
match val {
StarTransaction::Incoming(inner) => Ok(inner),
StarTransaction::Outgoing(_) => Err(Self::Error::new(
stringify!(StarTransaction),
stringify!(StarTransactionIncoming),
)),
}
}
}
impl From<crate::types::StarTransactionOutgoing> for StarTransaction {
fn from(val: crate::types::StarTransactionOutgoing) -> Self {
Self::Outgoing(val)
}
}
impl TryFrom<StarTransaction> for crate::types::StarTransactionOutgoing {
type Error = crate::errors::ConvertToTypeError;
fn try_from(val: StarTransaction) -> Result<Self, Self::Error> {
match val {
StarTransaction::Outgoing(inner) => Ok(inner),
StarTransaction::Incoming(_) => Err(Self::Error::new(
stringify!(StarTransaction),
stringify!(StarTransactionOutgoing),
)),
}
}
}