@@ -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::*;