Skip to content

Commit abc14d1

Browse files
Merge pull request marshallpierce#186 from marshallpierce/danieleades-refactor/code-quality
Danieleades refactor/code quality
2 parents c3e790c + 616a388 commit abc14d1

26 files changed

+148
-186
lines changed

.circleci/config.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ jobs:
5353
command: |
5454
rustup component add rustfmt
5555
cargo fmt -- --check
56+
- run:
57+
name: Check clippy lints
58+
# we only care about stable clippy -- nightly clippy is a bit wild
59+
command: |
60+
if [[ '<< parameters.toolchain_override >>' == 'stable' ]]
61+
then
62+
rustup component add clippy
63+
cargo clippy --all-targets
64+
fi
5665
- run:
5766
name: Build all targets
5867
command: cargo build --all-targets

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ This library's goals are to be *correct* and *fast*. It's thoroughly tested and
1313
## Example
1414

1515
```rust
16-
extern crate base64;
17-
1816
use base64::{encode, decode};
1917

2018
fn main() {

benches/benchmarks.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
extern crate base64;
21
#[macro_use]
32
extern crate criterion;
4-
extern crate rand;
53

64
use base64::display;
75
use base64::{

clippy.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
msrv = "1.47.0"

examples/base64.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ enum Alphabet {
1515

1616
impl Default for Alphabet {
1717
fn default() -> Self {
18-
Alphabet::Standard
18+
Self::Standard
1919
}
2020
}
2121

2222
impl FromStr for Alphabet {
2323
type Err = String;
24-
fn from_str(s: &str) -> Result<Alphabet, String> {
24+
fn from_str(s: &str) -> Result<Self, String> {
2525
match s {
26-
"standard" => Ok(Alphabet::Standard),
27-
"urlsafe" => Ok(Alphabet::UrlSafe),
26+
"standard" => Ok(Self::Standard),
27+
"urlsafe" => Ok(Self::UrlSafe),
2828
_ => Err(format!("alphabet '{}' unrecognized", s)),
2929
}
3030
}

fuzz/fuzzers/roundtrip_no_pad.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![no_main]
22
#[macro_use] extern crate libfuzzer_sys;
33
extern crate base64;
4+
45
use base64::engine::fast_portable;
56

67
fuzz_target!(|data: &[u8]| {

fuzz/fuzzers/utils.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
extern crate base64;
21
extern crate rand;
32
extern crate rand_pcg;
43
extern crate ring;

src/alphabet.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub struct Alphabet {
2727
impl Alphabet {
2828
/// Performs no checks so that it can be const.
2929
/// Used only for known-valid strings.
30-
const fn from_str_unchecked(alphabet: &str) -> Alphabet {
30+
const fn from_str_unchecked(alphabet: &str) -> Self {
3131
let mut symbols = [0_u8; ALPHABET_SIZE];
3232
let source_bytes = alphabet.as_bytes();
3333

@@ -38,7 +38,7 @@ impl Alphabet {
3838
index += 1;
3939
}
4040

41-
Alphabet { symbols }
41+
Self { symbols }
4242
}
4343

4444
/// Create an `Alphabet` from a string of 64 unique printable ASCII bytes.
@@ -97,7 +97,7 @@ impl convert::TryFrom<&str> for Alphabet {
9797
type Error = ParseAlphabetError;
9898

9999
fn try_from(value: &str) -> Result<Self, Self::Error> {
100-
Alphabet::from_str(value)
100+
Self::from_str(value)
101101
}
102102
}
103103

@@ -117,10 +117,10 @@ pub enum ParseAlphabetError {
117117
impl fmt::Display for ParseAlphabetError {
118118
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
119119
match self {
120-
ParseAlphabetError::InvalidLength => write!(f, "Invalid length - must be 64 bytes"),
121-
ParseAlphabetError::DuplicatedByte(b) => write!(f, "Duplicated byte: {:#04x}", b),
122-
ParseAlphabetError::UnprintableByte(b) => write!(f, "Unprintable byte: {:#04x}", b),
123-
ParseAlphabetError::ReservedByte(b) => write!(f, "Reserved byte: {:#04x}", b),
120+
Self::InvalidLength => write!(f, "Invalid length - must be 64 bytes"),
121+
Self::DuplicatedByte(b) => write!(f, "Duplicated byte: {:#04x}", b),
122+
Self::UnprintableByte(b) => write!(f, "Unprintable byte: {:#04x}", b),
123+
Self::ReservedByte(b) => write!(f, "Reserved byte: {:#04x}", b),
124124
}
125125
}
126126
}
@@ -238,6 +238,6 @@ mod tests {
238238
STANDARD,
239239
Alphabet::try_from("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
240240
.unwrap()
241-
)
241+
);
242242
}
243243
}

src/chunked_encoder.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,13 @@ pub mod tests {
201201

202202
fn chunked_encode_str(bytes: &[u8], config: FastPortableConfig) -> String {
203203
let mut s = String::new();
204-
{
205-
let mut sink = StringSink::from(&mut s);
206-
let engine = FastPortable::from(&STANDARD, config);
207-
let encoder = ChunkedEncoder::from(&engine);
208-
encoder.encode(bytes, &mut sink).unwrap();
209-
}
210204

211-
return s;
205+
let mut sink = StringSink::from(&mut s);
206+
let engine = FastPortable::from(&STANDARD, config);
207+
let encoder = ChunkedEncoder::from(&engine);
208+
encoder.encode(bytes, &mut sink).unwrap();
209+
210+
s
212211
}
213212

214213
// An abstraction around sinks so that we can have tests that easily to any sink implementation
@@ -222,10 +221,8 @@ pub mod tests {
222221
fn encode_to_string<E: Engine>(&self, engine: &E, bytes: &[u8]) -> String {
223222
let encoder = ChunkedEncoder::from(engine);
224223
let mut s = String::new();
225-
{
226-
let mut sink = StringSink::from(&mut s);
227-
encoder.encode(bytes, &mut sink).unwrap();
228-
}
224+
let mut sink = StringSink::from(&mut s);
225+
encoder.encode(bytes, &mut sink).unwrap();
229226

230227
s
231228
}

src/decode.rs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@ pub enum DecodeError {
3030
impl fmt::Display for DecodeError {
3131
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3232
match *self {
33-
DecodeError::InvalidByte(index, byte) => {
34-
write!(f, "Invalid byte {}, offset {}.", byte, index)
35-
}
36-
DecodeError::InvalidLength => write!(f, "Encoded text cannot have a 6-bit remainder."),
37-
DecodeError::InvalidLastSymbol(index, byte) => {
33+
Self::InvalidByte(index, byte) => write!(f, "Invalid byte {}, offset {}.", byte, index),
34+
Self::InvalidLength => write!(f, "Encoded text cannot have a 6-bit remainder."),
35+
Self::InvalidLastSymbol(index, byte) => {
3836
write!(f, "Invalid last symbol {}, offset {}.", byte, index)
3937
}
4038
}
@@ -45,9 +43,9 @@ impl fmt::Display for DecodeError {
4543
impl error::Error for DecodeError {
4644
fn description(&self) -> &str {
4745
match *self {
48-
DecodeError::InvalidByte(_, _) => "invalid byte",
49-
DecodeError::InvalidLength => "invalid length",
50-
DecodeError::InvalidLastSymbol(_, _) => "invalid last symbol",
46+
Self::InvalidByte(_, _) => "invalid byte",
47+
Self::InvalidLength => "invalid length",
48+
Self::InvalidLastSymbol(_, _) => "invalid last symbol",
5149
}
5250
}
5351

@@ -62,12 +60,8 @@ impl error::Error for DecodeError {
6260
///# Example
6361
///
6462
///```rust
65-
///extern crate base64;
66-
///
67-
///fn main() {
68-
/// let bytes = base64::decode("aGVsbG8gd29ybGQ=").unwrap();
69-
/// println!("{:?}", bytes);
70-
///}
63+
/// let bytes = base64::decode("aGVsbG8gd29ybGQ=").unwrap();
64+
/// println!("{:?}", bytes);
7165
///```
7266
#[cfg(any(feature = "alloc", feature = "std", test))]
7367
pub fn decode<T: AsRef<[u8]>>(input: T) -> Result<Vec<u8>, DecodeError> {
@@ -80,9 +74,6 @@ pub fn decode<T: AsRef<[u8]>>(input: T) -> Result<Vec<u8>, DecodeError> {
8074
///# Example
8175
///
8276
///```rust
83-
///extern crate base64;
84-
///
85-
///fn main() {
8677
/// let bytes = base64::decode_engine(
8778
/// "aGVsbG8gd29ybGR+Cg==",
8879
/// &base64::engine::DEFAULT_ENGINE,
@@ -98,7 +89,6 @@ pub fn decode<T: AsRef<[u8]>>(input: T) -> Result<Vec<u8>, DecodeError> {
9889
///
9990
/// ).unwrap();
10091
/// println!("{:?}", bytes_url);
101-
///}
10292
///```
10393
#[cfg(any(feature = "alloc", feature = "std", test))]
10494
pub fn decode_engine<E: Engine, T: AsRef<[u8]>>(
@@ -117,8 +107,6 @@ pub fn decode_engine<E: Engine, T: AsRef<[u8]>>(
117107
///# Example
118108
///
119109
///```rust
120-
///extern crate base64;
121-
///
122110
///const URL_SAFE_ENGINE: base64::engine::fast_portable::FastPortable =
123111
/// base64::engine::fast_portable::FastPortable::from(
124112
/// &base64::alphabet::URL_SAFE,
@@ -162,11 +150,8 @@ pub fn decode_engine_vec<E: Engine, T: AsRef<[u8]>>(
162150
.expect("Overflow when calculating output buffer length");
163151
buffer.resize(total_len_estimate, 0);
164152

165-
let bytes_written;
166-
{
167-
let buffer_slice = &mut buffer.as_mut_slice()[starting_output_len..];
168-
bytes_written = engine.decode(input_bytes, buffer_slice, estimate)?;
169-
}
153+
let buffer_slice = &mut buffer.as_mut_slice()[starting_output_len..];
154+
let bytes_written = engine.decode(input_bytes, buffer_slice, estimate)?;
170155

171156
buffer.truncate(starting_output_len + bytes_written);
172157

0 commit comments

Comments
 (0)