autofill/db/models/
credit_card.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2* License, v. 2.0. If a copy of the MPL was not distributed with this
3* file, You can obtain one at http://mozilla.org/MPL/2.0/.
4*/
5
6use super::Metadata;
7use rusqlite::Row;
8use sync_guid::Guid;
9
10#[derive(Debug, Clone, Default)]
11pub struct UpdatableCreditCardFields {
12    pub cc_name: String,
13    pub cc_number_enc: String,
14    pub cc_number_last_4: String,
15    pub cc_exp_month: i64,
16    pub cc_exp_year: i64,
17    // Credit card types are a fixed set of strings as defined in the link below
18    // (https://searchfox.org/mozilla-central/rev/7ef5cefd0468b8f509efe38e0212de2398f4c8b3/toolkit/modules/CreditCard.jsm#9-22)
19    pub cc_type: String,
20}
21
22#[derive(Debug, Clone, Default)]
23pub struct CreditCard {
24    pub guid: String,
25    pub cc_name: String,
26    pub cc_number_enc: String,
27    pub cc_number_last_4: String,
28    pub cc_exp_month: i64,
29    pub cc_exp_year: i64,
30
31    // Credit card types are a fixed set of strings as defined in the link below
32    // (https://searchfox.org/mozilla-central/rev/7ef5cefd0468b8f509efe38e0212de2398f4c8b3/toolkit/modules/CreditCard.jsm#9-22)
33    pub cc_type: String,
34
35    // The metadata
36    pub time_created: i64,
37    pub time_last_used: Option<i64>,
38    pub time_last_modified: i64,
39    pub times_used: i64,
40}
41
42// This is used to "externalize" a credit-card, suitable for handing back to
43// consumers.
44impl From<InternalCreditCard> for CreditCard {
45    fn from(icc: InternalCreditCard) -> Self {
46        CreditCard {
47            guid: icc.guid.to_string(),
48            cc_name: icc.cc_name,
49            cc_number_enc: icc.cc_number_enc,
50            cc_number_last_4: icc.cc_number_last_4,
51            cc_exp_month: icc.cc_exp_month,
52            cc_exp_year: icc.cc_exp_year,
53            cc_type: icc.cc_type,
54            // note we can't use u64 in uniffi
55            time_created: u64::from(icc.metadata.time_created) as i64,
56            time_last_used: if icc.metadata.time_last_used.0 == 0 {
57                None
58            } else {
59                Some(icc.metadata.time_last_used.0 as i64)
60            },
61            time_last_modified: u64::from(icc.metadata.time_last_modified) as i64,
62            times_used: icc.metadata.times_used,
63        }
64    }
65}
66
67// NOTE: No `PartialEq` here because the same card number will encrypt to a
68// different value each time it is encrypted, making it meaningless to compare.
69#[derive(Debug, Clone, Default)]
70pub struct InternalCreditCard {
71    pub guid: Guid,
72    pub cc_name: String,
73    pub cc_number_enc: String,
74    pub cc_number_last_4: String,
75    pub cc_exp_month: i64,
76    pub cc_exp_year: i64,
77    // Credit card types are a fixed set of strings as defined in the link below
78    // (https://searchfox.org/mozilla-central/rev/7ef5cefd0468b8f509efe38e0212de2398f4c8b3/toolkit/modules/CreditCard.jsm#9-22)
79    pub cc_type: String,
80    pub metadata: Metadata,
81}
82
83impl InternalCreditCard {
84    pub fn from_row(row: &Row<'_>) -> Result<InternalCreditCard, rusqlite::Error> {
85        Ok(Self {
86            guid: Guid::from_string(row.get("guid")?),
87            cc_name: row.get("cc_name")?,
88            cc_number_enc: row.get("cc_number_enc")?,
89            cc_number_last_4: row.get("cc_number_last_4")?,
90            cc_exp_month: row.get("cc_exp_month")?,
91            cc_exp_year: row.get("cc_exp_year")?,
92            cc_type: row.get("cc_type")?,
93            metadata: Metadata {
94                time_created: row.get("time_created")?,
95                time_last_used: row.get("time_last_used")?,
96                time_last_modified: row.get("time_last_modified")?,
97                times_used: row.get("times_used")?,
98                sync_change_counter: row.get("sync_change_counter")?,
99            },
100        })
101    }
102
103    pub fn has_scrubbed_data(&self) -> bool {
104        self.cc_number_enc.is_empty()
105    }
106}