Skip to content
Prev Previous commit
Next Next commit
fix(config): path of cache
  • Loading branch information
clearloop committed Jun 23, 2023
commit bd8fd18b5ca9cdc1a4ccc3adcf8c9b0d7467bba2
14 changes: 5 additions & 9 deletions src/config/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,13 @@ impl Storage {

/// get cache path
pub fn cache(&self) -> Result<String, crate::Error> {
let home = dirs::home_dir()
.ok_or(Error::NoneError)?
.to_string_lossy()
.to_string();
let path = PathBuf::from(self.cache.replace('~', &home));
if !path.is_dir() {
info!("Generate cache dir at {:?}.", &path);
fs::DirBuilder::new().recursive(true).create(&path)?;
let root = PathBuf::from(self.root()?);
if !root.exists() {
info!("Generate cache dir at {:?}.", &root);
fs::DirBuilder::new().recursive(true).create(&root)?;
}

Ok(path.join("Problems").to_string_lossy().to_string())
Ok(root.join("Problems").to_string_lossy().to_string())
}

/// get code path
Expand Down
9 changes: 8 additions & 1 deletion src/config/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ use serde::{Deserialize, Serialize};

const CATEGORIES: [&str; 4] = ["algorithms", "concurrency", "database", "shell"];

// TODO: find a better solution.
fn categories() -> Vec<String> {
CATEGORIES.into_iter().map(|s| s.into()).collect()
}

/// Leetcode API
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Urls {
Expand Down Expand Up @@ -54,7 +59,7 @@ impl Urls {

/// problems url with specific `$category`
pub fn problems(&self, category: &str) -> String {
self.problem.replace("$category", category)
self.problems.replace("$category", category)
}

/// submit url with specific `$slug`
Expand All @@ -81,7 +86,9 @@ impl Urls {
/// System settings, for leetcode api mainly
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Sys {
#[serde(default = "categories")]
pub categories: Vec<String>,
#[serde(default)]
pub urls: Urls,
}

Expand Down