@@ -97,30 +97,9 @@ impl HighlightingAssets {
99 pub fn from_cache(cache_path: &Path) -> Result<Self> {
99 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");
103− let syntax_set_file = File::open(&syntax_set_path).chain_err(|| {
105− "Could not load cached syntax set '{}'",
106− syntax_set_path.to_string_lossy()
109− let syntax_set: SyntaxSet = from_reader(BufReader::new(syntax_set_file))
110− .chain_err(|| "Could not parse cached syntax set")?;
112− let theme_set_file = File::open(&theme_set_path).chain_err(|| {
114− "Could not load cached theme set '{}'",
115− theme_set_path.to_string_lossy()
118− let theme_set: ThemeSet = from_reader(BufReader::new(theme_set_file))
119− .chain_err(|| "Could not parse cached theme set")?;
121 Ok(HighlightingAssets {
100 Ok(HighlightingAssets {
122− syntax_set,
101+ syntax_set: asset_from_cache(&cache_path.join("syntaxes.bin"), "syntax set")?,
123− theme_set,
102+ theme_set: asset_from_cache(&cache_path.join("themes.bin"), "theme set")?,
124 fallback_theme: None,
103 fallback_theme: None,
@@ -146,32 +125,12 @@ impl HighlightingAssets {
147 pub fn save_to_cache(&self, target_dir: &Path, current_version: &str) -> Result<()> {
126 pub fn save_to_cache(&self, target_dir: &Path, current_version: &str) -> Result<()> {
148 let _ = fs::create_dir_all(target_dir);
127 let _ = fs::create_dir_all(target_dir);
149− let theme_set_path = target_dir.join("themes.bin");
128+ asset_to_cache(&self.theme_set, &target_dir.join("themes.bin"), "theme set")?;
150− let syntax_set_path = target_dir.join("syntaxes.bin");
131+ &target_dir.join("syntaxes.bin"),
153− "Writing theme set to {} ... ",
154− theme_set_path.to_string_lossy()
156− dump_to_file(&self.theme_set, &theme_set_path).chain_err(|| {
158− "Could not save theme set to {}",
159− theme_set_path.to_string_lossy()
165− "Writing syntax set to {} ... ",
132+ "syntax set",
166− syntax_set_path.to_string_lossy()
168− dump_to_file(&self.syntax_set, &syntax_set_path).chain_err(|| {
170− "Could not save syntax set to {}",
171− syntax_set_path.to_string_lossy()
177 "Writing metadata to folder {} ... ",
136 "Writing metadata to folder {} ... ",
@@ -319,6 +278,31 @@ impl HighlightingAssets {
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(|| {
285+ "Could not save {} to {}",
287+ path.to_string_lossy()
294+fn asset_from_cache<T: serde::de::DeserializeOwned>(path: &Path, description: &str) -> Result<T> {
295+ let asset_file = File::open(&path).chain_err(|| {
297+ "Could not load cached {} '{}'",
299+ path.to_string_lossy()
302+ from_reader(BufReader::new(asset_file))
303+ .chain_err(|| format!("Could not parse cached {}", description))
322#[cfg(test)]
306#[cfg(test)]
323mod tests {
307mod tests {
324 use super::*;
308 use super::*;