🚨 Fix clippy warnings

This commit is contained in:
2024-03-30 18:24:04 +01:00
parent 448b81b65d
commit 83fe388e8d
6 changed files with 73 additions and 68 deletions

View File

@@ -41,14 +41,14 @@ impl<'a> CssColorVariable<'a> {
pub fn to_rust(&self) -> String { pub fn to_rust(&self) -> String {
format!( format!(
"const {name}: &str = \"{value}\";", "const {name}: &str = \"{value}\";",
name = self.name.replace("-", "_").to_uppercase(), name = self.name.replace('-', "_").to_uppercase(),
value = self.value value = self.value
) )
} }
} }
fn export_color_variables(src_path: &PathBuf, dst_path: &PathBuf) { fn export_color_variables(src_path: &PathBuf, dst_path: &PathBuf) {
let mut dst_file = File::create(&dst_path).unwrap(); let mut dst_file = File::create(dst_path).unwrap();
if let Err(err) = dst_file.write(b"#[allow(dead_code)]\nmod style {") { if let Err(err) = dst_file.write(b"#[allow(dead_code)]\nmod style {") {
println!("{}", err); println!("{}", err);
return; return;
@@ -57,7 +57,7 @@ fn export_color_variables(src_path: &PathBuf, dst_path: &PathBuf) {
let re = Regex::new(r"^\$([^:]+):[[:space:]]*#([^$]+);[[:space:]]*$").unwrap(); let re = Regex::new(r"^\$([^:]+):[[:space:]]*#([^$]+);[[:space:]]*$").unwrap();
if let Ok(lines) = read_lines(src_path) { if let Ok(lines) = read_lines(src_path) {
for line in lines.flatten() { for line in lines.map_while(Result::ok) {
let Some(groups) = re.captures(&line) else { let Some(groups) = re.captures(&line) else {
continue; continue;
}; };

View File

@@ -30,3 +30,9 @@ impl Interface {
self.sender.send(Tasks::ToggleRoom(room_id)) self.sender.send(Tasks::ToggleRoom(room_id))
} }
} }
impl Default for Interface {
fn default() -> Self {
Self::new()
}
}

View File

@@ -124,7 +124,7 @@ pub fn Pyramid<'a>(cx: Scope<'a, PyramidProps<'a>>) -> Element<'a> {
Icon { Icon {
class: ClassName::PYRAMID_ICON, class: ClassName::PYRAMID_ICON,
icon: PyramidShape { ratio: cx.props.ratio, color: color, progress_color: progress_color }, icon: PyramidShape { ratio: cx.props.ratio, color, progress_color },
} }
}) })
} }

View File

@@ -30,20 +30,20 @@ use style::{
turf::style_sheet!("src/components/login.scss"); turf::style_sheet!("src/components/login.scss");
const BACKGROUND_COLORS_STR: &'static str = formatcp!( const BACKGROUND_COLORS_STR: &str = formatcp!(
"{COLOR_PRIMARY_150},{COLOR_PRIMARY_140},\ "{COLOR_PRIMARY_150},{COLOR_PRIMARY_140},\
{COLOR_SECONDARY_150},{COLOR_SECONDARY_140},\ {COLOR_SECONDARY_150},{COLOR_SECONDARY_140},\
{COLOR_TERNARY_150},{COLOR_TERNARY_140}" {COLOR_TERNARY_150},{COLOR_TERNARY_140}"
); );
const SHAPE_1_COLORS_STR: &'static str = formatcp!( const SHAPE_1_COLORS_STR: &str = formatcp!(
"{COLOR_PRIMARY_120},{COLOR_PRIMARY_110},{COLOR_PRIMARY_100},{COLOR_PRIMARY_90},{COLOR_PRIMARY_80}" "{COLOR_PRIMARY_120},{COLOR_PRIMARY_110},{COLOR_PRIMARY_100},{COLOR_PRIMARY_90},{COLOR_PRIMARY_80}"
); );
const SHAPE_2_COLORS_STR: &'static str = formatcp!( const SHAPE_2_COLORS_STR: &str = formatcp!(
"{COLOR_SECONDARY_120},{COLOR_SECONDARY_110},{COLOR_SECONDARY_100},{COLOR_SECONDARY_90},{COLOR_SECONDARY_80}"); "{COLOR_SECONDARY_120},{COLOR_SECONDARY_110},{COLOR_SECONDARY_100},{COLOR_SECONDARY_90},{COLOR_SECONDARY_80}");
const SHAPE_3_COLORS_STR: &'static str = formatcp!( const SHAPE_3_COLORS_STR: &str = formatcp!(
"{COLOR_TERNARY_120},{COLOR_TERNARY_110},{COLOR_TERNARY_100},{COLOR_TERNARY_90},{COLOR_TERNARY_80}"); "{COLOR_TERNARY_120},{COLOR_TERNARY_110},{COLOR_TERNARY_100},{COLOR_TERNARY_90},{COLOR_TERNARY_80}");
async fn generate_random_avatar(url: &String) -> Option<String> { async fn generate_random_avatar(url: &String) -> Option<String> {
@@ -76,27 +76,27 @@ async fn generate_random_avatar(url: &String) -> Option<String> {
res res
} }
const REQUIRED_ERROR_NAME: &'static str = "required"; const REQUIRED_ERROR_NAME: &str = "required";
const REQUIRED_ERROR_HELPER_TEXT: &'static str = "This field must be completed"; const REQUIRED_ERROR_HELPER_TEXT: &str = "This field must be completed";
const INVALID_URL_ERROR_NAME: &'static str = "url"; const INVALID_URL_ERROR_NAME: &str = "url";
const INVALID_URL_ERROR_HELPER_TEXT: &'static str = "This field must be a valid URL"; const INVALID_URL_ERROR_HELPER_TEXT: &str = "This field must be a valid URL";
const INVALID_EMAIL_ERROR_NAME: &'static str = "email"; const INVALID_EMAIL_ERROR_NAME: &str = "email";
const INVALID_EMAIL_ERROR_HELPER_TEXT: &'static str = "This field must be a valid email"; const INVALID_EMAIL_ERROR_HELPER_TEXT: &str = "This field must be a valid email";
const TOO_WEAK_PASSWORD_ERROR_NAME: &'static str = "too_weak_password"; const TOO_WEAK_PASSWORD_ERROR_NAME: &str = "too_weak_password";
const TOO_WEAK_PASSWORD_ERROR_HELPER_TEXT: &'static str = "The password is too weak"; const TOO_WEAK_PASSWORD_ERROR_HELPER_TEXT: &str = "The password is too weak";
const FIELDS_MISMATCH_ERROR_NAME: &'static str = "mismatch"; const FIELDS_MISMATCH_ERROR_NAME: &str = "mismatch";
const HOMESERVER_FIELD_NAME: &'static str = "homeserver_url"; const HOMESERVER_FIELD_NAME: &str = "homeserver_url";
const ID_FIELD_NAME: &'static str = "id"; const ID_FIELD_NAME: &str = "id";
const PASSWORD_FIELD_NAME: &'static str = "password"; const PASSWORD_FIELD_NAME: &str = "password";
const CONFIRM_PASSWORD_FIELD_NAME: &'static str = "confirm password"; const CONFIRM_PASSWORD_FIELD_NAME: &str = "confirm password";
const LOGIN_ID_PLACEHOLDER: &'static str = "Username"; const LOGIN_ID_PLACEHOLDER: &str = "Username";
const REGISTER_ID_PLACEHOLDER: &'static str = "Email"; const REGISTER_ID_PLACEHOLDER: &str = "Email";
#[derive(PartialEq)] #[derive(PartialEq)]
enum Process { enum Process {
@@ -273,9 +273,7 @@ fn on_validation_errors(
) { ) {
for (field_name, errors) in field_errors { for (field_name, errors) in field_errors {
if let Some(handler) = handlers.get(field_name) { if let Some(handler) = handlers.get(field_name) {
errors errors.iter().for_each(|e| handler.on_validation_error(e));
.into_iter()
.for_each(|e| handler.on_validation_error(e));
} else if *field_name == "__all__" { } else if *field_name == "__all__" {
for error in *errors { for error in *errors {
let code = error.code.to_string(); let code = error.code.to_string();
@@ -289,11 +287,11 @@ fn on_validation_errors(
.collect::<Vec<_>>(); .collect::<Vec<_>>();
for field_name in field_names.iter() { for field_name in field_names.iter() {
if let Some(handler) = handlers.get(*field_name) { if let Some(handler) = handlers.get(field_name) {
let other_field_names = field_names let other_field_names = field_names
.iter() .iter()
.filter(|f| **f != *field_name) .filter(|f| **f != *field_name)
.map(|f| *f) .copied()
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let other_field_names_len = other_field_names.len(); let other_field_names_len = other_field_names.len();
@@ -366,12 +364,12 @@ fn validate_data(data: &Data, process: &Process) -> Result<(), ValidationError>
Process::Registration => { Process::Registration => {
let mut is_confirm_password_empty = true; let mut is_confirm_password_empty = true;
if let Some(confirm_password) = &data.confirm_password { if let Some(confirm_password) = &data.confirm_password {
if confirm_password.len() > 0 { if !confirm_password.is_empty() {
is_confirm_password_empty = false; is_confirm_password_empty = false;
} }
} }
if is_confirm_password_empty { if is_confirm_password_empty {
let mut err = ValidationError::new(&REQUIRED_ERROR_NAME); let mut err = ValidationError::new(REQUIRED_ERROR_NAME);
err.add_param(Cow::from("field_name"), &CONFIRM_PASSWORD_FIELD_NAME); err.add_param(Cow::from("field_name"), &CONFIRM_PASSWORD_FIELD_NAME);
return Err(err); return Err(err);
} }
@@ -389,11 +387,9 @@ fn validate_data(data: &Data, process: &Process) -> Result<(), ValidationError>
} }
fn validate_id(id: &Option<String>, process: &Process) -> Result<(), ValidationError> { fn validate_id(id: &Option<String>, process: &Process) -> Result<(), ValidationError> {
if *process == Process::Registration { if *process == Process::Registration && !id.validate_email() {
if !id.validate_email() { let err = ValidationError::new(INVALID_EMAIL_ERROR_NAME);
let err = ValidationError::new(&INVALID_EMAIL_ERROR_NAME); return Err(err);
return Err(err);
}
} }
Ok(()) Ok(())
@@ -415,7 +411,7 @@ impl PasswordValidationResult {
} }
fn compute_password_score( fn compute_password_score(
password: &String, password: &str,
with_suggestions: Option<bool>, with_suggestions: Option<bool>,
) -> Option<PasswordValidationResult> { ) -> Option<PasswordValidationResult> {
let Ok(estimate) = zxcvbn(password, &[]) else { let Ok(estimate) = zxcvbn(password, &[]) else {
@@ -705,7 +701,7 @@ pub fn Login<'a>(cx: Scope<'a, LoginProps>) -> Element<'a> {
spinner_animated.set(true); spinner_animated.set(true);
if let Err(errors) = on_login(&session, &data_ref) { if let Err(errors) = on_login(session, data_ref) {
let field_errors = errors.field_errors(); let field_errors = errors.field_errors();
on_validation_errors(&field_errors, &handlers); on_validation_errors(&field_errors, &handlers);
} }
@@ -728,7 +724,7 @@ pub fn Login<'a>(cx: Scope<'a, LoginProps>) -> Element<'a> {
spinner_animated.set(true); spinner_animated.set(true);
if let Err(errors) = on_register(&session, &data_ref) { if let Err(errors) = on_register(session, data_ref) {
let field_name = PASSWORD_FIELD_NAME; let field_name = PASSWORD_FIELD_NAME;
let field_errors = errors.field_errors(); let field_errors = errors.field_errors();
@@ -750,7 +746,7 @@ pub fn Login<'a>(cx: Scope<'a, LoginProps>) -> Element<'a> {
} }
}); });
if suggestions_msgs.len() > 0 { if !suggestions_msgs.is_empty() {
let mut modal_config = PasswordSuggestionsModalConfig::new( let mut modal_config = PasswordSuggestionsModalConfig::new(
None, None,
"Let's talk about your password".to_string(), "Let's talk about your password".to_string(),
@@ -794,11 +790,10 @@ pub fn Login<'a>(cx: Scope<'a, LoginProps>) -> Element<'a> {
let on_modal_confirm = move |_: Event<MouseData>| { let on_modal_confirm = move |_: Event<MouseData>| {
modal_config.set(None); modal_config.set(None);
}; };
let rendered_modal = if let Some(modal_config) = modal_config.get() { let rendered_modal = modal_config
Some(render!(generate_modal(modal_config, on_modal_confirm))) .get()
} else { .as_ref()
None .map(|modal_config| render!(generate_modal(modal_config, on_modal_confirm)));
};
let form_classes_str = form_classes.join(" "); let form_classes_str = form_classes.join(" ");
let password_classes_str = password_classes.join(" "); let password_classes_str = password_classes.join(" ");

View File

@@ -186,9 +186,7 @@ pub fn Modal<'a>(cx: Scope<'a, ModalProps<'a>>) -> Element<'a> {
Severity::Critical => ErrorButton, Severity::Critical => ErrorButton,
}; };
if figure.is_none() { figure.as_ref()?;
return None;
}
cx.render(rsx! { cx.render(rsx! {
style { STYLE_SHEET }, style { STYLE_SHEET },

View File

@@ -42,23 +42,26 @@ impl TextInputState {
} }
} }
impl Default for TextInputState {
fn default() -> Self {
Self::new()
}
}
impl InputPropsData for TextInputState {} impl InputPropsData for TextInputState {}
pub fn TextInput<'a>(cx: Scope<'a, InputProps<'a, TextInputState>>) -> Element<'a> { pub fn TextInput<'a>(cx: Scope<'a, InputProps<'a, TextInputState>>) -> Element<'a> {
let mut criticity_class = ""; let mut criticity_class = "";
let mut helper_text = "".to_string(); let mut helper_text = "".to_string();
match cx.props.state { if let Some(state) = cx.props.state {
Some(state) => { let state = state.read();
let state = state.read(); if !state.is_valid {
if !state.is_valid { criticity_class = ClassName::INVALID;
criticity_class = ClassName::INVALID; }
} if let Some(text) = &state.helper_text {
if let Some(text) = &state.helper_text { helper_text = text.to_string();
helper_text = text.to_string();
}
} }
None => {}
} }
let input_classes_str = [ClassName::TEXT_INPUT_INPUT, criticity_class].join(" "); let input_classes_str = [ClassName::TEXT_INPUT_INPUT, criticity_class].join(" ");
@@ -118,6 +121,12 @@ impl PasswordInputState {
} }
} }
impl Default for PasswordInputState {
fn default() -> Self {
Self::new()
}
}
impl InputPropsData for PasswordInputState {} impl InputPropsData for PasswordInputState {}
pub fn PasswordTextInput<'a>(cx: Scope<'a, InputProps<'a, PasswordInputState>>) -> Element<'a> { pub fn PasswordTextInput<'a>(cx: Scope<'a, InputProps<'a, PasswordInputState>>) -> Element<'a> {
@@ -127,18 +136,15 @@ pub fn PasswordTextInput<'a>(cx: Scope<'a, InputProps<'a, PasswordInputState>>)
let show_password = use_state(cx, || false); let show_password = use_state(cx, || false);
match cx.props.state { if let Some(state) = cx.props.state {
Some(state) => { let state = state.read();
let state = state.read(); if !state.text_input_state.is_valid {
if !state.text_input_state.is_valid { criticity_class = ClassName::INVALID;
criticity_class = ClassName::INVALID;
}
if let Some(text) = &state.text_input_state.helper_text {
helper_text = text.to_string();
}
score = Some(state.score);
} }
None => {} if let Some(text) = &state.text_input_state.helper_text {
helper_text = text.to_string();
}
score = Some(state.score);
} }
let text_input_classes = [ let text_input_classes = [