From 99d3a5de9d56bcb4f0367d8c8d6277ad14edd973 Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 30 Oct 2022 12:58:44 -0500 Subject: improve docs a little more --- azalea-client/src/account.rs | 8 +++++++- azalea-client/src/get_mc_dir.rs | 43 +++++++++++++++++++++++++++++++++-------- 2 files changed, 42 insertions(+), 9 deletions(-) (limited to 'azalea-client/src') diff --git a/azalea-client/src/account.rs b/azalea-client/src/account.rs index f63d342e..715f2fba 100644 --- a/azalea-client/src/account.rs +++ b/azalea-client/src/account.rs @@ -34,7 +34,13 @@ impl Account { /// a key for the cache, but it's recommended to use the real email to /// avoid confusion. pub async fn microsoft(email: &str) -> Result { - let minecraft_dir = get_mc_dir::minecraft_dir().unwrap(); + let minecraft_dir = get_mc_dir::minecraft_dir().expect( + format!( + "No {} environment variable found", + get_mc_dir::home_env_var() + ) + .as_str(), + ); let auth_result = azalea_auth::auth( email, azalea_auth::AuthOpts { diff --git a/azalea-client/src/get_mc_dir.rs b/azalea-client/src/get_mc_dir.rs index abc5b3c8..a8bddb1b 100644 --- a/azalea-client/src/get_mc_dir.rs +++ b/azalea-client/src/get_mc_dir.rs @@ -10,25 +10,52 @@ use std::path::PathBuf; /// Mac: `$HOME/Library/Application Support/minecraft`\ /// Linux: `$HOME/.minecraft` /// -/// Anywhere else it'll return None. +/// If the environment variable is not set, this will return `None`. pub fn minecraft_dir() -> Option { + let env_var = home_env_var(); + let home = std::env::var(env_var)?; + let path = PathBuf::from(home).join(minecraft_dir_relative()); + Some(path) +} + +/// Return the name of the environment variable that's used for the home folder +/// on the user's operating system. +pub fn home_env_var() -> &'static str { + #[cfg(target_os = "windows")] + { + "USERPROFILE" + } + #[cfg(target_os = "macos")] + { + "HOME" + } + #[cfg(target_os = "linux")] + { + "HOME" + } + #[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))] + { + "HOME" + } +} + +/// Return the path relative to the home folder where we expect to find the +/// .minecraft directory. +pub fn minecraft_dir_relative() -> &'static str { #[cfg(target_os = "windows")] { - let appdata = std::env::var("APPDATA").ok()?; - Some(PathBuf::from(appdata).join(".minecraft")) + ".minecraft" } #[cfg(target_os = "macos")] { - let home = std::env::var("HOME").ok()?; - Some(PathBuf::from(home).join("Library/Application Support/minecraft")) + "Library/Application Support/minecraft" } #[cfg(target_os = "linux")] { - let home = std::env::var("HOME").ok()?; - Some(PathBuf::from(home).join(".minecraft")) + ".minecraft" } #[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))] { - None + ".minecraft" } } -- cgit v1.2.3