Skip to content

Commit 8e63b8a

Browse files
committed
Clean up code organization and formatting
1 parent 02395ef commit 8e63b8a

File tree

4 files changed

+34
-48
lines changed

4 files changed

+34
-48
lines changed

src/commands/build.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@ use std::{
55
path::{Path, PathBuf},
66
};
77

8-
use anyhow::{bail, Result};
8+
use anyhow::{anyhow, bail, Result};
99
use bzip2::read::BzDecoder;
1010
use camino::Utf8Path;
1111
use flate2::read::GzDecoder;
1212
use indicatif::ProgressBar;
1313
use reqwest::{Client, Url};
14+
use sha2::{Digest, Sha256 as Sha256Hasher};
1415
use tar::Archive;
1516
use tracing::info;
1617
use xz2::read::XzDecoder;
1718

18-
use crate::{
19-
check_hash, create_tarball,
20-
package::{Package, Source, StepVariant},
21-
};
19+
use crate::package::{Package, Source, StepVariant};
2220

2321
pub async fn build() -> Result<()> {
2422
let package_path = current_dir()?.join("package.toml");
@@ -165,3 +163,31 @@ fn unpack_archive<R: Read>(decoder: R) -> Result<()> {
165163

166164
Ok(())
167165
}
166+
167+
pub fn check_hash<P: AsRef<Path>>(path: P, hash: &str) -> Result<bool> {
168+
let file = fs::read(path)?;
169+
let (hash_type, hash) = hash
170+
.split_once(':')
171+
.ok_or(anyhow!("Invalid checksum format"))?;
172+
173+
let computed_hash = match hash_type {
174+
"blake3" => blake3::hash(&file).to_hex().to_string(),
175+
"sha256" => base16ct::lower::encode_string(Sha256Hasher::digest(&file).as_slice()),
176+
_ => bail!("Unsupported hash"),
177+
};
178+
179+
Ok(hash == computed_hash)
180+
}
181+
182+
pub fn create_tarball<P: AsRef<Path>>(package_path: P, package: &Package) -> Result<()> {
183+
let tarball_name = format!("{}-{}.peach", package.info.name, package.info.version);
184+
let tarball_path = current_dir()?.join(&tarball_name);
185+
let tar_gz = File::create(&tarball_path)?;
186+
let enc = zstd::Encoder::new(tar_gz, 22)?;
187+
let mut tar = tar::Builder::new(enc);
188+
189+
tar.append_dir_all(".", package_path)?;
190+
191+
info!("Created package: {}", tarball_name);
192+
Ok(())
193+
}

src/commands/info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use tracing::info;
21
use anyhow::Result;
2+
use tracing::info;
33

44
pub fn info(name: &str) -> Result<()> {
55
info!("Retrieving info for package: {}", name);

src/commands/uninstall.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use tracing::info;
21
use anyhow::Result;
2+
use tracing::info;
33

44
pub fn uninstall(name: &str) -> Result<()> {
55
info!("Removing package: {}", name);
66
Ok(())
7-
}
7+
}

src/lib.rs

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,2 @@
1-
use std::{
2-
env::current_dir,
3-
fs::{self, File},
4-
path::Path,
5-
};
6-
7-
use anyhow::{anyhow, bail, Result};
8-
use sha2::{Digest, Sha256 as Sha256Hasher};
9-
use tracing::info;
10-
111
pub mod commands;
122
pub mod package;
13-
14-
use package::Package;
15-
16-
pub fn check_hash<P: AsRef<Path>>(path: P, hash: &str) -> Result<bool> {
17-
let file = fs::read(path)?;
18-
let (hash_type, hash) = hash
19-
.split_once(':')
20-
.ok_or(anyhow!("Invalid checksum format"))?;
21-
22-
let computed_hash = match hash_type {
23-
"blake3" => blake3::hash(&file).to_hex().to_string(),
24-
"sha256" => base16ct::lower::encode_string(Sha256Hasher::digest(&file).as_slice()),
25-
_ => bail!("Unsupported hash"),
26-
};
27-
28-
Ok(hash == computed_hash)
29-
}
30-
31-
pub fn create_tarball<P: AsRef<Path>>(package_path: P, package: &Package) -> Result<()> {
32-
let tarball_name = format!("{}-{}.peach", package.info.name, package.info.version);
33-
let tarball_path = current_dir()?.join(&tarball_name);
34-
let tar_gz = File::create(&tarball_path)?;
35-
let enc = zstd::Encoder::new(tar_gz, 22)?;
36-
let mut tar = tar::Builder::new(enc);
37-
38-
tar.append_dir_all(".", package_path)?;
39-
40-
info!("Created package: {}", tarball_name);
41-
Ok(())
42-
}

0 commit comments

Comments
 (0)