From e19d5db7c6025674fde716349524d96ba684841d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabor=20K=C3=B6rber?= Date: Fri, 5 Jul 2024 13:38:30 +0200 Subject: [PATCH] refactor: using match --- src/admin_examples/user_repository.rs | 47 +++++++++++++-------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/src/admin_examples/user_repository.rs b/src/admin_examples/user_repository.rs index a44d41a..29e36c0 100644 --- a/src/admin_examples/user_repository.rs +++ b/src/admin_examples/user_repository.rs @@ -98,32 +98,29 @@ impl AdminRepository for UserRepository { ..Default::default() }; - if let Some(value) = data.get("password") { - user.password = Set(value.as_str().unwrap().to_owned()); - } + let keys = [ + "password", + "first_name", + "last_name", + "email", + "is_staff", + "is_active", + "is_superuser", + ]; - if let Some(value) = data.get("first_name") { - user.first_name = Set(value.as_str().map(|s| s.to_owned())); - } - - if let Some(value) = data.get("last_name") { - user.last_name = Set(value.as_str().map(|s| s.to_owned())); - } - - if let Some(value) = data.get("email") { - user.email = Set(value.as_str().map(|s| s.to_owned())); - } - - if let Some(value) = data.get("is_staff") { - user.is_staff = Set(value.as_bool().unwrap_or(false)); - } - - if let Some(value) = data.get("is_active") { - user.is_active = Set(value.as_bool().unwrap_or(false)); - } - - if let Some(value) = data.get("is_superuser") { - user.is_superuser = Set(value.as_bool().unwrap_or(false)); + for key in &keys { + if let Some(value) = data.get(*key) { + match *key { + "password" => user.password = Set(value.as_str().unwrap().to_owned()), + "first_name" => user.first_name = Set(value.as_str().map(|s| s.to_owned())), + "last_name" => user.last_name = Set(value.as_str().map(|s| s.to_owned())), + "email" => user.email = Set(value.as_str().map(|s| s.to_owned())), + "is_staff" => user.is_staff = Set(value.as_bool().unwrap_or(false)), + "is_active" => user.is_active = Set(value.as_bool().unwrap_or(false)), + "is_superuser" => user.is_superuser = Set(value.as_bool().unwrap_or(false)), + _ => (), + } + } } if let Ok(user) = user.insert(&self.connection).await {