From 641eff33786339540b86690c88d9509bc6db3655 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Tue, 18 Apr 2023 17:48:40 -0400 Subject: [PATCH] Fix some clippy lints --- src/creator/mod.rs | 3 +- src/hooks/mod.rs | 8 +++--- src/hpk.rs | 2 +- src/installer/mod.rs | 66 ++++++++++++++++++++----------------------- src/item/mod.rs | 19 ++++++------- src/lib.rs | 4 +-- src/repository/mod.rs | 6 ++-- src/tar/header.rs | 22 ++++++--------- src/tar/node.rs | 1 + 9 files changed, 60 insertions(+), 71 deletions(-) diff --git a/src/creator/mod.rs b/src/creator/mod.rs index cf8d4ae..0e345ef 100644 --- a/src/creator/mod.rs +++ b/src/creator/mod.rs @@ -34,6 +34,7 @@ pub struct Creator { } impl Creator { + #[allow(clippy::missing_panics_doc)] pub fn new(path: &Path, specs: Specs) -> Result { let d = env::current_dir()?; env::set_current_dir(path)?; @@ -74,7 +75,7 @@ impl Creator { self.entries.is_empty() } - pub fn create(self, outdir: &Path, sender: Sender) -> Result<(), Box> { + pub fn create(self, outdir: &Path, sender: &Sender) -> Result<(), Box> { let d = env::current_dir()?; let plist = Mutex::new(Plist::default()); let totalsize: AtomicUsize = 0.into(); diff --git a/src/hooks/mod.rs b/src/hooks/mod.rs index db7d64b..4012f37 100644 --- a/src/hooks/mod.rs +++ b/src/hooks/mod.rs @@ -66,21 +66,21 @@ impl Hooks { } fn makeinfo() -> Result { - Command::new("makewhatis").output().map_err(|e| e.into()) + Command::new("makewhatis").output().map_err(Into::into) } fn compile_schemas() -> Result { Command::new("glib-compile-schemas") .arg("/usr/share/glib-2.0/schemas") .output() - .map_err(|e| e.into()) + .map_err(Into::into) } fn install_info(path: &str) -> Result { Command::new("install-info") .arg(path) .output() - .map_err(|e| e.into()) + .map_err(Into::into) } impl Pinstall { @@ -93,6 +93,6 @@ impl Pinstall { .arg(self.script.to_str().unwrap_or("")) .env("HPK_ROOT", self.root.to_str().unwrap_or("")) .output() - .map_err(|e| e.into()) + .map_err(Into::into) } } diff --git a/src/hpk.rs b/src/hpk.rs index 43c9257..1ff3341 100644 --- a/src/hpk.rs +++ b/src/hpk.rs @@ -100,7 +100,7 @@ fn create(matches: &ArgMatches) -> Result<(), Box> { } Ok::<(), CreationError>(()) }); - creator.create(&outdir, sender)?; + creator.create(&outdir, &sender)?; match handle.join() { Ok(_) => { println!("Package created successfully"); diff --git a/src/installer/mod.rs b/src/installer/mod.rs index c0c2dec..dae323e 100644 --- a/src/installer/mod.rs +++ b/src/installer/mod.rs @@ -1,5 +1,5 @@ #![allow(dead_code)] -pub use error::InstallError; +pub use error::Error; use { crate::{ @@ -80,13 +80,12 @@ impl Installer { self, hooks: &mut Vec, sender: Sender, - ) -> Result { + ) -> Result { let reader = Decoder::new(self.reader)?; let mut archive = Archive::read(reader)?; sender.send(InstallMessage::ArchiveRead)?; - let pr_node = match archive.pop("package.ron") { - Some(node) => node, - None => return Err(InstallError::MissingManifest), + let Some(pr_node) = archive.pop("package.ron") else { + return Err(Error::MissingManifest); }; let mut buf = vec![]; pr_node.write(&mut buf)?; @@ -94,12 +93,12 @@ impl Installer { if let Some(ref users) = package.users { users .iter() - .for_each(|u| hooks.push((u.clone(), Some(self.root.to_path_buf())).into())); + .for_each(|u| hooks.push((u.clone(), Some(self.root.clone())).into())); } if let Some(ref groups) = package.groups { groups .iter() - .for_each(|g| hooks.push((g.clone(), Some(self.root.to_path_buf())).into())); + .for_each(|g| hooks.push((g.clone(), Some(self.root.clone())).into())); } let mut db_pkgdir = crate::get_dbdir(Some(self.root.clone())); db_pkgdir.push(&package.name); @@ -120,11 +119,10 @@ impl Installer { pr_node.write(writer)?; let root = &self.root; let hooks = Mutex::new(hooks); - let s = sender.clone(); archive .nodes .par_iter() - .try_for_each_with(s, |sender, node| { + .try_for_each_with(sender, |sender, node| { let mut path = root.clone(); let fpath = node.header.file_path()?; if let Some(s) = node.header.prefix() { @@ -137,7 +135,7 @@ impl Installer { hooks .lock() .unwrap() - .push(Hooks::Info(fpath.to_str().unwrap().to_string())) + .push(Hooks::Info(fpath.to_str().unwrap().to_string())); } else if s.contains("/share/glib-2.0/schemas") { let mut h = hooks.lock().unwrap(); if !h.contains(&Hooks::GlibSchema) { @@ -157,14 +155,15 @@ impl Installer { mode: _, size: _, } => path == &fpath, - Entry::Link { path, target: _ } => path == &fpath, - Entry::Directory { path, mode: _ } => path == &fpath, + Entry::Link { path, target: _ } | Entry::Directory { path, mode: _ } => { + path == &fpath + } }) .unwrap(); path = path.join(&fpath); if let Some(parent) = path.parent() { if !parent.exists() { - fs::create_dir_all(&parent)?; + fs::create_dir_all(parent)?; } } if path.exists() { @@ -176,24 +175,20 @@ impl Installer { } let msg = extract_entry(entry, node, path)?; sender.send(msg)?; - Ok::<(), InstallError>(()) + Ok::<(), Error>(()) })?; Ok(package) } } -fn extract_entry( - entry: &Entry, - node: &Node, - path: PathBuf, -) -> Result { +fn extract_entry(entry: &Entry, node: &Node, path: PathBuf) -> Result { match entry { Entry::Directory { path: _, mode } => { DirBuilder::new().mode(*mode).create(&path)?; Ok(InstallMessage::DirectoryCreated(path)) } Entry::Link { path: _, target } => { - os::unix::fs::symlink(&path, &target)?; + os::unix::fs::symlink(&path, target)?; Ok(InstallMessage::LinkCreated(Link { path, target: target.clone(), @@ -215,7 +210,7 @@ fn extract_entry( write!(sum, "{c:02x}")?; } if sha256sum != &sum { - return Err(InstallError::ChecksumMismatch); + return Err(Error::ChecksumMismatch); } let fd = File::options().mode(*mode).write(true).open(&path)?; let mut writer = BufWriter::new(fd); @@ -225,7 +220,7 @@ fn extract_entry( } } -fn pop_appstream(archive: &mut Archive, db_pkgdir: &Path) -> Result<(), InstallError> { +fn pop_appstream(archive: &mut Archive, db_pkgdir: &Path) -> Result<(), Error> { let appstream = archive.pop("appdata.xml"); if let Some(node) = appstream { let mut appdatafile = db_pkgdir.to_path_buf(); @@ -242,7 +237,7 @@ fn pop_pinstall( hooks: &mut Vec, pkgname: &str, root: &Path, -) -> Result<(), InstallError> { +) -> Result<(), Error> { let pinstall = archive.pop("postinstall.sh"); if let Some(node) = pinstall { let mut path: PathBuf = ["/", "tmp", "hpk", pkgname].iter().collect(); @@ -263,7 +258,6 @@ mod error { use crate::{tar, Hooks}; use ron::error::SpannedError; use std::{ - error::Error, fmt, io, string::FromUtf8Error, sync::{ @@ -273,7 +267,7 @@ mod error { }; #[derive(Debug)] - pub enum InstallError { + pub enum Error { Fmt(fmt::Error), IO(io::Error), RonError(SpannedError), @@ -285,14 +279,14 @@ mod error { Utf8, } - impl fmt::Display for InstallError { + impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{self:?}") } } - impl Error for InstallError { - fn source(&self) -> Option<&(dyn Error + 'static)> { + impl std::error::Error for Error { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { match self { Self::Fmt(e) => Some(e), Self::IO(e) => Some(e), @@ -304,49 +298,49 @@ mod error { } } - impl From for InstallError { + impl From for Error { fn from(value: fmt::Error) -> Self { Self::Fmt(value) } } - impl From for InstallError { + impl From for Error { fn from(value: io::Error) -> Self { Self::IO(value) } } - impl From>> for InstallError { + impl From>> for Error { fn from(_value: PoisonError>) -> Self { Self::MutexError } } - impl From>> for InstallError { + impl From>> for Error { fn from(_value: PoisonError>) -> Self { Self::MutexError } } - impl From> for InstallError { + impl From> for Error { fn from(value: SendError) -> Self { Self::SendError(value) } } - impl From for InstallError { + impl From for Error { fn from(value: SpannedError) -> Self { Self::RonError(value) } } - impl From for InstallError { + impl From for Error { fn from(_value: FromUtf8Error) -> Self { Self::Utf8 } } - impl From for InstallError { + impl From for Error { fn from(value: crate::tar::Error) -> Self { Self::Tar(value) } diff --git a/src/item/mod.rs b/src/item/mod.rs index bed1df0..a56eae6 100644 --- a/src/item/mod.rs +++ b/src/item/mod.rs @@ -6,7 +6,6 @@ use { deku::DekuError, sha2::{Digest, Sha256}, std::{ - error::Error, ffi::OsStr, fmt::{self, Write}, fs, @@ -24,7 +23,7 @@ pub struct Item { } impl Item { - pub fn try_create(path: &Path) -> Result { + pub fn try_create(path: &Path) -> Result { let path = fix_path(path); let meta = fs::metadata(&path)?; let filename = format!("{}", path.display()); @@ -72,21 +71,21 @@ impl Item { } #[derive(Debug)] -pub enum ItemError { +pub enum Error { Io(io::Error), Fmt(fmt::Error), Tar(TarError), Deku(DekuError), } -impl fmt::Display for ItemError { +impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{self:?}") } } -impl Error for ItemError { - fn source(&self) -> Option<&(dyn Error + 'static)> { +impl std::error::Error for Error { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { match self { Self::Io(e) => Some(e), Self::Fmt(e) => Some(e), @@ -96,25 +95,25 @@ impl Error for ItemError { } } -impl From for ItemError { +impl From for Error { fn from(value: io::Error) -> Self { Self::Io(value) } } -impl From for ItemError { +impl From for Error { fn from(value: fmt::Error) -> Self { Self::Fmt(value) } } -impl From for ItemError { +impl From for Error { fn from(value: TarError) -> Self { Self::Tar(value) } } -impl From for ItemError { +impl From for Error { fn from(value: DekuError) -> Self { Self::Deku(value) } diff --git a/src/lib.rs b/src/lib.rs index 64ff4aa..812961c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,8 +17,8 @@ pub use { creator::{CreationError, Creator, Message}, db::Database, hooks::{Hooks, Pinstall}, - installer::{InstallError, InstallMessage, Installer}, - item::{Item, ItemError}, + installer::{Error as InstallError, InstallMessage, Installer}, + item::{Error as ItemError, Item}, package::{Arch, Dependency, Group, Package, Specs, User}, plist::{Entry, Plist}, repository::Repository, diff --git a/src/repository/mod.rs b/src/repository/mod.rs index f4dbaa5..bf495e6 100644 --- a/src/repository/mod.rs +++ b/src/repository/mod.rs @@ -34,9 +34,9 @@ pub enum Message { } impl Repository { - pub fn build(name: &str, url: &Url, sender: Sender) -> Result> { + pub fn build(name: &str, url: &Url, sender: &Sender) -> Result> { let url = url.join("packages.ron.zstd")?; - let resp = ureq::get(&url.to_string()) + let resp = ureq::get(url.as_ref()) .timeout(Duration::from_secs(10)) .call()?; if let Some(val) = resp.header("Content-Length") { @@ -57,7 +57,7 @@ impl Repository { let packages = ron::de::from_bytes(&buf)?; Ok(Self { name: name.to_string(), - base_url: url.clone(), + base_url: url, packages, }) } diff --git a/src/tar/header.rs b/src/tar/header.rs index aa797ad..0f5dc84 100644 --- a/src/tar/header.rs +++ b/src/tar/header.rs @@ -1,3 +1,4 @@ +#![allow(clippy::similar_names)] use crate::tar::Error; use deku::prelude::*; use std::{ @@ -131,9 +132,8 @@ impl Header { for c in self.fname { if c == 0 { break; - } else { - write!(s, "{}", char::from(c))?; } + write!(s, "{}", char::from(c))?; } Ok(s) } @@ -153,9 +153,8 @@ impl Header { for c in self.mode { if c == 0 { break; - } else { - write!(s, "{}", char::from(c))?; } + write!(s, "{}", char::from(c))?; } let mode = u32::from_str_radix(&s, 8)?; Ok(mode) @@ -166,9 +165,8 @@ impl Header { for c in self.mode { if c == 0 { break; - } else { - write!(s, "{}", char::from(c))?; } + write!(s, "{}", char::from(c))?; } let uid = u32::from_str_radix(&s, 8)?; Ok(uid) @@ -179,9 +177,8 @@ impl Header { for c in self.mode { if c == 0 { break; - } else { - write!(s, "{}", char::from(c))?; } + write!(s, "{}", char::from(c))?; } let gid = u32::from_str_radix(&s, 8)?; Ok(gid) @@ -192,9 +189,8 @@ impl Header { for c in self.username { if c == 0 { break; - } else { - write!(s, "{}", char::from(c))?; } + write!(s, "{}", char::from(c))?; } Ok(s) } @@ -204,9 +200,8 @@ impl Header { for c in self.groupname { if c == 0 { break; - } else { - write!(s, "{}", char::from(c))?; } + write!(s, "{}", char::from(c))?; } Ok(s) } @@ -239,9 +234,8 @@ impl Header { for c in self.file_prefix { if c == 0 { break; - } else { - write!(s, "{}", char::from(c)).ok()?; } + write!(s, "{}", char::from(c)).ok()?; } if s.is_empty() { None diff --git a/src/tar/node.rs b/src/tar/node.rs index ba57019..dd3aafe 100644 --- a/src/tar/node.rs +++ b/src/tar/node.rs @@ -1,3 +1,4 @@ +#![allow(clippy::similar_names)] use crate::tar::{header::Owner, Error, FileType, Header}; use deku::prelude::*; use std::{