Skip to content

Commit f8af02d

Browse files
authored
Merge pull request RustedLessPass#27 from RustedLessPass/Clippy-suggestions
refactor: ♻️ as suggested by clippy
2 parents 4afbcf7 + 42e66ea commit f8af02d

File tree

5 files changed

+73
-54
lines changed

5 files changed

+73
-54
lines changed

src/app.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,13 @@ impl Component for App {
175175
<button
176176
style="white-space: nowrap;margin-left: 0rem; padding-left: 0.5rem; padding-right: 0.5rem; align-self: center;"
177177
onclick={on_password_click}>
178-
<i class={match self.fingerprint.get(0) { Some(s)=> format!("fa fa-fw {}", s),
178+
<i class={match self.fingerprint.first() { Some(s)=> format!("fa fa-fw {}", s),
179179
None => String::new(),
180180
}} style="margin-right: 0.2rem;"></i>
181181
<i class={match self.fingerprint.get(1) { Some(s)=> format!("fa fa-fw {}", s),
182182
None => String::new(),
183183
}} style="margin-left: 0.2rem;margin-right: 0.2rem;p"></i>
184-
<i class={match self.fingerprint.get(2) { Some(s)=> format!("fa fa-fw {}", s),
184+
<i class={match self.fingerprint.last() { Some(s)=> format!("fa fa-fw {}", s),
185185
None => String::new(),
186186
}} style="margin-left: 0.2rem;"></i>
187187
</button> // Password fingerprint icons
@@ -190,24 +190,20 @@ impl Component for App {
190190
<nav>
191191

192192
<Switch label="a-z" onchange={settings_callback!(ctx.link(), settings; lowercase)}
193-
value={settings.lowercase.clone()} value_disabled={if self.settings.disabled.clone()=="a-z" { true } else { false
194-
}} /> // Switch for lowercase
193+
value={settings.lowercase} value_disabled={self.settings.disabled.clone() == "a-z"} /> // Switch for lowercase
195194
<Switch label="A-Z" onchange={settings_callback!(ctx.link(), settings; uppercase)}
196-
value={settings.uppercase.clone()} value_disabled={if self.settings.disabled.clone()=="A-Z" { true } else { false
197-
}} /> // Switch for uppercase
195+
value={settings.uppercase} value_disabled={self.settings.disabled.clone() == "A-Z"} /> // Switch for uppercase
198196
<Switch label="0-9" onchange={settings_callback!(ctx.link(), settings; numbers)}
199-
value={settings.numbers.clone()} value_disabled={if self.settings.disabled.clone()=="0-9" { true } else { false
200-
}} /> // Switch for numbers
197+
value={settings.numbers} value_disabled={self.settings.disabled.clone() == "0-9"} /> // Switch for numbers
201198
<Switch label="%!@" onchange={settings_callback!(ctx.link(), settings; symbols)}
202-
value={settings.symbols.clone()} value_disabled={if self.settings.disabled.clone()=="%!@" { true } else { false
203-
}} /> // Switch for symbols
199+
value={settings.symbols} value_disabled={self.settings.disabled.clone() == "%!@"} /> // Switch for symbols
204200

205201
</nav>
206202
<div class="grid" style="padding: 0rem;">
207203
<Slider label="Size" max=35 min=1 onchange={settings_callback!(ctx.link(), settings; size)}
208-
value={settings.size.clone()} /> // Slider for password size
204+
value={settings.size} /> // Slider for password size
209205
<Slider label="Counter" max=100 min=1 onchange={settings_callback!(ctx.link(), settings; counter)}
210-
value={settings.counter.clone()} /> // Slider for password counter
206+
value={settings.counter} /> // Slider for password counter
211207
</div>
212208

213209
</fieldset>

src/fingerprintgen.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use lesspass::get_fingerprint;
2-
2+
use std::fmt::Write;
33
// Define a constant array of icon names
44
const ICONS: [&str; 46] = [
55
"fa-hashtag",
@@ -78,8 +78,10 @@ pub fn fingerprint_calculate(input: &str) -> Vec<String> {
7878
let hashed_input: String = get_fingerprint(input)
7979
// Convert the byte array to a hexadecimal string
8080
.iter()
81-
.map(|b| format!("{:02x}", b))
82-
.collect();
81+
.fold(String::new(), |mut acc, &byte| {
82+
write!(acc, "{:02x}", byte).expect("Failed to write to string");
83+
acc
84+
});
8385

8486
// Divide the hashed input into segments and assign icons
8587
let mut x = 0;

src/passgen.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
use lesspass::{self, CharacterSet};
22

3+
pub struct PasswordOptions {
4+
pub domain: String,
5+
pub login: String,
6+
pub master_password: String,
7+
pub lowercase: bool,
8+
pub uppercase: bool,
9+
pub digits: bool,
10+
pub symbols: bool,
11+
pub length: usize,
12+
pub counter: u32,
13+
}
14+
315
/// Generates a password based on given parameters.
416
///
517
/// # Arguments
@@ -17,22 +29,21 @@ use lesspass::{self, CharacterSet};
1729
/// # Returns
1830
///
1931
/// A string representing the generated password.
20-
pub fn generate_password(
21-
domain: &str,
22-
login: &str,
23-
master_password: &str,
24-
lowercase: bool,
25-
uppercase: bool,
26-
digits: bool,
27-
symbols: bool,
28-
length: usize,
29-
counter: u32,
30-
) -> String {
31-
let salt = lesspass::generate_salt(domain, login, counter);
32-
let entropy =
33-
lesspass::generate_entropy(master_password, &salt, lesspass::Algorithm::SHA256, 100000);
34-
let charset = generate_charset(lowercase, uppercase, digits, symbols);
35-
lesspass::render_password(&entropy, charset, length)
32+
pub fn generate_password(options: PasswordOptions) -> String {
33+
let salt = lesspass::generate_salt(&options.domain, &options.login, options.counter);
34+
let entropy = lesspass::generate_entropy(
35+
&options.master_password,
36+
&salt,
37+
lesspass::Algorithm::SHA256,
38+
100000,
39+
);
40+
let charset = generate_charset(
41+
options.lowercase,
42+
options.uppercase,
43+
options.digits,
44+
options.symbols,
45+
);
46+
lesspass::render_password(&entropy, charset, options.length)
3647
}
3748

3849
/// Generates a character set based on given parameters.

src/password_utils.rs

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use wasm_bindgen_futures::spawn_local;
22

33
use crate::passgen::generate_password;
4+
use crate::passgen::PasswordOptions;
45
use crate::settings::Settings;
56

67
// Function to update disabled characters based on settings
@@ -11,7 +12,7 @@ pub fn update_disabled_characters(settings: &Settings) -> String {
1112
&& settings.numbers == 0
1213
&& settings.symbols == 0
1314
{
14-
return "a-z".to_string();
15+
"a-z".to_string()
1516
}
1617
// Check if only uppercase characters are enabled
1718
else if settings.lowercase == 0
@@ -57,32 +58,41 @@ pub fn update_show_state(
5758
// If 'show' is 0
5859
0 => {
5960
// Generate new password based on settings
60-
let new_password = generate_password(
61-
website,
62-
username,
63-
password,
64-
settings.lowercase != 0,
65-
settings.uppercase != 0,
66-
settings.numbers != 0,
67-
settings.symbols != 0,
68-
settings.size as usize,
69-
settings.counter as u32,
70-
);
61+
let password_options = PasswordOptions {
62+
domain: website.to_string(),
63+
login: username.to_string(),
64+
master_password: password.to_string(),
65+
lowercase: settings.lowercase != 0,
66+
uppercase: settings.uppercase != 0,
67+
digits: settings.numbers != 0,
68+
symbols: settings.symbols != 0,
69+
length: settings.size as usize,
70+
counter: settings.counter as u32,
71+
};
72+
73+
let new_password = generate_password(password_options);
74+
7175
// Clone the new password for asynchronous use
7276
let cloned_new_password = new_password.clone();
7377
// Spawn a local asynchronous task to interact with the clipboard
74-
let _task = spawn_local(async move {
78+
spawn_local(async move {
7579
let window = web_sys::window().expect("window");
7680
let nav = window.navigator().clipboard();
77-
match nav {
78-
Some(a) => {
79-
let p = a.write_text(&cloned_new_password);
80-
let _result = wasm_bindgen_futures::JsFuture::from(p)
81-
.await
82-
.expect("clipboard populated");
83-
}
84-
None => {}
85-
};
81+
// match nav {
82+
// Some(a) => {
83+
// let p = a.write_text(&cloned_new_password);
84+
// let _result = wasm_bindgen_futures::JsFuture::from(p)
85+
// .await
86+
// .expect("clipboard populated");
87+
// }
88+
// None => {}
89+
// };
90+
if let Some(a) = nav {
91+
let p = a.write_text(&cloned_new_password);
92+
let _result = wasm_bindgen_futures::JsFuture::from(p)
93+
.await
94+
.expect("clipboard populated");
95+
}
8696
});
8797
// Return the updated show state and the new password
8898
(1, new_password.to_string())

src/switch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl Component for Switch {
6666
// Render the switch component
6767
html! {
6868
<label for={id.clone()}>
69-
<input type="checkbox" {oninput} role="switch" id={label} name={label} checked={if display_value == 0 { false } else { true }} disabled={value_disabled}/>
69+
<input type="checkbox" {oninput} role="switch" id={label} name={label} checked={display_value != 0} disabled={value_disabled}/>
7070
{label}
7171
</label>
7272
}

0 commit comments

Comments
 (0)