1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
//! Enabled with the `blocking` feature //! //! ```rust,no_run //! use check_latest::*; //! //! if let Ok(Some(version)) = max_version!() { //! println!("We've released a new version: {}!", version); //! } //! ``` use anyhow::{Context, Result}; use crate::{build_url, CratesioResponse, Versions}; #[allow(deprecated)] use crate::MaxAndNew; pub use max::*; pub use newest::*; use semver::Version; /// *__NOTE__ You probably want to use `versions!`* /// /// `crate_name`: The crate that the version should be checked for. /// /// `user_agent`: without a proper User-Agent, the request to the [Crates.io] API /// will result in the following response, which we won't be able to parse into /// crate versions. /// /// ```text /// 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. /// /// Bad: /// User-Agent: <badge user agent that you used> /// /// Better: /// User-Agent: my_crawler /// /// Best: /// User-Agent: my_crawler (my_crawler.com/info) /// User-Agent: my_crawler (help@my_crawler.com) /// /// If you believe you've received this message in error, please email help@crates.io and include the request id {}. /// ``` /// /// /// # Example /// /// ```rust,no_run /// use check_latest::blocking::get_versions; /// use semver::Version; /// /// let current_version = Version::parse("1.0.0").unwrap(); /// /// if let Ok(versions) = get_versions("my-awesome-crate-bin", "my-awesome-crate-bin/1.0.0") { /// if versions.max_version > current_version { /// println!("Go get the new version!"); /// } /// } /// ``` /// /// [Crates.io]: https://crates.io/ #[deprecated(since = "0.4", note = "Please use Versions struct")] #[allow(deprecated)] pub fn get_versions(crate_name: &str, user_agent: &str) -> Result<MaxAndNew> { let url = build_url(crate_name); let response: CratesioResponse = reqwest::blocking::Client::builder() .user_agent(user_agent) .build() .context("Couldn't build client")? .get(&url) .send() .context("Couldn't request crate info")? .json() .context("Couldn't read as JSON")?; Ok(response.versions) } fn get_version_list(crate_name: &str, user_agent: &str) -> Result<Vec<Version>> { let url = build_url(crate_name); let response: CratesioResponse = reqwest::blocking::Client::builder() .user_agent(user_agent) .build() .context("Couldn't build client")? .get(&url) .send() .context("Couldn't request crate info")? .json() .context("Couldn't read as JSON")?; let versions = response.all_versions; let versions = versions .into_iter() .filter(|v| !v.yanked) .map(|v| v.version) .collect(); Ok(versions) } /// Makes it easier to run `get_versions`. /// /// `versions!()` will predict the `crate_name` and `user_agent`. `crate_name` /// will default to package name in your `Cargo.toml` file. /// `user_agent` will default to the name of your crate as defined in /// `Cargo.toml` followed by a `/` and the version of your package as defined /// in your `Cargo.toml` file (e.g. `my-crate-name/1.0.0`). /// /// If you do not want these defaults to be used, you can set your own values. /// See the examples below. /// /// # Examples /// /// ## Use Defaults /// /// ```rust,no_run /// use check_latest::versions; /// use semver::Version; /// /// let current_version = Version::parse("1.0.0").unwrap(); /// /// if let Ok(versions) = versions!() { /// if versions.max_version > current_version { /// println!("Go get a new version!"); /// } /// } /// ``` /// /// ## Set Crate Name /// /// ```rust,no_run /// use check_latest::versions; /// use semver::Version; /// /// let current_version = Version::parse("1.0.0").unwrap(); /// /// if let Ok(versions) = versions!(crate_name = "my-renamed-crate") { /// if versions.max_version > current_version { /// println!("Go get a new version!"); /// } /// } /// ``` /// /// ## Set User Agent /// /// ```rust,no_run /// use check_latest::versions; /// use semver::Version; /// /// let current_version = Version::parse("1.0.0").unwrap(); /// /// if let Ok(versions) = versions!(user_agent = "my extra detailed user agent") { /// if versions.max_version > current_version { /// println!("Go get a new version!"); /// } /// } /// ``` /// /// ## Set Both /// /// ```rust,no_run /// use check_latest::versions; /// use semver::Version; /// /// let current_version = Version::parse("1.0.0").unwrap(); /// /// let crate_name = "my-renamed-crate"; /// let user_agent = "my extra detailed user agent"; /// /// // This is reversible BTW /// let versions = versions!(crate_name = crate_name, user_agent = user_agent); /// /// if let Ok(versions) = versions { /// if versions.max_version > current_version { /// println!("Go get a new version!"); /// } /// } /// ``` #[macro_export] #[deprecated(since = "0.4", note = "Please use Versions struct")] macro_rules! versions { () => { $crate::versions!( crate_name = $crate::crate_name!(), user_agent = $crate::user_agent!(), ) }; (crate_name = $crate_name:expr, user_agent = $user_agent:expr $(,)?) => { $crate::blocking::get_versions($crate_name, $user_agent) }; (user_agent = $user_agent:expr, crate_name = $crate_name:expr $(,)?) => { $crate::versions!(crate_name = $crate_name, user_agent = $user_agent) }; (crate_name = $crate_name:expr $(,)?) => { $crate::versions!( crate_name = $crate_name, user_agent = $crate::user_agent!(), ) }; (user_agent = $user_agent:expr $(,)?) => { $crate::versions!( crate_name = $crate::crate_name!(), user_agent = $user_agent, ) }; } /// Checks if there is a version available that is greater than the current /// version. /// /// # Returns /// /// Assume the current version is `a.b.c`, and we are looking at versions that /// are `x.y.z`. /// /// - `Ok(Some(version))` if `x.y.z > a.b.c` where `version = max x.y.z` /// - `Ok(None)` if no version meets the rule `x.y.z > a.b.c` /// - `Err(e)` if comparison could not be made /// /// # Example /// /// ```rust,no_run /// use check_latest::check_max; /// /// if let Ok(Some(version)) = check_max!() { /// println!("A new version is available: {}", version); /// } /// ``` #[macro_export] macro_rules! check_max { () => { $crate::new_versions!() .map(|versions| { let max = versions.max_unyanked_version()? .clone(); if max > $crate::crate_version!() { Some(max) } else { None } }) }; } /// Checks if there is a higher minor version available with the same major /// version. /// /// # Returns /// /// Assume the current version is `a.b.c`, and we are looking at versions that /// are `a.y.z`. /// /// - `Ok(Some(version))` if `a.y.z > a.b.c` where `version = max a.b.z` /// - `Ok(None)` if no version meets the rule `a.y.z > a.b.c` /// - `Err(e)` if comparison could not be made /// /// # Example /// /// ```rust,no_run /// use check_latest::check_minor; /// /// if let Ok(Some(version)) = check_minor!() { /// println!("A new version is available: {}", version); /// } /// ``` #[macro_export] macro_rules! check_minor { () => { $crate::new_versions!() .and_then(|versions| { let major_version = $crate::crate_major_version!().parse()?; let max = versions.max_unyanked_minor_version(major_version); let max = max.cloned(); let max = max.filter(|max| max > $crate::crate_version!()); Ok(max) }) }; } /// Checks if there is a higher patch available, within the same major.minor /// version. /// /// # Returns /// /// Assume the current version is `a.b.c`, and we are looking at versions that /// are `a.b.z`. /// /// - `Ok(Some(version))` if `a.b.z > a.b.c`, where `version = max a.b.z` /// - `Ok(None)` if no version meets the rule `a.b.z > a.b.c` /// - `Err(e)` if comparison could not be made /// /// # Example /// /// ```rust,no_run /// use check_latest::check_patch; /// /// if let Ok(Some(version)) = check_patch!() { /// println!("We've implemented one or more bug fixes in {}", version); /// } /// ``` #[macro_export] macro_rules! check_patch { () => { $crate::new_versions!() .and_then(|versions| { let major_version = $crate::crate_major_version!().parse()?; let minor_version = $crate::crate_minor_version!().parse()?; let max = versions.max_unyanked_patch(major_version, minor_version); let max = max.cloned(); let max = max.filter(|max| max > $crate::crate_version!()); Ok(max) }) }; } impl Versions { /// - `crate_name`: The crate that the version should be checked for. /// - `user_agent`: without a proper User-Agent, the request to the /// [Crates.io] API will result in the response below, which we won't /// be able to parse into crate versions. /// /// # Example Response from Bad User Agent /// /// ```text /// 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. /// /// Bad: /// User-Agent: <bad user agent that you used> /// /// Better: /// User-Agent: my_crawler /// /// Best: /// User-Agent: my_crawler (my_crawler.com/info) /// User-Agent: my_crawler (help@my_crawler.com) /// /// If you believe you've received this message in error, please email help@crates.io and include the request id {}. /// ``` /// /// /// # Example /// /// ```rust,no_run /// use check_latest::Versions; /// /// if let Ok(versions) = Versions::new("my-awesome-crate-bin", "my-awesome-crate-bin/1.0.0") { /// /* Do your stuff */ /// } /// ``` /// /// [Crates.io]: https://crates.io/ pub fn new(crate_name: &str, user_agent: &str) -> Result<Versions> { let url = build_url(crate_name); let response: Versions = reqwest::blocking::Client::builder() .user_agent(user_agent) .build() .context("Couldn't build client")? .get(&url) .send() .context("Couldn't request crate info")? .json() .context("Couldn't read as JSON")?; Ok(response) } } /// Helper for creating a new `Versions`. /// /// Will assume the correct `crate_name` and `user_agent` based on the contents /// of *your* `Cargo.toml`, but these values can be overridden. /// /// # Examples /// /// ## Basic Usage /// /// ```rust,no_run /// use check_latest::new_versions; /// /// let versions = new_versions!(); /// ``` /// /// ## Overriding Default Values /// /// *__NOTE__ Overriding both defaults is no different than just using /// `Versions::new`. You will probably want to override only one field, if any, /// if using this macro.* /// /// ```rust,no_run /// use check_latest::new_versions; /// /// let versions = new_versions!( /// crate_name = "renamed-crate", /// user_agent = "my-user-agent", /// ); /// ``` #[macro_export] macro_rules! new_versions { (crate_name = $crate_name:expr, user_agent = $user_agent:expr $(,)?) => { $crate::Versions::new($crate_name, $user_agent) }; (user_agent = $user_agent:expr, crate_name = $crate_name:expr $(,)?) => { $crate::new_versions!( crate_name = $crate_name, user_agent = $user_agent, ) }; (crate_name = $crate_name:expr) => { $crate::new_versions!( crate_name = $crate_name, user_agent = $crate::user_agent!(), ) }; (user_agent = $user_agent:expr) => { $crate::new_versions!( crate_name = $crate::crate_name!(), user_agent = $user_agent, ) }; () => { $crate::new_versions!( crate_name = $crate::crate_name!(), user_agent = $crate::user_agent!(), ) }; } mod max; mod newest;