Skip to content

Commit

Permalink
Fix all complaints from clippy::pedantic
Browse files Browse the repository at this point in the history
  • Loading branch information
autarch committed May 28, 2024
1 parent 21b32fc commit cf003f9
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 160 deletions.
6 changes: 3 additions & 3 deletions src/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ impl GitHubAssetFetcher {
.get(url)
.header(ACCEPT, HeaderValue::from_str("application/json")?)
.build()?;
let res = client.execute(req).await?;
let resp = client.execute(req).await?;

if let Err(e) = res.error_for_status_ref() {
if let Err(e) = resp.error_for_status_ref() {
return Err(anyhow::Error::new(e));
}

Ok(res.json::<Release>().await?)
Ok(resp.json::<Release>().await?)
}
}
64 changes: 32 additions & 32 deletions src/installer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ impl Installer {
Installer { install_path, exe }
}

pub(crate) fn install(&self, download: Download) -> Result<()> {
self.extract_binary(download.archive_path)?;
pub(crate) fn install(&self, download: &Download) -> Result<()> {
self.extract_binary(&download.archive_path)?;
self.make_binary_executable()?;
info!("Installed binary into {}", self.install_path.display());

Ok(())
}

fn extract_binary(&self, downloaded_file: PathBuf) -> Result<()> {
fn extract_binary(&self, downloaded_file: &Path) -> Result<()> {
let filename = downloaded_file
.file_name()
.unwrap_or_else(|| {
Expand All @@ -47,33 +47,35 @@ impl Installer {
})
.to_string_lossy();
match Extension::from_path(filename)? {
Some(Extension::TarBz)
| Some(Extension::TarBz2)
| Some(Extension::TarGz)
| Some(Extension::TarXz)
| Some(Extension::Tbz)
| Some(Extension::Tgz)
| Some(Extension::Txz) => self.extract_tarball(downloaded_file),
Some(Extension::Bz) | Some(Extension::Bz2) => self.unbzip(downloaded_file),
Some(
Extension::TarBz
| Extension::TarBz2
| Extension::TarGz
| Extension::TarXz
| Extension::Tbz
| Extension::Tgz
| Extension::Txz,
) => self.extract_tarball(downloaded_file),
Some(Extension::Bz | Extension::Bz2) => self.unbzip(downloaded_file),
Some(Extension::Gz) => self.ungzip(downloaded_file),
Some(Extension::Xz) => self.unxz(downloaded_file),
Some(Extension::Zip) => self.extract_zip(downloaded_file),
Some(Extension::Exe) | None => self.copy_executable(downloaded_file),
}
}

fn extract_zip(&self, downloaded_file: PathBuf) -> Result<()> {
fn extract_zip(&self, downloaded_file: &Path) -> Result<()> {
debug!("extracting binary from zip file");

let mut zip = ZipArchive::new(open_file(&downloaded_file)?)?;
let mut zip = ZipArchive::new(open_file(downloaded_file)?)?;
for i in 0..zip.len() {
let mut zf = zip.by_index(i)?;
let path = PathBuf::from(zf.name());
if path.ends_with(&self.exe) {
let mut buffer: Vec<u8> = Vec::with_capacity(zf.size() as usize);
let mut buffer: Vec<u8> = Vec::with_capacity(usize::try_from(zf.size())?);
zf.read_to_end(&mut buffer)?;
let mut file = File::create(&self.install_path)?;
return file.write_all(&buffer).map_err(|e| e.into());
return file.write_all(&buffer).map_err(Into::into);
}
}

Expand All @@ -84,7 +86,7 @@ impl Installer {
))
}

fn extract_tarball(&self, downloaded_file: PathBuf) -> Result<()> {
fn extract_tarball(&self, downloaded_file: &Path) -> Result<()> {
debug!(
"extracting binary from tarball at {}",
downloaded_file.to_string_lossy(),
Expand Down Expand Up @@ -121,21 +123,21 @@ impl Installer {
))
}

fn unbzip(&self, downloaded_file: PathBuf) -> Result<()> {
fn unbzip(&self, downloaded_file: &Path) -> Result<()> {
debug!("uncompressing binary from bzip file");
let reader = BzDecoder::new(open_file(&downloaded_file)?);
let reader = BzDecoder::new(open_file(downloaded_file)?);
self.write_to_install_path(reader)
}

fn ungzip(&self, downloaded_file: PathBuf) -> Result<()> {
fn ungzip(&self, downloaded_file: &Path) -> Result<()> {
debug!("uncompressing binary from gzip file");
let reader = GzDecoder::new(open_file(&downloaded_file)?);
let reader = GzDecoder::new(open_file(downloaded_file)?);
self.write_to_install_path(reader)
}

fn unxz(&self, downloaded_file: PathBuf) -> Result<()> {
fn unxz(&self, downloaded_file: &Path) -> Result<()> {
debug!("uncompressing binary from xz file");
let reader = XzDecoder::new(open_file(&downloaded_file)?);
let reader = XzDecoder::new(open_file(downloaded_file)?);
self.write_to_install_path(reader)
}

Expand All @@ -146,7 +148,7 @@ impl Installer {
Ok(())
}

fn copy_executable(&self, exe_file: PathBuf) -> Result<()> {
fn copy_executable(&self, exe_file: &Path) -> Result<()> {
debug!("copying binary to final location");
std::fs::copy(exe_file, &self.install_path)?;

Expand All @@ -159,23 +161,21 @@ impl Installer {

#[cfg(target_family = "unix")]
match set_permissions(&self.install_path, Permissions::from_mode(0o755)) {
Ok(_) => Ok(()),
Ok(()) => Ok(()),
Err(e) => Err(anyhow::Error::new(e)),
}
}
}

fn tar_reader_for(downloaded_file: PathBuf) -> Result<Archive<Box<dyn Read>>> {
let file = open_file(&downloaded_file)?;
fn tar_reader_for(downloaded_file: &Path) -> Result<Archive<Box<dyn Read>>> {
let file = open_file(downloaded_file)?;

let ext = downloaded_file.extension();
match ext {
Some(ext) => match ext.to_str() {
Some("bz") | Some("tbz") | Some("bz2") | Some("tbz2") => {
Ok(Archive::new(Box::new(BzDecoder::new(file))))
}
Some("gz") | Some("tgz") => Ok(Archive::new(Box::new(GzDecoder::new(file)))),
Some("xz") | Some("txz") => Ok(Archive::new(Box::new(XzDecoder::new(file)))),
Some("bz" | "tbz" | "bz2" | "tbz2") => Ok(Archive::new(Box::new(BzDecoder::new(file)))),
Some("gz" | "tgz") => Ok(Archive::new(Box::new(GzDecoder::new(file)))),
Some("xz" | "txz") => Ok(Archive::new(Box::new(XzDecoder::new(file)))),
Some(e) => Err(anyhow!(
"don't know how to uncompress a tarball with extension = {}",
e,
Expand Down Expand Up @@ -219,7 +219,7 @@ mod tests {
let mut install_path = td.path().to_path_buf();
install_path.push("project");
let installer = Installer::new(install_path.clone(), exe.to_string());
installer.install(Download {
installer.install(&Download {
// It doesn't matter what we use here. We're not actually going to
// put anything in this temp dir.
_temp_dir: tempdir()?,
Expand Down
20 changes: 10 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ async fn main() {
0
}
Err(e) => {
print_err(e);
print_err(&e);
1
}
},
Err(e) => {
print_err(e);
print_err(&e);
127
}
};
Expand Down Expand Up @@ -138,7 +138,7 @@ fn cmd() -> Command {
.max_term_width(MAX_TERM_WIDTH)
}

pub fn init_logger(matches: &ArgMatches) -> Result<(), log::SetLoggerError> {
pub(crate) fn init_logger(matches: &ArgMatches) -> Result<(), log::SetLoggerError> {
let level = if matches.get_flag("debug") {
log::LevelFilter::Debug
} else if matches.get_flag("verbose") {
Expand Down Expand Up @@ -178,12 +178,12 @@ fn make_ubi<'a>(mut matches: ArgMatches) -> Result<(Ubi<'a>, Option<impl FnOnce(

Ok((
Ubi::new(
matches.get_one::<String>("project").map(|s| s.as_str()),
matches.get_one::<String>("tag").map(|s| s.as_str()),
matches.get_one::<String>("url").map(|s| s.as_str()),
matches.get_one::<String>("in").map(|s| s.as_str()),
matches.get_one::<String>("project").map(String::as_str),
matches.get_one::<String>("tag").map(String::as_str),
matches.get_one::<String>("url").map(String::as_str),
matches.get_one::<String>("in").map(String::as_str),
matches.get_one::<String>("matching").cloned(),
matches.get_one::<String>("exe").map(|s| s.as_str()),
matches.get_one::<String>("exe").map(String::as_str),
platform,
None,
)?,
Expand Down Expand Up @@ -240,7 +240,7 @@ fn self_upgrade_args() -> Result<(Vec<OsString>, Option<PathBuf>)> {
munged.append(
&mut vec!["--project", "houseabsolute/ubi", "--in"]
.into_iter()
.map(|a| a.into())
.map(std::convert::Into::into)
.collect(),
);
let current = env::current_exe()?;
Expand All @@ -267,7 +267,7 @@ fn self_upgrade_args() -> Result<(Vec<OsString>, Option<PathBuf>)> {
Ok((munged, to_delete))
}

fn print_err(e: Error) {
fn print_err(e: &Error) {
error!("{e}");
if let Some(ue) = e.downcast_ref::<UbiError>() {
match ue {
Expand Down
Loading

0 comments on commit cf003f9

Please sign in to comment.