update_available/
lib.rs

1use crate::data::{UpdateAvailable, UpdateInfo};
2
3mod data;
4mod logic;
5
6#[cfg(test)]
7mod test;
8
9/// A user identifier for GitHub repositories.
10pub type User = String;
11
12/// Represents the source from which to check for updates.
13pub enum Source {
14    /// Check for updates on crates.io.
15    CratesIo,
16    /// Check for updates on GitHub for a specific user.
17    Github(User),
18    /// Check for updates on Gitea for a specific user and Gitea URL.
19    Gitea(User, String),
20}
21
22/// Prints update information for a package from the specified source.
23///
24/// This is a convenience function that checks for updates and prints the result
25/// directly to stdout if an update is available.
26///
27/// # Arguments
28///
29/// * `name` - The name of the package to check
30/// * `current_version` - The current version string (e.g., "1.0.0")
31/// * `source` - The source to check for updates
32///
33/// # Examples
34///
35/// ```rust
36/// use update_available::{print_check, Source};
37///
38/// // Check crates.io
39/// print_check("serde", "1.0.0", Source::CratesIo);
40///
41/// // Check GitHub
42/// print_check("my-repo", "0.1.0", Source::Github("username".to_string()));
43///
44/// // Check Gitea
45/// print_check("my-repo", "0.1.0", Source::Gitea("username".to_string(), "https://gitea.example.com".to_string()));
46/// ```
47pub fn print_check(name: &str, current_version: &str, source: Source) {
48    let result = match source {
49        Source::CratesIo => check_crates_io(name, current_version),
50        Source::Github(user) => check_github(name, &user, current_version),
51        Source::Gitea(user, gitea_url) => {
52            let update_available = UpdateAvailable::new(name, current_version);
53            update_available.gitea(&user, &gitea_url)
54        }
55    };
56    if let Ok(info) = result {
57        println!("{info}");
58    }
59}
60
61/// Checks for updates on crates.io for the specified package.
62///
63/// This function queries the crates.io API to check if a newer version
64/// of the specified package is available.
65///
66/// # Arguments
67///
68/// * `name` - The name of the crate to check on crates.io
69/// * `current_version` - The current version string (e.g., "1.0.0")
70///
71/// # Returns
72///
73/// Returns a `Result<UpdateInfo, anyhow::Error>` containing update information
74/// if successful, or an error if the check fails.
75///
76/// # Errors
77///
78/// This function will return an error if:
79/// * The network request fails
80/// * The crates.io API returns an error
81/// * The version strings cannot be parsed
82/// * The response format is unexpected
83///
84/// # Examples
85///
86/// ```rust
87/// use update_available::check_crates_io;
88///
89/// match check_crates_io("serde", "1.0.0") {
90///     Ok(info) => println!("{}", info),
91///     Err(e) => eprintln!("Error checking for updates: {}", e),
92/// }
93/// ```
94pub fn check_crates_io(name: &str, current_version: &str) -> anyhow::Result<UpdateInfo> {
95    let update_available = UpdateAvailable::new(name, current_version);
96    update_available.crates_io()
97}
98
99/// Checks for updates on GitHub for the specified repository.
100///
101/// This function queries the GitHub API to check if a newer version
102/// of the specified repository is available.
103///
104/// # Arguments
105///
106/// * `name` - The name of the repository to check
107/// * `user` - The GitHub username or organization that owns the repository
108/// * `current_version` - The current version string (e.g., "1.0.0")
109///
110/// # Returns
111///
112/// Returns a `Result<UpdateInfo, anyhow::Error>` containing update information
113/// if successful, or an error if the check fails.
114///
115/// # Errors
116///
117/// This function will return an error if:
118/// * The network request fails
119/// * The GitHub API returns an error
120/// * The version strings cannot be parsed
121/// * The response format is unexpected
122/// * The repository does not exist or has no releases
123///
124/// # Examples
125///
126/// ```rust
127/// use update_available::check_github;
128///
129/// match check_github("my-repo", "username", "1.0.0") {
130///     Ok(info) => println!("{}", info),
131///     Err(e) => eprintln!("Error checking for updates: {}", e),
132/// }
133/// ```
134pub fn check_github(name: &str, user: &str, current_version: &str) -> anyhow::Result<UpdateInfo> {
135    let update_available = UpdateAvailable::new(name, current_version);
136    update_available.github(user)
137}
138
139/// Checks for updates on Gitea for the specified repository.
140///
141/// This function queries the Gitea API to check if a newer version
142/// of the specified repository is available.
143///
144/// # Arguments
145///
146/// * `name` - The name of the repository to check
147/// * `user` - The Gitea username or organization that owns the repository
148/// * `gitea_url` - The base URL of the Gitea instance (e.g., <https://gitea.example.com>)
149/// * `current_version` - The current version string (e.g., "1.0.0")
150///
151/// # Returns
152///
153/// Returns a `Result<UpdateInfo, anyhow::Error>` containing update information
154/// if successful, or an error if the check fails.
155///
156/// # Errors
157///
158/// This function will return an error if:
159/// * The network request fails
160/// * The Gitea API returns an error
161/// * The version strings cannot be parsed
162/// * The response format is unexpected
163/// * The repository does not exist or has no releases
164///
165/// # Examples
166///
167/// ```rust
168/// use update_available::check_gitea;
169///
170/// match check_gitea("my-repo", "username", "https://gitea.example.com", "1.0.0") {
171///     Ok(info) => println!("{}", info),
172///     Err(e) => eprintln!("Error checking for updates: {}", e),
173/// }
174/// ```
175pub fn check_gitea(
176    name: &str,
177    user: &str,
178    gitea_url: &str,
179    current_version: &str,
180) -> anyhow::Result<UpdateInfo> {
181    let update_available = UpdateAvailable::new(name, current_version);
182    update_available.gitea(user, gitea_url)
183}