From 3262f69bb06b619d333aacae7cb0e5abe8d497e5 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Sat, 20 Feb 2021 14:07:36 +0100 Subject: [PATCH] Single compute_hash fn + cargo fmt --- components/config/src/config/mod.rs | 4 +- components/imageproc/src/lib.rs | 20 +++--- components/site/src/lib.rs | 3 +- components/templates/src/filters.rs | 11 ++-- components/templates/src/global_fns/mod.rs | 74 +++++++++------------- components/templates/src/lib.rs | 2 +- 6 files changed, 50 insertions(+), 64 deletions(-) diff --git a/components/config/src/config/mod.rs b/components/config/src/config/mod.rs index a8118c78..57682b3b 100644 --- a/components/config/src/config/mod.rs +++ b/components/config/src/config/mod.rs @@ -170,8 +170,8 @@ impl Config { /// Parses a config file from the given path pub fn from_file>(path: P) -> Result { let path = path.as_ref(); - let content = read_file(path) - .map_err(|e| errors::Error::chain("Failed to load config", e))?; + let content = + read_file(path).map_err(|e| errors::Error::chain("Failed to load config", e))?; Config::parse(&content) } diff --git a/components/imageproc/src/lib.rs b/components/imageproc/src/lib.rs index e0daaf64..97cad10e 100644 --- a/components/imageproc/src/lib.rs +++ b/components/imageproc/src/lib.rs @@ -1,11 +1,11 @@ -use std::{collections::hash_map::DefaultHasher, io::Write}; use std::collections::hash_map::Entry as HEntry; use std::collections::HashMap; use std::fs::{self, File}; use std::hash::{Hash, Hasher}; use std::path::{Path, PathBuf}; +use std::{collections::hash_map::DefaultHasher, io::Write}; -use image::{EncodableLayout, imageops::FilterType}; +use image::{imageops::FilterType, EncodableLayout}; use image::{GenericImageView, ImageOutputFormat}; use lazy_static::lazy_static; use rayon::prelude::*; @@ -159,7 +159,7 @@ impl Format { None => Err(format!("Unsupported image file: {}", source).into()), }, "jpeg" | "jpg" => Ok(Jpeg(jpg_quality)), - "png" => Ok(Png), + "png" => Ok(Png), "webp" => Ok(WebP(quality)), _ => Err(format!("Invalid image format: {}", format).into()), } @@ -189,7 +189,7 @@ impl Format { match *self { Png => "png", Jpeg(_) => "jpg", - WebP(_) => "webp" + WebP(_) => "webp", } } } @@ -201,9 +201,9 @@ impl Hash for Format { let q = match *self { Png => 0, - Jpeg(q) => q, + Jpeg(q) => q, WebP(None) => 0, - WebP(Some(q)) => q + WebP(Some(q)) => q, }; hasher.write_u8(q); @@ -316,12 +316,8 @@ impl ImageOp { Format::WebP(q) => { let encoder = webp::Encoder::from_image(&img); let memory = match q { - Some(q) => { - encoder.encode(q as f32 / 100.) - } - None => { - encoder.encode_lossless() - } + Some(q) => encoder.encode(q as f32 / 100.), + None => encoder.encode_lossless(), }; let mut bytes = memory.as_bytes(); f.write_all(&mut bytes)?; diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index bc374134..bf15f75f 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -77,7 +77,8 @@ impl Site { if let Some(theme) = config.theme.clone() { // Grab data from the extra section of the theme - config.merge_with_theme(&path.join("themes").join(&theme).join("theme.toml"), &theme)?; + config + .merge_with_theme(&path.join("themes").join(&theme).join("theme.toml"), &theme)?; } let tera = load_tera(path, &config)?; diff --git a/components/templates/src/filters.rs b/components/templates/src/filters.rs index 5e8575bd..17ccc17c 100644 --- a/components/templates/src/filters.rs +++ b/components/templates/src/filters.rs @@ -6,7 +6,7 @@ use std::path::PathBuf; use base64::{decode, encode}; use config::Config; use rendering::{render_content, RenderContext}; -use tera::{Filter as TeraFilter, Result as TeraResult, Tera, Value, to_value, try_get_value}; +use tera::{to_value, try_get_value, Filter as TeraFilter, Result as TeraResult, Tera, Value}; use crate::load_tera; @@ -18,9 +18,12 @@ pub struct MarkdownFilter { } impl MarkdownFilter { - pub fn new(path: PathBuf, config: Config, permalinks: HashMap) -> TeraResult { - let tera = load_tera(&path, &config) - .map_err(|err| tera::Error::msg(err))?; + pub fn new( + path: PathBuf, + config: Config, + permalinks: HashMap, + ) -> TeraResult { + let tera = load_tera(&path, &config).map_err(|err| tera::Error::msg(err))?; Ok(Self { config, permalinks, tera }) } } diff --git a/components/templates/src/global_fns/mod.rs b/components/templates/src/global_fns/mod.rs index 8b8fc910..42b2cbf9 100644 --- a/components/templates/src/global_fns/mod.rs +++ b/components/templates/src/global_fns/mod.rs @@ -5,7 +5,7 @@ use std::sync::{Arc, Mutex, RwLock}; use std::{fs, io, result}; use base64::encode as encode_b64; -use sha2::{Digest, Sha256, Sha384, Sha512}; +use sha2::{digest, Sha256, Sha384, Sha512}; use svg_metadata as svg; use tera::{from_value, to_value, Error, Function as TeraFn, Result, Value}; @@ -90,40 +90,22 @@ fn open_file(search_paths: &[PathBuf], url: &str) -> result::Result result::Result { - let mut hasher = Sha256::new(); +fn compute_file_hash( + mut file: fs::File, + base64: bool, +) -> result::Result +where + digest::Output: core::fmt::LowerHex, + D: std::io::Write, +{ + let mut hasher = D::new(); io::copy(&mut file, &mut hasher)?; - Ok(format!("{:x}", hasher.finalize())) -} - -fn compute_file_sha256_base64(mut file: fs::File) -> result::Result { - let mut hasher = Sha256::new(); - io::copy(&mut file, &mut hasher)?; - Ok(format!("{}", encode_b64(hasher.finalize()))) -} - -fn compute_file_sha384(mut file: fs::File) -> result::Result { - let mut hasher = Sha384::new(); - io::copy(&mut file, &mut hasher)?; - Ok(format!("{:x}", hasher.finalize())) -} - -fn compute_file_sha384_base64(mut file: fs::File) -> result::Result { - let mut hasher = Sha384::new(); - io::copy(&mut file, &mut hasher)?; - Ok(format!("{}", encode_b64(hasher.finalize()))) -} - -fn compute_file_sha512(mut file: fs::File) -> result::Result { - let mut hasher = Sha512::new(); - io::copy(&mut file, &mut hasher)?; - Ok(format!("{:x}", hasher.finalize())) -} - -fn compute_file_sha512_base64(mut file: fs::File) -> result::Result { - let mut hasher = Sha512::new(); - io::copy(&mut file, &mut hasher)?; - Ok(format!("{}", encode_b64(hasher.finalize()))) + let val = format!("{:x}", hasher.finalize()); + if base64 { + Ok(encode_b64(val)) + } else { + Ok(val) + } } fn file_not_found_err(search_paths: &[PathBuf], url: &str) -> Result { @@ -174,7 +156,9 @@ impl TeraFn for GetUrl { } if cachebust { - match open_file(&self.search_paths, &path).and_then(compute_file_sha256) { + match open_file(&self.search_paths, &path) + .and_then(|f| compute_file_hash::(f, false)) + { Ok(hash) => { permalink = format!("{}?h={}", permalink, hash); } @@ -219,17 +203,19 @@ impl TeraFn for GetFileHash { ) .unwrap_or(DEFAULT_BASE64); - let compute_hash_fn = match (sha_type, base64) { - (256, true) => compute_file_sha256_base64, - (256, false) => compute_file_sha256, - (384, true) => compute_file_sha384_base64, - (384, false) => compute_file_sha384, - (512, true) => compute_file_sha512_base64, - (512, false) => compute_file_sha512, - _ => return Err("`get_file_hash`: bad arguments".into()), + let f = match open_file(&self.search_paths, &path) { + Ok(f) => f, + Err(e) => { + return Err(format!("File {} could not be open {}", path, e).into()); + } }; - let hash = open_file(&self.search_paths, &path).and_then(compute_hash_fn); + let hash = match sha_type { + 256 => compute_file_hash::(f, base64), + 384 => compute_file_hash::(f, base64), + 512 => compute_file_hash::(f, base64), + _ => return Err("`get_file_hash`: Invalid sha value".into()), + }; match hash { Ok(digest) => Ok(to_value(digest).unwrap()), diff --git a/components/templates/src/lib.rs b/components/templates/src/lib.rs index 5751fe10..d7ff9b84 100644 --- a/components/templates/src/lib.rs +++ b/components/templates/src/lib.rs @@ -7,7 +7,7 @@ use config::Config; use lazy_static::lazy_static; use tera::{Context, Tera}; -use errors::{Error, Result, bail}; +use errors::{bail, Error, Result}; use utils::templates::rewrite_theme_paths; lazy_static! {