1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
use directories::ProjectDirs;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct Config {
#[serde(skip)]
pub config_path: PathBuf,
pub music_paths: Vec<PathBuf>,
pub scan_interval: Option<u64>,
pub socket_path: Option<PathBuf>,
pub lastfm_api_key: Option<String>,
pub lastfm_api_secret: Option<String>,
pub lastfm_session_key: Option<String>,
pub listenbrainz_token: Option<String>,
pub volume: Option<u8>,
pub shuffle: Option<String>,
}
impl Config {
pub fn load() -> Result<Self, Box<dyn std::error::Error>> {
let proj_dirs = ProjectDirs::from("com", "rsplayer", "rsplayer")
.ok_or("Failed to get project directories")?;
let config_dir = if let Ok(xdg) = std::env::var("XDG_CONFIG_HOME") {
PathBuf::from(xdg).join("rsplayer")
} else {
proj_dirs.config_dir().to_path_buf()
};
let config_path = config_dir.join("config.toml");
if !config_path.exists() {
fs::create_dir_all(&config_dir)?;
let default = Self {
config_path: config_path.clone(),
music_paths: vec![dirs::audio_dir().unwrap_or_else(|| PathBuf::from("~/Music"))],
scan_interval: Some(3600),
socket_path: None,
..Default::default()
};
default.save()?;
return Ok(default);
}
let content = fs::read_to_string(&config_path)?;
let mut config: Self = toml::from_str(&content)?;
config.config_path = config_path;
Ok(config)
}
pub fn save(&self) -> Result<(), Box<dyn std::error::Error>> {
let content = toml::to_string_pretty(self)?;
if let Some(parent) = self.config_path.parent() {
fs::create_dir_all(parent)?;
}
fs::write(&self.config_path, content)?;
Ok(())
}
pub fn get(&self, key: &str) -> Result<String, Box<dyn std::error::Error>> {
match key {
"music_paths" => Ok(self
.music_paths
.iter()
.map(|p| p.display().to_string())
.collect::<Vec<_>>()
.join(", ")),
"scan_interval" => Ok(self
.scan_interval
.map(|i| i.to_string())
.unwrap_or_else(|| "not set".to_string())),
"socket_path" => Ok(self
.socket_path
.as_ref()
.map(|p| p.display().to_string())
.unwrap_or_else(|| "default".to_string())),
"lastfm_api_key" => Ok(self
.lastfm_api_key
.clone()
.unwrap_or_else(|| "not set".to_string())),
"lastfm_api_secret" => Ok(self
.lastfm_api_secret
.clone()
.unwrap_or_else(|| "not set".to_string())),
"lastfm_session_key" => Ok(self
.lastfm_session_key
.clone()
.unwrap_or_else(|| "not set".to_string())),
"listenbrainz_token" => Ok(self
.listenbrainz_token
.clone()
.unwrap_or_else(|| "not set".to_string())),
"volume" => Ok(self
.volume
.map(|v| v.to_string())
.unwrap_or_else(|| "not set".to_string())),
"shuffle" => Ok(self.shuffle.clone().unwrap_or_else(|| "off".to_string())),
_ => Err(format!("Unknown setting: {}", key).into()),
}
}
pub fn set(&mut self, key: &str, value: &str) -> Result<(), Box<dyn std::error::Error>> {
match key {
"music_paths" => {
self.music_paths = value.split(',').map(|p| PathBuf::from(p.trim())).collect();
}
"scan_interval" => {
self.scan_interval = Some(value.parse()?);
}
"socket_path" => {
self.socket_path = Some(PathBuf::from(value));
}
"lastfm_api_key" => {
self.lastfm_api_key = Some(value.to_string());
}
"lastfm_api_secret" => {
self.lastfm_api_secret = Some(value.to_string());
}
"lastfm_session_key" => {
self.lastfm_session_key = Some(value.to_string());
}
"listenbrainz_token" => {
self.listenbrainz_token = Some(value.to_string());
}
"volume" => {
self.volume = Some(value.parse()?);
}
"shuffle" => {
if value == "on" || value == "all" || value == "album" {
self.shuffle = Some(value.to_string());
} else if value == "off" || value == "none" {
self.shuffle = None;
} else {
return Err(format!("Invalid shuffle value: {}", value).into());
}
}
_ => return Err(format!("Unknown setting: {}", key).into()),
}
Ok(())
}
pub fn list(&self) {
println!("music_paths = {:?}", self.music_paths);
println!("scan_interval = {:?}", self.scan_interval);
println!("socket_path = {:?}", self.socket_path);
println!("lastfm_api_key = {:?}", self.lastfm_api_key);
println!("lastfm_api_secret = {:?}", self.lastfm_api_secret);
println!("lastfm_session_key = {:?}", self.lastfm_session_key);
println!("listenbrainz_token = {:?}", self.listenbrainz_token);
println!("volume = {:?}", self.volume);
println!("shuffle = {:?}", self.shuffle);
}
pub fn unset(&mut self, key: &str) -> Result<(), Box<dyn std::error::Error>> {
match key {
"music_paths" => self.music_paths.clear(),
"scan_interval" => self.scan_interval = None,
"socket_path" => self.socket_path = None,
"lastfm_api_key" => self.lastfm_api_key = None,
"lastfm_api_secret" => self.lastfm_api_secret = None,
"lastfm_session_key" => self.lastfm_session_key = None,
"listenbrainz_token" => self.listenbrainz_token = None,
"volume" => self.volume = None,
"shuffle" => self.shuffle = None,
_ => return Err(format!("Unknown setting: {}", key).into()),
}
Ok(())
}
pub fn get_socket_path(&self) -> PathBuf {
self.socket_path.clone().unwrap_or_else(|| {
let runtime_dir = std::env::var("XDG_RUNTIME_DIR")
.map(PathBuf::from)
.unwrap_or_else(|_| PathBuf::from("/tmp"));
runtime_dir.join("rsplayer").join("daemon.sock")
})
}
}
|