Skip to content

Commit 1d34831

Browse files
committed
Factorize impl_(load|dump) macros in libparsec_types
1 parent 839076e commit 1d34831

File tree

4 files changed

+58
-73
lines changed

4 files changed

+58
-73
lines changed

libparsec/crates/types/src/account.rs

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize};
77
use serde_with::*;
88
use thiserror::Error;
99

10+
use super::utils::{impl_decrypt_and_load, impl_dump, impl_dump_and_encrypt, impl_load};
1011
use libparsec_serialization_format::parsec_data;
1112

1213
use crate::{
@@ -27,55 +28,6 @@ pub const AUTH_METHOD_MAC_KEY_DERIVATION_UUID: uuid::Uuid =
2728
pub const AUTH_METHOD_SECRET_KEY_DERIVATION_UUID: uuid::Uuid =
2829
uuid::uuid!("22222222-2222-2222-2222-222222222222");
2930

30-
/*
31-
* Helpers
32-
*/
33-
34-
macro_rules! impl_dump {
35-
($name:ident) => {
36-
impl $name {
37-
pub fn dump(&self) -> Vec<u8> {
38-
format_v0_dump(&self)
39-
}
40-
}
41-
};
42-
}
43-
44-
macro_rules! impl_dump_and_encrypt {
45-
($name:ident) => {
46-
impl $name {
47-
pub fn dump_and_encrypt(&self, key: &::libparsec_crypto::SecretKey) -> Vec<u8> {
48-
let serialized = format_v0_dump(&self);
49-
key.encrypt(&serialized)
50-
}
51-
}
52-
};
53-
}
54-
55-
macro_rules! impl_load {
56-
($name:ident) => {
57-
impl $name {
58-
pub fn load(serialized: &[u8]) -> Result<$name, DataError> {
59-
format_vx_load(&serialized)
60-
}
61-
}
62-
};
63-
}
64-
65-
macro_rules! impl_decrypt_and_load {
66-
($name:ident) => {
67-
impl $name {
68-
pub fn decrypt_and_load(
69-
encrypted: &[u8],
70-
key: &::libparsec_crypto::SecretKey,
71-
) -> Result<$name, DataError> {
72-
let serialized = key.decrypt(encrypted).map_err(|_| DataError::Decryption)?;
73-
format_vx_load(&serialized)
74-
}
75-
}
76-
};
77-
}
78-
7931
/*
8032
* ValidationCode
8133
*/

libparsec/crates/types/src/async_enrollment.rs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize};
55

66
use libparsec_serialization_format::parsec_data;
77

8+
use super::utils::{impl_dump, impl_load};
89
use crate::{
910
self as libparsec_types,
1011
data_macros::impl_transparent_data_format_conversion,
@@ -13,30 +14,6 @@ use crate::{
1314
ParsecAddr, PublicKey, UserID, UserProfile, VerifyKey,
1415
};
1516

16-
/*
17-
* Helpers
18-
*/
19-
20-
macro_rules! impl_dump {
21-
($name:ident) => {
22-
impl $name {
23-
pub fn dump(&self) -> Vec<u8> {
24-
format_v0_dump(&self)
25-
}
26-
}
27-
};
28-
}
29-
30-
macro_rules! impl_load {
31-
($name:ident) => {
32-
impl $name {
33-
pub fn load(serialized: &[u8]) -> Result<$name, DataError> {
34-
format_vx_load(&serialized)
35-
}
36-
}
37-
};
38-
}
39-
4017
/*
4118
* AsyncEnrollmentSubmitPayload
4219
*/

libparsec/crates/types/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ mod serialization;
7979
mod shamir;
8080
mod time;
8181
mod token;
82+
mod utils;
8283

8384
pub use account::*;
8485
pub use addr::*;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Parsec Cloud (https://parsec.cloud) Copyright (c) BUSL-1.1 2016-present Scille SAS
2+
3+
/*
4+
* Helpers
5+
*/
6+
7+
macro_rules! impl_dump {
8+
($name:ident) => {
9+
impl $name {
10+
pub fn dump(&self) -> Vec<u8> {
11+
format_v0_dump(&self)
12+
}
13+
}
14+
};
15+
}
16+
17+
macro_rules! impl_dump_and_encrypt {
18+
($name:ident) => {
19+
impl $name {
20+
pub fn dump_and_encrypt(&self, key: &::libparsec_crypto::SecretKey) -> Vec<u8> {
21+
let serialized = format_v0_dump(&self);
22+
key.encrypt(&serialized)
23+
}
24+
}
25+
};
26+
}
27+
28+
macro_rules! impl_load {
29+
($name:ident) => {
30+
impl $name {
31+
pub fn load(serialized: &[u8]) -> Result<$name, DataError> {
32+
format_vx_load(&serialized)
33+
}
34+
}
35+
};
36+
}
37+
38+
macro_rules! impl_decrypt_and_load {
39+
($name:ident) => {
40+
impl $name {
41+
pub fn decrypt_and_load(
42+
encrypted: &[u8],
43+
key: &::libparsec_crypto::SecretKey,
44+
) -> Result<$name, DataError> {
45+
let serialized = key.decrypt(encrypted).map_err(|_| DataError::Decryption)?;
46+
format_vx_load(&serialized)
47+
}
48+
}
49+
};
50+
}
51+
52+
pub(super) use impl_decrypt_and_load;
53+
pub(super) use impl_dump;
54+
pub(super) use impl_dump_and_encrypt;
55+
pub(super) use impl_load;

0 commit comments

Comments
 (0)