diff --git a/Cargo.toml b/Cargo.toml index 77a6fd4..57c595a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "manifold" -version = "3.1.0" +version = "3.1.1" authors = ["Lucy Bladen "] edition = "2021" diff --git a/src/models/fueltank.rs b/src/models/fueltank.rs index bed2f2a..3e6c773 100644 --- a/src/models/fueltank.rs +++ b/src/models/fueltank.rs @@ -25,7 +25,7 @@ pub struct FuelTank { cache_mode: TankMode, } -#[derive(Copy, Clone, Debug, Serialize, Deserialize)] +#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)] pub enum TankMode { Cache, NoCache, @@ -37,48 +37,70 @@ impl FuelTank { U: Serialize, U: DeserializeOwned, U: Pump { - debug!("Filling tank!"); - let mut cache = { - let mut path = PathBuf::new(); - path.push(home_dir().ok_or(ManifoldError::from("No home dir"))?); - path.push(".local"); - path.push(format!("manifold-{}-cache-{}.json", self.cache_name.clone().unwrap(), CACHE_VERSION)); - debug!("Cache path: {:?}", &path); - fs::OpenOptions::new() - .read(true) - .write(true) - .create(true) - .open(path) - .map_err(|e| { - error!("Error creating cache file: {}", e); - ManifoldError::from("CACHE ERROR") - })? - }; + let path = PathBuf::new() + .join(home_dir().ok_or(ManifoldError::from("No home dir"))?) + .join(".local") + .join(format!("manifold-{}-cache-{}.json", self.cache_name.clone().unwrap(), CACHE_VERSION)); - debug!("Cache: {:?}", &cache); + let mut fuel = match self.cache_mode { + TankMode::Cache => { + // Read cache only if we have one + debug!("Filling tank!"); + let mut cache = { + debug!("Cache path: {:?}", &path); + fs::OpenOptions::new() + .read(true) + .create(true) + .open(&path) + .map_err(|e| { + error!("Error creating cache file: {}", e); + ManifoldError::from("CACHE ERROR") + })? + }; - let json: Result = serde_json::from_reader(&mut cache); - debug!("Json: {:?}", &json); - let mut fuel = match json { - Ok(contents) => { - match contents.len() { - 0 => self.emit().await?, - _ => contents, + debug!("Cache: {:?}", &cache); + + let json: Result = serde_json::from_reader(&mut cache); + debug!("Json: {:?}", &json); + match json { + Ok(contents) => { + match contents.len() { + 0 => self.emit().await?, + _ => contents, + } + } + _ => self.emit().await?, } - } - _ => self.emit().await?, + }, + TankMode::NoCache => { + self.emit().await? + }, }; debug!("Got tips: {:?}", &fuel); let single = fuel.pump().ok_or(ManifoldError::from("CACHE ERROR"))?; debug!("Got single tip: {:?}", &single); - cache - .set_len(0) - .and_then(|_| cache.seek(SeekFrom::Start(0)))?; + if self.cache_mode == TankMode::Cache { + // Write cache only if we have one + let mut cache = { + debug!("Cache path: {:?}", &path); + fs::OpenOptions::new() + .write(true) + .create(true) + .open(&path) + .map_err(|e| { + error!("Error creating cache file: {}", e); + ManifoldError::from("CACHE ERROR") + })? + }; + cache + .set_len(0) + .and_then(|_| cache.seek(SeekFrom::Start(0)))?; - debug!("Writing cache"); - serde_json::to_writer(cache, &fuel)?; + debug!("Writing cache"); + serde_json::to_writer(cache, &fuel)?; + } return Ok(single) }