From be6da4a9d82467690e7388bc69db4d9facf7a6b8 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 18 Apr 2020 16:13:17 -0400 Subject: refactor --- src/actions.rs | 107 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 65 insertions(+), 42 deletions(-) (limited to 'src/actions.rs') diff --git a/src/actions.rs b/src/actions.rs index 634fa9a..0fea7df 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -46,18 +46,16 @@ pub async fn unlock( pub async fn sync( access_token: &str, refresh_token: &str, -) -> Result<(Option, String, Vec)> { - let res = sync_once(access_token).await; - match res { - Ok((protected_key, ciphers)) => Ok((None, protected_key, ciphers)), - Err(crate::error::Error::RequestUnauthorized) => { - let access_token = - exchange_refresh_token_async(refresh_token).await?; - let (protected_key, ciphers) = sync_once(&access_token).await?; - Ok((Some(access_token), protected_key, ciphers)) - } - Err(e) => Err(e), - } +) -> Result<(Option, (String, Vec))> { + with_exchange_refresh_token_async( + access_token, + refresh_token, + |access_token| { + let access_token = access_token.to_string(); + Box::pin(async move { sync_once(&access_token).await }) + }, + ) + .await } async fn sync_once( @@ -76,16 +74,10 @@ pub fn add( username: Option<&str>, password: Option<&str>, notes: Option<&str>, -) -> Result> { - match add_once(access_token, name, username, password, notes) { - Ok(()) => Ok(None), - Err(crate::error::Error::RequestUnauthorized) => { - let access_token = exchange_refresh_token(refresh_token)?; - add_once(&access_token, name, username, password, notes)?; - Ok(Some(access_token)) - } - Err(e) => Err(e), - } +) -> Result<(Option, ())> { + with_exchange_refresh_token(access_token, refresh_token, |access_token| { + add_once(access_token, name, username, password, notes) + }) } fn add_once( @@ -110,16 +102,10 @@ pub fn edit( username: Option<&str>, password: Option<&str>, notes: Option<&str>, -) -> Result> { - match edit_once(access_token, id, name, username, password, notes) { - Ok(()) => Ok(None), - Err(crate::error::Error::RequestUnauthorized) => { - let access_token = exchange_refresh_token(refresh_token)?; - edit_once(&access_token, id, name, username, password, notes)?; - Ok(Some(access_token)) - } - Err(e) => Err(e), - } +) -> Result<(Option, ())> { + with_exchange_refresh_token(access_token, refresh_token, |access_token| { + edit_once(access_token, id, name, username, password, notes) + }) } fn edit_once( @@ -141,16 +127,10 @@ pub fn remove( access_token: &str, refresh_token: &str, id: &str, -) -> Result> { - match remove_once(access_token, id) { - Ok(()) => Ok(None), - Err(crate::error::Error::RequestUnauthorized) => { - let access_token = exchange_refresh_token(refresh_token)?; - remove_once(&access_token, id)?; - Ok(Some(access_token)) - } - Err(e) => Err(e), - } +) -> Result<(Option, ())> { + with_exchange_refresh_token(access_token, refresh_token, |access_token| { + remove_once(access_token, id) + }) } fn remove_once(access_token: &str, id: &str) -> Result<()> { @@ -161,6 +141,49 @@ fn remove_once(access_token: &str, id: &str) -> Result<()> { Ok(()) } +fn with_exchange_refresh_token( + access_token: &str, + refresh_token: &str, + f: F, +) -> Result<(Option, T)> +where + F: Fn(&str) -> Result, +{ + match f(access_token) { + Ok(t) => Ok((None, t)), + Err(crate::error::Error::RequestUnauthorized) => { + let access_token = exchange_refresh_token(refresh_token)?; + let t = f(&access_token)?; + Ok((Some(access_token), t)) + } + Err(e) => Err(e), + } +} + +async fn with_exchange_refresh_token_async( + access_token: &str, + refresh_token: &str, + f: F, +) -> Result<(Option, T)> +where + F: Fn( + &str, + ) -> std::pin::Pin< + Box> + Send>, + >, +{ + match f(access_token).await { + Ok(t) => Ok((None, t)), + Err(crate::error::Error::RequestUnauthorized) => { + let access_token = + exchange_refresh_token_async(refresh_token).await?; + let t = f(&access_token).await?; + Ok((Some(access_token), t)) + } + Err(e) => Err(e), + } +} + fn exchange_refresh_token(refresh_token: &str) -> Result { let config = crate::config::Config::load()?; let client = -- cgit v1.2.3-54-g00ecf