on master help sign in
De-duplicate some themes.bin and syntaxes.bin related code
commit6ef2bb3283e1ba5f41316ed819bebe63f758054f authorMartin Nordholts <enselic@gmail.com> dateTue 13 Jul 2021, 21:53 (5 years ago) parentfc0794a
2 files changed+42−64
src/assets.rs+33−49 src/bin/bat/assets.rs+9−15
src/assets.rs+33−49
@@ -97,30 +97,9 @@ impl HighlightingAssets {
9797 }
9898
9999 pub fn from_cache(cache_path: &Path) -> Result<Self> {
100− let syntax_set_path = cache_path.join("syntaxes.bin");
101− let theme_set_path = cache_path.join("themes.bin");
102−
103− let syntax_set_file = File::open(&syntax_set_path).chain_err(|| {
104− format!(
105− "Could not load cached syntax set '{}'",
106− syntax_set_path.to_string_lossy()
107− )
108− })?;
109− let syntax_set: SyntaxSet = from_reader(BufReader::new(syntax_set_file))
110− .chain_err(|| "Could not parse cached syntax set")?;
111−
112− let theme_set_file = File::open(&theme_set_path).chain_err(|| {
113− format!(
114− "Could not load cached theme set '{}'",
115− theme_set_path.to_string_lossy()
116− )
117− })?;
118− let theme_set: ThemeSet = from_reader(BufReader::new(theme_set_file))
119− .chain_err(|| "Could not parse cached theme set")?;
120−
121100 Ok(HighlightingAssets {
122− syntax_set,
123− theme_set,
101+ syntax_set: asset_from_cache(&cache_path.join("syntaxes.bin"), "syntax set")?,
102+ theme_set: asset_from_cache(&cache_path.join("themes.bin"), "theme set")?,
124103 fallback_theme: None,
125104 })
126105 }
@@ -146,32 +125,12 @@ impl HighlightingAssets {
146125
147126 pub fn save_to_cache(&self, target_dir: &Path, current_version: &str) -> Result<()> {
148127 let _ = fs::create_dir_all(target_dir);
149− let theme_set_path = target_dir.join("themes.bin");
150− let syntax_set_path = target_dir.join("syntaxes.bin");
151−
152− print!(
153− "Writing theme set to {} ... ",
154− theme_set_path.to_string_lossy()
155− );
156− dump_to_file(&self.theme_set, &theme_set_path).chain_err(|| {
157− format!(
158− "Could not save theme set to {}",
159− theme_set_path.to_string_lossy()
160− )
161− })?;
162− println!("okay");
163−
164− print!(
165− "Writing syntax set to {} ... ",
166− syntax_set_path.to_string_lossy()
167− );
168− dump_to_file(&self.syntax_set, &syntax_set_path).chain_err(|| {
169− format!(
170− "Could not save syntax set to {}",
171− syntax_set_path.to_string_lossy()
172− )
173− })?;
174− println!("okay");
128+ asset_to_cache(&self.theme_set, &target_dir.join("themes.bin"), "theme set")?;
129+ asset_to_cache(
130+ &self.syntax_set,
131+ &target_dir.join("syntaxes.bin"),
132+ "syntax set",
133+ )?;
175134
176135 print!(
177136 "Writing metadata to folder {} ... ",
@@ -319,6 +278,31 @@ impl HighlightingAssets {
319278 }
320279}
321280
281+fn asset_to_cache<T: serde::Serialize>(asset: &T, path: &Path, description: &str) -> Result<()> {
282+ print!("Writing {} to {} ... ", description, path.to_string_lossy());
283+ dump_to_file(asset, &path).chain_err(|| {
284+ format!(
285+ "Could not save {} to {}",
286+ description,
287+ path.to_string_lossy()
288+ )
289+ })?;
290+ println!("okay");
291+ Ok(())
292+}
293+
294+fn asset_from_cache<T: serde::de::DeserializeOwned>(path: &Path, description: &str) -> Result<T> {
295+ let asset_file = File::open(&path).chain_err(|| {
296+ format!(
297+ "Could not load cached {} '{}'",
298+ description,
299+ path.to_string_lossy()
300+ )
301+ })?;
302+ from_reader(BufReader::new(asset_file))
303+ .chain_err(|| format!("Could not parse cached {}", description))
304+}
305+
322306#[cfg(test)]
323307mod tests {
324308 use super::*;
src/bin/bat/assets.rs+9−15
@@ -18,21 +18,9 @@ pub fn cache_dir() -> Cow<'static, str> {
1818}
1919
2020pub fn clear_assets() {
21− let theme_set_path = PROJECT_DIRS.cache_dir().join("themes.bin");
22− let syntax_set_path = PROJECT_DIRS.cache_dir().join("syntaxes.bin");
23− let metadata_file = PROJECT_DIRS.cache_dir().join("metadata.yaml");
24−
25− print!("Clearing theme set cache ... ");
26− fs::remove_file(theme_set_path).ok();
27− println!("okay");
28−
29− print!("Clearing syntax set cache ... ");
30− fs::remove_file(syntax_set_path).ok();
31− println!("okay");
32−
33− print!("Clearing metadata file ... ");
34− fs::remove_file(metadata_file).ok();
35− println!("okay");
21+ clear_asset("themes.bin", "theme set cache");
22+ clear_asset("syntaxes.bin", "syntax set cache");
23+ clear_asset("metadata.yaml", "metadata file");
3624}
3725
3826pub fn assets_from_cache_or_binary() -> Result<HighlightingAssets> {
@@ -56,3 +44,9 @@ pub fn assets_from_cache_or_binary() -> Result<HighlightingAssets> {
5644 Ok(HighlightingAssets::from_cache(&cache_dir)
5745 .unwrap_or_else(|_| HighlightingAssets::from_binary()))
5846}
47+
48+fn clear_asset(filename: &str, description: &str) {
49+ print!("Clearing {} ... ", description);
50+ fs::remove_file(PROJECT_DIRS.cache_dir().join(filename)).ok();
51+ println!("okay");
52+}