From 83fe388e8d5102e54b6c5f8efa0085df0040b3ec Mon Sep 17 00:00:00 2001 From: Adrien Date: Sat, 30 Mar 2024 18:24:04 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A8=20Fix=20clippy=20warnings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.rs | 6 +- src/components/chats_window/interface.rs | 6 ++ src/components/icons.rs | 2 +- src/components/login.rs | 75 +++++++++++------------- src/components/modal.rs | 4 +- src/components/text_input.rs | 48 ++++++++------- 6 files changed, 73 insertions(+), 68 deletions(-) diff --git a/build.rs b/build.rs index 785f8c1..90cf41a 100644 --- a/build.rs +++ b/build.rs @@ -41,14 +41,14 @@ impl<'a> CssColorVariable<'a> { pub fn to_rust(&self) -> String { format!( "const {name}: &str = \"{value}\";", - name = self.name.replace("-", "_").to_uppercase(), + name = self.name.replace('-', "_").to_uppercase(), value = self.value ) } } 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 {") { println!("{}", err); return; @@ -57,7 +57,7 @@ fn export_color_variables(src_path: &PathBuf, dst_path: &PathBuf) { let re = Regex::new(r"^\$([^:]+):[[:space:]]*#([^$]+);[[:space:]]*$").unwrap(); 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 { continue; }; diff --git a/src/components/chats_window/interface.rs b/src/components/chats_window/interface.rs index b797a04..0a43fd3 100644 --- a/src/components/chats_window/interface.rs +++ b/src/components/chats_window/interface.rs @@ -30,3 +30,9 @@ impl Interface { self.sender.send(Tasks::ToggleRoom(room_id)) } } + +impl Default for Interface { + fn default() -> Self { + Self::new() + } +} diff --git a/src/components/icons.rs b/src/components/icons.rs index 98e1097..dca1134 100644 --- a/src/components/icons.rs +++ b/src/components/icons.rs @@ -124,7 +124,7 @@ pub fn Pyramid<'a>(cx: Scope<'a, PyramidProps<'a>>) -> Element<'a> { 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 }, } }) } diff --git a/src/components/login.rs b/src/components/login.rs index 78b5534..6f4e49c 100644 --- a/src/components/login.rs +++ b/src/components/login.rs @@ -30,20 +30,20 @@ use style::{ 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_SECONDARY_150},{COLOR_SECONDARY_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}" ); -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}"); -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}"); async fn generate_random_avatar(url: &String) -> Option { @@ -76,27 +76,27 @@ async fn generate_random_avatar(url: &String) -> Option { res } -const REQUIRED_ERROR_NAME: &'static str = "required"; -const REQUIRED_ERROR_HELPER_TEXT: &'static str = "This field must be completed"; +const REQUIRED_ERROR_NAME: &str = "required"; +const REQUIRED_ERROR_HELPER_TEXT: &str = "This field must be completed"; -const INVALID_URL_ERROR_NAME: &'static str = "url"; -const INVALID_URL_ERROR_HELPER_TEXT: &'static str = "This field must be a valid URL"; +const INVALID_URL_ERROR_NAME: &str = "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_HELPER_TEXT: &'static str = "This field must be a valid email"; +const INVALID_EMAIL_ERROR_NAME: &str = "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_HELPER_TEXT: &'static str = "The password is too weak"; +const TOO_WEAK_PASSWORD_ERROR_NAME: &str = "too_weak_password"; +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 ID_FIELD_NAME: &'static str = "id"; -const PASSWORD_FIELD_NAME: &'static str = "password"; -const CONFIRM_PASSWORD_FIELD_NAME: &'static str = "confirm password"; +const HOMESERVER_FIELD_NAME: &str = "homeserver_url"; +const ID_FIELD_NAME: &str = "id"; +const PASSWORD_FIELD_NAME: &str = "password"; +const CONFIRM_PASSWORD_FIELD_NAME: &str = "confirm password"; -const LOGIN_ID_PLACEHOLDER: &'static str = "Username"; -const REGISTER_ID_PLACEHOLDER: &'static str = "Email"; +const LOGIN_ID_PLACEHOLDER: &str = "Username"; +const REGISTER_ID_PLACEHOLDER: &str = "Email"; #[derive(PartialEq)] enum Process { @@ -273,9 +273,7 @@ fn on_validation_errors( ) { for (field_name, errors) in field_errors { if let Some(handler) = handlers.get(field_name) { - errors - .into_iter() - .for_each(|e| handler.on_validation_error(e)); + errors.iter().for_each(|e| handler.on_validation_error(e)); } else if *field_name == "__all__" { for error in *errors { let code = error.code.to_string(); @@ -289,11 +287,11 @@ fn on_validation_errors( .collect::>(); 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 .iter() .filter(|f| **f != *field_name) - .map(|f| *f) + .copied() .collect::>(); let other_field_names_len = other_field_names.len(); @@ -366,12 +364,12 @@ fn validate_data(data: &Data, process: &Process) -> Result<(), ValidationError> Process::Registration => { let mut is_confirm_password_empty = true; if let Some(confirm_password) = &data.confirm_password { - if confirm_password.len() > 0 { + if !confirm_password.is_empty() { is_confirm_password_empty = false; } } 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); return Err(err); } @@ -389,11 +387,9 @@ fn validate_data(data: &Data, process: &Process) -> Result<(), ValidationError> } fn validate_id(id: &Option, process: &Process) -> Result<(), ValidationError> { - if *process == Process::Registration { - if !id.validate_email() { - let err = ValidationError::new(&INVALID_EMAIL_ERROR_NAME); - return Err(err); - } + if *process == Process::Registration && !id.validate_email() { + let err = ValidationError::new(INVALID_EMAIL_ERROR_NAME); + return Err(err); } Ok(()) @@ -415,7 +411,7 @@ impl PasswordValidationResult { } fn compute_password_score( - password: &String, + password: &str, with_suggestions: Option, ) -> Option { let Ok(estimate) = zxcvbn(password, &[]) else { @@ -705,7 +701,7 @@ pub fn Login<'a>(cx: Scope<'a, LoginProps>) -> Element<'a> { 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(); on_validation_errors(&field_errors, &handlers); } @@ -728,7 +724,7 @@ pub fn Login<'a>(cx: Scope<'a, LoginProps>) -> Element<'a> { 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_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( None, "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| { modal_config.set(None); }; - let rendered_modal = if let Some(modal_config) = modal_config.get() { - Some(render!(generate_modal(modal_config, on_modal_confirm))) - } else { - None - }; + let rendered_modal = modal_config + .get() + .as_ref() + .map(|modal_config| render!(generate_modal(modal_config, on_modal_confirm))); let form_classes_str = form_classes.join(" "); let password_classes_str = password_classes.join(" "); diff --git a/src/components/modal.rs b/src/components/modal.rs index 96cdf86..a440fb1 100644 --- a/src/components/modal.rs +++ b/src/components/modal.rs @@ -186,9 +186,7 @@ pub fn Modal<'a>(cx: Scope<'a, ModalProps<'a>>) -> Element<'a> { Severity::Critical => ErrorButton, }; - if figure.is_none() { - return None; - } + figure.as_ref()?; cx.render(rsx! { style { STYLE_SHEET }, diff --git a/src/components/text_input.rs b/src/components/text_input.rs index 8d9115c..56d770e 100644 --- a/src/components/text_input.rs +++ b/src/components/text_input.rs @@ -42,23 +42,26 @@ impl TextInputState { } } +impl Default for TextInputState { + fn default() -> Self { + Self::new() + } +} + impl InputPropsData for TextInputState {} pub fn TextInput<'a>(cx: Scope<'a, InputProps<'a, TextInputState>>) -> Element<'a> { let mut criticity_class = ""; let mut helper_text = "".to_string(); - match cx.props.state { - Some(state) => { - let state = state.read(); - if !state.is_valid { - criticity_class = ClassName::INVALID; - } - if let Some(text) = &state.helper_text { - helper_text = text.to_string(); - } + if let Some(state) = cx.props.state { + let state = state.read(); + if !state.is_valid { + criticity_class = ClassName::INVALID; + } + if let Some(text) = &state.helper_text { + helper_text = text.to_string(); } - None => {} } 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 {} 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); - match cx.props.state { - Some(state) => { - let state = state.read(); - if !state.text_input_state.is_valid { - criticity_class = ClassName::INVALID; - } - if let Some(text) = &state.text_input_state.helper_text { - helper_text = text.to_string(); - } - score = Some(state.score); + if let Some(state) = cx.props.state { + let state = state.read(); + if !state.text_input_state.is_valid { + criticity_class = ClassName::INVALID; } - None => {} + if let Some(text) = &state.text_input_state.helper_text { + helper_text = text.to_string(); + } + score = Some(state.score); } let text_input_classes = [