harpocrates_musik_player

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

setting.rs (2724B)


      1 use directories::ProjectDirs;
      2 use std::path::PathBuf;
      3 
      4 #[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
      5 pub struct Settings {
      6     audio: AudioSettings,
      7     keybind: KeybindSettings,
      8 }
      9 
     10 #[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
     11 pub struct AudioSettings {
     12     volume: f32,
     13     output_device: String,
     14     library_path: String,
     15 }
     16 
     17 #[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
     18 pub struct KeybindSettings {
     19     play_pause: String,
     20     stop: String,
     21     next: String,
     22 }
     23 
     24 impl Default for Settings {
     25     fn default() -> Self {
     26         Settings {
     27             audio: AudioSettings {
     28                 volume: 1.0,
     29                 output_device: std::env::var("DEFAULT_OUTPUT_DEVICE").unwrap_or_default(),
     30                 library_path: std::env::var("DEFAULT_LIBRARY_PATH").unwrap_or_default(),
     31             },
     32             keybind: KeybindSettings {
     33                 play_pause: "p".to_string(),
     34                 stop: "s".to_string(),
     35                 next: "n".to_string(),
     36             },
     37         }
     38     }
     39 }
     40 
     41 impl Settings {
     42     pub fn audio(&self) -> &AudioSettings {
     43         &self.audio
     44     }
     45 
     46     pub fn audio_mut(&mut self) -> &mut AudioSettings {
     47         &mut self.audio
     48     }
     49 
     50     pub fn save(&self) -> Result<(), Box<dyn std::error::Error>> {
     51         let path = Self::get_path().ok_or("Failed to get config directory path")?;
     52         let content = toml::to_string(self)?;
     53         std::fs::write(path, content)?;
     54         Ok(())
     55     }
     56 
     57     pub fn load() -> Result<Self, Box<dyn std::error::Error>> {
     58         let path = Self::get_path().ok_or("Failed to get config directory path")?;
     59         if !path.exists() {
     60             return Ok(Settings::default());
     61         }
     62         let content = std::fs::read_to_string(path)?;
     63         let config: Settings = toml::from_str(&content)?;
     64         Ok(config)
     65     }
     66 
     67     pub fn get_path() -> Option<PathBuf> {
     68         ProjectDirs::from("", "", "harpocrates_musik_player").map(|proj_dirs| {
     69             let config_dir = proj_dirs.config_dir();
     70             if !config_dir.exists() {
     71                 let _ = std::fs::create_dir_all(config_dir);
     72             }
     73             config_dir.join("config.toml")
     74         })
     75     }
     76 }
     77 
     78 impl AudioSettings {
     79     pub fn library_path(&self) -> &str {
     80         &self.library_path
     81     }
     82 
     83     pub fn volume(&self) -> f32 {
     84         self.volume
     85     }
     86 
     87     pub fn set_volume(&mut self, volume: f32) {
     88         self.volume = volume;
     89     }
     90     pub fn output_device(&self) -> &str {
     91         &self.output_device
     92     }
     93     pub fn set_output_device(&mut self, output_device: String) {
     94         self.output_device = output_device;
     95     }
     96 }