Skip to content

Commit 1ad5237

Browse files
committed
style: fix clippy warnings
- Collapse if statements with if-let chains - Use &Path instead of &PathBuf in function parameters
1 parent 84313e9 commit 1ad5237

3 files changed

Lines changed: 22 additions & 25 deletions

File tree

src/actor/job/executor.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rand::Rng;
22
use std::collections::HashMap;
33
use std::fs::{self, File, OpenOptions};
44
use std::io::Write;
5-
use std::path::PathBuf;
5+
use std::path::{Path, PathBuf};
66
use std::time::Duration;
77
use tokio::process::Command;
88
use tokio::time::sleep;
@@ -16,7 +16,7 @@ use crate::webhook::{self, JobFailure};
1616
/// Default jitter ratio when not explicitly configured (25% of base delay)
1717
const AUTO_JITTER_RATIO: u32 = 25;
1818

19-
pub async fn execute_job(job: &Job, sot_path: &PathBuf, runner: &RunnerConfig) -> bool {
19+
pub async fn execute_job(job: &Job, sot_path: &Path, runner: &RunnerConfig) -> bool {
2020
let job_dir = git::get_job_dir(sot_path, &job.id);
2121
let work_dir = resolve_work_dir(sot_path, &job.id, &job.working_dir);
2222
let mut log_file = job
@@ -28,8 +28,8 @@ pub async fn execute_job(job: &Job, sot_path: &PathBuf, runner: &RunnerConfig) -
2828
let mut last_result: Option<CommandResult> = None;
2929

3030
for attempt in 0..max_attempts {
31-
if attempt > 0 {
32-
if let Some(retry) = job.retry.as_ref() {
31+
if attempt > 0
32+
&& let Some(retry) = job.retry.as_ref() {
3333
let delay = calculate_backoff(retry, attempt - 1);
3434
info!(
3535
target: "rollcron::job",
@@ -41,7 +41,6 @@ pub async fn execute_job(job: &Job, sot_path: &PathBuf, runner: &RunnerConfig) -
4141
);
4242
sleep(delay).await;
4343
}
44-
}
4544

4645
info!(
4746
target: "rollcron::job",
@@ -116,7 +115,7 @@ pub async fn execute_job(job: &Job, sot_path: &PathBuf, runner: &RunnerConfig) -
116115
false
117116
}
118117

119-
fn resolve_work_dir(sot_path: &PathBuf, job_id: &str, working_dir: &Option<String>) -> PathBuf {
118+
fn resolve_work_dir(sot_path: &Path, job_id: &str, working_dir: &Option<String>) -> PathBuf {
120119
let job_dir = git::get_job_dir(sot_path, job_id);
121120
match working_dir {
122121
Some(dir) => {
@@ -141,8 +140,8 @@ fn resolve_work_dir(sot_path: &PathBuf, job_id: &str, working_dir: &Option<Strin
141140

142141
async fn run_command(
143142
job: &Job,
144-
work_dir: &PathBuf,
145-
sot_path: &PathBuf,
143+
work_dir: &Path,
144+
sot_path: &Path,
146145
runner: &RunnerConfig,
147146
) -> CommandResult {
148147
let env_vars = match merge_env_vars(job, work_dir, sot_path, runner) {
@@ -180,7 +179,7 @@ async fn run_command(
180179
/// Load runner-level env vars for webhook URL expansion.
181180
/// Returns None on error (webhook will fall back to process env).
182181
fn load_runner_env_vars(
183-
sot_path: &PathBuf,
182+
sot_path: &Path,
184183
runner: &RunnerConfig,
185184
) -> Option<HashMap<String, String>> {
186185
let mut env_vars = HashMap::new();
@@ -210,8 +209,8 @@ fn load_runner_env_vars(
210209

211210
fn merge_env_vars(
212211
job: &Job,
213-
work_dir: &PathBuf,
214-
sot_path: &PathBuf,
212+
work_dir: &Path,
213+
sot_path: &Path,
215214
runner: &RunnerConfig,
216215
) -> anyhow::Result<HashMap<String, String>> {
217216
let mut env_vars = HashMap::new();
@@ -319,26 +318,24 @@ fn generate_jitter(max: Duration) -> Duration {
319318

320319
// === Logging ===
321320

322-
fn rotate_log_file(path: &PathBuf, max_size: u64) {
323-
if let Ok(meta) = fs::metadata(path) {
324-
if meta.len() >= max_size {
321+
fn rotate_log_file(path: &Path, max_size: u64) {
322+
if let Ok(meta) = fs::metadata(path)
323+
&& meta.len() >= max_size {
325324
let old_path = path.with_extension("log.old");
326325
let _ = fs::remove_file(&old_path);
327326
let _ = fs::rename(path, &old_path);
328327
}
329-
}
330328
}
331329

332-
fn create_log_file(job_dir: &PathBuf, log_path: &str, max_size: u64) -> Option<File> {
330+
fn create_log_file(job_dir: &Path, log_path: &str, max_size: u64) -> Option<File> {
333331
let expanded = env::expand_string(log_path);
334332
let full_path = job_dir.join(&expanded);
335333

336-
if let Some(parent) = full_path.parent() {
337-
if let Err(e) = fs::create_dir_all(parent) {
334+
if let Some(parent) = full_path.parent()
335+
&& let Err(e) = fs::create_dir_all(parent) {
338336
warn!(target: "rollcron::job", error = %e, "Failed to create log directory");
339337
return None;
340338
}
341-
}
342339

343340
rotate_log_file(&full_path, max_size);
344341

src/actor/runner/git_poll.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::{ConfigUpdate, GetRunnerConfig};
22
use crate::config::{self, RunnerConfig};
33
use crate::{env, git, webhook};
44
use std::collections::HashMap;
5-
use std::path::PathBuf;
5+
use std::path::{Path, PathBuf};
66
use std::time::Duration;
77
use tokio::time::interval;
88
use tracing::{error, info, warn};
@@ -55,7 +55,7 @@ where
5555
}
5656
}
5757

58-
async fn notify_config_error<A>(addr: &Address<A, Weak>, sot_path: &PathBuf, error: &str)
58+
async fn notify_config_error<A>(addr: &Address<A, Weak>, sot_path: &Path, error: &str)
5959
where
6060
A: Handler<GetRunnerConfig, Return = RunnerConfig>,
6161
{
@@ -83,7 +83,7 @@ where
8383
}
8484
}
8585

86-
fn load_runner_env(sot_path: &PathBuf, runner: &RunnerConfig) -> Option<HashMap<String, String>> {
86+
fn load_runner_env(sot_path: &Path, runner: &RunnerConfig) -> Option<HashMap<String, String>> {
8787
let mut env_vars = HashMap::new();
8888

8989
if let Some(env_file_path) = &runner.env_file {
@@ -107,7 +107,7 @@ fn load_runner_env(sot_path: &PathBuf, runner: &RunnerConfig) -> Option<HashMap<
107107
Some(env_vars)
108108
}
109109

110-
fn load_config(sot_path: &PathBuf) -> anyhow::Result<(config::RunnerConfig, Vec<config::Job>)> {
110+
fn load_config(sot_path: &Path) -> anyhow::Result<(config::RunnerConfig, Vec<config::Job>)> {
111111
let config_path = sot_path.join(CONFIG_FILE);
112112
let content = std::fs::read_to_string(&config_path)
113113
.map_err(|e| anyhow::anyhow!("Failed to read {}: {}", config_path.display(), e))?;

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod webhook;
88
use actor::runner::{GetJobIds, GracefulShutdown, Initialize, RunnerActor};
99
use anyhow::{Context, Result};
1010
use clap::Parser;
11-
use std::path::PathBuf;
11+
use std::path::{Path, PathBuf};
1212
use std::time::Duration;
1313
use tracing::{error, info};
1414
use xtra::prelude::*;
@@ -84,7 +84,7 @@ async fn main() -> Result<()> {
8484
Ok(())
8585
}
8686

87-
fn load_config(sot_path: &PathBuf) -> Result<(config::RunnerConfig, Vec<config::Job>)> {
87+
fn load_config(sot_path: &Path) -> Result<(config::RunnerConfig, Vec<config::Job>)> {
8888
let config_path = sot_path.join(CONFIG_FILE);
8989
let content = std::fs::read_to_string(&config_path)
9090
.map_err(|e| anyhow::anyhow!("Failed to read {}: {}", config_path.display(), e))?;

0 commit comments

Comments
 (0)