| 193 | |
| 194 | impl Settings { |
| 195 | pub async fn new() -> Result<Self, DatabaseError> { |
| 196 | if cfg!(test) { |
| 197 | return Ok(Self::default()); |
| 198 | } |
| 199 | |
| 200 | let path = GlobalPaths::settings_path_static()?; |
| 201 | |
| 202 | // If the folder doesn't exist, create it. |
| 203 | if let Some(parent) = path.parent() { |
| 204 | if !parent.exists() { |
| 205 | std::fs::create_dir_all(parent)?; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | Ok(Self(match path.exists() { |
| 210 | true => { |
| 211 | let mut file = RwLock::new(File::open(&path).await?); |
| 212 | let mut buf = Vec::new(); |
| 213 | file.write()?.read_to_end(&mut buf).await?; |
| 214 | serde_json::from_slice(&buf)? |
| 215 | }, |
| 216 | false => { |
| 217 | let mut file = RwLock::new(File::create(path).await?); |
| 218 | file.write()?.write_all(b"{}").await?; |
| 219 | serde_json::Map::new() |
| 220 | }, |
| 221 | })) |
| 222 | } |
| 223 | |
| 224 | pub fn map(&self) -> &'_ Map<String, Value> { |
| 225 | &self.0 |