From 62ec58b1edc43bd81b3175ae86bf7dc3338f8ae5 Mon Sep 17 00:00:00 2001 From: dasha_uwu Date: Sat, 17 Jan 2026 07:20:23 +0500 Subject: [PATCH] Introduce extract!(), rewrite part of uiaa username handling --- src/core/utils/mod.rs | 10 ++++++++++ src/service/uiaa/mod.rs | 16 +++++----------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/core/utils/mod.rs b/src/core/utils/mod.rs index a6927380..ceaeb543 100644 --- a/src/core/utils/mod.rs +++ b/src/core/utils/mod.rs @@ -61,6 +61,16 @@ macro_rules! extract_variant { }; } +#[macro_export] +macro_rules! extract { + ($e:expr_2021, $out:ident in $variant:pat) => { + match $e { + | $variant => Some($out), + | _ => None, + } + }; +} + /// Functor for !is_empty() #[macro_export] macro_rules! is_not_empty { diff --git a/src/service/uiaa/mod.rs b/src/service/uiaa/mod.rs index 1ab83b26..c833394b 100644 --- a/src/service/uiaa/mod.rs +++ b/src/service/uiaa/mod.rs @@ -11,8 +11,8 @@ use ruma::{ }, }; use tuwunel_core::{ - Err, Result, debug_warn, err, error, implement, utils, - utils::{hash, string::EMPTY}, + Err, Result, debug_warn, err, error, extract, implement, + utils::{self, BoolExt, hash, string::EMPTY}, }; use tuwunel_database::{Deserialized, Json, Map}; @@ -89,15 +89,9 @@ pub async fn try_auth( match auth { // Find out what the user completed | AuthData::Password(Password { identifier, password, user, .. }) => { - let username = if let Some(UserIdentifier::UserIdOrLocalpart(username)) = identifier { - username - } else if cfg!(feature = "element_hacks") - && let Some(username) = user - { - username - } else { - return Err!(Request(Unrecognized("Identifier type not recognized."))); - }; + let username = extract!(identifier, x in Some(UserIdentifier::UserIdOrLocalpart(x))) + .or_else(|| cfg!(feature = "element_hacks").and(user.as_ref())) + .ok_or(err!(Request(Unrecognized("Identifier type not recognized."))))?; let user_id_from_username = UserId::parse_with_server_name( username.clone(),