Skip to content

Commit f0e2772

Browse files
committed
rustfmt: Run on lightning-invoice/src/ser.rs
1 parent 83c316d commit f0e2772

File tree

1 file changed

+46
-66
lines changed

1 file changed

+46
-66
lines changed

lightning-invoice/src/ser.rs

Lines changed: 46 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
use crate::prelude::*;
2+
use bech32::{u5, Base32Len, ToBase32, WriteBase32};
13
use core::fmt;
24
use core::fmt::{Display, Formatter};
3-
use bech32::{ToBase32, u5, WriteBase32, Base32Len};
4-
use crate::prelude::*;
55

6-
use super::{Bolt11Invoice, Sha256, TaggedField, ExpiryTime, MinFinalCltvExpiryDelta, Fallback, PayeePubKey, Bolt11InvoiceSignature, PositiveTimestamp,
7-
PrivateRoute, Description, RawTaggedField, Currency, RawHrp, SiPrefix, constants, SignedRawBolt11Invoice, RawDataPart};
6+
use super::{
7+
constants, Bolt11Invoice, Bolt11InvoiceSignature, Currency, Description, ExpiryTime, Fallback,
8+
MinFinalCltvExpiryDelta, PayeePubKey, PositiveTimestamp, PrivateRoute, RawDataPart, RawHrp,
9+
RawTaggedField, Sha256, SiPrefix, SignedRawBolt11Invoice, TaggedField,
10+
};
811

912
/// Converts a stream of bytes written to it to base32. On finalization the according padding will
1013
/// be applied. That means the results of writing two data blocks with one or two `BytesToBase32`
@@ -24,11 +27,7 @@ impl<'a, W: WriteBase32> BytesToBase32<'a, W> {
2427
/// Create a new bytes-to-base32 converter with `writer` as a sink for the resulting base32
2528
/// data.
2629
pub fn new(writer: &'a mut W) -> BytesToBase32<'a, W> {
27-
BytesToBase32 {
28-
writer,
29-
buffer: 0,
30-
buffer_bits: 0,
31-
}
30+
BytesToBase32 { writer, buffer: 0, buffer_bits: 0 }
3231
}
3332

3433
/// Add more bytes to the current conversion unit
@@ -44,9 +43,7 @@ impl<'a, W: WriteBase32> BytesToBase32<'a, W> {
4443
// buffer holds too many bits, so we don't have to combine buffer bits with new bits
4544
// from this rounds byte.
4645
if self.buffer_bits >= 5 {
47-
self.writer.write_u5(
48-
u5::try_from_u8((self.buffer & 0b11111000) >> 3 ).expect("<32")
49-
)?;
46+
self.writer.write_u5(u5::try_from_u8((self.buffer & 0b11111000) >> 3).expect("<32"))?;
5047
self.buffer <<= 5;
5148
self.buffer_bits -= 5;
5249
}
@@ -63,18 +60,16 @@ impl<'a, W: WriteBase32> BytesToBase32<'a, W> {
6360
Ok(())
6461
}
6562

66-
pub fn finalize(mut self) -> Result<(), W::Err> {
63+
pub fn finalize(mut self) -> Result<(), W::Err> {
6764
self.inner_finalize()?;
6865
core::mem::forget(self);
6966
Ok(())
7067
}
7168

72-
fn inner_finalize(&mut self) -> Result<(), W::Err>{
69+
fn inner_finalize(&mut self) -> Result<(), W::Err> {
7370
// There can be at most two u5s left in the buffer after processing all bytes, write them.
7471
if self.buffer_bits >= 5 {
75-
self.writer.write_u5(
76-
u5::try_from_u8((self.buffer & 0b11111000) >> 3).expect("<32")
77-
)?;
72+
self.writer.write_u5(u5::try_from_u8((self.buffer & 0b11111000) >> 3).expect("<32"))?;
7873
self.buffer <<= 5;
7974
self.buffer_bits -= 5;
8075
}
@@ -115,7 +110,7 @@ impl Display for Bolt11Invoice {
115110
impl Display for SignedRawBolt11Invoice {
116111
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
117112
let hrp = self.raw_invoice.hrp.to_string();
118-
let mut data = self.raw_invoice.data.to_base32();
113+
let mut data = self.raw_invoice.data.to_base32();
119114
data.extend_from_slice(&self.signature.to_base32());
120115

121116
bech32::encode_to_fmt(f, &hrp, data, bech32::Variant::Bech32).expect("HRP is valid")?;
@@ -137,13 +132,7 @@ impl Display for RawHrp {
137132
None => String::new(),
138133
};
139134

140-
write!(
141-
f,
142-
"ln{}{}{}",
143-
self.currency,
144-
amount,
145-
si_prefix
146-
)
135+
write!(f, "ln{}{}{}", self.currency, amount, si_prefix)
147136
}
148137
}
149138

@@ -162,7 +151,9 @@ impl Display for Currency {
162151

163152
impl Display for SiPrefix {
164153
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
165-
write!(f, "{}",
154+
write!(
155+
f,
156+
"{}",
166157
match *self {
167158
SiPrefix::Milli => "m",
168159
SiPrefix::Micro => "u",
@@ -215,7 +206,8 @@ fn encode_int_be_base256<T: Into<u64>>(int: T) -> Vec<u8> {
215206
/// Appends the default value of `T` to the front of the `in_vec` till it reaches the length
216207
/// `target_length`. If `in_vec` already is too lang `None` is returned.
217208
fn try_stretch<T>(mut in_vec: Vec<T>, target_len: usize) -> Option<Vec<T>>
218-
where T: Default + Copy
209+
where
210+
T: Default + Copy,
219211
{
220212
if in_vec.len() > target_len {
221213
None
@@ -248,20 +240,16 @@ impl ToBase32 for PositiveTimestamp {
248240
// FIXME: use writer for int encoding
249241
writer.write(
250242
&try_stretch(encode_int_be_base32(self.as_unix_timestamp()), 7)
251-
.expect("Can't be longer due than 7 u5s due to timestamp bounds")
243+
.expect("Can't be longer due than 7 u5s due to timestamp bounds"),
252244
)
253245
}
254246
}
255247

256248
impl ToBase32 for RawTaggedField {
257249
fn write_base32<W: WriteBase32>(&self, writer: &mut W) -> Result<(), <W as WriteBase32>::Err> {
258250
match *self {
259-
RawTaggedField::UnknownSemantics(ref content) => {
260-
writer.write(content)
261-
},
262-
RawTaggedField::KnownSemantics(ref tagged_field) => {
263-
tagged_field.write_base32(writer)
264-
}
251+
RawTaggedField::UnknownSemantics(ref content) => writer.write(content),
252+
RawTaggedField::KnownSemantics(ref tagged_field) => tagged_field.write_base32(writer),
265253
}
266254
}
267255
}
@@ -279,13 +267,13 @@ impl Base32Len for Sha256 {
279267

280268
impl ToBase32 for Description {
281269
fn write_base32<W: WriteBase32>(&self, writer: &mut W) -> Result<(), <W as WriteBase32>::Err> {
282-
self.0.0.as_bytes().write_base32(writer)
270+
self.0 .0.as_bytes().write_base32(writer)
283271
}
284272
}
285273

286274
impl Base32Len for Description {
287275
fn base32_len(&self) -> usize {
288-
self.0.0.as_bytes().base32_len()
276+
self.0 .0.as_bytes().base32_len()
289277
}
290278
}
291279

@@ -328,7 +316,7 @@ impl Base32Len for MinFinalCltvExpiryDelta {
328316
impl ToBase32 for Fallback {
329317
fn write_base32<W: WriteBase32>(&self, writer: &mut W) -> Result<(), <W as WriteBase32>::Err> {
330318
match *self {
331-
Fallback::SegWitProgram {version: v, program: ref p} => {
319+
Fallback::SegWitProgram { version: v, program: ref p } => {
332320
writer.write_u5(u5::try_from_u8(v.to_num()).expect("witness version <= 16"))?;
333321
p.write_base32(writer)
334322
},
@@ -339,20 +327,18 @@ impl ToBase32 for Fallback {
339327
Fallback::ScriptHash(ref hash) => {
340328
writer.write_u5(u5::try_from_u8(18).expect("18 < 32"))?;
341329
(&hash[..]).write_base32(writer)
342-
}
330+
},
343331
}
344332
}
345333
}
346334

347335
impl Base32Len for Fallback {
348336
fn base32_len(&self) -> usize {
349337
match *self {
350-
Fallback::SegWitProgram {program: ref p, ..} => {
338+
Fallback::SegWitProgram { program: ref p, .. } => {
351339
bytes_size_to_base32_size(p.len()) + 1
352340
},
353-
Fallback::PubKeyHash(_) | Fallback::ScriptHash(_) => {
354-
33
355-
},
341+
Fallback::PubKeyHash(_) | Fallback::ScriptHash(_) => 33,
356342
}
357343
}
358344
}
@@ -363,28 +349,21 @@ impl ToBase32 for PrivateRoute {
363349

364350
for hop in (self.0).0.iter() {
365351
converter.append(&hop.src_node_id.serialize()[..])?;
366-
let short_channel_id = try_stretch(
367-
encode_int_be_base256(hop.short_channel_id),
368-
8
369-
).expect("sizeof(u64) == 8");
352+
let short_channel_id = try_stretch(encode_int_be_base256(hop.short_channel_id), 8)
353+
.expect("sizeof(u64) == 8");
370354
converter.append(&short_channel_id)?;
371355

372-
let fee_base_msat = try_stretch(
373-
encode_int_be_base256(hop.fees.base_msat),
374-
4
375-
).expect("sizeof(u32) == 4");
356+
let fee_base_msat = try_stretch(encode_int_be_base256(hop.fees.base_msat), 4)
357+
.expect("sizeof(u32) == 4");
376358
converter.append(&fee_base_msat)?;
377359

378-
let fee_proportional_millionths = try_stretch(
379-
encode_int_be_base256(hop.fees.proportional_millionths),
380-
4
381-
).expect("sizeof(u32) == 4");
360+
let fee_proportional_millionths =
361+
try_stretch(encode_int_be_base256(hop.fees.proportional_millionths), 4)
362+
.expect("sizeof(u32) == 4");
382363
converter.append(&fee_proportional_millionths)?;
383364

384-
let cltv_expiry_delta = try_stretch(
385-
encode_int_be_base256(hop.cltv_expiry_delta),
386-
2
387-
).expect("sizeof(u16) == 2");
365+
let cltv_expiry_delta = try_stretch(encode_int_be_base256(hop.cltv_expiry_delta), 2)
366+
.expect("sizeof(u16) == 2");
388367
converter.append(&cltv_expiry_delta)?;
389368
}
390369

@@ -404,17 +383,18 @@ impl ToBase32 for TaggedField {
404383
/// Writes a tagged field: tag, length and data. `tag` should be in `0..32` otherwise the
405384
/// function will panic.
406385
fn write_tagged_field<W, P>(writer: &mut W, tag: u8, payload: &P) -> Result<(), W::Err>
407-
where W: WriteBase32,
408-
P: ToBase32 + Base32Len,
386+
where
387+
W: WriteBase32,
388+
P: ToBase32 + Base32Len,
409389
{
410390
let len = payload.base32_len();
411391
assert!(len < 1024, "Every tagged field data can be at most 1023 bytes long.");
412392

413393
writer.write_u5(u5::try_from_u8(tag).expect("invalid tag, not in 0..32"))?;
414-
writer.write(&try_stretch(
415-
encode_int_be_base32(len as u64),
416-
2
417-
).expect("Can't be longer than 2, see assert above."))?;
394+
writer.write(
395+
&try_stretch(encode_int_be_base32(len as u64), 2)
396+
.expect("Can't be longer than 2, see assert above."),
397+
)?;
418398
payload.write_base32(writer)
419399
}
420400

@@ -444,10 +424,10 @@ impl ToBase32 for TaggedField {
444424
write_tagged_field(writer, constants::TAG_PRIVATE_ROUTE, route_hops)
445425
},
446426
TaggedField::PaymentSecret(ref payment_secret) => {
447-
write_tagged_field(writer, constants::TAG_PAYMENT_SECRET, payment_secret)
427+
write_tagged_field(writer, constants::TAG_PAYMENT_SECRET, payment_secret)
448428
},
449429
TaggedField::PaymentMetadata(ref payment_metadata) => {
450-
write_tagged_field(writer, constants::TAG_PAYMENT_METADATA, payment_metadata)
430+
write_tagged_field(writer, constants::TAG_PAYMENT_METADATA, payment_metadata)
451431
},
452432
TaggedField::Features(ref features) => {
453433
write_tagged_field(writer, constants::TAG_FEATURES, features)

0 commit comments

Comments
 (0)