From 152d00aac38e38df52a69bb922c74f27a2b29dfa Mon Sep 17 00:00:00 2001 From: OpenByte Date: Sun, 30 Jul 2023 04:26:53 +0200 Subject: [PATCH] Cargo fmt and clippy fix --- burnt-sushi/src/blocker.rs | 7 +++-- burnt-sushi/src/logger/console/log.rs | 7 ++--- burnt-sushi/src/logger/file.rs | 41 ++++++++++++++++++--------- burnt-sushi/src/logger/global.rs | 11 +++---- burnt-sushi/src/logger/mod.rs | 4 +-- burnt-sushi/src/logger/traits.rs | 2 +- burnt-sushi/src/main.rs | 7 ++++- burnt-sushi/src/tray.rs | 2 +- shared/src/lib.rs | 2 +- 9 files changed, 51 insertions(+), 32 deletions(-) diff --git a/burnt-sushi/src/blocker.rs b/burnt-sushi/src/blocker.rs index 10e590a..e448b27 100644 --- a/burnt-sushi/src/blocker.rs +++ b/burnt-sushi/src/blocker.rs @@ -88,7 +88,7 @@ impl SpotifyHookState { match spotify.process.pid().ok() { Some(pid) => info!("Found Spotify (PID={pid})"), - None => info!("Found Spotify") + None => info!("Found Spotify"), } let syringe = Syringe::for_process(spotify.process); @@ -116,7 +116,7 @@ impl SpotifyHookState { info!("Ejecting previous blocker..."); match syringe.eject(prev_payload) { Ok(_) => info!("Ejected previous blocker"), - Err(_) => error!("Failed to eject previous blocker") + Err(_) => error!("Failed to eject previous blocker"), }; } @@ -131,7 +131,8 @@ impl SpotifyHookState { .context("Failed to resolve blocker.")?; info!("Injecting blocker..."); - let payload = syringe.inject(payload_path) + let payload = syringe + .inject(payload_path) .context("Failed to inject blocker.")?; debug!("Starting RPC..."); diff --git a/burnt-sushi/src/logger/console/log.rs b/burnt-sushi/src/logger/console/log.rs index 274b3d2..15bf3c1 100644 --- a/burnt-sushi/src/logger/console/log.rs +++ b/burnt-sushi/src/logger/console/log.rs @@ -23,7 +23,7 @@ use winapi::{ }, }; -use crate::{APP_NAME_WITH_VERSION, logger::SimpleLog}; +use crate::{logger::SimpleLog, APP_NAME_WITH_VERSION}; use super::raw; @@ -34,10 +34,7 @@ pub struct Console(ConsoleImpl); enum ConsoleImpl { Attach, Alloc, - Piped { - process: OwnedProcess, - pipe: File, - }, + Piped { process: OwnedProcess, pipe: File }, } unsafe impl Send for Console {} diff --git a/burnt-sushi/src/logger/file.rs b/burnt-sushi/src/logger/file.rs index 8b0f269..970bcda 100644 --- a/burnt-sushi/src/logger/file.rs +++ b/burnt-sushi/src/logger/file.rs @@ -1,9 +1,10 @@ #![allow(dead_code)] use std::{ - fs::{File, self}, - io::{Write, BufWriter, Read, SeekFrom, Seek}, - path::PathBuf, time::Instant, + fs::{self, File}, + io::{BufWriter, Read, Seek, SeekFrom, Write}, + path::PathBuf, + time::Instant, }; use anyhow::Context; @@ -14,7 +15,7 @@ use super::SimpleLog; pub struct FileLog { path: PathBuf, file: Option>, - last_written: Option + last_written: Option, } impl FileLog { @@ -22,7 +23,7 @@ impl FileLog { Self { path: path.into(), file: None, - last_written: None + last_written: None, } } @@ -34,26 +35,37 @@ impl FileLog { if let Some(dir) = self.path.parent() { fs::create_dir_all(dir).context("Failed to create parent directories for log file.")?; } - let mut file = File::options().create(true).append(true).open(&self.path).context("Failed to open or create log file.")?; - + let mut file = File::options() + .create(true) + .append(true) + .open(&self.path) + .context("Failed to open or create log file.")?; + dbg!(); if dbg!(dbg!(file.metadata().unwrap().len()) > 100 * 1024) { - file = File::options().write(true).read(true).open(&self.path).context("Failed to open or create log file.")?; + file = File::options() + .write(true) + .read(true) + .open(&self.path) + .context("Failed to open or create log file.")?; let mut contents = String::new(); - file.read_to_string(&mut contents).context("Failed to read log file.")?; + file.read_to_string(&mut contents) + .context("Failed to read log file.")?; let mut truncated_contents = String::new(); for (index, _) in contents.match_indices('\n') { - let succeeding = &contents[(index+1)..]; + let succeeding = &contents[(index + 1)..]; if succeeding.len() > 10 * 1024 { continue; } truncated_contents.push_str(succeeding); break; } - file.seek(SeekFrom::Start(0)).context("Failed to seek in log file.")?; + file.seek(SeekFrom::Start(0)) + .context("Failed to seek in log file.")?; file.set_len(0).context("Failed to clear log file.")?; - file.write_all(truncated_contents.as_bytes()).context("Failed to write log file.")?; + file.write_all(truncated_contents.as_bytes()) + .context("Failed to write log file.")?; } let writer = BufWriter::new(file); Ok(self.file.insert(writer)) @@ -62,7 +74,10 @@ impl FileLog { impl SimpleLog for FileLog { fn log(&mut self, message: &str) { - let file = self.open_file().context("Failed to prepare log file.").unwrap(); + let file = self + .open_file() + .context("Failed to prepare log file.") + .unwrap(); writeln!(file, "{}", message).unwrap(); file.flush().unwrap(); } diff --git a/burnt-sushi/src/logger/global.rs b/burnt-sushi/src/logger/global.rs index a19fba8..17beef8 100644 --- a/burnt-sushi/src/logger/global.rs +++ b/burnt-sushi/src/logger/global.rs @@ -1,11 +1,13 @@ -use std::{sync::{Mutex, MutexGuard}, fmt::Debug}; +use std::{ + fmt::Debug, + sync::{Mutex, MutexGuard}, +}; use log::Log; use crate::APP_NAME; -use super::{SimpleLog, Console, FileLog}; - +use super::{Console, FileLog, SimpleLog}; static LOGGER: GlobalLoggerHolder = GlobalLoggerHolder(Mutex::new(GlobalLogger::new())); @@ -37,12 +39,11 @@ impl GlobalLogger { pub const fn new() -> Self { GlobalLogger { console: None, - file: None + file: None, } } } - impl Log for GlobalLoggerHolder { fn enabled(&self, _metadata: &log::Metadata) -> bool { true diff --git a/burnt-sushi/src/logger/mod.rs b/burnt-sushi/src/logger/mod.rs index 85f8a3b..0b411d4 100644 --- a/burnt-sushi/src/logger/mod.rs +++ b/burnt-sushi/src/logger/mod.rs @@ -1,10 +1,10 @@ -pub mod global; pub mod console; pub mod file; +pub mod global; pub mod noop; mod traits; -pub use traits::*; pub use console::Console; pub use file::FileLog; +pub use traits::*; diff --git a/burnt-sushi/src/logger/traits.rs b/burnt-sushi/src/logger/traits.rs index f2bed2f..0f56ee6 100644 --- a/burnt-sushi/src/logger/traits.rs +++ b/burnt-sushi/src/logger/traits.rs @@ -1,4 +1,4 @@ -use std::{fmt::Debug, any::Any}; +use std::{any::Any, fmt::Debug}; pub trait SimpleLog: Any + Debug + Send + Sync { fn log(&mut self, message: &str); diff --git a/burnt-sushi/src/main.rs b/burnt-sushi/src/main.rs index 0671741..90918ff 100644 --- a/burnt-sushi/src/main.rs +++ b/burnt-sushi/src/main.rs @@ -20,7 +20,12 @@ use winapi::{ use std::{env, io, os::windows::prelude::FromRawHandle, time::Duration}; -use crate::{args::{ARGS, LogLevel}, blocker::SpotifyAdBlocker, named_mutex::NamedMutex, logger::{Console, FileLog}}; +use crate::{ + args::{LogLevel, ARGS}, + blocker::SpotifyAdBlocker, + logger::{Console, FileLog}, + named_mutex::NamedMutex, +}; mod args; mod blocker; diff --git a/burnt-sushi/src/tray.rs b/burnt-sushi/src/tray.rs index a54c669..d388742 100644 --- a/burnt-sushi/src/tray.rs +++ b/burnt-sushi/src/tray.rs @@ -121,7 +121,7 @@ impl SystemTrayIcon { fn show_console(&self) { let mut l = logger::global::get(); - if !l.console.is_some() { + if l.console.is_none() { l.console = Some(Console::piped().unwrap()); } } diff --git a/shared/src/lib.rs b/shared/src/lib.rs index 0e38266..1cda1c2 100644 --- a/shared/src/lib.rs +++ b/shared/src/lib.rs @@ -12,7 +12,7 @@ pub mod rpc { pub use super::spotify_ad_guard_capnp::*; } -#[allow(clippy::derive_hash_xor_eq)] +#[allow(clippy::derived_hash_with_manual_eq)] impl hash::Hash for rpc::blocker_service::FilterHook { fn hash(&self, state: &mut H) { core::mem::discriminant(self).hash(state)