Skip to content

Commit

Permalink
Use cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ktwrd committed Aug 14, 2024
1 parent 5b7a2f1 commit 79a1701
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 42 deletions.
17 changes: 13 additions & 4 deletions src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,23 @@ impl RunnerContext
{
let tar_tmp_location = helper::get_tmp_file("data.tar".to_string());

if let Err(e) = crate::extract::decompress_zstd(zstd_location.clone(), tar_tmp_location.clone(), true) {
if let Err(e) =
crate::extract::decompress_zstd(zstd_location.clone(), tar_tmp_location.clone(), true)
{
debug!("{:#?}", e);
error!("[RunnerContext::extract_package] Failed to decompress file {} ({:})", zstd_location, e);
error!(
"[RunnerContext::extract_package] Failed to decompress file {} ({:})",
zstd_location, e
);
return Err(e);
}
if let Err(e) = crate::extract::unpack_tarball(tar_tmp_location.clone(), out_dir, true) {
if let Err(e) = crate::extract::unpack_tarball(tar_tmp_location.clone(), out_dir, true)
{
debug!("{:#?}", e);
error!("[RunnerContext::extract_package] Failed to unpack tarball {} ({:})", tar_tmp_location, e);
error!(
"[RunnerContext::extract_package] Failed to unpack tarball {} ({:})",
tar_tmp_location, e
);
return Err(e);
}
if helper::file_exists(tar_tmp_location.clone())
Expand Down
85 changes: 58 additions & 27 deletions src/extract.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
use std::{backtrace::Backtrace,
fs::File,
io::Read};

use indicatif::{ProgressBar,
ProgressStyle};
use log::info;
use std::{backtrace::Backtrace, fs::File, io::Read};
use indicatif::{ProgressBar, ProgressStyle};
use zstd::stream::read::Decoder as ZstdDecoder;

use crate::BeansError;

pub fn unpack_tarball(tarball_location: String, output_directory: String, show_progress: bool) -> Result<(), BeansError>
pub fn unpack_tarball(
tarball_location: String,
output_directory: String,
show_progress: bool
) -> Result<(), BeansError>
{
let tarball = match File::open(&tarball_location) {
let tarball = match File::open(&tarball_location)
{
Ok(x) => x,
Err(e) => {
Err(e) =>
{
return Err(BeansError::TarExtractFailure {
src_file: tarball_location,
target_dir: output_directory,
Expand All @@ -19,7 +29,8 @@ pub fn unpack_tarball(tarball_location: String, output_directory: String, show_p
}
};
let mut archive = tar::Archive::new(&tarball);
if show_progress {
if show_progress
{
let archive_entries = match archive.entries()
{
Ok(v) => v,
Expand All @@ -35,33 +46,41 @@ pub fn unpack_tarball(tarball_location: String, output_directory: String, show_p
};
let archive_entry_count = (&archive_entries.count()).clone() as u64;
info!("Extracting {} files", archive_entry_count);

let pb = ProgressBar::new(archive_entry_count);
pb.set_style(ProgressStyle::with_template("{msg}\n{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {pos}/{len} ({eta})")
.unwrap()
.with_key("eta", |state: &indicatif::ProgressState, w: &mut dyn std::fmt::Write| write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap())
.progress_chars("#>-"));
pb.set_message("Extracting files");

archive = tar::Archive::new(&tarball);
match archive.entries() {
match archive.entries()
{
Ok(etrs) =>
{
for entry in etrs {
match entry {
Ok(mut x) => {
for entry in etrs
{
match entry
{
Ok(mut x) =>
{
let ln = x.link_name();
pb.set_message("Extracting files");
let mut filename = String::new();
if let Ok(n) = ln {
if let Some(p) = n {
if let Some(s) = p.to_str() {
if let Ok(n) = ln
{
if let Some(p) = n
{
if let Some(s) = p.to_str()
{
pb.set_message(format!("{:}", s));
filename = String::from(s);
}
}
}
if let Err(e) = x.unpack_in(&output_directory) {
if let Err(e) = x.unpack_in(&output_directory)
{
return Err(BeansError::TarUnpackItemFailure {
src_file: tarball_location,
target_dir: output_directory,
Expand All @@ -71,8 +90,9 @@ pub fn unpack_tarball(tarball_location: String, output_directory: String, show_p
});
}
pb.inc(1);
},
Err(e) => {
}
Err(e) =>
{
return Err(BeansError::TarExtractFailure {
src_file: tarball_location,
target_dir: output_directory,
Expand All @@ -82,7 +102,7 @@ pub fn unpack_tarball(tarball_location: String, output_directory: String, show_p
}
};
}
},
}
Err(e) =>
{
return Err(BeansError::TarExtractFailure {
Expand All @@ -94,31 +114,42 @@ pub fn unpack_tarball(tarball_location: String, output_directory: String, show_p
}
};
pb.finish();
} else {
}
else
{
archive.unpack(output_directory);
}
return Ok(());
}
pub fn decompress_zstd(zstd_location: String, output_file: String, show_progress: bool) -> Result<(), BeansError>
pub fn decompress_zstd(
zstd_location: String,
output_file: String,
show_progress: bool
) -> Result<(), BeansError>
{
let zstd_file = File::open(&zstd_location)?;
let zstd_file_length = &zstd_file.metadata()?.len();
let mut tar_tmp_file = File::create_new(&output_file)?;
if show_progress {
if show_progress
{
let decoder = ZstdDecoder::new(zstd_file)?;
// estimate extracted size as x2 since idk how to get the decompressed size with zstd
// estimate extracted size as x2 since idk how to get the decompressed size with
// zstd
let pb_decompress = ProgressBar::new((zstd_file_length.clone() * 2) as u64);
pb_decompress
.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})")
.unwrap()
.with_key("eta", |state: &indicatif::ProgressState, w: &mut dyn std::fmt::Write| write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap())
.progress_chars("#>-"));

std::io::copy(&mut pb_decompress.wrap_read(decoder), &mut tar_tmp_file).expect("Failed to decompress file");

std::io::copy(&mut pb_decompress.wrap_read(decoder), &mut tar_tmp_file)
.expect("Failed to decompress file");
pb_decompress.finish();
} else {
}
else
{
zstd::stream::copy_decode(zstd_file, &tar_tmp_file)?;
}

Ok(())
}
}
26 changes: 15 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ pub use error::*;

pub mod appvar;
pub mod butler;
pub mod extract;
pub mod flags;
pub mod gui;
pub mod logger;
pub mod extract;

/// NOTE do not change, fetches from the version of beans-rs on build
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down Expand Up @@ -60,33 +60,36 @@ pub fn data_dir() -> String

/// Temporary directory which is specified by `ADASTRAL_TMPDIR`.
///
/// Will return `None` when the environment variable couldn't be found, or it's an empty string.
/// Will return `None` when the environment variable couldn't be found, or it's
/// an empty string.
pub fn env_custom_tmpdir() -> Option<String>
{
let s = helper::try_get_env_var(String::from("ADASTRAL_TMPDIR"));
match s {
Some(x) => match x.trim().is_empty() {
match s
{
Some(x) => match x.trim().is_empty()
{
true => None,
false => Some(x)
},
None => s
}
}
/// Return `true` when the environment variable `BEANS_DEBUG` or `ADASTRAL_DEBUG` exists and
/// equals `1` or `true`.
/// Return `true` when the environment variable `BEANS_DEBUG` or
/// `ADASTRAL_DEBUG` exists and equals `1` or `true`.
pub fn env_debug() -> bool
{
check_env_bool("BEANS_DEBUG") || check_env_bool("ADASTRAL_DEBUG")
}
/// Return `true` when the environment variable `BEANS_HEADLESS` or `ADASTRAL_HEADLESS` exists and
/// equals `1` or `true`.
/// Return `true` when the environment variable `BEANS_HEADLESS` or
/// `ADASTRAL_HEADLESS` exists and equals `1` or `true`.
pub fn env_headless() -> bool
{
check_env_bool("BEANS_HEADLESS") || check_env_bool("ADASTRAL_HEADLESS")
}

/// Return `true` when the environment variable exists, and it's value equals `1` or `true (when
/// trimmed and made lowercase).
/// Return `true` when the environment variable exists, and it's value equals
/// `1` or `true (when trimmed and made lowercase).
fn check_env_bool<K: AsRef<std::ffi::OsStr>>(key: K) -> bool
{
std::env::var(key).is_ok_and(|x| {
Expand All @@ -112,7 +115,8 @@ pub fn has_gui_support() -> bool
}
}

if env_headless() {
if env_headless()
{
return true;
}

Expand Down

0 comments on commit 79a1701

Please sign in to comment.