Single compute_hash fn + cargo fmt

This commit is contained in:
Vincent Prouillet 2021-02-20 14:07:36 +01:00
parent d3ab3936de
commit 3262f69bb0
6 changed files with 50 additions and 64 deletions

View file

@ -170,8 +170,8 @@ impl Config {
/// Parses a config file from the given path
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Config> {
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)
}

View file

@ -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::*;
@ -189,7 +189,7 @@ impl Format {
match *self {
Png => "png",
Jpeg(_) => "jpg",
WebP(_) => "webp"
WebP(_) => "webp",
}
}
}
@ -203,7 +203,7 @@ impl Hash for Format {
Png => 0,
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)?;

View file

@ -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)?;

View file

@ -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<String, String>) -> TeraResult<Self> {
let tera = load_tera(&path, &config)
.map_err(|err| tera::Error::msg(err))?;
pub fn new(
path: PathBuf,
config: Config,
permalinks: HashMap<String, String>,
) -> TeraResult<Self> {
let tera = load_tera(&path, &config).map_err(|err| tera::Error::msg(err))?;
Ok(Self { config, permalinks, tera })
}
}

View file

@ -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<fs::File, io
Err(io::Error::from(io::ErrorKind::NotFound))
}
fn compute_file_sha256(mut file: fs::File) -> result::Result<String, io::Error> {
let mut hasher = Sha256::new();
fn compute_file_hash<D: digest::Digest>(
mut file: fs::File,
base64: bool,
) -> result::Result<String, io::Error>
where
digest::Output<D>: core::fmt::LowerHex,
D: std::io::Write,
{
let mut hasher = D::new();
io::copy(&mut file, &mut hasher)?;
Ok(format!("{:x}", hasher.finalize()))
let val = format!("{:x}", hasher.finalize());
if base64 {
Ok(encode_b64(val))
} else {
Ok(val)
}
fn compute_file_sha256_base64(mut file: fs::File) -> result::Result<String, io::Error> {
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<String, io::Error> {
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<String, io::Error> {
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<String, io::Error> {
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<String, io::Error> {
let mut hasher = Sha512::new();
io::copy(&mut file, &mut hasher)?;
Ok(format!("{}", encode_b64(hasher.finalize())))
}
fn file_not_found_err(search_paths: &[PathBuf], url: &str) -> Result<Value> {
@ -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::<Sha256>(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::<Sha256>(f, base64),
384 => compute_file_hash::<Sha384>(f, base64),
512 => compute_file_hash::<Sha512>(f, base64),
_ => return Err("`get_file_hash`: Invalid sha value".into()),
};
match hash {
Ok(digest) => Ok(to_value(digest).unwrap()),

View file

@ -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! {