1use serde::Deserialize;
2
3use crate::{
4 error::PlatformError,
5 platform::{Release, ReleaseAsset, ReleasePlatform},
6};
7
8pub struct Github;
9impl ReleasePlatform for Github {
10 const API_BASE_PRIMARY: &'static str = "https://api.github.com";
11
12 const API_BASE_PKGFORGE: &'static str = "https://api.gh.pkgforge.dev";
13
14 const TOKEN_ENV_VAR: &'static str = "GITHUB_TOKEN";
15
16 fn format_project_path(project: &str) -> Result<(String, String), PlatformError> {
17 match project.split_once('/') {
18 Some((owner, repo)) if !owner.trim().is_empty() && !repo.trim().is_empty() => {
19 Ok((owner.to_string(), repo.to_string()))
20 }
21 _ => Err(PlatformError::InvalidInput(format!(
22 "Github project '{}' must be in 'owner/repo' format",
23 project
24 ))),
25 }
26 }
27
28 fn format_api_path(project: &str) -> Result<String, PlatformError> {
29 let (owner, repo) = Self::format_project_path(project)?;
30 Ok(format!("/repos/{}/{}/releases?per_page=100", owner, repo))
31 }
32}
33
34#[derive(Debug, Deserialize)]
35pub struct GithubRelease {
36 name: Option<String>,
37 tag_name: String,
38 prerelease: bool,
39 published_at: String,
40 assets: Vec<GithubAsset>,
41}
42
43impl Release<GithubAsset> for GithubRelease {
44 fn name(&self) -> &str {
45 self.name.as_deref().unwrap_or("")
46 }
47
48 fn tag_name(&self) -> &str {
49 &self.tag_name
50 }
51
52 fn is_prerelease(&self) -> bool {
53 self.prerelease
54 }
55
56 fn published_at(&self) -> &str {
57 &self.published_at
58 }
59
60 fn assets(&self) -> Vec<GithubAsset> {
61 self.assets.clone()
62 }
63}
64
65#[derive(Clone, Debug, Deserialize)]
66pub struct GithubAsset {
67 pub name: String,
68 pub size: u64,
69 pub browser_download_url: String,
70}
71
72impl ReleaseAsset for GithubAsset {
73 fn name(&self) -> &str {
74 &self.name
75 }
76
77 fn size(&self) -> Option<u64> {
78 Some(self.size)
79 }
80
81 fn download_url(&self) -> &str {
82 &self.browser_download_url
83 }
84}