From daa2aedf0c4ce932f0bbe33d6a97384466e5d91e Mon Sep 17 00:00:00 2001 From: jane400 Date: Sun, 18 Aug 2024 18:03:15 +0200 Subject: [PATCH] feat: add `deprecated` LibraryError --- libpaket/src/lib.rs | 2 ++ paket/src/login.rs | 76 +++++++++++++++++---------------------------- 2 files changed, 30 insertions(+), 48 deletions(-) diff --git a/libpaket/src/lib.rs b/libpaket/src/lib.rs index 3de0a18..e88610e 100644 --- a/libpaket/src/lib.rs +++ b/libpaket/src/lib.rs @@ -37,6 +37,8 @@ pub enum LibraryError { DecodeError(String), #[error("upstream api was changed. not continuing")] APIChange, + #[error("upstream api was changed. this method is deprecated")] + Deprecated, } pub type LibraryResult = Result; diff --git a/paket/src/login.rs b/paket/src/login.rs index b491532..e7fc8a5 100644 --- a/paket/src/login.rs +++ b/paket/src/login.rs @@ -323,62 +323,42 @@ impl Login { libpaket::LibraryError::Unauthorized => { sender.input(LoginInput::NeedsLogin); } + libpaket::LibraryError::Deprecated => { + sender.output(LoginOutput::Error(res)).unwrap(); + sender.input(LoginInput::BreakWorld); + } } } } } } -fn convert_library_error_to_response_type(err: &LibraryError) -> ResponseType { - match err { - LibraryError::NetworkFetch => ResponseType::Retry, - LibraryError::Unauthorized => ResponseType::Okay, - LibraryError::InvalidArgument(_) => ResponseType::Okay, - LibraryError::DecodeError(_) => ResponseType::Okay, - LibraryError::APIChange => ResponseType::Okay, - } -} +macro_rules! received { + ($func_name:ident, $args:tt, $calling:ident, $calling_args:tt) => { + async fn $func_name$args -> LoginCommand { + let client = OpenIdClient::new(); + let mut err = LibraryError::NetworkFetch; + for _ in 0..6 { + let result: Result = client + .$calling$calling_args + .await; -async fn received_auth_code(auth_code: String, code_verifier: CodeVerfier) -> LoginCommand { - let client = OpenIdClient::new(); - let mut err = LibraryError::NetworkFetch; - for _ in 0..6 { - let result: Result = client - .token_authorization(auth_code.clone(), &code_verifier) - .await; - - if let Ok(result) = result { - return LoginCommand::Token(Ok(result)) + err = match result { + Ok(result) => return LoginCommand::Token(Ok(result)), + Err(err) => err, + }; + + if err == LibraryError::NetworkFetch { + continue; + } else { + return LoginCommand::Token(Err(err)); + } + } + LoginCommand::Token(Err(err)) } - err = result.unwrap_err(); - let response_type = convert_library_error_to_response_type(&err); - if response_type == ResponseType::Retry { - continue; - } else { - return LoginCommand::Token(Err(err)); - } - } - LoginCommand::Token(Err(err)) + }; } -async fn use_refresh_token(refresh_token: RefreshToken) -> LoginCommand { - let client = OpenIdClient::new(); - let mut err = LibraryError::NetworkFetch; - for _ in 0..6 { - let result: Result = - client.token_refresh(&refresh_token).await; - - err = match result { - Ok(result) => return LoginCommand::Token(Ok(result)), - Err(err) => err, - }; - let response_type = convert_library_error_to_response_type(&err); - if response_type == ResponseType::Retry { - continue; - } else { - return LoginCommand::Token(Err(err)); - } - } - LoginCommand::Token(Err(err)) -} +received!(received_auth_code, (auth_code: String, code_verifier: CodeVerfier), token_authorization, (auth_code.clone(), &code_verifier)); +received!(use_refresh_token, (refresh_token: RefreshToken), token_refresh, (&refresh_token));