From 5fa3db4d619acd60cb0cafefffb118c09e0bcb0d Mon Sep 17 00:00:00 2001 From: Jose Quintana Date: Thu, 28 Apr 2022 22:13:00 +0200 Subject: [PATCH] refactor: error context when compiling glob sources --- src/custom_headers.rs | 6 +++--- src/handler.rs | 4 ++-- src/settings/mod.rs | 32 ++++++++++++++++++++------------ 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/custom_headers.rs b/src/custom_headers.rs index a60b864..e680ea0 100644 --- a/src/custom_headers.rs +++ b/src/custom_headers.rs @@ -1,14 +1,14 @@ use hyper::{Body, Response}; -use crate::settings::HeadersOpt; +use crate::settings::Header; /** Append custom HTTP headers to current response. */ pub fn append_headers( uri: &str, - multiple_headers: &Option>, + headers_opts_vec: &Option>, resp: &mut Response, ) { - if let Some(multiple_headers) = multiple_headers { + if let Some(multiple_headers) = headers_opts_vec { for headers_entry in multiple_headers.iter() { // Match header glob pattern against request uri if headers_entry.source.is_match(uri) { diff --git a/src/handler.rs b/src/handler.rs index dadbbb1..61eab62 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -3,7 +3,7 @@ use std::{future::Future, path::PathBuf, sync::Arc}; use crate::{ basic_auth, compression, control_headers, cors, custom_headers, error_page, fallback_page, - security_headers, settings::AdvancedOpts, static_files, Error, Result, + security_headers, settings::Advanced, static_files, Error, Result, }; /// It defines options for a request handler. @@ -22,7 +22,7 @@ pub struct RequestHandlerOpts { pub basic_auth: String, // Advanced options - pub advanced_opts: Option, + pub advanced_opts: Option, } /// It defines the main request handler used by the Hyper service request. diff --git a/src/settings/mod.rs b/src/settings/mod.rs index 33474d9..cd1168a 100644 --- a/src/settings/mod.rs +++ b/src/settings/mod.rs @@ -9,21 +9,25 @@ pub mod file; use cli::General; -pub struct AdvancedOpts { - pub headers: Option>, -} - -pub struct HeadersOpt { +/// Headers file options. +pub struct Header { + /// Source pattern glob matcher pub source: GlobMatcher, + /// Custom HTTP headers pub headers: HeaderMap, } +/// The advanced file options. +pub struct Advanced { + pub headers: Option>, +} + /// The Server CLI and File settings. pub struct Settings { /// General server options pub general: General, /// Advanced server options - pub advanced: Option, + pub advanced: Option, } impl Settings { @@ -56,7 +60,7 @@ impl Settings { let mut page_fallback = opts.page_fallback.to_owned(); // Define the advanced file options - let mut settings_advanced: Option = None; + let mut settings_advanced: Option = None; // Handle config file options and set them when available // NOTE: All config file based options shouldn't be mandatory, therefore `Some()` wrapped @@ -67,7 +71,9 @@ impl Settings { .with_context(|| "error resolving toml config file path")?; let settings = file::Settings::read(&path_resolved) - .with_context(|| {"can not read toml config file because has invalid or unsupported format/options" })?; + .with_context(|| { + "can not read toml config file because has invalid or unsupported format/options" + })?; config_file = Some(path_resolved); @@ -139,17 +145,19 @@ impl Settings { if let Some(advanced) = settings.advanced { if let Some(multiple_headers) = advanced.headers { - let mut headers_vec: Vec = Vec::new(); + let mut headers_vec: Vec
= Vec::new(); // Compile glob patterns for header sources for headers_entry in multiple_headers.iter() { - let source = Glob::new(&headers_entry.source)?.compile_matcher(); - headers_vec.push(HeadersOpt { + let source = Glob::new(&headers_entry.source) + .with_context(|| "can not compile glob pattern for header source")? + .compile_matcher(); + headers_vec.push(Header { source, headers: headers_entry.headers.to_owned(), }); } - settings_advanced = Some(AdvancedOpts { + settings_advanced = Some(Advanced { headers: Some(headers_vec), }); } -- libgit2 1.7.2