Skip to main content

burncloud_auto_update/
config.rs

1//! 自动更新配置模块
2
3use std::env;
4
5/// 自动更新配置
6#[derive(Debug, Clone)]
7pub struct UpdateConfig {
8    /// GitHub 仓库所有者
9    pub github_owner: String,
10    /// GitHub 仓库名称
11    pub github_repo: String,
12    /// 二进制文件名
13    pub bin_name: String,
14    /// 当前版本
15    pub current_version: String,
16}
17
18impl Default for UpdateConfig {
19    fn default() -> Self {
20        Self {
21            github_owner: "burncloud".to_string(),
22            github_repo: "burncloud".to_string(),
23            bin_name: "burncloud".to_string(),
24            current_version: env!("CARGO_PKG_VERSION").to_string(),
25        }
26    }
27}
28
29impl UpdateConfig {
30    /// 创建新的配置
31    pub fn new(
32        github_owner: impl Into<String>,
33        github_repo: impl Into<String>,
34        bin_name: impl Into<String>,
35        current_version: impl Into<String>,
36    ) -> Self {
37        Self {
38            github_owner: github_owner.into(),
39            github_repo: github_repo.into(),
40            bin_name: bin_name.into(),
41            current_version: current_version.into(),
42        }
43    }
44
45    /// 设置 GitHub 仓库信息
46    pub fn with_github_repo(mut self, owner: impl Into<String>, repo: impl Into<String>) -> Self {
47        self.github_owner = owner.into();
48        self.github_repo = repo.into();
49        self
50    }
51
52    /// 设置二进制文件名
53    pub fn with_bin_name(mut self, name: impl Into<String>) -> Self {
54        self.bin_name = name.into();
55        self
56    }
57
58    /// 设置当前版本
59    pub fn with_current_version(mut self, version: impl Into<String>) -> Self {
60        self.current_version = version.into();
61        self
62    }
63
64    /// 获取 GitHub 下载链接
65    pub fn github_releases_url(&self) -> String {
66        format!(
67            "https://github.com/{}/{}/releases",
68            self.github_owner, self.github_repo
69        )
70    }
71
72    /// 获取 Gitee 下载链接
73    pub fn gitee_releases_url(&self) -> String {
74        format!(
75            "https://gitee.com/{}/{}/releases",
76            self.github_owner, self.github_repo
77        )
78    }
79
80    /// 获取下载链接元组 (GitHub, Gitee)
81    pub fn download_links(&self) -> (String, String) {
82        (self.github_releases_url(), self.gitee_releases_url())
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89
90    #[test]
91    fn test_default_config() {
92        let config = UpdateConfig::default();
93        assert_eq!(config.github_owner, "burncloud");
94        assert_eq!(config.github_repo, "burncloud");
95        assert_eq!(config.bin_name, "burncloud");
96        assert_eq!(config.current_version, env!("CARGO_PKG_VERSION"));
97    }
98
99    #[test]
100    fn test_config_builder() {
101        let config = UpdateConfig::new("owner", "repo", "app", "1.0.0")
102            .with_github_repo("new_owner", "new_repo")
103            .with_bin_name("new_app")
104            .with_current_version("2.0.0");
105
106        assert_eq!(config.github_owner, "new_owner");
107        assert_eq!(config.github_repo, "new_repo");
108        assert_eq!(config.bin_name, "new_app");
109        assert_eq!(config.current_version, "2.0.0");
110    }
111
112    #[test]
113    fn test_download_links() {
114        let config = UpdateConfig::default();
115        let (github, gitee) = config.download_links();
116
117        assert_eq!(github, "https://github.com/burncloud/burncloud/releases");
118        assert_eq!(gitee, "https://gitee.com/burncloud/burncloud/releases");
119    }
120}