Skip to content

Commit

Permalink
refactor!(download): use builder pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Jan 2, 2025
1 parent 3e0d14d commit 9ad3c94
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 24 deletions.
9 changes: 4 additions & 5 deletions rocks-bin/src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use clap::Args;
use eyre::Result;
use rocks_lib::{
config::Config,
operations,
package::PackageReq,
progress::{MultiProgress, Progress},
remote_package_db::RemotePackageDB,
};

#[derive(Args)]
Expand All @@ -13,13 +13,12 @@ pub struct Download {
}

pub async fn download(dl_data: Download, config: Config) -> Result<()> {
let package_db = RemotePackageDB::from_config(&config).await?;
let progress = MultiProgress::new();
let bar = Progress::Progress(progress.new_bar());

let rock =
rocks_lib::operations::download_to_file(&dl_data.package_req, None, &package_db, &bar)
.await?;
let rock = operations::Download::new(&dl_data.package_req, &config, &bar)
.download_src_rock_to_file(None)
.await?;

bar.map(|b| {
b.finish_with_message(format!(
Expand Down
9 changes: 5 additions & 4 deletions rocks-bin/src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::path::PathBuf;
use eyre::Result;
use rocks_lib::{
config::Config,
operations::Download,
progress::{MultiProgress, Progress},
remote_package_db::RemotePackageDB,
};

use crate::unpack::UnpackRemote;
Expand All @@ -13,9 +13,10 @@ pub async fn fetch_remote(data: UnpackRemote, config: Config) -> Result<()> {
let package_req = data.package_req;
let progress = MultiProgress::new();
let bar = Progress::Progress(progress.new_bar());
let package_db = RemotePackageDB::from_config(&config).await?;
let rockspec =
rocks_lib::operations::download_rockspec(&package_req, &package_db, &bar).await?;

let rockspec = Download::new(&package_req, &config, &bar)
.download_rockspec()
.await?;

let destination = data
.path
Expand Down
8 changes: 4 additions & 4 deletions rocks-bin/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use clap::Args;
use eyre::Result;
use rocks_lib::{
config::{Config, LuaVersion},
operations::download_rockspec,
operations::Download,
package::PackageReq,
progress::{MultiProgress, Progress},
remote_package_db::RemotePackageDB,
tree::Tree,
};

Expand All @@ -17,12 +16,13 @@ pub struct Info {
pub async fn info(data: Info, config: Config) -> Result<()> {
// TODO(vhyrro): Add `Tree::from(&Config)`
let tree = Tree::new(config.tree().clone(), LuaVersion::from(&config)?)?;
let package_db = RemotePackageDB::from_config(&config).await?;

let progress = MultiProgress::new();
let bar = Progress::Progress(progress.new_bar());

let rockspec = download_rockspec(&data.package, &package_db, &bar).await?;
let rockspec = Download::new(&data.package, &config, &bar)
.download_rockspec()
.await?;

bar.map(|b| b.finish_and_clear());

Expand Down
6 changes: 3 additions & 3 deletions rocks-bin/src/unpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use clap::Args;
use eyre::Result;
use rocks_lib::{
config::Config,
operations,
package::PackageReq,
progress::{MultiProgress, Progress},
remote_package_db::RemotePackageDB,
};

#[derive(Args)]
Expand Down Expand Up @@ -49,10 +49,10 @@ and type `rocks make` to build.",

pub async fn unpack_remote(data: UnpackRemote, config: Config) -> Result<()> {
let package_req = data.package_req;
let package_db = RemotePackageDB::from_config(&config).await?;
let progress = MultiProgress::new();
let bar = Progress::Progress(progress.new_bar());
let rock = rocks_lib::operations::search_and_download_src_rock(&package_req, &package_db, &bar)
let rock = operations::Download::new(&package_req, &config, &bar)
.search_and_download_src_rock()
.await?;
let cursor = Cursor::new(rock.bytes);

Expand Down
85 changes: 81 additions & 4 deletions rocks-lib/src/operations/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,85 @@ use bytes::Bytes;
use thiserror::Error;

use crate::{
config::Config,
package::{PackageName, PackageReq, PackageVersion, RemotePackage},
progress::{Progress, ProgressBar},
remote_package_db::{RemotePackageDB, SearchError},
remote_package_db::{RemotePackageDB, RemotePackageDBError, SearchError},
rockspec::{Rockspec, RockspecError},
};

pub struct Download<'a> {
package_req: &'a PackageReq,
package_db: Option<RemotePackageDB>,
config: &'a Config,
progress: &'a Progress<ProgressBar>,
}

/// Builder for a rock downloader.
impl<'a> Download<'a> {
/// Construct a new `.src.rock` downloader.
pub fn new(
package_req: &'a PackageReq,
config: &'a Config,
progress: &'a Progress<ProgressBar>,
) -> Self {
Self {
package_req,
package_db: None,
config,
progress,
}
}

/// Sets the package database to use for searching for packages.
/// Instantiated from the config if not set.
pub fn package_db(self, package_db: RemotePackageDB) -> Self {
Self {
package_db: Some(package_db),
..self
}
}

/// Download the package's Rockspec.
pub async fn download_rockspec(self) -> Result<Rockspec, SearchAndDownloadError> {
let package_db = match self.package_db {
Some(db) => db,
None => RemotePackageDB::from_config(self.config).await?,
};
download_rockspec(self.package_req, &package_db, self.progress).await
}

/// Download a `.src.rock` to a file.
/// `destination_dir` defaults to the current working directory if not set.
pub async fn download_src_rock_to_file(
self,
destination_dir: Option<PathBuf>,
) -> Result<DownloadedSrcRock, SearchAndDownloadError> {
let package_db = match self.package_db {
Some(db) => db,
None => RemotePackageDB::from_config(self.config).await?,
};
download_src_rock_to_file(
self.package_req,
destination_dir,
&package_db,
self.progress,
)
.await
}

/// Search for a `.src.rock` and download it to memory.
pub async fn search_and_download_src_rock(
self,
) -> Result<DownloadedSrcRockBytes, SearchAndDownloadError> {
let package_db = match self.package_db {
Some(db) => db,
None => RemotePackageDB::from_config(self.config).await?,
};
search_and_download_src_rock(self.package_req, &package_db, self.progress).await
}
}

pub struct DownloadedSrcRockBytes {
pub name: PackageName,
pub version: PackageVersion,
Expand All @@ -29,9 +102,11 @@ pub enum DownloadRockspecError {
Request(#[from] reqwest::Error),
#[error("failed to convert rockspec response: {0}")]
ResponseConversion(#[from] FromUtf8Error),
#[error("error initialising remote package DB: {0}")]
RemotePackageDB(#[from] RemotePackageDBError),
}

pub async fn download_rockspec(
async fn download_rockspec(
package_req: &PackageReq,
package_db: &RemotePackageDB,
progress: &Progress<ProgressBar>,
Expand All @@ -55,9 +130,11 @@ pub enum SearchAndDownloadError {
Utf8(#[from] FromUtf8Error),
#[error(transparent)]
Rockspec(#[from] RockspecError),
#[error("error initialising remote package DB: {0}")]
RemotePackageDB(#[from] RemotePackageDBError),
}

pub async fn search_and_download_src_rock(
async fn search_and_download_src_rock(
package_req: &PackageReq,
package_db: &RemotePackageDB,
progress: &Progress<ProgressBar>,
Expand All @@ -79,7 +156,7 @@ pub(crate) async fn download_src_rock(
download_src_rock_impl(remote_package).await
}

pub async fn download_to_file(
async fn download_src_rock_to_file(
package_req: &PackageReq,
destination_dir: Option<PathBuf>,
package_db: &RemotePackageDB,
Expand Down
8 changes: 4 additions & 4 deletions rocks-lib/src/operations/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
rockspec::Rockspec,
};

use super::{download_rockspec, SearchAndDownloadError};
use super::{Download, SearchAndDownloadError};

#[derive(Clone, Debug)]
pub(crate) struct PackageInstallSpec {
Expand Down Expand Up @@ -52,9 +52,9 @@ pub(crate) async fn get_all_dependencies(
tokio::spawn(async move {
let bar = progress.map(|p| p.new_bar());

let rockspec = download_rockspec(&package, &package_db, &bar)
.await
.unwrap();
let rockspec = Download::new(&package, &config, &bar)
.download_rockspec()
.await?;

let constraint =
if *package.version_req() == PackageVersionReq::SemVer(VersionReq::STAR) {
Expand Down

0 comments on commit 9ad3c94

Please sign in to comment.