Skip to content

Commit

Permalink
nitpick. slight refactor because Ok(()) looks yuck in some contexts
Browse files Browse the repository at this point in the history
Signed-off-by: Soc Virnyl Estela <[email protected]>
  • Loading branch information
uncomfyhalomacro committed Dec 5, 2023
1 parent eb3819e commit 91c5dc8
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 104 deletions.
12 changes: 5 additions & 7 deletions cargo/src/bin/cargo_vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(hasterminfodb) => hasterminfodb.get::<cap::MaxColors>().is_some(),
Err(_) => false,
};

let to_color = matches!(std::io::stdout().is_terminal(), true if {
let coloroption = &args.color;
match coloroption {
Expand Down Expand Up @@ -78,11 +79,8 @@ This rewrite introduces some small changes to how vendoring functions for your p
"#
);

match args.src.run_vendor(&args) {
Ok(_) => Ok(()),
Err(err) => {
error!("{}", err);
Err(err.into())
}
}
Ok(args.src.run_vendor(&args).map_err(|err| {
error!("{}", err);
err
})?)
}
10 changes: 3 additions & 7 deletions cargo/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,8 @@ impl Vendor for Src {
}
};
drop(newworkdir);
match tmpdir.close() {
Ok(_) => Ok(()),
Err(err) => Err(OBSCargoError::new(
OBSCargoErrorKind::VendorError,
err.to_string(),
)),
}
Ok(tmpdir
.close()
.map_err(|err| OBSCargoError::new(OBSCargoErrorKind::VendorError, err.to_string()))?)
}
}
3 changes: 1 addition & 2 deletions cargo/src/utils/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ pub fn tar_builder<T: Write>(
}
}

builder.finish()?;
Ok(())
Ok(builder.finish()?)
}

pub fn targz(
Expand Down
45 changes: 24 additions & 21 deletions cargo/src/utils/decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ pub fn targz(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> Result<(),
src.seek(io::SeekFrom::Start(0))?;
let enc = GzDecoder::new(src);
let mut ar = tar::Archive::new(enc);
ar.unpack(outdir.as_ref())?;
debug!(
"Successfully decompressed Gz archive from {} to {}",
srcpath.as_ref().to_string_lossy(),
outdir.as_ref().to_string_lossy(),
);
Ok(())
Ok({
ar.unpack(outdir.as_ref())?;
debug!(
"Successfully decompressed Gz archive from {} to {}",
srcpath.as_ref().to_string_lossy(),
outdir.as_ref().to_string_lossy(),
);
})
}

pub fn tarzst(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> Result<(), io::Error> {
Expand All @@ -36,13 +37,14 @@ pub fn tarzst(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> Result<(),
src.seek(io::SeekFrom::Start(0))?;
let enc = Decoder::new(src)?;
let mut ar = tar::Archive::new(enc);
ar.unpack(outdir.as_ref())?;
debug!(
"Successfully decompressed Zst archive from {} to {}",
srcpath.as_ref().to_string_lossy(),
outdir.as_ref().to_string_lossy(),
);
Ok(())
Ok({
ar.unpack(outdir.as_ref())?;
debug!(
"Successfully decompressed Zst archive from {} to {}",
srcpath.as_ref().to_string_lossy(),
outdir.as_ref().to_string_lossy(),
);
})
}

pub fn tarxz(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> Result<(), io::Error> {
Expand All @@ -51,11 +53,12 @@ pub fn tarxz(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> Result<(),
src.seek(io::SeekFrom::Start(0))?;
let enc = XzDecoder::new(src);
let mut ar = tar::Archive::new(enc);
ar.unpack(outdir.as_ref())?;
debug!(
"Successfully decompressed Xz archive from {} to {}",
srcpath.as_ref().to_string_lossy(),
outdir.as_ref().to_string_lossy(),
);
Ok(())
Ok({
ar.unpack(outdir.as_ref())?;
debug!(
"Successfully decompressed Xz archive from {} to {}",
srcpath.as_ref().to_string_lossy(),
outdir.as_ref().to_string_lossy(),
);
})
}
5 changes: 2 additions & 3 deletions cargo/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn copy_dir_all(src: impl AsRef<Path>, dst: &Path) -> Result<(), io::Error>
debug!("Copying sources");
debug!(?dst);
fs::create_dir_all(dst)?;
for entry in fs::read_dir(src)? {
Ok(for entry in fs::read_dir(src)? {
let entry = entry?;
let ty = entry.file_type()?;
trace!(?entry);
Expand All @@ -59,8 +59,7 @@ pub fn copy_dir_all(src: impl AsRef<Path>, dst: &Path) -> Result<(), io::Error>
trace!(?ty, "Is file?");
fs::copy(&entry.path(), &mut dst.join(&entry.file_name()))?;
};
}
Ok(())
})
}

pub fn process_src(args: &Opts, prjdir: &Path) -> Result<(), OBSCargoError> {
Expand Down
128 changes: 64 additions & 64 deletions cargo/src/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ pub fn update(
manifest_path.as_ref().into(),
];

cargo_command("update", &update_options, &prjdir).map_err(|e| {
error!(err = %e);
OBSCargoError::new(
OBSCargoErrorKind::VendorError,
"Unable to execute cargo".to_string(),
)
})?;
info!("⏫ Successfully ran cargo update");
Ok(())
Ok({
cargo_command("update", &update_options, &prjdir).map_err(|e| {
error!(err = %e);
OBSCargoError::new(
OBSCargoErrorKind::VendorError,
"Unable to execute cargo".to_string(),
)
})?;
info!("⏫ Successfully ran cargo update");
})
}

pub fn vendor(
Expand Down Expand Up @@ -91,16 +92,15 @@ pub fn vendor(
)
})?;
// Write the stdout which is used by the package later.
file_cargo_config
Ok(file_cargo_config
.write_all(cargo_vendor_output.as_bytes())
.map_err(|err| {
error!(?err, "Failed to write to file for cargo config");
OBSCargoError::new(
OBSCargoErrorKind::VendorError,
"failed to write to file for cargo config".to_string(),
)
})?;
Ok(())
})?)
}

pub fn compress(
Expand Down Expand Up @@ -131,62 +131,62 @@ pub fn compress(
};

let mut vendor_out = outpath.as_ref().join(tar_name);
match compression {
Compression::Gz => {
vendor_out.set_extension("tar.gz");
if vendor_out.exists() {
warn!(
replacing = ?vendor_out,
"🔦 Compressed tarball for vendor exists AND will be replaced."
);
Ok({
match compression {
Compression::Gz => {
vendor_out.set_extension("tar.gz");
if vendor_out.exists() {
warn!(
replacing = ?vendor_out,
"🔦 Compressed tarball for vendor exists AND will be replaced."
);
}
compress::targz(&vendor_out, &prjdir, paths_to_archive).map_err(|err| {
error!(?err, "gz compression failed");
OBSCargoError::new(
OBSCargoErrorKind::VendorCompressionFailed,
"gz compression failed".to_string(),
)
})?;
debug!("Compressed to {}", vendor_out.to_string_lossy());
}
compress::targz(&vendor_out, &prjdir, paths_to_archive).map_err(|err| {
error!(?err, "gz compression failed");
OBSCargoError::new(
OBSCargoErrorKind::VendorCompressionFailed,
"gz compression failed".to_string(),
)
})?;
debug!("Compressed to {}", vendor_out.to_string_lossy());
}
Compression::Xz => {
vendor_out.set_extension("tar.xz");
if vendor_out.exists() {
warn!(
replacing = ?vendor_out,
"🔦 Compressed tarball for vendor exists AND will be replaced."
);
Compression::Xz => {
vendor_out.set_extension("tar.xz");
if vendor_out.exists() {
warn!(
replacing = ?vendor_out,
"🔦 Compressed tarball for vendor exists AND will be replaced."
);
}
compress::tarxz(&vendor_out, &prjdir, paths_to_archive).map_err(|err| {
error!(?err, "xz compression failed");
OBSCargoError::new(
OBSCargoErrorKind::VendorCompressionFailed,
"xz compression failed".to_string(),
)
})?;
debug!("Compressed to {}", vendor_out.to_string_lossy());
}
compress::tarxz(&vendor_out, &prjdir, paths_to_archive).map_err(|err| {
error!(?err, "xz compression failed");
OBSCargoError::new(
OBSCargoErrorKind::VendorCompressionFailed,
"xz compression failed".to_string(),
)
})?;
debug!("Compressed to {}", vendor_out.to_string_lossy());
}
Compression::Zst => {
vendor_out.set_extension("tar.zst");
if vendor_out.exists() {
warn!(
replacing = ?vendor_out,
"🔦 Compressed tarball for vendor exists AND will be replaced."
);
Compression::Zst => {
vendor_out.set_extension("tar.zst");
if vendor_out.exists() {
warn!(
replacing = ?vendor_out,
"🔦 Compressed tarball for vendor exists AND will be replaced."
);
}
compress::tarzst(&vendor_out, &prjdir, paths_to_archive).map_err(|err| {
error!(?err, "zst compression failed");
OBSCargoError::new(
OBSCargoErrorKind::VendorCompressionFailed,
"zst compression failed".to_string(),
)
})?;
debug!("Compressed to {}", vendor_out.to_string_lossy());
}
compress::tarzst(&vendor_out, &prjdir, paths_to_archive).map_err(|err| {
error!(?err, "zst compression failed");
OBSCargoError::new(
OBSCargoErrorKind::VendorCompressionFailed,
"zst compression failed".to_string(),
)
})?;
debug!("Compressed to {}", vendor_out.to_string_lossy());
}
};
debug!("Finished creating {} compressed tarball", compression);

Ok(())
debug!("Finished creating {} compressed tarball", compression);
})
}

pub fn is_workspace(src: &Path) -> Result<bool, OBSCargoError> {
Expand Down

0 comments on commit 91c5dc8

Please sign in to comment.