Format with rustfmt

This commit is contained in:
oSumAtrIX 2022-01-28 23:13:05 +01:00
parent dbe6e6bc6c
commit 40d65a7213
No known key found for this signature in database
GPG key ID: A9B3094ACDB604B4

View file

@ -3,14 +3,14 @@ use crate::error::SpotifyError;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tokio::{ use tokio::{
fs::create_dir_all,
fs::File, fs::File,
fs::create_dir_all,
io::{AsyncReadExt, AsyncWriteExt}, io::{AsyncReadExt, AsyncWriteExt},
}; };
use std::{ use std::{
env, env,
path::{Path,PathBuf}, path::{Path, PathBuf},
}; };
// Structure for holding all the settings // Structure for holding all the settings
@ -28,59 +28,56 @@ pub struct Settings {
// XDG Base Directory Specification for storing config files // XDG Base Directory Specification for storing config files
#[cfg(target_family = "unix")] #[cfg(target_family = "unix")]
fn get_config_folder_path() -> PathBuf { fn get_config_folder_path() -> PathBuf {
match env::var("XDG_CONFIG_HOME") { match env::var("XDG_CONFIG_HOME") {
Ok(v) => Path::new(&v).join("down_on_spot").to_path_buf(),
Ok(v) => Path::new(&v).join("down_on_spot").to_path_buf(), Err(_) => Path::new(&env::var("HOME").unwrap()).join(".config/down_on_spot"),
}
Err(_) => Path::new(&env::var("HOME").unwrap()).join(".config/down_on_spot"),
}
} }
// On Windows, follow whatever windows does for AppData // On Windows, follow whatever windows does for AppData
#[cfg(target_family = "windows")] #[cfg(target_family = "windows")]
fn get_config_folder_path() -> PathBuf { fn get_config_folder_path() -> PathBuf {
Path::new(&env::var("APPDATA").unwrap()).join("down_on_spot"); Path::new(&env::var("APPDATA").unwrap()).join("down_on_spot");
} }
impl Settings { impl Settings {
// Create new instance // Create new instance
pub fn new(username: &str, password: &str, client_id: &str, client_secret: &str) -> Settings { pub fn new(username: &str, password: &str, client_id: &str, client_secret: &str) -> Settings {
Settings { Settings {
username: username.to_string(), username: username.to_string(),
password: password.to_string(), password: password.to_string(),
client_id: client_id.to_string(), client_id: client_id.to_string(),
client_secret: client_secret.to_string(), client_secret: client_secret.to_string(),
refresh_ui_seconds: 1, refresh_ui_seconds: 1,
downloader: DownloaderConfig::new(), downloader: DownloaderConfig::new(),
} }
} }
// Save config // Save config
pub async fn save(&self) -> Result<(), SpotifyError> { pub async fn save(&self) -> Result<(), SpotifyError> {
// Get and create config folder path, generate config file path // Get and create config folder path, generate config file path
let config_folder_path = get_config_folder_path(); let config_folder_path = get_config_folder_path();
create_dir_all(&config_folder_path).await?; create_dir_all(&config_folder_path).await?;
let config_file_path = config_folder_path.join("settings.json"); let config_file_path = config_folder_path.join("settings.json");
// Serialize the settings to a json file
let data = serde_json::to_string_pretty(self)?;
let mut file = File::create(config_file_path).await?;
file.write_all(data.as_bytes()).await?;
Ok(())
}
// Serialize the settings to a json file // Load config
let data = serde_json::to_string_pretty(self)?; pub async fn load() -> Result<Settings, SpotifyError> {
let mut file = File::create(config_file_path).await?; // Get config folder path, generate config file path
file.write_all(data.as_bytes()).await?; let config_folder_path = get_config_folder_path();
Ok(()) let config_file_path = config_folder_path.join("settings.json");
}
// Load config // Deserialize the settings from a json file
pub async fn load() -> Result<Settings, SpotifyError> { let mut file = File::open(config_file_path).await?;
// Get config folder path, generate config file path let mut buf = String::new();
let config_folder_path = get_config_folder_path(); file.read_to_string(&mut buf).await?;
let config_file_path = config_folder_path.join("settings.json"); Ok(serde_json::from_str(&buf)?)
}
// Deserialize the settings from a json file
let mut file = File::open(config_file_path).await?;
let mut buf = String::new();
file.read_to_string(&mut buf).await?;
Ok(serde_json::from_str(&buf)?)
}
} }