From d06264401df1b473d1afe49a9e688ffdce11ad58 Mon Sep 17 00:00:00 2001 From: didier amyot Date: Wed, 21 Dec 2022 08:17:06 -0500 Subject: [PATCH] refactored ids from u32 to i64. --- src/db/_accounts.rs | 8 +++---- src/db/_external_id.rs | 53 +++++++++++++++++++++--------------------- src/db/_securities.rs | 2 +- src/db/_users.rs | 2 +- 4 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/db/_accounts.rs b/src/db/_accounts.rs index bc0a9ab..7497e2e 100644 --- a/src/db/_accounts.rs +++ b/src/db/_accounts.rs @@ -24,8 +24,8 @@ impl AccountType { } pub struct Account { - id: u32, - amount: u32, + id: i64, + amount: i64, timestamp: i64, security: Security, account_type: AccountType, @@ -46,7 +46,7 @@ impl Account { }); } - pub fn raw_amount(&self) -> u32 { + pub fn raw_amount(&self) -> i64 { self.amount } } @@ -95,7 +95,7 @@ fn create_account(tx: &Transaction, sec: &SecurityId, user: &UserId, account_typ AccountType::SecurityEmitter => "security_emitter", }; tx.execute(q, (sec, account_type))?; - let id = AccountId::from_id(tx.last_insert_rowid() as u32); + let id = AccountId::from_id(tx.last_insert_rowid() as i64); let q = "INSERT INTO account_user_permissions(user_id, account_id) VALUES(?1, ?2)"; diff --git a/src/db/_external_id.rs b/src/db/_external_id.rs index 5a1a025..5c6391c 100644 --- a/src/db/_external_id.rs +++ b/src/db/_external_id.rs @@ -5,8 +5,8 @@ use regex::{Captures, Regex}; #[derive(PartialEq, Debug, Clone)] struct SafeId { - encrypted_id: [u8; 4], - signature: [u8; 4], + encrypted_id: [u8; 8], + signature: [u8; 8], } @@ -14,8 +14,8 @@ pub trait Id { type Output; fn from_regex_captures(caps: &Captures) -> Option; fn from_string(s: &String) -> Option; - fn from_id(id: u32) -> Self::Output; - fn get_id(&self) -> u32; + fn from_id(id: i64) -> Self::Output; + fn get_id(&self) -> i64; } impl fmt::Display for SafeId { @@ -43,16 +43,16 @@ impl Id for SafeId { return Self::from_regex_captures(&caps); } - fn from_id(id: u32) -> SafeId { - let bytes = u32::to_be_bytes(id); + fn from_id(id: i64) -> SafeId { + let bytes = i64::to_be_bytes(id); SafeId { encrypted_id: bytes, signature: bytes, } } - fn get_id(&self) -> u32 { - return u32::from_be_bytes(self.encrypted_id); + fn get_id(&self) -> i64 { + return i64::from_be_bytes(self.encrypted_id); } } @@ -91,11 +91,11 @@ macro_rules! impl_ids { Self::from_regex_captures(& caps) } - fn from_id(id: u32) -> $T { + fn from_id(id: i64) -> $T { $T(SafeId::from_id(id)) } - fn get_id(&self) -> u32 { + fn get_id(&self) -> i64 { self.0.get_id() } } @@ -123,8 +123,8 @@ pub enum ExternalId { } -fn deserialize_str(s: &str) -> Option<[u8; 4]> { - let mut encrypted_id_bytes = [0u8; 4]; +fn deserialize_str(s: &str) -> Option<[u8; 8]> { + let mut encrypted_id_bytes = [0u8; 8]; let r = hex::decode_to_slice(s, &mut encrypted_id_bytes as &mut [u8]); return match r { Ok(_) => Some(encrypted_id_bytes), @@ -157,7 +157,7 @@ impl ExternalId { } } - pub fn get_id(&self) -> u32 { + pub fn get_id(&self) -> i64 { match self { ExternalId::UserId(x) => x.get_id(), ExternalId::AccountId(x) => x.get_id(), @@ -175,16 +175,16 @@ pub mod test { #[test] fn test_id_macro() { let id = SecurityId::from_id(33); - assert_eq!(id.to_string(), "s-00000021-00000021".to_string()); - assert!(SecurityId::from_string(&"s-00000021-00000021".to_string()).is_some()); - assert!(SecurityId::from_string(&"u-00000021-00000021".to_string()).is_none()); - assert!(SecurityId::from_string(&"00000021-00000021".to_string()).is_none()); + assert_eq!(id.to_string(), "s-0000000000000021-0000000000000021".to_string()); + assert!(SecurityId::from_string(&"s-0000000000000021-0000000000000021".to_string()).is_some()); + assert!(SecurityId::from_string(&"u-0000000000000021-0000000000000021".to_string()).is_none()); + assert!(SecurityId::from_string(&"0000000000000021-0000000000000021".to_string()).is_none()); } #[test] fn safe_id_happy_path_1() { - let sid = SafeId { encrypted_id: [1, 2, 3, 4], signature: [7, 8, 9, 10] }; - assert_eq!("01020304-0708090a".to_string(), sid.to_string()); + let sid = SafeId { encrypted_id: [1, 2, 3, 4, 5, 6, 7, 8], signature: [8, 7, 6, 5, 4, 3, 2, 1] }; + assert_eq!("0102030405060708-0807060504030201".to_string(), sid.to_string()); let deserialized_eid = SafeId::from_string(&sid.to_string()).unwrap(); assert_eq!(sid, deserialized_eid); } @@ -192,7 +192,7 @@ pub mod test { #[test] fn safe_id_happy_path_2() { let sid = SafeId::from_id(56); - assert_eq!("00000038-00000038".to_string(), sid.to_string()); + assert_eq!("0000000000000038-0000000000000038".to_string(), sid.to_string()); let deserialized_eid = SafeId::from_string(&sid.to_string()).unwrap(); assert_eq!(sid, deserialized_eid); assert_eq!(56, deserialized_eid.get_id()); @@ -209,25 +209,26 @@ pub mod test { #[test] fn external_id_simple_test() { - let eid = ExternalId::from_string(&"u-01020304-0708090a".to_string()).unwrap(); + let base = "0000000000000038-0000000000000038"; + let eid = ExternalId::from_string(& format!("u-{:}", base)).unwrap(); assert!(match eid { ExternalId::UserId(_) => true, _ => false }); - assert_eq!(eid.to_string(), "u-01020304-0708090a".to_string()); + assert_eq!(eid.to_string(), format!("u-{:}", base)); - let eid = ExternalId::from_string(&"a-01020304-0708090a".to_string()).unwrap(); + let eid = ExternalId::from_string(&format!("a-{:}", base)).unwrap(); assert!(match eid { ExternalId::AccountId(_) => true, _ => false }); - assert_eq!(eid.to_string(), "a-01020304-0708090a".to_string()); + assert_eq!(eid.to_string(), format!("a-{:}", base)); - let eid = ExternalId::from_string(&"s-01020304-0708090a".to_string()).unwrap(); + let eid = ExternalId::from_string(&format!("s-{:}", base)).unwrap(); assert!(match eid { ExternalId::SecurityId(_) => true, _ => false }); - assert_eq!(eid.to_string(), "s-01020304-0708090a".to_string()); + assert_eq!(eid.to_string(), format!("s-{:}", base)); } } \ No newline at end of file diff --git a/src/db/_securities.rs b/src/db/_securities.rs index 74d5166..5dc0ed7 100644 --- a/src/db/_securities.rs +++ b/src/db/_securities.rs @@ -34,7 +34,7 @@ impl ToSql for SecurityType { #[derive(Debug, Serialize, PartialEq)] pub struct Security { - id: u32, + id: i64, security_type: SecurityType, symbol: String, } diff --git a/src/db/_users.rs b/src/db/_users.rs index ea7cada..0481d15 100644 --- a/src/db/_users.rs +++ b/src/db/_users.rs @@ -5,7 +5,7 @@ use crate::db::_external_id::{UserId, Id}; #[derive(Debug, Serialize)] pub struct User { - id: u32, + id: i64, name: String, public_key: String, }