on master help sign in
HighlightingAssets: Introduce private fn new() helper

It already now reduces code duplication slightly, but will become even more useful in the future when we add more complicated logic such as lazy-loading.

commitf6fc826dc64b5d06025b131bd969ed386fe57006 authorMartin Nordholts <enselic@gmail.com> dateMon 19 Jul 2021, 05:41 (5 years ago) parent375d55a
1 file changed+19−17
src/assets.rs+19−17
src/assets.rs+19−17
@@ -41,6 +41,14 @@ const IGNORED_SUFFIXES: [&str; 10] = [
4141];
4242
4343impl HighlightingAssets {
44+ fn new(syntax_set: SyntaxSet, theme_set: ThemeSet) -> Self {
45+ HighlightingAssets {
46+ syntax_set,
47+ theme_set,
48+ fallback_theme: None,
49+ }
50+ }
51+
4452 pub fn default_theme() -> &'static str {
4553 "Monokai Extended"
4654 }
@@ -89,19 +97,17 @@ impl HighlightingAssets {
8997 );
9098 }
9199
92− Ok(HighlightingAssets {
93− syntax_set: syntax_set_builder.build(),
100+ Ok(HighlightingAssets::new(
101+ syntax_set_builder.build(),
94102 theme_set,
95− fallback_theme: None,
96− })
103+ ))
97104 }
98105
99106 pub fn from_cache(cache_path: &Path) -> Result<Self> {
100− Ok(HighlightingAssets {
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")?,
103− fallback_theme: None,
104− })
107+ Ok(HighlightingAssets::new(
108+ asset_from_cache(&cache_path.join("syntaxes.bin"), "syntax set")?,
109+ asset_from_cache(&cache_path.join("themes.bin"), "theme set")?,
110+ ))
105111 }
106112
107113 fn get_integrated_syntaxset() -> SyntaxSet {
@@ -113,14 +119,10 @@ impl HighlightingAssets {
113119 }
114120
115121 pub fn from_binary() -> Self {
116− let syntax_set = Self::get_integrated_syntaxset();
117− let theme_set = Self::get_integrated_themeset();
118−
119− HighlightingAssets {
120− syntax_set,
121− theme_set,
122− fallback_theme: None,
123− }
122+ HighlightingAssets::new(
123+ Self::get_integrated_syntaxset(),
124+ Self::get_integrated_themeset(),
125+ )
124126 }
125127
126128 pub fn save_to_cache(&self, target_dir: &Path, current_version: &str) -> Result<()> {