pub enum AuthError {
Nonexistent(String),
Bad(String),
+ SaveFailed(String),
}
impl super::TopLevelErrorCandidate 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)
+ }
}
}
}
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()
}
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(())
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(())
}