Skip to content

Commit

Permalink
Refactor asset picker to operate on Keys<...> where the keys are as…
Browse files Browse the repository at this point in the history
…set names

It never looked at the `Asset.url` field, so having it work with an owned `Vec<Asset>` was
pointless. This is step 1 of adding support for checksums, since to do that we have to preserve the
original list of assets, so we can find the checksums file after we pick the release asset we want
to download.
  • Loading branch information
autarch committed Dec 28, 2024
1 parent 7d015ab commit 43aaa26
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 112 deletions.
41 changes: 41 additions & 0 deletions ubi/src/assets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
ops::{Deref, DerefMut},
};
use url::Url;

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
pub(crate) struct Asset {
pub(crate) name: String,
pub(crate) url: Url,
}

#[derive(Debug, PartialEq, Eq)]
pub(crate) struct Assets(HashMap<String, Url>);

impl From<Vec<Asset>> for Assets {
fn from(assets: Vec<Asset>) -> Assets {
Assets(assets.into_iter().map(|a| (a.name, a.url)).collect())
}
}

impl FromIterator<Asset> for Assets {
fn from_iter<I: IntoIterator<Item = Asset>>(iter: I) -> Self {
Assets(iter.into_iter().map(|a| (a.name, a.url)).collect())
}
}

impl Deref for Assets {
type Target = HashMap<String, Url>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for Assets {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
4 changes: 2 additions & 2 deletions ubi/src/forge.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::ubi::Asset;
use crate::assets::Assets;
use anyhow::Result;
use async_trait::async_trait;
use reqwest::{
Expand All @@ -22,7 +22,7 @@ pub enum ForgeType {

#[async_trait]
pub(crate) trait Forge: std::fmt::Debug {
async fn fetch_assets(&self, client: &Client) -> Result<Vec<Asset>>;
async fn fetch_assets(&self, client: &Client) -> Result<Assets>;

fn release_info_url(&self) -> Url;
fn maybe_add_token_header(&self, req_builder: RequestBuilder) -> Result<RequestBuilder>;
Expand Down
10 changes: 6 additions & 4 deletions ubi/src/github.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
assets::{Asset, Assets},
forge::{Forge, ForgeType},
ubi::Asset,
};
use anyhow::Result;
use async_trait::async_trait;
Expand Down Expand Up @@ -31,13 +31,15 @@ pub(crate) struct Release {

#[async_trait]
impl Forge for GitHub {
async fn fetch_assets(&self, client: &Client) -> Result<Vec<Asset>> {
async fn fetch_assets(&self, client: &Client) -> Result<Assets> {
Ok(self
.make_release_info_request(client)
.await?
.json::<Release>()
.await?
.assets)
.assets
.into_iter()
.collect())
}

fn release_info_url(&self) -> Url {
Expand Down Expand Up @@ -164,7 +166,7 @@ mod tests {

let client = Client::new();
let got_assets = github.fetch_assets(&client).await?;
assert_eq!(got_assets, assets);
assert_eq!(got_assets, assets.into());

m.assert_async().await;

Expand Down
10 changes: 6 additions & 4 deletions ubi/src/gitlab.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
assets::{Asset, Assets},
forge::{Forge, ForgeType},
ubi::Asset,
};
use anyhow::Result;
use async_trait::async_trait;
Expand Down Expand Up @@ -33,14 +33,16 @@ struct GitLabAssets {

#[async_trait]
impl Forge for GitLab {
async fn fetch_assets(&self, client: &Client) -> Result<Vec<Asset>> {
async fn fetch_assets(&self, client: &Client) -> Result<Assets> {
Ok(self
.make_release_info_request(client)
.await?
.json::<Release>()
.await?
.assets
.links)
.links
.into_iter()
.collect())
}

fn release_info_url(&self) -> Url {
Expand Down Expand Up @@ -169,7 +171,7 @@ mod tests {

let client = Client::new();
let got_assets = github.fetch_assets(&client).await?;
assert_eq!(got_assets, assets);
assert_eq!(got_assets, assets.into());

m.assert_async().await;

Expand Down
1 change: 1 addition & 0 deletions ubi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
#![doc = document_features::document_features!()]

mod arch;
mod assets;
mod builder;
mod extension;
mod forge;
Expand Down
Loading

0 comments on commit 43aaa26

Please sign in to comment.