aboutsummaryrefslogtreecommitdiff
path: root/src/guilds.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/guilds.rs')
-rw-r--r--src/guilds.rs32
1 files changed, 23 insertions, 9 deletions
diff --git a/src/guilds.rs b/src/guilds.rs
index e847841..115de42 100644
--- a/src/guilds.rs
+++ b/src/guilds.rs
@@ -5,6 +5,8 @@ use std::collections::HashMap;
use std::fs::File;
use std::sync::RwLock;
+use anyhow::Result;
+use log::{error, warn};
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
@@ -51,19 +53,31 @@ pub fn lookup(api_id: &str) -> Option<Guild> {
/// Loads the cache from the file system.
pub fn prepare_cache() {
let path = paths::cache_path();
- if !path.is_file() {
- return;
- }
+ if let Some(path) = path {
+ if !path.is_file() {
+ return;
+ }
- let file = File::open(path).expect("Unable to read cache file");
- let cache = serde_json::from_reader(file).expect("Cache file has invalid format");
- let mut target = CACHE.write().unwrap();
- *target = cache;
+ let file = File::open(path).expect("Unable to read cache file");
+ let cache = serde_json::from_reader(file).expect("Cache file has invalid format");
+ let mut target = CACHE.write().unwrap();
+ *target = cache;
+ } else {
+ warn!("Could not determine the cache path, the persistent guild cache is disabled");
+ }
}
/// Saves the cache to the file system
pub fn save_cache() {
let path = paths::cache_path();
- let file = File::create(path).expect("Cannot open cache for writing");
- serde_json::to_writer(file, &*CACHE.read().unwrap()).unwrap();
+ // We already warned about that earlier
+ if let Some(path) = path {
+ let result: Result<()> = try {
+ let file = File::create(path)?;
+ serde_json::to_writer(file, &*CACHE.read().unwrap())?;
+ };
+ if let Err(e) = result {
+ error!("Error saving the cache: {}", e);
+ }
+ }
}