From 0d836b899fe7c73d0bbc422cd380c61bd1b5f789 Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Sat, 17 Oct 2020 23:44:52 +0200 Subject: [PATCH] Style --- src/builder.rs | 2 +- src/command_runner.rs | 6 ++++-- src/locator.rs | 20 +++++++++++++------- src/setup/symbol_runner.rs | 38 +++++++++++++++----------------------- src/symbols/user.rs | 6 ++++-- 5 files changed, 37 insertions(+), 35 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 7a89085..481096d 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -719,7 +719,7 @@ impl ImplementationBuilder> for DefaultBuilder { impl ImplementationBuilder> for DefaultBuilder { type Prerequisites = (); - fn prerequisites(resource: &PostgresqlDatabase) -> Self::Prerequisites { + fn prerequisites(_: &PostgresqlDatabase) -> Self::Prerequisites { () } diff --git a/src/command_runner.rs b/src/command_runner.rs index 5d8fd2f..b69e7ee 100644 --- a/src/command_runner.rs +++ b/src/command_runner.rs @@ -125,8 +125,8 @@ impl, C: CommandRunner> CommandRunner for SetuidCommandRunner<'_, let uid = get_user_by_name(self.user_name.as_ref()) .expect("User does not exist") .uid(); - let _set_home = TempSetEnv::new("HOME", format!("/home/{}", self.user_name.as_ref())); - let _set_dbus = TempSetEnv::new("XDG_RUNTIME_DIR", format!("/run/user/{}", uid)); + let set_home = TempSetEnv::new("HOME", format!("/home/{}", self.user_name.as_ref())); + let set_dbus = TempSetEnv::new("XDG_RUNTIME_DIR", format!("/run/user/{}", uid)); //println!("{} {:?}", program, args); let mut child = Command::new(program) .args(args) @@ -143,6 +143,8 @@ impl, C: CommandRunner> CommandRunner for SetuidCommandRunner<'_, .await .expect("Failed to write to stdin"); let res = child.wait_with_output().await; + drop(set_home); + drop(set_dbus); //println!("{:?}", res); res } diff --git a/src/locator.rs b/src/locator.rs index bdf17a7..739c8c3 100644 --- a/src/locator.rs +++ b/src/locator.rs @@ -15,23 +15,29 @@ use std::marker::PhantomData; use std::path::{Path, PathBuf}; pub trait Policy { + #[must_use] fn acme_user() -> &'static str { "acme" } + + #[must_use] fn user_home(user_name: &str) -> PathBuf { - format!("/home/{}", user_name).into() + Path::new("/home").join(user_name) } + #[must_use] fn user_name_for_domain(domain_name: &'_ str) -> String { domain_name.split('.').rev().fold(String::new(), |result, part| if result.is_empty() { result } else { result + "_" } + part) } + #[must_use] fn php_version() -> &'static str { "7.0" } + #[must_use] fn path_for_data(name: impl Display) -> PathBuf { - ("/root/data".as_ref() as &Path).join(format!("_{}", name)) + Path::new("/root/data").join(format!("_{}", name)) } } @@ -286,7 +292,7 @@ impl, POLICY> ResourceLocator> for DefaultLocator< resource: &ServeCustom, ) -> ( as Resource>::Artifact, Self::Prerequisites) { ( - PathArtifact::from(("/etc/nginx/sites-enabled/".as_ref() as &Path).join(&resource.0)), + PathArtifact::from(Path::new("/etc/nginx/sites-enabled/").join(&resource.0)), (), ) } @@ -298,7 +304,7 @@ impl, P, POLICY> ResourceLocator> for DefaultLocat resource: &ServePhp, ) -> ( as Resource>::Artifact, Self::Prerequisites) { ( - PathArtifact::from(("/etc/nginx/sites-enabled/".as_ref() as &Path).join(&resource.0)), + PathArtifact::from(Path::new("/etc/nginx/sites-enabled/").join(&resource.0)), (), ) } @@ -313,7 +319,7 @@ impl, P, POLICY> ResourceLocator> for DefaultL Self::Prerequisites, ) { ( - PathArtifact::from(("/etc/nginx/sites-enabled/".as_ref() as &Path).join(&resource.0)), + PathArtifact::from(Path::new("/etc/nginx/sites-enabled/").join(&resource.0)), (), ) } @@ -325,7 +331,7 @@ impl, POLICY> ResourceLocator> for DefaultLocator

, ) -> ( as Resource>::Artifact, Self::Prerequisites) { ( - PathArtifact::from(("/etc/nginx/sites-enabled/".as_ref() as &Path).join(&resource.0)), + PathArtifact::from(Path::new("/etc/nginx/sites-enabled/").join(&resource.0)), (), ) } @@ -340,7 +346,7 @@ impl, P, POLICY> ResourceLocator> for DefaultLo Self::Prerequisites, ) { ( - PathArtifact::from(("/etc/nginx/sites-enabled/".as_ref() as &Path).join(&resource.0)), + PathArtifact::from(Path::new("/etc/nginx/sites-enabled/").join(&resource.0)), (), ) } diff --git a/src/setup/symbol_runner.rs b/src/setup/symbol_runner.rs index 5448521..378cd90 100644 --- a/src/setup/symbol_runner.rs +++ b/src/setup/symbol_runner.rs @@ -17,27 +17,14 @@ pub trait SymbolRunner { ) -> Result>; } -#[derive(Debug)] -pub enum SymbolRunError { - Symbol(Box), - ExecuteDidNotReach(()), -} +#[derive(Debug, Default)] +pub struct ExecuteDidNotReachError; -impl Error for SymbolRunError { - fn cause(&self) -> Option<&dyn Error> { - match self { - Self::Symbol(ref e) => Some(&**e), - Self::ExecuteDidNotReach(_) => None, - } - } -} +impl Error for ExecuteDidNotReachError {} -impl fmt::Display for SymbolRunError { +impl fmt::Display for ExecuteDidNotReachError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Symbol(ref e) => write!(f, "{}", e), - Self::ExecuteDidNotReach(_) => write!(f, "Target not reached after executing symbol"), - } + write!(f, "Target not reached after executing symbol") } } @@ -45,7 +32,8 @@ impl fmt::Display for SymbolRunError { pub struct InitializingSymbolRunner; impl InitializingSymbolRunner { - pub fn new() -> Self { + #[must_use] + pub const fn new() -> Self { Self } @@ -64,7 +52,7 @@ impl InitializingSymbolRunner { if target_reached { Ok(()) } else { - Err(Box::new(SymbolRunError::ExecuteDidNotReach(()))) + Err(Box::new(ExecuteDidNotReachError)) } } } @@ -102,7 +90,8 @@ impl SymbolRunner for InitializingSymbolRunner { pub struct DelayingSymbolRunner(R); impl DelayingSymbolRunner { - pub fn new(symbol_runner: R) -> Self { + #[must_use] + pub const fn new(symbol_runner: R) -> Self { Self(symbol_runner) } } @@ -137,7 +126,8 @@ where pub struct DrySymbolRunner; impl DrySymbolRunner { - pub fn new() -> Self { + #[must_use] + pub const fn new() -> Self { Self } } @@ -172,7 +162,8 @@ impl SymbolRunner for DrySymbolRunner { pub struct ReportingSymbolRunner(R); impl ReportingSymbolRunner { - pub fn new(symbol_runner: R) -> Self { + #[must_use] + pub const fn new(symbol_runner: R) -> Self { Self(symbol_runner) } } @@ -251,6 +242,7 @@ mod test { T: Iterator>>, > DummySymbol { + #[must_use] fn new< IE: IntoIterator>>, IT: IntoIterator>>, diff --git a/src/symbols/user.rs b/src/symbols/user.rs index 5d54f3f..3c67809 100644 --- a/src/symbols/user.rs +++ b/src/symbols/user.rs @@ -41,7 +41,7 @@ impl, C: CommandRunner> Symbol for User { // adduser is not reentrant because finding the next uid // and creating the account is not an atomic operation let wait = WAIT.acquire().await; - self + let res = self .command_runner .run_successfully( "adduser", @@ -51,7 +51,9 @@ impl, C: CommandRunner> Symbol for User { self.user_name.as_ref(), ], ) - .await + .await; + drop(wait); + res } }