refactored ids from u32 to i64.

This commit is contained in:
didier amyot committed 2022-12-21 08:17:06 -05:00
1 parent 352bb498d4
commit d06264401d
4 files changed
+33 -32

No files matched your search

+4 -4
View File
@@ -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)";
+27 -26
View File
@@ -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<Self::Output>;
fn from_string(s: &String) -> Option<Self::Output>;
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));
}
}
+1 -1
View File
@@ -34,7 +34,7 @@ impl ToSql for SecurityType {
#[derive(Debug, Serialize, PartialEq)]
pub struct Security {
id: u32,
id: i64,
security_type: SecurityType,
symbol: String,
}
+1 -1
View File
@@ -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,
}