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