@@ -51,6 +51,11 @@ lazy_static! {
51
51
/// println!("Generated password: {}", password);
52
52
/// ```
53
53
///
54
+ /// # Panics
55
+ ///
56
+ /// The function may panic in the event a word from the list the crate embeds were to contain
57
+ /// non-UTF-8 characters.
58
+ ///
54
59
/// # Returns
55
60
///
56
61
/// A `String` containing the generated memorable password
@@ -64,16 +69,15 @@ pub fn memorable_password<R: Rng>(
64
69
) -> String {
65
70
// Get the random words and format them
66
71
let formatted_words: Vec < String > = get_random_words ( rng, word_count)
67
- . iter ( )
72
+ . into_iter ( )
68
73
. map ( |word| {
69
74
let mut word = word. to_string ( ) ;
70
75
71
76
// Scramble the word if requested
72
77
if scramble {
73
78
let mut bytes = word. to_string ( ) . into_bytes ( ) ;
74
79
bytes. shuffle ( rng) ;
75
- word =
76
- String :: from_utf8 ( bytes. to_vec ( ) ) . expect ( "random words should be valid UTF-8" ) ;
80
+ word = String :: from_utf8 ( bytes) . expect ( "random words should be valid UTF-8" ) ;
77
81
}
78
82
79
83
// Capitalize the word if requested
@@ -95,18 +99,18 @@ pub fn memorable_password<R: Rng>(
95
99
Separator :: Underscore => formatted_words. join ( "_" ) ,
96
100
Separator :: Numbers => formatted_words
97
101
. iter ( )
98
- . map ( |s| s . to_string ( ) )
102
+ . map ( String :: to_string)
99
103
. intersperse_with ( || rng. gen_range ( 0 ..10 ) . to_string ( ) )
100
104
. collect ( ) ,
101
105
Separator :: NumbersAndSymbols => {
102
106
let numbers_and_symbols: Vec < char > = SYMBOL_CHARS
103
107
. iter ( )
104
108
. chain ( NUMBER_CHARS . iter ( ) )
105
- . cloned ( )
109
+ . copied ( )
106
110
. collect ( ) ;
107
111
formatted_words
108
112
. iter ( )
109
- . map ( |s| s . to_string ( ) )
113
+ . map ( String :: to_string)
110
114
. intersperse_with ( || {
111
115
numbers_and_symbols
112
116
. choose ( rng)
@@ -156,6 +160,10 @@ pub enum Separator {
156
160
/// * `numbers: bool` - A flag indicating whether numbers should be included in the password
157
161
/// * `symbols: bool` - A flag indicating whether symbols should be included in the password
158
162
///
163
+ /// # Panics
164
+ ///
165
+ /// The function may panic in the event that the provided `characters` argument is 0.
166
+ ///
159
167
/// # Returns
160
168
///
161
169
/// * `String` - The generated random password
@@ -193,8 +201,7 @@ pub fn random_password<R: Rng>(
193
201
194
202
// If either numbers or symbols is true, but not the other, we want
195
203
// to make sure that we apply the following distribution: 80% letters, 20% numbers.
196
- ( true , false ) => vec ! [ 8 , 2 ] ,
197
- ( false , true ) => vec ! [ 8 , 2 ] ,
204
+ ( true , false ) | ( false , true ) => vec ! [ 8 , 2 ] ,
198
205
199
206
// Otherwise we want to make sure that we apply the following distribution: 100% letters.
200
207
( false , false ) => vec ! [ 10 ] ,
@@ -260,7 +267,7 @@ const SYMBOL_CHARS: &[char] = &['!', '@', '#', '$', '%', '^', '&', '*', '(', ')'
260
267
261
268
// get_random_words returns a vector of n random words from the word list
262
269
fn get_random_words < R : Rng > ( rng : & mut R , n : usize ) -> Vec < & ' static str > {
263
- WORDS_LIST . choose_multiple ( rng, n) . cloned ( ) . collect ( )
270
+ WORDS_LIST . choose_multiple ( rng, n) . copied ( ) . collect ( )
264
271
}
265
272
266
273
#[ cfg( test) ]
0 commit comments