Skip to content

Commit 4699462

Browse files
committed
fix: clippy lints and formatting
1 parent b25997b commit 4699462

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

cron/crond.rs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,36 @@
77
// SPDX-License-Identifier: MIT
88
//
99

10-
use cron::job::Database;
1110
use chrono::Local;
11+
use cron::job::Database;
1212
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
13+
use std::cmp::Ordering::{Greater, Less};
1314
use std::env;
1415
use std::error::Error;
15-
use std::str::FromStr;
16-
use std::fs;
17-
use std::time::UNIX_EPOCH;
1816
use std::fmt;
17+
use std::fs;
18+
use std::str::FromStr;
1919
use std::sync::Mutex;
20+
use std::time::UNIX_EPOCH;
2021

2122
static CRONTAB: Mutex<Option<Database>> = Mutex::new(None);
2223
static LAST_MODIFIED: Mutex<Option<u64>> = Mutex::new(None);
2324

2425
#[derive(Debug)]
25-
enum CronError{
26+
enum CronError {
2627
Fork,
2728
NoLogname,
28-
NoCrontab
29+
NoCrontab,
2930
}
3031

3132
impl Error for CronError {}
3233

3334
impl fmt::Display for CronError {
3435
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35-
match self{
36+
match self {
3637
Self::NoLogname => write!(f, "Could not obtain the user's logname"),
3738
Self::NoCrontab => write!(f, "Could not format database"),
38-
Self::Fork => write!(f, "Could not create child process")
39+
Self::Fork => write!(f, "Could not create child process"),
3940
}
4041
}
4142
}
@@ -46,15 +47,15 @@ fn is_file_changed(filepath: &str) -> Result<bool, Box<dyn Error>> {
4647
.modified()?
4748
.duration_since(UNIX_EPOCH)?
4849
.as_secs();
49-
50-
let Some(last_checked) = *LAST_MODIFIED.lock().unwrap() else{
50+
51+
let Some(last_checked) = *LAST_MODIFIED.lock().unwrap() else {
5152
*LAST_MODIFIED.lock().unwrap() = Some(last_modified);
5253
return Ok(true);
5354
};
54-
if last_checked <= last_modified{
55+
if last_checked <= last_modified {
5556
*LAST_MODIFIED.lock().unwrap() = Some(last_modified);
5657
Ok(true)
57-
}else{
58+
} else {
5859
Ok(false)
5960
}
6061
}
@@ -68,18 +69,19 @@ fn sync_cronfile() -> Result<(), Box<dyn Error>> {
6869
let file = format!("/var/spool/cron/{logname}");
6970
#[cfg(target_os = "macos")]
7071
let file = format!("/var/at/tabs/{logname}");
71-
if (*CRONTAB.lock().unwrap()).is_none() || is_file_changed(&file)?{
72+
if (*CRONTAB.lock().unwrap()).is_none() || is_file_changed(&file)? {
7273
let s = fs::read_to_string(&file)?;
73-
let crontab = s.lines()
74+
let crontab = s
75+
.lines()
7476
.filter_map(|x| Database::from_str(x).ok())
7577
.fold(Database(vec![]), |acc, next| acc.merge(next));
76-
*CRONTAB.lock().unwrap() = Some(crontab);
78+
*CRONTAB.lock().unwrap() = Some(crontab);
7779
}
7880
Ok(())
7981
}
8082

8183
/// Create new daemon process of crond
82-
fn setup() -> i32{
84+
fn setup() -> i32 {
8385
unsafe {
8486
use libc::*;
8587

@@ -94,15 +96,15 @@ fn setup() -> i32{
9496
close(STDIN_FILENO);
9597
close(STDOUT_FILENO);
9698
close(STDERR_FILENO);
97-
98-
return pid;
99+
100+
pid
99101
}
100102
}
101103

102104
/// Handles incoming signals
103105
fn handle_signals(signal_code: libc::c_int) {
104106
if signal_code == libc::SIGHUP {
105-
if let Err(err) = sync_cronfile(){
107+
if let Err(err) = sync_cronfile() {
106108
eprintln!("{err}");
107109
std::process::exit(1);
108110
}
@@ -113,7 +115,7 @@ fn handle_signals(signal_code: libc::c_int) {
113115
fn daemon_loop() -> Result<(), Box<dyn Error>> {
114116
loop {
115117
sync_cronfile()?;
116-
let Some(db) = CRONTAB.lock().unwrap().clone() else{
118+
let Some(db) = CRONTAB.lock().unwrap().clone() else {
117119
return Err(Box::new(CronError::NoCrontab));
118120
};
119121
let Some(x) = db.nearest_job() else {
@@ -144,10 +146,10 @@ fn main() -> Result<(), Box<dyn Error>> {
144146

145147
let pid = setup();
146148

147-
if pid < 0{
148-
return Err(Box::new(CronError::Fork));
149-
}else if pid > 0{
150-
return Ok(());
149+
match pid.cmp(&0) {
150+
Less => return Err(Box::new(CronError::Fork)),
151+
Greater => return Ok(()),
152+
_ => {}
151153
}
152154

153155
unsafe {
@@ -159,4 +161,4 @@ fn main() -> Result<(), Box<dyn Error>> {
159161

160162
fn sleep(target: u32) {
161163
unsafe { libc::sleep(target) };
162-
}
164+
}

cron/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
// SPDX-License-Identifier: MIT
88
//
99

10-
pub mod job;
10+
pub mod job;

cron/tests/crond/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ fn test_signal() {
226226
let file = format!("/var/at/tabs/{logname}");
227227
let mut tmp_file_created = false;
228228
let filepath = std::path::PathBuf::from_str(&file).unwrap();
229-
if !filepath.exists(){
229+
if !filepath.exists() {
230230
std::fs::File::create(&file).unwrap();
231231
tmp_file_created = true;
232232
}
@@ -236,8 +236,8 @@ fn test_signal() {
236236

237237
let pids = pid::get_pids("target/debug/crond").unwrap();
238238
assert!(!pids.is_empty());
239-
for pid in &pids{
240-
unsafe{
239+
for pid in &pids {
240+
unsafe {
241241
libc::kill(*pid, libc::SIGHUP);
242242
}
243243
}
@@ -251,7 +251,7 @@ fn test_signal() {
251251

252252
let _ = pid::kill("target/debug/crond").unwrap();
253253

254-
if tmp_file_created && filepath.starts_with("/var/at/tabs"){
254+
if tmp_file_created && filepath.starts_with("/var/at/tabs") {
255255
let _ = std::fs::remove_file(file);
256256
}
257257
}

0 commit comments

Comments
 (0)