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
use std::fmt::Display;
use std::fmt::Formatter;
use std::iter::FromIterator;

use derive_more::Deref;
use derive_more::IntoIterator;
use serde::Deserialize;
use serde_repr::Deserialize_repr;
use serde_repr::Serialize_repr;

/// A collection of [`Confirmation`]
#[derive(IntoIterator, Deref, Default, Debug)]
pub struct Confirmations(#[into_iterator(owned, ref)] pub Vec<Confirmation>);

impl<'a> FromIterator<&'a Confirmation> for Confirmations {
    fn from_iter<T>(iter: T) -> Self
    where
        T: IntoIterator<Item = &'a Confirmation>,
    {
        let buffer = iter.into_iter().cloned().collect::<Vec<_>>();
        Self(buffer)
    }
}

/// A pending Steam confirmation.
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct Confirmation {
    pub id: String,
    #[serde(rename = "nonce")]
    pub key: String,
    #[serde(rename = "type")]
    pub kind: EConfirmationType,
    pub creation_time: i64,
    pub creator_id: String,
    pub type_name: String,
    // from below here, nothing really useful
    // pub cancel: String,
    // pub accept: String,
    // pub icon: String,
    // pub multi: bool,
    // pub headline: String,
    // pub summary: Vec<String>,
    // pub warn: Option<String>,
}

impl Display for Confirmation {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "Confirmation {} of {:?}", self.key, self.kind)
    }
}

impl Confirmation {
    pub fn has_trade_offer_id(&self, offer_id: u64) -> bool {
        self.kind == EConfirmationType::Trade && offer_id == self.creator_id.parse::<u64>().unwrap()
    }
    pub fn trade_offer_id(&self) -> Option<u64> {
        if self.kind == EConfirmationType::Trade {
            self.creator_id.parse().ok()
        } else {
            None
        }
    }
}

/// We retrieve [`ConfirmationDetails`] as a json object.
/// There is also the need to already have a [Confirmation].
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
pub struct ConfirmationDetails {
    /// ID of the trade offer. Has a value if EConfirmationType::Trade
    pub trade_offer_id: Option<i64>,
}

/// Kinds of mobile confirmations
#[derive(Debug, Copy, Clone, Serialize_repr, Deserialize_repr, Eq, PartialEq)]
#[repr(u8)]
#[non_exhaustive]
pub enum EConfirmationType {
    /// Unknown confirmation
    Unknown = 0,
    /// Under rare circumstances this might pop up
    Generic = 1,
    /// Confirmation from Trade Offer
    Trade = 2,
    /// Confirmation from Steam's Market
    Market = 3,

    /// Unknown
    FeatureOptOut = 4,
    /// Confirmation for a phone number change
    PhoneNumberChange = 5,
    /// Confirmation for account recovery
    AccountRecovery = 6,
    /// Confirmation for creating a new API Key,
    APIKey = 9,
}

impl From<Vec<Confirmation>> for Confirmations {
    fn from(confirmations_vec: Vec<Confirmation>) -> Self {
        Self(confirmations_vec)
    }
}

#[allow(missing_docs)]
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
pub enum ConfirmationAction {
    Retrieve,
    Accept,
    Deny,
}

impl ConfirmationAction {
    pub(crate) const fn as_operation(self) -> Option<&'static str> {
        Some(match self {
            Self::Accept => "allow",
            Self::Deny => "cancel",
            _ => return None,
        })
    }
    pub(crate) const fn as_tag(self) -> &'static str {
        "conf"
    }
}

#[derive(Copy, Clone, Debug)]
enum EInventoryPrivacy {
    Unknown,
    Private,
    FriendsOnly,
    Public,
}

#[cfg(test)]
mod tests {
    use super::*;

    fn get_confirmations() -> Confirmations {
        let confirmations = vec![
            Confirmation {
                id: "7676451136".to_string(),
                key: "18064583892738866189".to_string(),
                kind: EConfirmationType::Trade,
                details: Some(ConfirmationDetails {
                    trade_offer_id: Some(4009687284),
                }),
            },
            Confirmation {
                id: "7652515663".to_string(),
                key: "10704556181383316145".to_string(),
                kind: EConfirmationType::Trade,
                details: Some(ConfirmationDetails {
                    trade_offer_id: Some(4000980011),
                }),
            },
            Confirmation {
                id: "7652555421".to_string(),
                key: "10704556181383323456".to_string(),
                kind: EConfirmationType::Trade,
                details: Some(ConfirmationDetails {
                    trade_offer_id: Some(4000793103),
                }),
            },
            Confirmation {
                id: "7652515663".to_string(),
                key: "20845677815483316145".to_string(),
                kind: EConfirmationType::Market,
                details: None,
            },
        ];
        Confirmations::from(confirmations)
    }
}