chiark / gitweb /
Move saving of auth file into auth.rs.
authorSimon Tatham <anakin@pobox.com>
Wed, 24 Jan 2024 20:04:34 +0000 (20:04 +0000)
committerSimon Tatham <anakin@pobox.com>
Wed, 24 Jan 2024 20:08:37 +0000 (20:08 +0000)
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
src/login.rs

index 78a174dba6aac916797ed8e4d0b9b4f71454008d..69a6dcacbc334b181a15404d923c071e7112c346 100644 (file)
@@ -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()
     }
index 2a73ae2b7823e30e408c0710023ba2360955b7b3..0e9cf4ebf81c5f9a85fcba2f459cd8d0abd51d48 100644 (file)
@@ -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(())
 }