From 0291d6e486e9baae22feb38d4c270f53b897c210 Mon Sep 17 00:00:00 2001 From: Jiang Liu Date: Sun, 19 Feb 2023 14:26:36 +0800 Subject: [PATCH] api: define helpers to detect cache type Define helper functions to detect caceh types. Signed-off-by: Jiang Liu --- api/src/config.rs | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/api/src/config.rs b/api/src/config.rs index 616da06cac6..6c65fcf317e 100644 --- a/api/src/config.rs +++ b/api/src/config.rs @@ -130,18 +130,14 @@ impl ConfigV2 { /// Get cache working directory. pub fn get_cache_working_directory(&self) -> Result { let cache = self.get_cache_config()?; - match cache.cache_type.as_str() { - "blobcache" | "filecache" => { - if let Some(c) = cache.file_cache.as_ref() { - return Ok(c.work_dir.clone()); - } + if cache.is_filecache() { + if let Some(c) = cache.file_cache.as_ref() { + return Ok(c.work_dir.clone()); } - "fscache" => { - if let Some(c) = cache.fs_cache.as_ref() { - return Ok(c.work_dir.clone()); - } + } else if cache.is_fscache() { + if let Some(c) = cache.fs_cache.as_ref() { + return Ok(c.work_dir.clone()); } - _ => {} } Err(Error::new( @@ -634,25 +630,35 @@ impl CacheConfigV2 { true } + /// Check whether the cache type is `filecache` + pub fn is_filecache(&self) -> bool { + self.cache_type == "blobcache" || self.cache_type == "filecache" + } + + /// Check whether the cache type is `fscache` + pub fn is_fscache(&self) -> bool { + self.cache_type == "fscache" + } + /// Get configuration information for file cache. pub fn get_filecache_config(&self) -> Result<&FileCacheConfig> { - if self.cache_type != "blobcache" && self.cache_type != "filecache" { - Err(einval!("cache type is not 'filecache'")) - } else { + if self.is_filecache() { self.file_cache .as_ref() .ok_or_else(|| einval!("no configuration information for filecache")) + } else { + Err(einval!("cache type is not 'filecache'")) } } /// Get configuration information for fscache. pub fn get_fscache_config(&self) -> Result<&FsCacheConfig> { - if self.cache_type != "fscache" { - Err(einval!("cache type is not 'fscache'")) - } else { + if self.is_fscache() { self.fs_cache .as_ref() .ok_or_else(|| einval!("no configuration information for fscache")) + } else { + Err(einval!("cache type is not 'fscache'")) } } }