autofill/db/models/
passport.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// What you pass to create or update a passport.
11#[derive(Debug, Clone, Default)]
12pub struct UpdatablePassportFields {
13    pub name: String,
14    pub country: String,
15    pub passport_number: String,
16    pub issue_date_month: i64,
17    pub issue_date_day: i64,
18    pub issue_date_year: i64,
19    pub expiry_date_month: i64,
20    pub expiry_date_day: i64,
21    pub expiry_date_year: i64,
22}
23
24// "Passport" is what we return to consumers and has most of the metadata.
25#[derive(Debug, Clone, Hash, PartialEq, Eq, Default)]
26pub struct Passport {
27    pub guid: String,
28    pub name: String,
29    pub country: String,
30    pub passport_number: String,
31    pub issue_date_month: i64,
32    pub issue_date_day: i64,
33    pub issue_date_year: i64,
34    pub expiry_date_month: i64,
35    pub expiry_date_day: i64,
36    pub expiry_date_year: i64,
37
38    // We expose some of the metadata
39    pub time_created: i64,
40    pub time_last_used: Option<i64>,
41    pub time_last_modified: i64,
42    pub times_used: i64,
43}
44
45// This is used to "externalize" a passport, suitable for handing back to
46// consumers.
47impl From<InternalPassport> for Passport {
48    fn from(ip: InternalPassport) -> Self {
49        Passport {
50            guid: ip.guid.to_string(),
51            name: ip.name,
52            country: ip.country,
53            passport_number: ip.passport_number,
54            issue_date_month: ip.issue_date_month,
55            issue_date_day: ip.issue_date_day,
56            issue_date_year: ip.issue_date_year,
57            expiry_date_month: ip.expiry_date_month,
58            expiry_date_day: ip.expiry_date_day,
59            expiry_date_year: ip.expiry_date_year,
60            // note we can't use u64 in uniffi
61            time_created: u64::from(ip.metadata.time_created) as i64,
62            time_last_used: if ip.metadata.time_last_used.0 == 0 {
63                None
64            } else {
65                Some(ip.metadata.time_last_used.0 as i64)
66            },
67            time_last_modified: u64::from(ip.metadata.time_last_modified) as i64,
68            times_used: ip.metadata.times_used,
69        }
70    }
71}
72
73// An "internal" passport is used by the public APIs and by sync.
74#[derive(Debug, Clone, Default)]
75pub struct InternalPassport {
76    pub guid: Guid,
77    pub name: String,
78    pub country: String,
79    pub passport_number: String,
80    pub issue_date_month: i64,
81    pub issue_date_day: i64,
82    pub issue_date_year: i64,
83    pub expiry_date_month: i64,
84    pub expiry_date_day: i64,
85    pub expiry_date_year: i64,
86    pub metadata: Metadata,
87}
88
89impl InternalPassport {
90    pub fn from_row(row: &Row<'_>) -> Result<InternalPassport, rusqlite::Error> {
91        Ok(Self {
92            guid: Guid::from_string(row.get("guid")?),
93            name: row.get("name")?,
94            country: row.get("country")?,
95            passport_number: row.get("passport_number")?,
96            issue_date_month: row.get("issue_date_month")?,
97            issue_date_day: row.get("issue_date_day")?,
98            issue_date_year: row.get("issue_date_year")?,
99            expiry_date_month: row.get("expiry_date_month")?,
100            expiry_date_day: row.get("expiry_date_day")?,
101            expiry_date_year: row.get("expiry_date_year")?,
102            metadata: Metadata {
103                time_created: row.get("time_created")?,
104                time_last_used: row.get("time_last_used")?,
105                time_last_modified: row.get("time_last_modified")?,
106                times_used: row.get("times_used")?,
107                sync_change_counter: row.get("sync_change_counter")?,
108            },
109        })
110    }
111}