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
//! Enabled with the `blocking` feature //! //! ```rust,no_run //! use check_latest::*; //! //! if let Ok(Some(version)) = check_max!() { //! println!("We've released a new version: {}!", version); //! } //! ``` use crate::{build_url, Versions}; use anyhow::{Context, Result}; /// 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!(), ) }; }