Skip to content

Commit 1e2b840

Browse files
authored
Merge pull request #29 from tnull/2024-02-align-rustfmt
Align `rustfmt.toml` with `rust-lightning`
2 parents c1f06c7 + 6d5f0e5 commit 1e2b840

File tree

13 files changed

+366
-193
lines changed

13 files changed

+366
-193
lines changed

rustfmt.toml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
hard_tabs = true # use tab characters for indentation, spaces for alignment
2-
use_field_init_shorthand = true
3-
max_width = 120
41
use_small_heuristics = "Max"
5-
chain_width = 80
62
fn_params_layout = "Compressed"
3+
hard_tabs = true
4+
use_field_init_shorthand = true
5+
max_width = 100
6+
match_block_trailing_comma = true
7+
# UNSTABLE: format_code_in_doc_comments = true
8+
# UNSTABLE: overflow_delimited_expr = true
9+
# UNSTABLE: comment_width = 100
10+
# UNSTABLE: format_macro_matchers = true
11+
# UNSTABLE: format_strings = true
12+
# UNSTABLE: group_imports = "StdExternalCrate"

src/client.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use std::sync::Arc;
88
use crate::error::VssError;
99
use crate::headers::{get_headermap, FixedHeaders, VssHeaderProvider};
1010
use crate::types::{
11-
DeleteObjectRequest, DeleteObjectResponse, GetObjectRequest, GetObjectResponse, ListKeyVersionsRequest,
12-
ListKeyVersionsResponse, PutObjectRequest, PutObjectResponse,
11+
DeleteObjectRequest, DeleteObjectResponse, GetObjectRequest, GetObjectResponse,
12+
ListKeyVersionsRequest, ListKeyVersionsResponse, PutObjectRequest, PutObjectResponse,
1313
};
1414
use crate::util::retry::{retry, RetryPolicy};
1515

@@ -37,13 +37,20 @@ impl<R: RetryPolicy<E = VssError>> VssClient<R> {
3737

3838
/// Constructs a [`VssClient`] from a given [`reqwest::Client`], using `base_url` as the VSS server endpoint.
3939
pub fn from_client(base_url: String, client: Client, retry_policy: R) -> Self {
40-
Self { base_url, client, retry_policy, header_provider: Arc::new(FixedHeaders::new(HashMap::new())) }
40+
Self {
41+
base_url,
42+
client,
43+
retry_policy,
44+
header_provider: Arc::new(FixedHeaders::new(HashMap::new())),
45+
}
4146
}
4247

4348
/// Constructs a [`VssClient`] using `base_url` as the VSS server endpoint.
4449
///
4550
/// HTTP headers will be provided by the given `header_provider`.
46-
pub fn new_with_headers(base_url: String, retry_policy: R, header_provider: Arc<dyn VssHeaderProvider>) -> Self {
51+
pub fn new_with_headers(
52+
base_url: String, retry_policy: R, header_provider: Arc<dyn VssHeaderProvider>,
53+
) -> Self {
4754
let client = Client::new();
4855
Self { base_url, client, retry_policy, header_provider }
4956
}
@@ -56,7 +63,9 @@ impl<R: RetryPolicy<E = VssError>> VssClient<R> {
5663
/// Fetches a value against a given `key` in `request`.
5764
/// Makes a service call to the `GetObject` endpoint of the VSS server.
5865
/// For API contract/usage, refer to docs for [`GetObjectRequest`] and [`GetObjectResponse`].
59-
pub async fn get_object(&self, request: &GetObjectRequest) -> Result<GetObjectResponse, VssError> {
66+
pub async fn get_object(
67+
&self, request: &GetObjectRequest,
68+
) -> Result<GetObjectResponse, VssError> {
6069
retry(
6170
|| async {
6271
let url = format!("{}/getObject", self.base_url);
@@ -79,7 +88,9 @@ impl<R: RetryPolicy<E = VssError>> VssClient<R> {
7988
/// Makes a service call to the `PutObject` endpoint of the VSS server, with multiple items.
8089
/// Items in the `request` are written in a single all-or-nothing transaction.
8190
/// For API contract/usage, refer to docs for [`PutObjectRequest`] and [`PutObjectResponse`].
82-
pub async fn put_object(&self, request: &PutObjectRequest) -> Result<PutObjectResponse, VssError> {
91+
pub async fn put_object(
92+
&self, request: &PutObjectRequest,
93+
) -> Result<PutObjectResponse, VssError> {
8394
retry(
8495
|| async {
8596
let url = format!("{}/putObjects", self.base_url);
@@ -93,7 +104,9 @@ impl<R: RetryPolicy<E = VssError>> VssClient<R> {
93104
/// Deletes the given `key` and `value` in `request`.
94105
/// Makes a service call to the `DeleteObject` endpoint of the VSS server.
95106
/// For API contract/usage, refer to docs for [`DeleteObjectRequest`] and [`DeleteObjectResponse`].
96-
pub async fn delete_object(&self, request: &DeleteObjectRequest) -> Result<DeleteObjectResponse, VssError> {
107+
pub async fn delete_object(
108+
&self, request: &DeleteObjectRequest,
109+
) -> Result<DeleteObjectResponse, VssError> {
97110
retry(
98111
|| async {
99112
let url = format!("{}/deleteObject", self.base_url);
@@ -120,7 +133,9 @@ impl<R: RetryPolicy<E = VssError>> VssClient<R> {
120133
.await
121134
}
122135

123-
async fn post_request<Rq: Message, Rs: Message + Default>(&self, request: &Rq, url: &str) -> Result<Rs, VssError> {
136+
async fn post_request<Rq: Message, Rs: Message + Default>(
137+
&self, request: &Rq, url: &str,
138+
) -> Result<Rs, VssError> {
124139
let request_body = request.encode_to_vec();
125140
let headermap = self
126141
.header_provider

src/crypto/chacha20.rs

Lines changed: 115 additions & 85 deletions
Large diffs are not rendered by default.

src/crypto/chacha20poly1305.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ mod real_chachapoly {
4848
mac.input(aad);
4949
ChaCha20Poly1305::pad_mac_16(&mut mac, aad.len());
5050

51-
ChaCha20Poly1305 { cipher, mac, finished: false, data_len: 0, aad_len: aad.len() as u64 }
51+
ChaCha20Poly1305 {
52+
cipher,
53+
mac,
54+
finished: false,
55+
data_len: 0,
56+
aad_len: aad.len() as u64,
57+
}
5258
}
5359

5460
pub fn encrypt(&mut self, input: &[u8], output: &mut [u8], out_tag: &mut [u8]) {

src/crypto/poly1305.rs

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,22 @@ pub struct Poly1305 {
2424
impl Poly1305 {
2525
pub fn new(key: &[u8]) -> Poly1305 {
2626
assert!(key.len() == 32);
27-
let mut poly =
28-
Poly1305 { r: [0u32; 5], h: [0u32; 5], pad: [0u32; 4], leftover: 0, buffer: [0u8; 16], finalized: false };
27+
let mut poly = Poly1305 {
28+
r: [0u32; 5],
29+
h: [0u32; 5],
30+
pad: [0u32; 4],
31+
leftover: 0,
32+
buffer: [0u8; 16],
33+
finalized: false,
34+
};
2935

3036
// r &= 0xffffffc0ffffffc0ffffffc0fffffff
3137
poly.r[0] = (u32::from_le_bytes(key[0..4].try_into().expect("len is 4"))) & 0x3ffffff;
3238
poly.r[1] = (u32::from_le_bytes(key[3..7].try_into().expect("len is 4")) >> 2) & 0x3ffff03;
3339
poly.r[2] = (u32::from_le_bytes(key[6..10].try_into().expect("len is 4")) >> 4) & 0x3ffc0ff;
3440
poly.r[3] = (u32::from_le_bytes(key[9..13].try_into().expect("len is 4")) >> 6) & 0x3f03fff;
35-
poly.r[4] = (u32::from_le_bytes(key[12..16].try_into().expect("len is 4")) >> 8) & 0x00fffff;
41+
poly.r[4] =
42+
(u32::from_le_bytes(key[12..16].try_into().expect("len is 4")) >> 8) & 0x00fffff;
3643

3744
poly.pad[0] = u32::from_le_bytes(key[16..20].try_into().expect("len is 4"));
3845
poly.pad[1] = u32::from_le_bytes(key[20..24].try_into().expect("len is 4"));
@@ -272,22 +279,28 @@ mod test {
272279
#[test]
273280
fn test_nacl_vector() {
274281
let key = [
275-
0xee, 0xa6, 0xa7, 0x25, 0x1c, 0x1e, 0x72, 0x91, 0x6d, 0x11, 0xc2, 0xcb, 0x21, 0x4d, 0x3c, 0x25, 0x25, 0x39,
276-
0x12, 0x1d, 0x8e, 0x23, 0x4e, 0x65, 0x2d, 0x65, 0x1f, 0xa4, 0xc8, 0xcf, 0xf8, 0x80,
282+
0xee, 0xa6, 0xa7, 0x25, 0x1c, 0x1e, 0x72, 0x91, 0x6d, 0x11, 0xc2, 0xcb, 0x21, 0x4d,
283+
0x3c, 0x25, 0x25, 0x39, 0x12, 0x1d, 0x8e, 0x23, 0x4e, 0x65, 0x2d, 0x65, 0x1f, 0xa4,
284+
0xc8, 0xcf, 0xf8, 0x80,
277285
];
278286

279287
let msg = [
280-
0x8e, 0x99, 0x3b, 0x9f, 0x48, 0x68, 0x12, 0x73, 0xc2, 0x96, 0x50, 0xba, 0x32, 0xfc, 0x76, 0xce, 0x48, 0x33,
281-
0x2e, 0xa7, 0x16, 0x4d, 0x96, 0xa4, 0x47, 0x6f, 0xb8, 0xc5, 0x31, 0xa1, 0x18, 0x6a, 0xc0, 0xdf, 0xc1, 0x7c,
282-
0x98, 0xdc, 0xe8, 0x7b, 0x4d, 0xa7, 0xf0, 0x11, 0xec, 0x48, 0xc9, 0x72, 0x71, 0xd2, 0xc2, 0x0f, 0x9b, 0x92,
283-
0x8f, 0xe2, 0x27, 0x0d, 0x6f, 0xb8, 0x63, 0xd5, 0x17, 0x38, 0xb4, 0x8e, 0xee, 0xe3, 0x14, 0xa7, 0xcc, 0x8a,
284-
0xb9, 0x32, 0x16, 0x45, 0x48, 0xe5, 0x26, 0xae, 0x90, 0x22, 0x43, 0x68, 0x51, 0x7a, 0xcf, 0xea, 0xbd, 0x6b,
285-
0xb3, 0x73, 0x2b, 0xc0, 0xe9, 0xda, 0x99, 0x83, 0x2b, 0x61, 0xca, 0x01, 0xb6, 0xde, 0x56, 0x24, 0x4a, 0x9e,
286-
0x88, 0xd5, 0xf9, 0xb3, 0x79, 0x73, 0xf6, 0x22, 0xa4, 0x3d, 0x14, 0xa6, 0x59, 0x9b, 0x1f, 0x65, 0x4c, 0xb4,
288+
0x8e, 0x99, 0x3b, 0x9f, 0x48, 0x68, 0x12, 0x73, 0xc2, 0x96, 0x50, 0xba, 0x32, 0xfc,
289+
0x76, 0xce, 0x48, 0x33, 0x2e, 0xa7, 0x16, 0x4d, 0x96, 0xa4, 0x47, 0x6f, 0xb8, 0xc5,
290+
0x31, 0xa1, 0x18, 0x6a, 0xc0, 0xdf, 0xc1, 0x7c, 0x98, 0xdc, 0xe8, 0x7b, 0x4d, 0xa7,
291+
0xf0, 0x11, 0xec, 0x48, 0xc9, 0x72, 0x71, 0xd2, 0xc2, 0x0f, 0x9b, 0x92, 0x8f, 0xe2,
292+
0x27, 0x0d, 0x6f, 0xb8, 0x63, 0xd5, 0x17, 0x38, 0xb4, 0x8e, 0xee, 0xe3, 0x14, 0xa7,
293+
0xcc, 0x8a, 0xb9, 0x32, 0x16, 0x45, 0x48, 0xe5, 0x26, 0xae, 0x90, 0x22, 0x43, 0x68,
294+
0x51, 0x7a, 0xcf, 0xea, 0xbd, 0x6b, 0xb3, 0x73, 0x2b, 0xc0, 0xe9, 0xda, 0x99, 0x83,
295+
0x2b, 0x61, 0xca, 0x01, 0xb6, 0xde, 0x56, 0x24, 0x4a, 0x9e, 0x88, 0xd5, 0xf9, 0xb3,
296+
0x79, 0x73, 0xf6, 0x22, 0xa4, 0x3d, 0x14, 0xa6, 0x59, 0x9b, 0x1f, 0x65, 0x4c, 0xb4,
287297
0x5a, 0x74, 0xe3, 0x55, 0xa5,
288298
];
289299

290-
let expected = [0xf3, 0xff, 0xc7, 0x70, 0x3f, 0x94, 0x00, 0xe5, 0x2a, 0x7d, 0xfb, 0x4b, 0x3d, 0x33, 0x05, 0xd9];
300+
let expected = [
301+
0xf3, 0xff, 0xc7, 0x70, 0x3f, 0x94, 0x00, 0xe5, 0x2a, 0x7d, 0xfb, 0x4b, 0x3d, 0x33,
302+
0x05, 0xd9,
303+
];
291304

292305
let mut mac = [0u8; 16];
293306
poly1305(&key, &msg, &mut mac);
@@ -312,25 +325,35 @@ mod test {
312325
#[test]
313326
fn donna_self_test() {
314327
let wrap_key = [
315-
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
328+
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
316329
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
330+
0x00, 0x00, 0x00, 0x00,
317331
];
318332

319-
let wrap_msg = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
333+
let wrap_msg = [
334+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
335+
0xff, 0xff,
336+
];
320337

321-
let wrap_mac = [0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
338+
let wrap_mac = [
339+
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
340+
0x00, 0x00,
341+
];
322342

323343
let mut mac = [0u8; 16];
324344
poly1305(&wrap_key, &wrap_msg, &mut mac);
325345
assert_eq!(&mac[..], &wrap_mac[..]);
326346

327347
let total_key = [
328-
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xff, 0xff, 0xff, 0xff,
329-
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
348+
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9,
349+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
350+
0x00, 0x00, 0x00, 0x00,
330351
];
331352

332-
let total_mac =
333-
[0x64, 0xaf, 0xe2, 0xe8, 0xd6, 0xad, 0x7b, 0xbd, 0xd2, 0x87, 0xf9, 0x7c, 0x44, 0x62, 0x3d, 0x39];
353+
let total_mac = [
354+
0x64, 0xaf, 0xe2, 0xe8, 0xd6, 0xad, 0x7b, 0xbd, 0xd2, 0x87, 0xf9, 0x7c, 0x44, 0x62,
355+
0x3d, 0x39,
356+
];
334357

335358
let mut tpoly = Poly1305::new(&total_key);
336359
for i in 0..256 {
@@ -349,13 +372,19 @@ mod test {
349372
// from http://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04
350373
let key = b"this is 32-byte key for Poly1305";
351374
let msg = [0u8; 32];
352-
let expected = [0x49, 0xec, 0x78, 0x09, 0x0e, 0x48, 0x1e, 0xc6, 0xc2, 0x6b, 0x33, 0xb9, 0x1c, 0xcc, 0x03, 0x07];
375+
let expected = [
376+
0x49, 0xec, 0x78, 0x09, 0x0e, 0x48, 0x1e, 0xc6, 0xc2, 0x6b, 0x33, 0xb9, 0x1c, 0xcc,
377+
0x03, 0x07,
378+
];
353379
let mut mac = [0u8; 16];
354380
poly1305(key, &msg, &mut mac);
355381
assert_eq!(&mac[..], &expected[..]);
356382

357383
let msg = b"Hello world!";
358-
let expected = [0xa6, 0xf7, 0x45, 0x00, 0x8f, 0x81, 0xc9, 0x16, 0xa2, 0x0d, 0xcc, 0x74, 0xee, 0xf2, 0xb2, 0xf0];
384+
let expected = [
385+
0xa6, 0xf7, 0x45, 0x00, 0x8f, 0x81, 0xc9, 0x16, 0xa2, 0x0d, 0xcc, 0x74, 0xee, 0xf2,
386+
0xb2, 0xf0,
387+
];
359388
poly1305(key, msg, &mut mac);
360389
assert_eq!(&mac[..], &expected[..]);
361390
}

src/error.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ impl VssError {
3636
match ErrorResponse::decode(&payload[..]) {
3737
Ok(error_response) => VssError::from(error_response),
3838
Err(e) => {
39-
let message =
40-
format!("Unable to decode ErrorResponse from server, HttpStatusCode: {}, DecodeErr: {}", status, e);
39+
let message = format!(
40+
"Unable to decode ErrorResponse from server, HttpStatusCode: {}, DecodeErr: {}",
41+
status, e
42+
);
4143
VssError::InternalError(message)
42-
}
44+
},
4345
}
4446
}
4547
}
@@ -49,22 +51,22 @@ impl Display for VssError {
4951
match self {
5052
VssError::NoSuchKeyError(message) => {
5153
write!(f, "Requested key does not exist: {}", message)
52-
}
54+
},
5355
VssError::InvalidRequestError(message) => {
5456
write!(f, "Request sent to VSS Storage was invalid: {}", message)
55-
}
57+
},
5658
VssError::ConflictError(message) => {
5759
write!(f, "Potential version conflict in write operation: {}", message)
58-
}
60+
},
5961
VssError::AuthError(message) => {
6062
write!(f, "Authentication or Authorization failure: {}", message)
61-
}
63+
},
6264
VssError::InternalServerError(message) => {
6365
write!(f, "InternalServerError: {}", message)
64-
}
66+
},
6567
VssError::InternalError(message) => {
6668
write!(f, "InternalError: {}", message)
67-
}
69+
},
6870
}
6971
}
7072
}
@@ -75,10 +77,14 @@ impl From<ErrorResponse> for VssError {
7577
fn from(error_response: ErrorResponse) -> Self {
7678
match error_response.error_code() {
7779
ErrorCode::NoSuchKeyException => VssError::NoSuchKeyError(error_response.message),
78-
ErrorCode::InvalidRequestException => VssError::InvalidRequestError(error_response.message),
80+
ErrorCode::InvalidRequestException => {
81+
VssError::InvalidRequestError(error_response.message)
82+
},
7983
ErrorCode::ConflictException => VssError::ConflictError(error_response.message),
8084
ErrorCode::AuthException => VssError::AuthError(error_response.message),
81-
ErrorCode::InternalServerException => VssError::InternalServerError(error_response.message),
85+
ErrorCode::InternalServerException => {
86+
VssError::InternalServerError(error_response.message)
87+
},
8288
_ => VssError::InternalError(format!(
8389
"VSS responded with an unknown error code: {}, message: {}",
8490
error_response.error_code, error_response.message

0 commit comments

Comments
 (0)