Skip to content

Commit

Permalink
Fixed selecting Proton version erroring out
Browse files Browse the repository at this point in the history
No longer builds a github api request from a release tag.
Now using one Github API call to get Download info
Cleaned up main.rs; now it parses user data and starts a function
Fixed some minor spelling mistakes and other errors
  • Loading branch information
ddotthomas committed Oct 19, 2023
1 parent 60969c8 commit f0877a6
Show file tree
Hide file tree
Showing 10 changed files with 1,391 additions and 1,068 deletions.
1,435 changes: 1,064 additions & 371 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 1 addition & 8 deletions libprotonup/src/apps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,7 @@ impl fmt::Display for App {
}

impl App {
pub fn app_available_variants(&self) -> Vec<Variant> {
match *self {
Self::Steam => vec![Variant::GEProton],
Self::Lutris => vec![Variant::WineGE, Variant::GEProton],
}
}

pub fn app_default_variant(&self) -> Variant {
pub fn app_wine_version(&self) -> Variant {
match *self {
Self::Steam => Variant::GEProton,
Self::Lutris => Variant::WineGE,
Expand Down
129 changes: 43 additions & 86 deletions libprotonup/src/github.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::constants;
use crate::variants::VariantParameters;
use crate::variants::VariantGithubParameters;
use anyhow::Result;
use serde::{Deserialize, Serialize};

Expand All @@ -8,16 +8,39 @@ pub type ReleaseList = Vec<Release>;
#[derive(Serialize, Deserialize, Debug)]
pub struct Release {
/// API URL of the Release
url: String,
url: Option<String>,
/// Tag name of the Release, examples "8.7-GE-1-Lol" "GE-Proton8-5"
pub tag_name: String,
/// Release post name, examples "Wine-GE-Proton8-5 Released" " Lutris-GE-8.7-1-LoL"
name: String,
published_at: String,
/// Asset list for each Release, usually the tar.gz/tar.xz file and a sha512sum file for integrity checking
assets: Vec<Asset>,
}

impl std::fmt::Display for Release {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "{}", self.tag_name)
}
}

impl Release {
/// Returns a Download struct corresponding to the Release
pub fn get_download_info(&self) -> Download {
let mut download: Download = Download::default();
download.version = self.tag_name.clone();
for asset in &self.assets {
if asset.name.ends_with("sha512sum") {
download.sha512sum_url = asset.browser_download_url.clone();
} else if asset.name.ends_with("tar.gz") || asset.name.ends_with("tar.xz") {
download.download_url = asset.browser_download_url.clone();
download.size = asset.size as u64;
}
}

download
}
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Asset {
/// API URL of the Asset
Expand All @@ -28,14 +51,15 @@ pub struct Asset {
name: String,
/// Size in Bytes, divide by 1_000_000 for MB
size: i64,
created_at: String,
updated_at: String,
/// Direct download URL
browser_download_url: String,
}

/// Returns a Vec of Releases from a GitHub repository, the URL used for the request is built from the passed in VariantParameters
pub async fn list_releases(source: &VariantParameters) -> Result<ReleaseList, reqwest::Error> {
pub async fn list_releases(
source: &VariantGithubParameters,
) -> Result<ReleaseList, reqwest::Error> {
let agent = format!("{}/v{}", constants::USER_AGENT, constants::VERSION,);

let url = format!(
Expand All @@ -60,37 +84,6 @@ pub struct Download {
pub download_url: String,
/// Size of Wine or Proton archive in Bytes
pub size: u64,
pub created_at: String,
}

/// Returns a Download struct from the passed in VariantParameters and version tag.
pub async fn fetch_data_from_tag(
tag: &str,
source: &VariantParameters,
) -> Result<Download, reqwest::Error> {
let agent = format!("{}/v{}", constants::USER_AGENT, constants::VERSION,);

let client = reqwest::Client::builder().user_agent(agent).build()?;

let mut download = Download::default();
let url = format!(
"{}/{}/{}/releases/{}",
source.repository_url, source.repository_account, source.repository_name, tag
);
let release: Release = client.get(url).send().await?.json().await?;

download.version = release.tag_name;
for asset in &release.assets {
if asset.name.ends_with("sha512sum") {
download.sha512sum_url = asset.browser_download_url.as_str().to_string();
}
if asset.name.ends_with("tar.gz") || asset.name.ends_with("tar.xz") {
download.created_at = asset.created_at.clone();
download.download_url = asset.browser_download_url.as_str().to_string();
download.size = asset.size as u64;
}
}
Ok(download)
}

#[cfg(test)]
Expand All @@ -100,66 +93,24 @@ mod tests {
use super::*;

#[tokio::test]
async fn test_fetch_data_from_tag() {
async fn test_list_releases() {
let conditions = &[
(
variants::Variant::WineGE.parameters(),
"latest",
"Get Steam",
variants::Variant::WineGE.get_github_parameters(),
"List WineGE",
),
(
variants::Variant::GEProton.parameters(),
"latest",
"Download Lutris",
variants::Variant::GEProton.get_github_parameters(),
"List GEProton",
),
];
for (source_parameters, tag, desc) in conditions {
let result = fetch_data_from_tag(tag, source_parameters).await;

assert!(
result.is_ok(),
"case :{} test: fetch_data_from_tag returned error",
desc
);

let result = result.unwrap();

assert!(
result.download_url.len() > 5,
"case : '{}' test: fetch_data_from_tag returned an wrong download link",
desc
);
assert!(
result.sha512sum_url.len() > 5,
"case : '{}' test: fetch_data_from_tag returned an wrong sha512sum",
desc
);
assert!(
result.size > 100,
"case : '{}' test: fetch_data_from_tag returned an wrong sha512sum",
desc
);
assert!(
result.version.len() > 2,
"case : '{}' test: fetch_data_from_tag returned an wrong version",
desc
);
}
}

#[tokio::test]
async fn test_list_releases() {
let conditions = &[
(variants::Variant::WineGE.parameters(), "List WineGE"),
(variants::Variant::GEProton.parameters(), "List GEProton"),
];

for (source_parameters, desc) in conditions {
let result = list_releases(source_parameters).await;

assert!(
result.is_ok(),
"case : '{}' test: fetch_data_from_tag returned error",
"case : '{}' test: list_releases returned error",
desc
);

Expand Down Expand Up @@ -188,8 +139,14 @@ mod tests {
};

let conditions = &[
(variants::Variant::WineGE.parameters(), "Get WineGE"),
(variants::Variant::GEProton.parameters(), "Get GEProton"),
(
variants::Variant::WineGE.get_github_parameters(),
"Get WineGE",
),
(
variants::Variant::GEProton.get_github_parameters(),
"Get GEProton",
),
];
for (source_parameters, desc) in conditions {
let url = format!(
Expand Down
16 changes: 8 additions & 8 deletions libprotonup/src/variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{fmt, str::FromStr};

/// Struct used to build GitHub api request URLs.
/// Contains the GitHub URL, username for GE, the repository name for either Wine GE or Proton GE, and a Variant Enum for identifying the parameters type
pub struct VariantParameters {
pub struct VariantGithubParameters {
/// this is a link back to the enum variant
variant_ref: Variant,
/// URL of the repository server (GitHub compatible URL only at the moment)
Expand All @@ -14,15 +14,15 @@ pub struct VariantParameters {
pub repository_name: String,
}

impl VariantParameters {
impl VariantGithubParameters {
/// new_custom is a generator for custom VariantParameters
pub fn new_custom(
variant: Variant,
repository_url: String,
repository_account: String,
repository_name: String,
) -> VariantParameters {
VariantParameters {
) -> VariantGithubParameters {
VariantGithubParameters {
variant_ref: variant,
repository_url,
repository_account,
Expand Down Expand Up @@ -74,16 +74,16 @@ impl Variant {
}
}

/// returns the default parameters for this Variant.
pub fn parameters(&self) -> VariantParameters {
/// Returns the default parameters for this Variant, used to build the GitHub URL
pub fn get_github_parameters(&self) -> VariantGithubParameters {
match self {
Variant::GEProton => VariantParameters {
Variant::GEProton => VariantGithubParameters {
variant_ref: Variant::GEProton,
repository_url: GITHUB_URL.to_owned(),
repository_name: GEPROTON_GITHUB_REPO.to_owned(),
repository_account: GE_GITHUB_ACCOUNT.to_owned(),
},
Variant::WineGE => VariantParameters {
Variant::WineGE => VariantGithubParameters {
variant_ref: Variant::WineGE,
repository_url: GITHUB_URL.to_owned(),
repository_name: WINEGE_GITHUB_REPO.to_owned(),
Expand Down
Loading

0 comments on commit f0877a6

Please sign in to comment.