Skip to content

Commit 88730a5

Browse files
committed
Merge #67: Add rustfmt.toml
afa5525 Add rustfmt.toml (Tobin C. Harding) Pull request description: Introduce a new configuration for `rustfmt`. To keep all commits passing CI do as a single patch the following: - Add a `rustfmt` configuration file copied over from `rust-bitcoin`. - Use new `fn_params_layout = "Tall"` (replace deprecated `fn_args_layout`) - Update CI to run `cargo fmt` using the nightly toolchain since we use a bunch of unstable options. - Run `cargo +nightly fmt` ACKs for top commit: apoelstra: ACK afa5525 Tree-SHA512: b24a4999f85687661e290de6858c033b913d49a560f356c93fcd19831c7ef833293511eb00c7852950a4a76268b3603fe133d015dd1a2f361ee8c1e437aa4510
2 parents 4411810 + afa5525 commit 88730a5

File tree

3 files changed

+101
-75
lines changed

3 files changed

+101
-75
lines changed

.github/workflows/rust.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,12 @@ jobs:
2727
fmt:
2828
name: Rustfmt
2929
runs-on: ubuntu-latest
30-
strategy:
31-
matrix:
32-
rust:
33-
- stable
3430
steps:
3531
- uses: actions/checkout@v2
3632
- uses: actions-rs/toolchain@v1
3733
with:
3834
profile: minimal
39-
toolchain: ${{ matrix.rust }}
35+
toolchain: nightly
4036
override: true
4137
- run: rustup component add rustfmt
4238
- uses: actions-rs/cargo@v1

rustfmt.toml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
ignore = []
2+
3+
hard_tabs = false
4+
tab_spaces = 4
5+
newline_style = "Auto"
6+
indent_style = "Block"
7+
8+
max_width = 100 # This is number of characters.
9+
# `use_small_heuristics` is ignored if the granular width config values are explicitly set.
10+
use_small_heuristics = "Max" # "Max" == All granular width settings same as `max_width`.
11+
# # Granular width configuration settings. These are percentages of `max_width`.
12+
# fn_call_width = 60
13+
# attr_fn_like_width = 70
14+
# struct_lit_width = 18
15+
# struct_variant_width = 35
16+
# array_width = 60
17+
# chain_width = 60
18+
# single_line_if_else_max_width = 50
19+
20+
wrap_comments = false
21+
format_code_in_doc_comments = false
22+
comment_width = 100 # Default 80
23+
normalize_comments = false
24+
normalize_doc_attributes = false
25+
format_strings = false
26+
format_macro_matchers = false
27+
format_macro_bodies = true
28+
hex_literal_case = "Preserve"
29+
empty_item_single_line = true
30+
struct_lit_single_line = true
31+
fn_single_line = true # Default false
32+
where_single_line = false
33+
imports_indent = "Block"
34+
imports_layout = "Mixed"
35+
imports_granularity = "Module" # Default "Preserve"
36+
group_imports = "StdExternalCrate" # Default "Preserve"
37+
reorder_imports = true
38+
reorder_modules = true
39+
reorder_impl_items = false
40+
type_punctuation_density = "Wide"
41+
space_before_colon = false
42+
space_after_colon = true
43+
spaces_around_ranges = false
44+
binop_separator = "Front"
45+
remove_nested_parens = true
46+
combine_control_expr = true
47+
overflow_delimited_expr = false
48+
struct_field_align_threshold = 0
49+
enum_discrim_align_threshold = 0
50+
match_arm_blocks = false # Default true
51+
match_arm_leading_pipes = "Never"
52+
force_multiline_blocks = false
53+
fn_params_layout = "Tall"
54+
brace_style = "SameLineWhere"
55+
control_brace_style = "AlwaysSameLine"
56+
trailing_semicolon = true
57+
trailing_comma = "Vertical"
58+
match_block_trailing_comma = false
59+
blank_lines_upper_bound = 1
60+
blank_lines_lower_bound = 0
61+
edition = "2018"
62+
version = "One"
63+
inline_attribute_width = 0
64+
format_generated_files = true
65+
merge_derives = true
66+
use_try_shorthand = false
67+
use_field_init_shorthand = false
68+
force_explicit_abi = true
69+
condense_wildcard_suffixes = false
70+
color = "Auto"
71+
unstable_features = false
72+
disable_all_formatting = false
73+
skip_children = false
74+
hide_parse_errors = false
75+
error_on_line_overflow = false
76+
error_on_unformatted = false
77+
emit_mode = "Files"
78+
make_backup = false

src/lib.rs

Lines changed: 22 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,15 @@ extern crate alloc;
6363
#[cfg(any(test, feature = "std"))]
6464
extern crate core;
6565

66-
#[cfg(all(not(feature = "std"), not(test)))]
67-
use alloc::{string::String, vec::Vec};
68-
6966
#[cfg(all(not(feature = "std"), not(test)))]
7067
use alloc::borrow::Cow;
68+
#[cfg(all(not(feature = "std"), not(test)))]
69+
use alloc::{string::String, vec::Vec};
70+
use core::convert::Infallible;
71+
use core::{fmt, mem};
7172
#[cfg(any(feature = "std", test))]
7273
use std::borrow::Cow;
7374

74-
use core::{convert::Infallible, fmt, mem};
75-
7675
/// Integer in the range `0..32`
7776
#[derive(PartialEq, Eq, Debug, Copy, Clone, Default, PartialOrd, Ord, Hash)]
7877
#[allow(non_camel_case_types)]
@@ -89,26 +88,18 @@ impl u5 {
8988
}
9089

9190
/// Returns a copy of the underlying `u8` value
92-
pub fn to_u8(self) -> u8 {
93-
self.0
94-
}
91+
pub fn to_u8(self) -> u8 { self.0 }
9592

9693
/// Get char representing this 5 bit value as defined in BIP173
97-
pub fn to_char(self) -> char {
98-
CHARSET[self.to_u8() as usize]
99-
}
94+
pub fn to_char(self) -> char { CHARSET[self.to_u8() as usize] }
10095
}
10196

10297
impl From<u5> for u8 {
103-
fn from(v: u5) -> u8 {
104-
v.0
105-
}
98+
fn from(v: u5) -> u8 { v.0 }
10699
}
107100

108101
impl AsRef<u8> for u5 {
109-
fn as_ref(&self) -> &u8 {
110-
&self.0
111-
}
102+
fn as_ref(&self) -> &u8 { &self.0 }
112103
}
113104

114105
/// Interface to write `u5`s into a sink
@@ -148,11 +139,7 @@ impl<'a> Bech32Writer<'a> {
148139
variant: Variant,
149140
fmt: &'a mut fmt::Write,
150141
) -> Result<Bech32Writer<'a>, fmt::Error> {
151-
let mut writer = Bech32Writer {
152-
formatter: fmt,
153-
chk: 1,
154-
variant,
155-
};
142+
let mut writer = Bech32Writer { formatter: fmt, chk: 1, variant };
156143

157144
writer.formatter.write_str(hrp)?;
158145
writer.formatter.write_char(SEP)?;
@@ -196,8 +183,7 @@ impl<'a> Bech32Writer<'a> {
196183
let plm: u32 = self.chk ^ self.variant.constant();
197184

198185
for p in 0..CHECKSUM_LENGTH {
199-
self.formatter
200-
.write_char(u5(((plm >> (5 * (5 - p))) & 0x1f) as u8).to_char())?;
186+
self.formatter.write_char(u5(((plm >> (5 * (5 - p))) & 0x1f) as u8).to_char())?;
201187
}
202188

203189
Ok(())
@@ -216,8 +202,7 @@ impl<'a> WriteBase32 for Bech32Writer<'a> {
216202

217203
impl<'a> Drop for Bech32Writer<'a> {
218204
fn drop(&mut self) {
219-
self.write_checksum()
220-
.expect("Unhandled error writing the checksum on drop.")
205+
self.write_checksum().expect("Unhandled error writing the checksum on drop.")
221206
}
222207
}
223208

@@ -250,9 +235,7 @@ impl FromBase32 for Vec<u8> {
250235

251236
/// Convert base32 to base256, removes null-padding if present, returns
252237
/// `Err(Error::InvalidPadding)` if padding bits are unequal `0`
253-
fn from_base32(b32: &[u5]) -> Result<Self, Self::Err> {
254-
convert_bits(b32, 5, 8, false)
255-
}
238+
fn from_base32(b32: &[u5]) -> Result<Self, Self::Err> { convert_bits(b32, 5, 8, false) }
256239
}
257240

258241
/// A trait for converting a value to a type `T` that represents a `u5` slice.
@@ -344,10 +327,7 @@ impl<T: AsRef<[u8]>> CheckBase32<Vec<u5>> for T {
344327
type Err = Error;
345328

346329
fn check_base32(self) -> Result<Vec<u5>, Self::Err> {
347-
self.as_ref()
348-
.iter()
349-
.map(|x| u5::try_from_u8(*x))
350-
.collect::<Result<Vec<u5>, Error>>()
330+
self.as_ref().iter().map(|x| u5::try_from_u8(*x)).collect::<Result<Vec<u5>, Error>>()
351331
}
352332
}
353333

@@ -534,9 +514,7 @@ pub fn decode(s: &str) -> Result<(String, Vec<u5>, Variant), Error> {
534514
/// Decode a bech32 string into the raw HRP and the data bytes, assuming no checksum.
535515
///
536516
/// Returns the HRP in lowercase and the data.
537-
pub fn decode_without_checksum(s: &str) -> Result<(String, Vec<u5>), Error> {
538-
split_and_decode(s)
539-
}
517+
pub fn decode_without_checksum(s: &str) -> Result<(String, Vec<u5>), Error> { split_and_decode(s) }
540518

541519
/// Decode a bech32 string into the raw HRP and the `u5` data.
542520
fn split_and_decode(s: &str) -> Result<(String, Vec<u5>), Error> {
@@ -651,13 +629,7 @@ const CHARSET_REV: [i8; 128] = [
651629
];
652630

653631
/// Generator coefficients
654-
const GEN: [u32; 5] = [
655-
0x3b6a_57b2,
656-
0x2650_8e6d,
657-
0x1ea1_19fa,
658-
0x3d42_33dd,
659-
0x2a14_62b3,
660-
];
632+
const GEN: [u32; 5] = [0x3b6a_57b2, 0x2650_8e6d, 0x1ea1_19fa, 0x3d42_33dd, 0x2a14_62b3];
661633

662634
/// Error types for Bech32 encoding / decoding.
663635
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
@@ -865,13 +837,7 @@ mod tests {
865837
(vec![0x01], 8, 8, true, vec![0x01]),
866838
(vec![0x01], 8, 4, true, vec![0x00, 0x01]),
867839
(vec![0x01], 8, 2, true, vec![0x00, 0x00, 0x00, 0x01]),
868-
(
869-
vec![0x01],
870-
8,
871-
1,
872-
true,
873-
vec![0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01],
874-
),
840+
(vec![0x01], 8, 1, true, vec![0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]),
875841
(vec![0xff], 8, 5, true, vec![0x1f, 0x1c]),
876842
(vec![0x1f, 0x1c], 5, 8, false, vec![0xff]),
877843
];
@@ -921,30 +887,20 @@ mod tests {
921887
assert!([0u8, 1, 2, 30, 31, 255].check_base32().is_err());
922888

923889
assert!([1u8, 2, 3, 4].check_base32().is_ok());
924-
assert_eq!(
925-
[30u8, 31, 35, 20].check_base32(),
926-
Err(Error::InvalidData(35))
927-
);
890+
assert_eq!([30u8, 31, 35, 20].check_base32(), Err(Error::InvalidData(35)));
928891
}
929892

930893
#[test]
931894
fn test_encode() {
932895
assert_eq!(
933-
encode(
934-
"",
935-
vec![1u8, 2, 3, 4].check_base32().unwrap(),
936-
Variant::Bech32
937-
),
896+
encode("", vec![1u8, 2, 3, 4].check_base32().unwrap(), Variant::Bech32),
938897
Err(Error::InvalidLength)
939898
);
940899
}
941900

942901
#[test]
943902
fn from_base32() {
944-
assert_eq!(
945-
Vec::from_base32(&[0x1f, 0x1c].check_base32().unwrap()),
946-
Ok(vec![0xff])
947-
);
903+
assert_eq!(Vec::from_base32(&[0x1f, 0x1c].check_base32().unwrap()), Ok(vec![0xff]));
948904
assert_eq!(
949905
Vec::from_base32(&[0x1f, 0x1f].check_base32().unwrap()),
950906
Err(Error::InvalidPadding)
@@ -966,9 +922,8 @@ mod tests {
966922
}
967923
}
968924

969-
let expected_rev_charset = (0u8..128)
970-
.map(|i| get_char_value(i as char))
971-
.collect::<Vec<_>>();
925+
let expected_rev_charset =
926+
(0u8..128).map(|i| get_char_value(i as char)).collect::<Vec<_>>();
972927

973928
assert_eq!(&(CHARSET_REV[..]), expected_rev_charset.as_slice());
974929
}
@@ -1003,10 +958,7 @@ mod tests {
1003958

1004959
let encoded_str = encode_without_checksum(hrp, data).unwrap();
1005960

1006-
assert_eq!(
1007-
encoded_str,
1008-
written_str[..written_str.len() - CHECKSUM_LENGTH]
1009-
);
961+
assert_eq!(encoded_str, written_str[..written_str.len() - CHECKSUM_LENGTH]);
1010962
}
1011963

1012964
#[test]

0 commit comments

Comments
 (0)