From 6a112a376aae220100b678b2e95f7507e2000b4c Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Wed, 24 Jan 2024 20:04:34 +0000 Subject: [PATCH] Move saving of auth file into auth.rs. Makes more sense there, right next to AuthConfig::load. And we were doing it twice, so it needs to move _somewhere_ central. --- src/auth.rs | 17 +++++++++++++++++ src/login.rs | 10 ++-------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/auth.rs b/src/auth.rs index 78a174d..69a6dca 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -6,6 +6,7 @@ use super::config::ConfigLocation; pub enum AuthError { Nonexistent(String), Bad(String), + SaveFailed(String), } impl super::TopLevelErrorCandidate for AuthError {} @@ -22,6 +23,9 @@ impl std::fmt::Display for AuthError { AuthError::Bad(ref msg) => { write!(f, "unable to read authentication: {}", msg) } + AuthError::SaveFailed(ref msg) => { + write!(f, "unable to save authentication data: {}", msg) + } } } } @@ -59,6 +63,19 @@ impl AuthConfig { Ok(auth) } + pub fn save(&self, cfgloc: &ConfigLocation) -> Result<(), AuthError> { + let mut json = serde_json::to_string_pretty(self) + .expect("should never fail to format this at all"); + json.push('\n'); + cfgloc.create_file("auth", &json).map_err(|e| { + AuthError::SaveFailed(format!( + "unable to parse config file '{}': {}", + cfgloc.get_path("auth").display(), + e + )) + }) + } + pub fn is_logged_in(&self) -> bool { self.account_id.is_some() } diff --git a/src/login.rs b/src/login.rs index 2a73ae2..0e9cf4e 100644 --- a/src/login.rs +++ b/src/login.rs @@ -665,11 +665,8 @@ impl LoginMenu { user_token: Some(token.access_token.clone()), }; - let mut json = serde_json::to_string_pretty(&client.auth).unwrap(); - json.push('\n'); - self.cfgloc.create_file("auth", &json)?; + client.auth.save(&self.cfgloc)?; - // FIXME: in final step, self.finish_account_setup(client, &app, token.access_token.clone()) self.state = LoginState::RegisterFinal; Ok(()) @@ -1072,10 +1069,7 @@ pub fn finish_account_setup( user_token: token, }; - let mut json = serde_json::to_string_pretty(&client.auth).unwrap(); - json.push('\n'); - cfgloc.create_file("auth", &json)?; - + client.auth.save(cfgloc)?; Ok(()) } -- 2.30.2