check_latest/async/mod.rs
1//! Enabled with the `async` feature
2//!
3//! ```rust,no_run
4//! # async fn run() {
5//! use check_latest::*;
6//!
7//! if let Ok(Some(version)) = check_max_async!().await {
8//! println!("We've released a new version: {}!", version);
9//! }
10//! # }
11//! ```
12
13use crate::{build_url, Versions};
14use anyhow::{Context, Result};
15
16/// Checks if there is a version available that is greater than the current
17/// version.
18///
19/// # Returns
20///
21/// Assume the current version is `a.b.c`, and we are looking at versions that
22/// are `x.y.z`.
23///
24/// - `Ok(Some(version))` if `x.y.z > a.b.c` where `version = max x.y.z`
25/// - `Ok(None)` if no version meets the rule `x.y.z > a.b.c`
26/// - `Err(e)` if comparison could not be made
27///
28/// # Example
29///
30/// ```rust,no_run
31/// # async fn run() {
32/// use check_latest::check_max_async;
33///
34/// if let Ok(Some(version)) = check_max_async!().await {
35/// println!("A new version is available: {}", version);
36/// }
37/// # }
38/// ```
39#[macro_export]
40macro_rules! check_max_async {
41 () => {
42 async {
43 $crate::new_versions_async!().await.map(|versions| {
44 let max = versions.max_unyanked_version()?.clone();
45 if max > $crate::crate_version!() {
46 Some(max)
47 } else {
48 None
49 }
50 })
51 }
52 };
53}
54/// Checks if there is a higher minor version available with the same major
55/// version
56///
57/// # Returns
58///
59/// Assume the current version is `a.b.c`, and we are looking at versions that
60/// are `a.y.z`.
61///
62/// - `Ok(Some(version))` if `a.y.z > a.b.c` where `version = max a.b.z`
63/// - `Ok(None)` if no version meets the rule `a.y.z > a.b.c`
64/// - `Err(e)` if comparison could not be made
65///
66/// # Example
67///
68/// ```rust,no_run
69/// # async fn run() {
70/// use check_latest::check_minor_async;
71///
72/// if let Ok(Some(version)) = check_minor_async!().await {
73/// println!("A new version is available: {}", version);
74/// }
75/// # }
76/// ```
77#[macro_export]
78macro_rules! check_minor_async {
79 () => {
80 async {
81 $crate::new_versions_async!().await.and_then(|versions| {
82 let major_version = $crate::crate_major_version!().parse()?;
83 let max = versions.max_unyanked_minor_version(major_version);
84 let max = max.cloned();
85 let max = max.filter(|max| max > $crate::crate_version!());
86 Ok(max)
87 })
88 }
89 };
90}
91
92/// Checks if there is a higher patch available, within the same major.minor
93/// version.
94///
95/// # Returns
96///
97/// Assume the current version is `a.b.c`, and we are looking at versions that
98/// are `a.b.z`.
99///
100/// - `Ok(Some(version))` if `a.b.z > a.b.c`, where `version = max a.b.z`
101/// - `Ok(None)` if no version meets the rule `a.b.z > a.b.c`
102/// - `Err(e)` if comparison could not be made
103///
104/// # Example
105///
106/// ```rust,no_run
107/// # async fn run() {
108/// use check_latest::check_patch_async;
109///
110/// if let Ok(Some(version)) = check_patch_async!().await {
111/// println!("We've implemented one or more bug fixes in {}", version);
112/// }
113/// # }
114/// ```
115#[macro_export]
116macro_rules! check_patch_async {
117 () => {
118 async {
119 $crate::new_versions_async!().await.and_then(|versions| {
120 let major_version = $crate::crate_major_version!().parse()?;
121 let minor_version = $crate::crate_minor_version!().parse()?;
122 let max = versions.max_unyanked_patch(major_version, minor_version);
123 let max = max.cloned();
124 let max = max.filter(|max| max > $crate::crate_version!());
125 Ok(max)
126 })
127 }
128 };
129}
130
131impl Versions {
132 /// - `crate_name`: The crate that the version should be checked for.
133 /// - `user_agent`: without a proper User-Agent, the request to the
134 /// [Crates.io] API will result in the response below, which we won't
135 /// be able to parse into crate versions.
136 ///
137 /// # Example Response from Bad User Agent
138 ///
139 /// ```text
140 /// We require that all requests include a `User-Agent` header. To allow us to determine the impact your bot has on our service, we ask that your user agent actually identify your bot, and not just report the HTTP client library you're using. Including contact information will also reduce the chance that we will need to take action against your bot.
141 ///
142 /// Bad:
143 /// User-Agent: <bad user agent that you used>
144 ///
145 /// Better:
146 /// User-Agent: my_crawler
147 ///
148 /// Best:
149 /// User-Agent: my_crawler (my_crawler.com/info)
150 /// User-Agent: my_crawler (help@my_crawler.com)
151 ///
152 /// If you believe you've received this message in error, please email help@crates.io and include the request id {}.
153 /// ```
154 ///
155 ///
156 /// # Example
157 ///
158 /// ```rust,no_run
159 /// # async fn run() {
160 /// use check_latest::Versions;
161 ///
162 /// if let Ok(versions) = Versions::async_new("my-awesome-crate-bin", "my-awesome-crate-bin/1.0.0").await {
163 /// /* Do your stuff */
164 /// }
165 /// # }
166 /// ```
167 ///
168 /// [Crates.io]: https://crates.io/
169 pub async fn async_new(crate_name: &str, user_agent: &str) -> Result<Versions> {
170 let url = build_url(crate_name);
171 let response: Versions = reqwest::Client::builder()
172 .user_agent(user_agent)
173 .build()
174 .context("Couldn't build client")?
175 .get(&url)
176 .send()
177 .await
178 .context("Couldn't request crate info")?
179 .json()
180 .await
181 .context("Couldn't read as JSON")?;
182 Ok(response)
183 }
184}
185
186/// Helper for creating a new `Versions`.
187///
188/// Will assume the correct `crate_name` and `user_agent` based on the contents
189/// of *your* `Cargo.toml`, but these values can be overridden.
190///
191/// # Examples
192///
193/// ## Basic Usage
194///
195/// ```rust,no_run
196/// # async fn run() {
197/// use check_latest::new_versions_async;
198///
199/// let versions = new_versions_async!().await;
200/// # }
201/// ```
202///
203/// ## Overriding Default Values
204///
205/// *__NOTE__ Overriding both defaults is no different than just using
206/// `Versions::new`. You will probably want to override only one field, if any,
207/// if using this macro.
208///
209/// ```rust,no_run
210/// # async fn run() {
211/// use check_latest::new_versions_async;
212///
213/// let versions = new_versions_async!(
214/// crate_name = "renamed-crate",
215/// user_agent = "my-user-agent",
216/// ).await;
217/// # }
218/// ```
219#[macro_export]
220macro_rules! new_versions_async {
221 (crate_name = $crate_name:expr, user_agent = $user_agent:expr $(,)?) => {
222 $crate::Versions::async_new($crate_name, $user_agent)
223 };
224 (user_agent = $user_agent:expr, crate_name = $crate_name:expr $(,)?) => {
225 $crate::new_versions_async!(crate_name = $crate_name, user_agent = $user_agent,)
226 };
227 (crate_name = $crate_name:expr) => {
228 $crate::new_versions_async!(crate_name = $crate_name, user_agent = $crate::user_agent!(),)
229 };
230 (user_agent = $user_agent:expr) => {
231 $crate::new_versions_async!(crate_name = $crate::crate_name!(), user_agent = $user_agent,)
232 };
233 () => {
234 $crate::new_versions_async!(
235 crate_name = $crate::crate_name!(),
236 user_agent = $crate::user_agent!(),
237 )
238 };
239}