Merge pull request #16 from thatpix3l/main
Set Nightly Rust as default, store config in better places
This commit is contained in:
commit
cd29108f82
2 changed files with 39 additions and 4 deletions
1
rust-toolchain
Normal file
1
rust-toolchain
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
nightly
|
||||||
|
|
@ -3,10 +3,16 @@ use crate::error::SpotifyError;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use tokio::{
|
use tokio::{
|
||||||
|
fs::create_dir_all,
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{AsyncReadExt, AsyncWriteExt},
|
io::{AsyncReadExt, AsyncWriteExt},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use std::{
|
||||||
|
env,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
};
|
||||||
|
|
||||||
// Structure for holding all the settings
|
// Structure for holding all the settings
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct Settings {
|
pub struct Settings {
|
||||||
|
|
@ -18,6 +24,23 @@ pub struct Settings {
|
||||||
pub downloader: DownloaderConfig,
|
pub downloader: DownloaderConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// On UNIX systems (eg. Linux, *BSD, even macOS), follow the
|
||||||
|
// XDG Base Directory Specification for storing config files
|
||||||
|
#[cfg(target_family = "unix")]
|
||||||
|
fn get_config_folder_path() -> PathBuf {
|
||||||
|
match env::var("XDG_CONFIG_HOME") {
|
||||||
|
Ok(v) => Path::new(&v).join("down_on_spot").to_path_buf(),
|
||||||
|
|
||||||
|
Err(_) => Path::new(&env::var("HOME").unwrap()).join(".config/down_on_spot"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// On Windows, follow whatever windows does for AppData
|
||||||
|
#[cfg(target_family = "windows")]
|
||||||
|
fn get_config_folder_path() -> PathBuf {
|
||||||
|
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 {
|
||||||
|
|
@ -31,17 +54,28 @@ impl Settings {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serialize the settings to a json file
|
// 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
|
||||||
|
let config_folder_path = get_config_folder_path();
|
||||||
|
create_dir_all(&config_folder_path).await?;
|
||||||
|
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 data = serde_json::to_string_pretty(self)?;
|
||||||
let mut file = File::create("settings.json").await?;
|
let mut file = File::create(config_file_path).await?;
|
||||||
file.write_all(data.as_bytes()).await?;
|
file.write_all(data.as_bytes()).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deserialize the settings from a json file
|
// Load config
|
||||||
pub async fn load() -> Result<Settings, SpotifyError> {
|
pub async fn load() -> Result<Settings, SpotifyError> {
|
||||||
let mut file = File::open("settings.json").await?;
|
// Get config folder path, generate config file path
|
||||||
|
let config_folder_path = get_config_folder_path();
|
||||||
|
let config_file_path = config_folder_path.join("settings.json");
|
||||||
|
|
||||||
|
// Deserialize the settings from a json file
|
||||||
|
let mut file = File::open(config_file_path).await?;
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
file.read_to_string(&mut buf).await?;
|
file.read_to_string(&mut buf).await?;
|
||||||
Ok(serde_json::from_str(&buf)?)
|
Ok(serde_json::from_str(&buf)?)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue