From b8fed59235d0bd58f588c93d0deabfd0aaad3969 Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Wed, 27 Mar 2019 19:58:44 +0100 Subject: [PATCH] Enhance code formatting --- acmed/src/acmed.rs | 10 ++++---- acmed/src/config.rs | 4 ++-- acmed/src/encoding.rs | 8 +++---- acmed/src/hooks.rs | 2 +- acmed/src/logs.rs | 14 +++++------ acmed/src/main.rs | 2 +- acmed/src/storage.rs | 55 +++++++++++++++++++++++++------------------ 7 files changed, 53 insertions(+), 42 deletions(-) diff --git a/acmed/src/acmed.rs b/acmed/src/acmed.rs index fb3e7a1..09eb6fa 100644 --- a/acmed/src/acmed.rs +++ b/acmed/src/acmed.rs @@ -1,13 +1,13 @@ -use acme_lib::{Directory, DirectoryUrl}; use crate::config::{self, Hook}; use crate::errors::Error; use crate::hooks; use crate::storage::Storage; +use acme_lib::{Directory, DirectoryUrl}; use log::{debug, info, warn}; use openssl; use serde::Serialize; -use std::{fmt, thread}; use std::time::Duration; +use std::{fmt, thread}; use x509_parser::parse_x509_der; #[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord)] @@ -220,10 +220,12 @@ impl Certificate { let mut raw_crt = vec![]; let mut raw_pk = vec![]; if self.kp_reuse { - raw_crt = self.storage + raw_crt = self + .storage .get_certificate(&Format::Der)? .unwrap_or_else(|| vec![]); - raw_pk = self.storage + raw_pk = self + .storage .get_private_key(&Format::Der)? .unwrap_or_else(|| vec![]); }; diff --git a/acmed/src/config.rs b/acmed/src/config.rs index b720562..c137dab 100644 --- a/acmed/src/config.rs +++ b/acmed/src/config.rs @@ -3,8 +3,8 @@ use crate::errors::Error; use log::info; use serde::Deserialize; use std::fs::{self, File}; -use std::path::Path; use std::io::prelude::*; +use std::path::Path; #[derive(Deserialize)] pub struct Config { @@ -266,7 +266,7 @@ impl Certificate { } } -fn get_hooks(lst: &Vec, cnf: &Config) -> Result, Error> { +fn get_hooks(lst: &[String], cnf: &Config) -> Result, Error> { let mut res = vec![]; for name in lst.iter() { let mut h = cnf.get_hook(&name)?; diff --git a/acmed/src/encoding.rs b/acmed/src/encoding.rs index 2243c18..612bd47 100644 --- a/acmed/src/encoding.rs +++ b/acmed/src/encoding.rs @@ -1,6 +1,6 @@ -use acme_lib::Error; -use acme_lib::persist::PersistKind; use crate::acmed::Format; +use acme_lib::persist::PersistKind; +use acme_lib::Error; use log::debug; use pem::{encode, Pem}; @@ -71,9 +71,9 @@ pub fn convert( #[cfg(test)] mod tests { - use acme_lib::persist::PersistKind; - use crate::acmed::Format; use super::convert; + use crate::acmed::Format; + use acme_lib::persist::PersistKind; // Test data generated using: // diff --git a/acmed/src/hooks.rs b/acmed/src/hooks.rs index 237fc64..4efcc26 100644 --- a/acmed/src/hooks.rs +++ b/acmed/src/hooks.rs @@ -20,7 +20,7 @@ macro_rules! get_hook_output { }}; } -pub fn call_multiple(data: &T, hooks: &Vec) -> Result<(), Error> { +pub fn call_multiple(data: &T, hooks: &[Hook]) -> Result<(), Error> { for hook in hooks.iter() { call(data, &hook)?; } diff --git a/acmed/src/logs.rs b/acmed/src/logs.rs index 431c3ab..7e8d9b6 100644 --- a/acmed/src/logs.rs +++ b/acmed/src/logs.rs @@ -44,13 +44,13 @@ pub fn set_log_system( has_stderr: bool, ) -> Result<(LogSystem, LevelFilter), Error> { let log_level = get_loglevel(log_level)?; - let mut logtype = crate::DEFAULT_LOG_SYSTEM; - if has_stderr { - logtype = LogSystem::StdErr; - } - if has_syslog { - logtype = LogSystem::SysLog; - } + let logtype = if has_syslog { + LogSystem::SysLog + } else if has_stderr { + LogSystem::StdErr + } else { + crate::DEFAULT_LOG_SYSTEM + }; match logtype { LogSystem::SysLog => set_log_syslog(log_level)?, LogSystem::StdErr => set_log_stderr(log_level)?, diff --git a/acmed/src/main.rs b/acmed/src/main.rs index f46dddc..05ae50f 100644 --- a/acmed/src/main.rs +++ b/acmed/src/main.rs @@ -61,7 +61,7 @@ fn main() { Arg::with_name("foregroung") .short("f") .long("foregroung") - .help("Runs in the foregroung") + .help("Runs in the foregroung"), ) .arg( Arg::with_name("pid-file") diff --git a/acmed/src/storage.rs b/acmed/src/storage.rs index 7ba32f0..5abbae9 100644 --- a/acmed/src/storage.rs +++ b/acmed/src/storage.rs @@ -1,10 +1,10 @@ -use acme_lib::Error; -use acme_lib::persist::{Persist, PersistKey, PersistKind}; use crate::acmed::{Algorithm, Format}; use crate::config::Hook; -use crate::errors; use crate::encoding::convert; +use crate::errors; use crate::hooks; +use acme_lib::persist::{Persist, PersistKey, PersistKind}; +use acme_lib::Error; use log::debug; use serde::Serialize; use std::fs::{File, OpenOptions}; @@ -78,25 +78,29 @@ impl Storage { } }; let uid = match uid { - Some(u) => if u.bytes().all(|b| b.is_ascii_digit()) { - let raw_uid = u.parse::().unwrap(); - let nix_uid = nix::unistd::Uid::from_raw(raw_uid); - Some(nix_uid) - } else { - // TODO: handle username - None - }, + Some(u) => { + if u.bytes().all(|b| b.is_ascii_digit()) { + let raw_uid = u.parse::().unwrap(); + let nix_uid = nix::unistd::Uid::from_raw(raw_uid); + Some(nix_uid) + } else { + // TODO: handle username + None + } + } None => None, }; let gid = match gid { - Some(g) => if g.bytes().all(|b| b.is_ascii_digit()) { - let raw_gid = g.parse::().unwrap(); - let nix_gid = nix::unistd::Gid::from_raw(raw_gid); - Some(nix_gid) - } else { - // TODO: handle group name - None - }, + Some(g) => { + if g.bytes().all(|b| b.is_ascii_digit()) { + let raw_gid = g.parse::().unwrap(); + let nix_gid = nix::unistd::Gid::from_raw(raw_gid); + Some(nix_gid) + } else { + // TODO: handle group name + None + } + } None => None, }; match nix::unistd::chown(path, uid, gid) { @@ -122,7 +126,7 @@ impl Storage { path.push(&file_name); FileData { file_directory: base_path.to_string(), - file_name: file_name, + file_name, file_path: path, } } @@ -170,13 +174,17 @@ impl Persist for Storage { if file_exists { hooks::call_multiple(&file_data, &self.file_pre_edit_hooks).map_err(to_acme_err)?; } else { - hooks::call_multiple(&file_data, &self.file_pre_create_hooks).map_err(to_acme_err)?; + hooks::call_multiple(&file_data, &self.file_pre_create_hooks) + .map_err(to_acme_err)?; } { let mut f = if cfg!(unix) { let mut options = OpenOptions::new(); options.mode(self.get_file_mode(key.kind)); - options.write(true).create(true).open(&file_data.file_path)? + options + .write(true) + .create(true) + .open(&file_data.file_path)? } else { File::create(&file_data.file_path)? }; @@ -193,7 +201,8 @@ impl Persist for Storage { self.set_owner(&file_data.file_path, key.kind)?; } if file_exists { - hooks::call_multiple(&file_data, &self.file_post_edit_hooks).map_err(to_acme_err)?; + hooks::call_multiple(&file_data, &self.file_post_edit_hooks) + .map_err(to_acme_err)?; } else { hooks::call_multiple(&file_data, &self.file_post_create_hooks) .map_err(to_acme_err)?;