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*/
56use super::Metadata;
7use rusqlite::Row;
8use sync_guid::Guid;
910#[derive(Debug, Clone, Default)]
11pub struct UpdatableCreditCardFields {
12pub cc_name: String,
13pub cc_number_enc: String,
14pub cc_number_last_4: String,
15pub cc_exp_month: i64,
16pub 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)
19pub cc_type: String,
20}
2122#[derive(Debug, Clone, Default)]
23pub struct CreditCard {
24pub guid: String,
25pub cc_name: String,
26pub cc_number_enc: String,
27pub cc_number_last_4: String,
28pub cc_exp_month: i64,
29pub cc_exp_year: i64,
3031// 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)
33pub cc_type: String,
3435// The metadata
36pub time_created: i64,
37pub time_last_used: Option<i64>,
38pub time_last_modified: i64,
39pub times_used: i64,
40}
4142// This is used to "externalize" a credit-card, suitable for handing back to
43// consumers.
44impl From<InternalCreditCard> for CreditCard {
45fn 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
55time_created: u64::from(icc.metadata.time_created) as i64,
56 time_last_used: if icc.metadata.time_last_used.0 == 0 {
57None
58} else {
59Some(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}
6667// 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 {
71pub guid: Guid,
72pub cc_name: String,
73pub cc_number_enc: String,
74pub cc_number_last_4: String,
75pub cc_exp_month: i64,
76pub 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)
79pub cc_type: String,
80pub metadata: Metadata,
81}
8283impl InternalCreditCard {
84pub fn from_row(row: &Row<'_>) -> Result<InternalCreditCard, rusqlite::Error> {
85Ok(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 }
102103pub fn has_scrubbed_data(&self) -> bool {
104self.cc_number_enc.is_empty()
105 }
106}