mirror of
				https://github.com/sharkdp/bat.git
				synced 2025-10-31 15:12:12 +00:00 
			
		
		
		
	De-duplicate some themes.bin and syntaxes.bin related code
This commit is contained in:
		| @@ -97,30 +97,9 @@ impl HighlightingAssets { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     pub fn from_cache(cache_path: &Path) -> Result<Self> { |     pub fn from_cache(cache_path: &Path) -> Result<Self> { | ||||||
|         let syntax_set_path = cache_path.join("syntaxes.bin"); |  | ||||||
|         let theme_set_path = cache_path.join("themes.bin"); |  | ||||||
|  |  | ||||||
|         let syntax_set_file = File::open(&syntax_set_path).chain_err(|| { |  | ||||||
|             format!( |  | ||||||
|                 "Could not load cached syntax set '{}'", |  | ||||||
|                 syntax_set_path.to_string_lossy() |  | ||||||
|             ) |  | ||||||
|         })?; |  | ||||||
|         let syntax_set: SyntaxSet = from_reader(BufReader::new(syntax_set_file)) |  | ||||||
|             .chain_err(|| "Could not parse cached syntax set")?; |  | ||||||
|  |  | ||||||
|         let theme_set_file = File::open(&theme_set_path).chain_err(|| { |  | ||||||
|             format!( |  | ||||||
|                 "Could not load cached theme set '{}'", |  | ||||||
|                 theme_set_path.to_string_lossy() |  | ||||||
|             ) |  | ||||||
|         })?; |  | ||||||
|         let theme_set: ThemeSet = from_reader(BufReader::new(theme_set_file)) |  | ||||||
|             .chain_err(|| "Could not parse cached theme set")?; |  | ||||||
|  |  | ||||||
|         Ok(HighlightingAssets { |         Ok(HighlightingAssets { | ||||||
|             syntax_set, |             syntax_set: asset_from_cache(&cache_path.join("syntaxes.bin"), "syntax set")?, | ||||||
|             theme_set, |             theme_set: asset_from_cache(&cache_path.join("themes.bin"), "theme set")?, | ||||||
|             fallback_theme: None, |             fallback_theme: None, | ||||||
|         }) |         }) | ||||||
|     } |     } | ||||||
| @@ -146,32 +125,12 @@ impl HighlightingAssets { | |||||||
|  |  | ||||||
|     pub fn save_to_cache(&self, target_dir: &Path, current_version: &str) -> Result<()> { |     pub fn save_to_cache(&self, target_dir: &Path, current_version: &str) -> Result<()> { | ||||||
|         let _ = fs::create_dir_all(target_dir); |         let _ = fs::create_dir_all(target_dir); | ||||||
|         let theme_set_path = target_dir.join("themes.bin"); |         asset_to_cache(&self.theme_set, &target_dir.join("themes.bin"), "theme set")?; | ||||||
|         let syntax_set_path = target_dir.join("syntaxes.bin"); |         asset_to_cache( | ||||||
|  |             &self.syntax_set, | ||||||
|         print!( |             &target_dir.join("syntaxes.bin"), | ||||||
|             "Writing theme set to {} ... ", |             "syntax set", | ||||||
|             theme_set_path.to_string_lossy() |         )?; | ||||||
|         ); |  | ||||||
|         dump_to_file(&self.theme_set, &theme_set_path).chain_err(|| { |  | ||||||
|             format!( |  | ||||||
|                 "Could not save theme set to {}", |  | ||||||
|                 theme_set_path.to_string_lossy() |  | ||||||
|             ) |  | ||||||
|         })?; |  | ||||||
|         println!("okay"); |  | ||||||
|  |  | ||||||
|         print!( |  | ||||||
|             "Writing syntax set to {} ... ", |  | ||||||
|             syntax_set_path.to_string_lossy() |  | ||||||
|         ); |  | ||||||
|         dump_to_file(&self.syntax_set, &syntax_set_path).chain_err(|| { |  | ||||||
|             format!( |  | ||||||
|                 "Could not save syntax set to {}", |  | ||||||
|                 syntax_set_path.to_string_lossy() |  | ||||||
|             ) |  | ||||||
|         })?; |  | ||||||
|         println!("okay"); |  | ||||||
|  |  | ||||||
|         print!( |         print!( | ||||||
|             "Writing metadata to folder {} ... ", |             "Writing metadata to folder {} ... ", | ||||||
| @@ -319,6 +278,31 @@ impl HighlightingAssets { | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | fn asset_to_cache<T: serde::Serialize>(asset: &T, path: &Path, description: &str) -> Result<()> { | ||||||
|  |     print!("Writing {} to {} ... ", description, path.to_string_lossy()); | ||||||
|  |     dump_to_file(asset, &path).chain_err(|| { | ||||||
|  |         format!( | ||||||
|  |             "Could not save {} to {}", | ||||||
|  |             description, | ||||||
|  |             path.to_string_lossy() | ||||||
|  |         ) | ||||||
|  |     })?; | ||||||
|  |     println!("okay"); | ||||||
|  |     Ok(()) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | fn asset_from_cache<T: serde::de::DeserializeOwned>(path: &Path, description: &str) -> Result<T> { | ||||||
|  |     let asset_file = File::open(&path).chain_err(|| { | ||||||
|  |         format!( | ||||||
|  |             "Could not load cached {} '{}'", | ||||||
|  |             description, | ||||||
|  |             path.to_string_lossy() | ||||||
|  |         ) | ||||||
|  |     })?; | ||||||
|  |     from_reader(BufReader::new(asset_file)) | ||||||
|  |         .chain_err(|| format!("Could not parse cached {}", description)) | ||||||
|  | } | ||||||
|  |  | ||||||
| #[cfg(test)] | #[cfg(test)] | ||||||
| mod tests { | mod tests { | ||||||
|     use super::*; |     use super::*; | ||||||
|   | |||||||
| @@ -18,21 +18,9 @@ pub fn cache_dir() -> Cow<'static, str> { | |||||||
| } | } | ||||||
|  |  | ||||||
| pub fn clear_assets() { | pub fn clear_assets() { | ||||||
|     let theme_set_path = PROJECT_DIRS.cache_dir().join("themes.bin"); |     clear_asset("themes.bin", "theme set cache"); | ||||||
|     let syntax_set_path = PROJECT_DIRS.cache_dir().join("syntaxes.bin"); |     clear_asset("syntaxes.bin", "syntax set cache"); | ||||||
|     let metadata_file = PROJECT_DIRS.cache_dir().join("metadata.yaml"); |     clear_asset("metadata.yaml", "metadata file"); | ||||||
|  |  | ||||||
|     print!("Clearing theme set cache ... "); |  | ||||||
|     fs::remove_file(theme_set_path).ok(); |  | ||||||
|     println!("okay"); |  | ||||||
|  |  | ||||||
|     print!("Clearing syntax set cache ... "); |  | ||||||
|     fs::remove_file(syntax_set_path).ok(); |  | ||||||
|     println!("okay"); |  | ||||||
|  |  | ||||||
|     print!("Clearing metadata file ... "); |  | ||||||
|     fs::remove_file(metadata_file).ok(); |  | ||||||
|     println!("okay"); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| pub fn assets_from_cache_or_binary() -> Result<HighlightingAssets> { | pub fn assets_from_cache_or_binary() -> Result<HighlightingAssets> { | ||||||
| @@ -56,3 +44,9 @@ pub fn assets_from_cache_or_binary() -> Result<HighlightingAssets> { | |||||||
|     Ok(HighlightingAssets::from_cache(&cache_dir) |     Ok(HighlightingAssets::from_cache(&cache_dir) | ||||||
|         .unwrap_or_else(|_| HighlightingAssets::from_binary())) |         .unwrap_or_else(|_| HighlightingAssets::from_binary())) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | fn clear_asset(filename: &str, description: &str) { | ||||||
|  |     print!("Clearing {} ... ", description); | ||||||
|  |     fs::remove_file(PROJECT_DIRS.cache_dir().join(filename)).ok(); | ||||||
|  |     println!("okay"); | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user