add button to download latest gta-tools exe

This commit is contained in:
2025-04-17 20:28:25 +01:00
parent 92cbec7934
commit 9df757c169
6 changed files with 332 additions and 3 deletions
+48
View File
@@ -0,0 +1,48 @@
use semver::Version;
const APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
#[derive(Debug)]
pub struct Release {
pub version: Version,
pub download_url: String,
}
impl Default for Release {
fn default() -> Self {
Self {
version: Version::parse(env!("CARGO_PKG_VERSION")).unwrap(),
download_url: String::new(),
}
}
}
pub fn get_latest_release() -> Option<Release> {
let request_url = "https://codeberg.org/api/v1/repos/futile/gta-tools/releases/latest";
let ureq_config = ureq::Agent::config_builder()
.user_agent(APP_USER_AGENT)
.build();
let ureq = ureq::Agent::new_with_config(ureq_config);
let Ok(mut response) = ureq.get(request_url).call() else {
return None;
};
let Ok(json) = response.body_mut().read_json::<serde_json::Value>() else {
return None;
};
let version: Version;
if let Some(tag_name) = &json["tag_name"].as_str() {
version = Version::parse(tag_name).unwrap();
} else {
return None;
}
let download_url: String;
if let Some(browser_download_url) = &json["assets"][0]["browser_download_url"].as_str() {
download_url = String::from(*browser_download_url);
} else {
return None;
}
Some(Release {
version,
download_url,
})
}
+25
View File
@@ -0,0 +1,25 @@
use crate::util::codeberg;
use semver::Version;
#[derive(Debug)]
pub struct Meta {
pub current_version: Version,
pub latest_release: codeberg::Release,
pub newer_version_available: bool,
}
impl Default for Meta {
fn default() -> Self {
let current_version = Version::parse(env!("CARGO_PKG_VERSION")).unwrap();
let latest_release = codeberg::get_latest_release().unwrap_or_default();
let newer_version_available = matches!(
&current_version.cmp_precedence(&latest_release.version),
std::cmp::Ordering::Less
);
Self {
current_version,
latest_release,
newer_version_available,
}
}
}
+2
View File
@@ -1,3 +1,5 @@
pub mod codeberg;
pub mod consts;
pub mod countdown;
pub mod meta;
pub mod win;