From 96df281a20dd84ba7c73722d259ec16ca24fc2a9 Mon Sep 17 00:00:00 2001 From: Nathan McCarty Date: Mon, 20 Mar 2023 23:44:04 -0400 Subject: [PATCH] refactor: Simplify Site::write --- src/site.rs | 54 +++++++++++++++++++++++++---------------------------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/src/site.rs b/src/site.rs index 4aa1974..aceca37 100644 --- a/src/site.rs +++ b/src/site.rs @@ -144,37 +144,33 @@ impl Site { ); } else { debug!(?styles_dir, "Creating styles directory"); + let style_sheets: BTreeMap<&'static str, &'static [u8]> = [ + ( + "default.css", + include_bytes!("../assets/css/default.css").as_ref(), + ), + ( + "index.css", + include_bytes!("../assets/css/index.css").as_ref(), + ), + ( + "post.css", + include_bytes!("../assets/css/post.css").as_ref(), + ), + ] + .into_iter() + .collect(); create_dir_all(&styles_dir).context(CreateDirectorySnafu { path: &styles_dir })?; // Copy over the built in styles - // default - let default_path = styles_dir.join("default.css"); - debug!(?default_path, "Copying over default.css"); - let mut default = File::create(&default_path).context(WriteConfigSnafu { - path: &default_path, - })?; - default - .write_all(include_bytes!("../assets/css/default.css")) - .context(WriteConfigSnafu { - path: &default_path, - })?; - self.styles.insert(PathBuf::from("default.css")); - // index - let index_path = styles_dir.join("index.css"); - debug!(?index_path, "Copying over index.css"); - let mut index = - File::create(&index_path).context(WriteConfigSnafu { path: &index_path })?; - index - .write_all(include_bytes!("../assets/css/index.css")) - .context(WriteConfigSnafu { path: &index_path })?; - self.styles.insert(PathBuf::from("index.css")); - // post - let post_path = styles_dir.join("post.css"); - debug!(?post_path, "Copying over post.css"); - let mut post = - File::create(&post_path).context(WriteConfigSnafu { path: &post_path })?; - post.write_all(include_bytes!("../assets/css/post.css")) - .context(WriteConfigSnafu { path: &post_path })?; - self.styles.insert(PathBuf::from("post.css")); + for (style_name, style) in style_sheets { + let path = styles_dir.join(style_name); + debug!(?path, "Copying over {style_name}"); + let mut sheet = File::create(&path).context(WriteConfigSnafu { path: &path })?; + sheet + .write_all(style) + .context(WriteConfigSnafu { path: &path })?; + self.styles.insert(PathBuf::from(style_name)); + } } Ok(())