From c86244923e784f55c3ffcf1b4d53be7a2fd76fe8 Mon Sep 17 00:00:00 2001 From: Eric Kidd Date: Sat, 30 Mar 2024 08:52:59 -0400 Subject: [PATCH] substudy: Create cache directory if it doesn't exist --- substudy/src/cache.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/substudy/src/cache.rs b/substudy/src/cache.rs index a8215f3..3c5e97a 100644 --- a/substudy/src/cache.rs +++ b/substudy/src/cache.rs @@ -2,10 +2,10 @@ //! built for speed, because it's caching expensive AI requests that are made //! over the network. Instead, we focus on reliability. -use std::{marker::PhantomData, path::PathBuf}; +use std::{fs, marker::PhantomData, path::PathBuf}; use anyhow::Context as _; -use log::{debug, trace}; +use log::{debug, trace, warn}; use rusqlite::Connection; use serde::{de::DeserializeOwned, Serialize}; @@ -33,6 +33,14 @@ where cache_name: &str, approx_max_entries: u64, ) -> Result { + // Make sure our parent directory exists. + if let Some(parent) = path.parent() { + fs::create_dir_all(parent)?; + } else { + warn!("no parent directory for cache path"); + } + + // Set up the cache table. let conn = Connection::open(&path)?; conn.execute( "CREATE TABLE IF NOT EXISTS substudy_cache (