From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- .../rust/version_check/.cargo-checksum.json | 1 + third_party/rust/version_check/Cargo.toml | 24 + third_party/rust/version_check/LICENSE-APACHE | 201 +++++++++ third_party/rust/version_check/LICENSE-MIT | 19 + third_party/rust/version_check/README.md | 80 ++++ third_party/rust/version_check/src/channel.rs | 193 ++++++++ third_party/rust/version_check/src/date.rs | 203 +++++++++ third_party/rust/version_check/src/lib.rs | 493 +++++++++++++++++++++ third_party/rust/version_check/src/version.rs | 316 +++++++++++++ 9 files changed, 1530 insertions(+) create mode 100644 third_party/rust/version_check/.cargo-checksum.json create mode 100644 third_party/rust/version_check/Cargo.toml create mode 100644 third_party/rust/version_check/LICENSE-APACHE create mode 100644 third_party/rust/version_check/LICENSE-MIT create mode 100644 third_party/rust/version_check/README.md create mode 100644 third_party/rust/version_check/src/channel.rs create mode 100644 third_party/rust/version_check/src/date.rs create mode 100644 third_party/rust/version_check/src/lib.rs create mode 100644 third_party/rust/version_check/src/version.rs (limited to 'third_party/rust/version_check') diff --git a/third_party/rust/version_check/.cargo-checksum.json b/third_party/rust/version_check/.cargo-checksum.json new file mode 100644 index 0000000000..dd79d121d3 --- /dev/null +++ b/third_party/rust/version_check/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.toml":"f25d88044914cb3466df43bc39a199e1589dda1aad3226c9c7e7ac4d2f8751d0","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"b7e650f3fce5c53249d1cdc608b54df156a97edd636cf9d23498d0cfe7aec63e","README.md":"ac2a0a360812436bd5798f5fe2affe7d6ed9eb7f15d6e4d73931e95b437560f2","src/channel.rs":"d2443d503d4cc469a171a51a26eca3ec0d2a58b5f7375a84542c36f1421766a8","src/date.rs":"09580a0a2008fad2ccbc43fb42a88f42221b98b01692702022a296dc9c86bf37","src/lib.rs":"760f0d29567ecaa61287088cf23cf74b3c0efbbcd3077cea5fb7c88359e96c7e","src/version.rs":"dba18a25983ec6e37b952f4cdc5219c9e5abba2c3a76cef87465e1fba6f8ac89"},"package":"49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"} \ No newline at end of file diff --git a/third_party/rust/version_check/Cargo.toml b/third_party/rust/version_check/Cargo.toml new file mode 100644 index 0000000000..39fedbd48b --- /dev/null +++ b/third_party/rust/version_check/Cargo.toml @@ -0,0 +1,24 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +name = "version_check" +version = "0.9.4" +authors = ["Sergio Benitez "] +exclude = ["static"] +description = "Tiny crate to check the version of the installed/running rustc." +documentation = "https://docs.rs/version_check/" +readme = "README.md" +keywords = ["version", "rustc", "minimum", "check"] +license = "MIT/Apache-2.0" +repository = "https://github.com/SergioBenitez/version_check" + +[dependencies] diff --git a/third_party/rust/version_check/LICENSE-APACHE b/third_party/rust/version_check/LICENSE-APACHE new file mode 100644 index 0000000000..16fe87b06e --- /dev/null +++ b/third_party/rust/version_check/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/third_party/rust/version_check/LICENSE-MIT b/third_party/rust/version_check/LICENSE-MIT new file mode 100644 index 0000000000..dfc0e73b19 --- /dev/null +++ b/third_party/rust/version_check/LICENSE-MIT @@ -0,0 +1,19 @@ +The MIT License (MIT) +Copyright (c) 2017-2018 Sergio Benitez + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/third_party/rust/version_check/README.md b/third_party/rust/version_check/README.md new file mode 100644 index 0000000000..8637d2ab1d --- /dev/null +++ b/third_party/rust/version_check/README.md @@ -0,0 +1,80 @@ +# version\_check + +[![Build Status](https://github.com/SergioBenitez/version_check/workflows/CI/badge.svg)](https://github.com/SergioBenitez/version_check/actions) +[![Current Crates.io Version](https://img.shields.io/crates/v/version_check.svg)](https://crates.io/crates/version_check) +[![rustdocs on docs.rs](https://docs.rs/version_check/badge.svg)](https://docs.rs/version_check) + +This tiny crate checks that the running or installed `rustc` meets some version +requirements. The version is queried by calling the Rust compiler with +`--version`. The path to the compiler is determined first via the `RUSTC` +environment variable. If it is not set, then `rustc` is used. If that fails, no +determination is made, and calls return `None`. + +## Usage + +Add to your `Cargo.toml` file, typically as a build dependency: + +```toml +[build-dependencies] +version_check = "0.9" +``` + +`version_check` is compatible and compiles with Rust 1.0.0 and beyond. + +## Examples + +Set a `cfg` flag in `build.rs` if the running compiler was determined to be +at least version `1.13.0`: + +```rust +extern crate version_check as rustc; + +if rustc::is_min_version("1.13.0").unwrap_or(false) { + println!("cargo:rustc-cfg=question_mark_operator"); +} +``` + +Check that the running compiler was released on or after `2018-12-18`: + +```rust +extern crate version_check as rustc; + +match rustc::is_min_date("2018-12-18") { + Some(true) => "Yep! It's recent!", + Some(false) => "No, it's older.", + None => "Couldn't determine the rustc version." +}; +``` + +Check that the running compiler supports feature flags: + +```rust +extern crate version_check as rustc; + +match rustc::is_feature_flaggable() { + Some(true) => "Yes! It's a dev or nightly release!", + Some(false) => "No, it's stable or beta.", + None => "Couldn't determine the rustc version." +}; +``` + +See the [rustdocs](https://docs.rs/version_check) for more examples and complete +documentation. + +## Alternatives + +This crate is dead simple with no dependencies. If you need something more and +don't care about panicking if the version cannot be obtained, or if you don't +mind adding dependencies, see [rustc_version]. If you'd instead prefer a feature +detection library that works by dynamically invoking `rustc` with a +representative code sample, see [autocfg]. + +[rustc_version]: https://crates.io/crates/rustc_version +[autocfg]: https://crates.io/crates/autocfg + +## License + +`version_check` is licensed under either of the following, at your option: + + * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) + * MIT License ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) diff --git a/third_party/rust/version_check/src/channel.rs b/third_party/rust/version_check/src/channel.rs new file mode 100644 index 0000000000..f84c508d16 --- /dev/null +++ b/third_party/rust/version_check/src/channel.rs @@ -0,0 +1,193 @@ +use std::fmt; + +#[derive(Debug, PartialEq, Eq, Copy, Clone)] +enum Kind { + Dev, + Nightly, + Beta, + Stable, +} + +/// Release channel: "dev", "nightly", "beta", or "stable". +#[derive(Debug, PartialEq, Eq, Copy, Clone)] +pub struct Channel(Kind); + +impl Channel { + /// Reads the release channel of the running compiler. If it cannot be + /// determined (see the [top-level documentation](crate)), returns `None`. + /// + /// # Example + /// + /// ```rust + /// use version_check::Channel; + /// + /// match Channel::read() { + /// Some(c) => format!("The channel is: {}", c), + /// None => format!("Failed to read the release channel.") + /// }; + /// ``` + pub fn read() -> Option { + ::get_version_and_date() + .and_then(|(version, _)| version) + .and_then(|version| Channel::parse(&version)) + } + + /// Parse a Rust release channel from a Rust release version string (of the + /// form `major[.minor[.patch[-channel]]]`). Returns `None` if `version` is + /// not a valid Rust version string. + /// + /// # Example + /// + /// ```rust + /// use version_check::Channel; + /// + /// let dev = Channel::parse("1.3.0-dev").unwrap(); + /// assert!(dev.is_dev()); + /// + /// let nightly = Channel::parse("1.42.2-nightly").unwrap(); + /// assert!(nightly.is_nightly()); + /// + /// let beta = Channel::parse("1.32.0-beta").unwrap(); + /// assert!(beta.is_beta()); + /// + /// let stable = Channel::parse("1.4.0").unwrap(); + /// assert!(stable.is_stable()); + /// ``` + pub fn parse(version: &str) -> Option { + let version = version.trim(); + if version.contains("-dev") || version == "dev" { + Some(Channel(Kind::Dev)) + } else if version.contains("-nightly") || version == "nightly" { + Some(Channel(Kind::Nightly)) + } else if version.contains("-beta") || version == "beta" { + Some(Channel(Kind::Beta)) + } else if !version.contains("-") { + Some(Channel(Kind::Stable)) + } else { + None + } + } + + /// Returns the name of the release channel. + fn as_str(&self) -> &'static str { + match self.0 { + Kind::Dev => "dev", + Kind::Beta => "beta", + Kind::Nightly => "nightly", + Kind::Stable => "stable", + } + } + + /// Returns `true` if this channel supports feature flags. In other words, + /// returns `true` if the channel is either `dev` or `nightly`. + /// + /// # Example + /// + /// ```rust + /// use version_check::Channel; + /// + /// let dev = Channel::parse("1.3.0-dev").unwrap(); + /// assert!(dev.supports_features()); + /// + /// let nightly = Channel::parse("1.42.2-nightly").unwrap(); + /// assert!(nightly.supports_features()); + /// + /// let beta = Channel::parse("1.32.0-beta").unwrap(); + /// assert!(!beta.supports_features()); + /// + /// let stable = Channel::parse("1.4.0").unwrap(); + /// assert!(!stable.supports_features()); + /// ``` + pub fn supports_features(&self) -> bool { + match self.0 { + Kind::Dev | Kind::Nightly => true, + Kind::Beta | Kind::Stable => false + } + } + + /// Returns `true` if this channel is `dev` and `false` otherwise. + /// + /// # Example + /// + /// ```rust + /// use version_check::Channel; + /// + /// let dev = Channel::parse("1.3.0-dev").unwrap(); + /// assert!(dev.is_dev()); + /// + /// let stable = Channel::parse("1.0.0").unwrap(); + /// assert!(!stable.is_dev()); + /// ``` + pub fn is_dev(&self) -> bool { + match self.0 { + Kind::Dev => true, + _ => false + } + } + + /// Returns `true` if this channel is `nightly` and `false` otherwise. + /// + /// # Example + /// + /// ```rust + /// use version_check::Channel; + /// + /// let nightly = Channel::parse("1.3.0-nightly").unwrap(); + /// assert!(nightly.is_nightly()); + /// + /// let stable = Channel::parse("1.0.0").unwrap(); + /// assert!(!stable.is_nightly()); + /// ``` + pub fn is_nightly(&self) -> bool { + match self.0 { + Kind::Nightly => true, + _ => false + } + } + + /// Returns `true` if this channel is `beta` and `false` otherwise. + /// + /// # Example + /// + /// ```rust + /// use version_check::Channel; + /// + /// let beta = Channel::parse("1.3.0-beta").unwrap(); + /// assert!(beta.is_beta()); + /// + /// let stable = Channel::parse("1.0.0").unwrap(); + /// assert!(!stable.is_beta()); + /// ``` + pub fn is_beta(&self) -> bool { + match self.0 { + Kind::Beta => true, + _ => false + } + } + + /// Returns `true` if this channel is `stable` and `false` otherwise. + /// + /// # Example + /// + /// ```rust + /// use version_check::Channel; + /// + /// let stable = Channel::parse("1.0.0").unwrap(); + /// assert!(stable.is_stable()); + /// + /// let beta = Channel::parse("1.3.0-beta").unwrap(); + /// assert!(!beta.is_stable()); + /// ``` + pub fn is_stable(&self) -> bool { + match self.0 { + Kind::Stable => true, + _ => false + } + } +} + +impl fmt::Display for Channel { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.as_str()) + } +} diff --git a/third_party/rust/version_check/src/date.rs b/third_party/rust/version_check/src/date.rs new file mode 100644 index 0000000000..de0b2d05d7 --- /dev/null +++ b/third_party/rust/version_check/src/date.rs @@ -0,0 +1,203 @@ +use std::fmt; + +/// Release date including year, month, and day. +// Internal storage is: y[31..9] | m[8..5] | d[5...0]. +#[derive(Debug, PartialEq, Eq, Copy, Clone, PartialOrd, Ord)] +pub struct Date(u32); + +impl Date { + /// Reads the release date of the running compiler. If it cannot be + /// determined (see the [top-level documentation](crate)), returns `None`. + /// + /// # Example + /// + /// ```rust + /// use version_check::Date; + /// + /// match Date::read() { + /// Some(d) => format!("The release date is: {}", d), + /// None => format!("Failed to read the release date.") + /// }; + /// ``` + pub fn read() -> Option { + ::get_version_and_date() + .and_then(|(_, date)| date) + .and_then(|date| Date::parse(&date)) + } + + /// Parse a release date of the form `%Y-%m-%d`. Returns `None` if `date` is + /// not in `%Y-%m-%d` format. + /// + /// # Example + /// + /// ```rust + /// use version_check::Date; + /// + /// let date = Date::parse("2016-04-20").unwrap(); + /// + /// assert!(date.at_least("2016-01-10")); + /// assert!(date.at_most("2016-04-20")); + /// assert!(date.exactly("2016-04-20")); + /// + /// assert!(Date::parse("2021-12-31").unwrap().exactly("2021-12-31")); + /// + /// assert!(Date::parse("March 13, 2018").is_none()); + /// assert!(Date::parse("1-2-3-4-5").is_none()); + /// assert!(Date::parse("2020-300-23120").is_none()); + /// assert!(Date::parse("2020-12-12 1").is_none()); + /// assert!(Date::parse("2020-10").is_none()); + /// assert!(Date::parse("2020").is_none()); + /// ``` + pub fn parse(date: &str) -> Option { + let mut ymd = [0u16; 3]; + for (i, split) in date.split('-').map(|s| s.parse::()).enumerate() { + ymd[i] = match (i, split) { + (3, _) | (_, Err(_)) => return None, + (_, Ok(v)) => v, + }; + } + + let (year, month, day) = (ymd[0], ymd[1], ymd[2]); + if year == 0 || month == 0 || month > 12 || day == 0 || day > 31 { + return None; + } + + Some(Date::from_ymd(year, month as u8, day as u8)) + } + + /// Creates a `Date` from `(year, month, day)` date components. + /// + /// Does not check the validity of `year`, `month`, or `day`, but `year` is + /// truncated to 23 bits (% 8,388,608), `month` to 4 bits (% 16), and `day` + /// to 5 bits (% 32). + /// + /// # Example + /// + /// ```rust + /// use version_check::Date; + /// + /// assert!(Date::from_ymd(2021, 7, 30).exactly("2021-07-30")); + /// assert!(Date::from_ymd(2010, 3, 23).exactly("2010-03-23")); + /// assert!(Date::from_ymd(2090, 1, 31).exactly("2090-01-31")); + /// + /// // Truncation: 33 % 32 == 0x21 & 0x1F == 1. + /// assert!(Date::from_ymd(2090, 1, 33).exactly("2090-01-01")); + /// ``` + pub fn from_ymd(year: u16, month: u8, day: u8) -> Date { + let year = (year as u32) << 9; + let month = ((month as u32) & 0xF) << 5; + let day = (day as u32) & 0x1F; + Date(year | month | day) + } + + /// Return the original (YYYY, MM, DD). + fn to_ymd(&self) -> (u16, u8, u8) { + let y = self.0 >> 9; + let m = (self.0 >> 5) & 0xF; + let d = self.0 & 0x1F; + (y as u16, m as u8, d as u8) + } + + /// Returns `true` if `self` occurs on or after `date`. + /// + /// If `date` occurs before `self`, or if `date` is not in `%Y-%m-%d` + /// format, returns `false`. + /// + /// # Example + /// + /// ```rust + /// use version_check::Date; + /// + /// let date = Date::parse("2020-01-01").unwrap(); + /// + /// assert!(date.at_least("2019-12-31")); + /// assert!(date.at_least("2020-01-01")); + /// assert!(date.at_least("2014-04-31")); + /// + /// assert!(!date.at_least("2020-01-02")); + /// assert!(!date.at_least("2024-08-18")); + /// ``` + pub fn at_least(&self, date: &str) -> bool { + Date::parse(date) + .map(|date| self >= &date) + .unwrap_or(false) + } + + /// Returns `true` if `self` occurs on or before `date`. + /// + /// If `date` occurs after `self`, or if `date` is not in `%Y-%m-%d` + /// format, returns `false`. + /// + /// # Example + /// + /// ```rust + /// use version_check::Date; + /// + /// let date = Date::parse("2020-01-01").unwrap(); + /// + /// assert!(date.at_most("2020-01-01")); + /// assert!(date.at_most("2020-01-02")); + /// assert!(date.at_most("2024-08-18")); + /// + /// assert!(!date.at_most("2019-12-31")); + /// assert!(!date.at_most("2014-04-31")); + /// ``` + pub fn at_most(&self, date: &str) -> bool { + Date::parse(date) + .map(|date| self <= &date) + .unwrap_or(false) + } + + /// Returns `true` if `self` occurs exactly on `date`. + /// + /// If `date` is not exactly `self`, or if `date` is not in `%Y-%m-%d` + /// format, returns `false`. + /// + /// # Example + /// + /// ```rust + /// use version_check::Date; + /// + /// let date = Date::parse("2020-01-01").unwrap(); + /// + /// assert!(date.exactly("2020-01-01")); + /// + /// assert!(!date.exactly("2019-12-31")); + /// assert!(!date.exactly("2014-04-31")); + /// assert!(!date.exactly("2020-01-02")); + /// assert!(!date.exactly("2024-08-18")); + /// ``` + pub fn exactly(&self, date: &str) -> bool { + Date::parse(date) + .map(|date| self == &date) + .unwrap_or(false) + } +} + +impl fmt::Display for Date { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let (y, m, d) = self.to_ymd(); + write!(f, "{}-{:02}-{:02}", y, m, d) + } +} + +#[cfg(test)] +mod tests { + use super::Date; + + macro_rules! reflexive_display { + ($string:expr) => ( + assert_eq!(Date::parse($string).unwrap().to_string(), $string); + ) + } + + #[test] + fn display() { + reflexive_display!("2019-05-08"); + reflexive_display!("2000-01-01"); + reflexive_display!("2000-12-31"); + reflexive_display!("2090-12-31"); + reflexive_display!("1999-02-19"); + reflexive_display!("9999-12-31"); + } +} diff --git a/third_party/rust/version_check/src/lib.rs b/third_party/rust/version_check/src/lib.rs new file mode 100644 index 0000000000..6c16074822 --- /dev/null +++ b/third_party/rust/version_check/src/lib.rs @@ -0,0 +1,493 @@ +//! This tiny crate checks that the running or installed `rustc` meets some +//! version requirements. The version is queried by calling the Rust compiler +//! with `--version`. The path to the compiler is determined first via the +//! `RUSTC` environment variable. If it is not set, then `rustc` is used. If +//! that fails, no determination is made, and calls return `None`. +//! +//! # Examples +//! +//! * Set a `cfg` flag in `build.rs` if the running compiler was determined to +//! be at least version `1.13.0`: +//! +//! ```rust +//! extern crate version_check as rustc; +//! +//! if rustc::is_min_version("1.13.0").unwrap_or(false) { +//! println!("cargo:rustc-cfg=question_mark_operator"); +//! } +//! ``` +//! +//! See [`is_max_version`] or [`is_exact_version`] to check if the compiler +//! is _at most_ or _exactly_ a certain version. +//! +//! * Check that the running compiler was released on or after `2018-12-18`: +//! +//! ```rust +//! extern crate version_check as rustc; +//! +//! match rustc::is_min_date("2018-12-18") { +//! Some(true) => "Yep! It's recent!", +//! Some(false) => "No, it's older.", +//! None => "Couldn't determine the rustc version." +//! }; +//! ``` +//! +//! See [`is_max_date`] or [`is_exact_date`] to check if the compiler was +//! released _prior to_ or _exactly on_ a certain date. +//! +//! * Check that the running compiler supports feature flags: +//! +//! ```rust +//! extern crate version_check as rustc; +//! +//! match rustc::is_feature_flaggable() { +//! Some(true) => "Yes! It's a dev or nightly release!", +//! Some(false) => "No, it's stable or beta.", +//! None => "Couldn't determine the rustc version." +//! }; +//! ``` +//! +//! * Check that the running compiler supports a specific feature: +//! +//! ```rust +//! extern crate version_check as rustc; +//! +//! if let Some(true) = rustc::supports_feature("doc_cfg") { +//! println!("cargo:rustc-cfg=has_doc_cfg"); +//! } +//! ``` +//! +//! * Check that the running compiler is on the stable channel: +//! +//! ```rust +//! extern crate version_check as rustc; +//! +//! match rustc::Channel::read() { +//! Some(c) if c.is_stable() => format!("Yes! It's stable."), +//! Some(c) => format!("No, the channel {} is not stable.", c), +//! None => format!("Couldn't determine the rustc version.") +//! }; +//! ``` +//! +//! To interact with the version, release date, and release channel as structs, +//! use [`Version`], [`Date`], and [`Channel`], respectively. The [`triple()`] +//! function returns all three values efficiently. +//! +//! # Alternatives +//! +//! This crate is dead simple with no dependencies. If you need something more +//! and don't care about panicking if the version cannot be obtained, or if you +//! don't mind adding dependencies, see +//! [rustc_version](https://crates.io/crates/rustc_version). + +#![allow(deprecated)] + +mod version; +mod channel; +mod date; + +use std::env; +use std::process::Command; + +#[doc(inline)] pub use version::*; +#[doc(inline)] pub use channel::*; +#[doc(inline)] pub use date::*; + +/// Parses (version, date) as available from rustc version string. +fn version_and_date_from_rustc_version(s: &str) -> (Option, Option) { + let last_line = s.lines().last().unwrap_or(s); + let mut components = last_line.trim().split(" "); + let version = components.nth(1); + let date = components.filter(|c| c.ends_with(')')).next() + .map(|s| s.trim_right().trim_right_matches(")").trim_left().trim_left_matches('(')); + (version.map(|s| s.to_string()), date.map(|s| s.to_string())) +} + +/// Parses (version, date) as available from rustc verbose version output. +fn version_and_date_from_rustc_verbose_version(s: &str) -> (Option, Option) { + let (mut version, mut date) = (None, None); + for line in s.lines() { + let split = |s: &str| s.splitn(2, ":").nth(1).map(|s| s.trim().to_string()); + match line.trim().split(" ").nth(0) { + Some("rustc") => { + let (v, d) = version_and_date_from_rustc_version(line); + version = version.or(v); + date = date.or(d); + }, + Some("release:") => version = split(line), + Some("commit-date:") if line.ends_with("unknown") => date = None, + Some("commit-date:") => date = split(line), + _ => continue + } + } + + (version, date) +} + +/// Returns (version, date) as available from `rustc --version`. +fn get_version_and_date() -> Option<(Option, Option)> { + let rustc = env::var("RUSTC").unwrap_or_else(|_| "rustc".to_string()); + Command::new(rustc).arg("--verbose").arg("--version").output().ok() + .and_then(|output| String::from_utf8(output.stdout).ok()) + .map(|s| version_and_date_from_rustc_verbose_version(&s)) +} + +/// Reads the triple of [`Version`], [`Channel`], and [`Date`] of the installed +/// or running `rustc`. +/// +/// If any attribute cannot be determined (see the [top-level +/// documentation](crate)), returns `None`. +/// +/// To obtain only one of three attributes, use [`Version::read()`], +/// [`Channel::read()`], or [`Date::read()`]. +pub fn triple() -> Option<(Version, Channel, Date)> { + let (version_str, date_str) = match get_version_and_date() { + Some((Some(version), Some(date))) => (version, date), + _ => return None + }; + + // Can't use `?` or `try!` for `Option` in 1.0.0. + match Version::parse(&version_str) { + Some(version) => match Channel::parse(&version_str) { + Some(channel) => match Date::parse(&date_str) { + Some(date) => Some((version, channel, date)), + _ => None, + }, + _ => None, + }, + _ => None + } +} + +/// Checks that the running or installed `rustc` was released **on or after** +/// some date. +/// +/// The format of `min_date` must be YYYY-MM-DD. For instance: `2016-12-20` or +/// `2017-01-09`. +/// +/// If the date cannot be retrieved or parsed, or if `min_date` could not be +/// parsed, returns `None`. Otherwise returns `true` if the installed `rustc` +/// was release on or after `min_date` and `false` otherwise. +pub fn is_min_date(min_date: &str) -> Option { + match (Date::read(), Date::parse(min_date)) { + (Some(rustc_date), Some(min_date)) => Some(rustc_date >= min_date), + _ => None + } +} + +/// Checks that the running or installed `rustc` was released **on or before** +/// some date. +/// +/// The format of `max_date` must be YYYY-MM-DD. For instance: `2016-12-20` or +/// `2017-01-09`. +/// +/// If the date cannot be retrieved or parsed, or if `max_date` could not be +/// parsed, returns `None`. Otherwise returns `true` if the installed `rustc` +/// was release on or before `max_date` and `false` otherwise. +pub fn is_max_date(max_date: &str) -> Option { + match (Date::read(), Date::parse(max_date)) { + (Some(rustc_date), Some(max_date)) => Some(rustc_date <= max_date), + _ => None + } +} + +/// Checks that the running or installed `rustc` was released **exactly** on +/// some date. +/// +/// The format of `date` must be YYYY-MM-DD. For instance: `2016-12-20` or +/// `2017-01-09`. +/// +/// If the date cannot be retrieved or parsed, or if `date` could not be parsed, +/// returns `None`. Otherwise returns `true` if the installed `rustc` was +/// release on `date` and `false` otherwise. +pub fn is_exact_date(date: &str) -> Option { + match (Date::read(), Date::parse(date)) { + (Some(rustc_date), Some(date)) => Some(rustc_date == date), + _ => None + } +} + +/// Checks that the running or installed `rustc` is **at least** some minimum +/// version. +/// +/// The format of `min_version` is a semantic version: `1.3.0`, `1.15.0-beta`, +/// `1.14.0`, `1.16.0-nightly`, etc. +/// +/// If the version cannot be retrieved or parsed, or if `min_version` could not +/// be parsed, returns `None`. Otherwise returns `true` if the installed `rustc` +/// is at least `min_version` and `false` otherwise. +pub fn is_min_version(min_version: &str) -> Option { + match (Version::read(), Version::parse(min_version)) { + (Some(rustc_ver), Some(min_ver)) => Some(rustc_ver >= min_ver), + _ => None + } +} + +/// Checks that the running or installed `rustc` is **at most** some maximum +/// version. +/// +/// The format of `max_version` is a semantic version: `1.3.0`, `1.15.0-beta`, +/// `1.14.0`, `1.16.0-nightly`, etc. +/// +/// If the version cannot be retrieved or parsed, or if `max_version` could not +/// be parsed, returns `None`. Otherwise returns `true` if the installed `rustc` +/// is at most `max_version` and `false` otherwise. +pub fn is_max_version(max_version: &str) -> Option { + match (Version::read(), Version::parse(max_version)) { + (Some(rustc_ver), Some(max_ver)) => Some(rustc_ver <= max_ver), + _ => None + } +} + +/// Checks that the running or installed `rustc` is **exactly** some version. +/// +/// The format of `version` is a semantic version: `1.3.0`, `1.15.0-beta`, +/// `1.14.0`, `1.16.0-nightly`, etc. +/// +/// If the version cannot be retrieved or parsed, or if `version` could not be +/// parsed, returns `None`. Otherwise returns `true` if the installed `rustc` is +/// exactly `version` and `false` otherwise. +pub fn is_exact_version(version: &str) -> Option { + match (Version::read(), Version::parse(version)) { + (Some(rustc_ver), Some(version)) => Some(rustc_ver == version), + _ => None + } +} + +/// Checks whether the running or installed `rustc` supports feature flags. +/// +/// In other words, if the channel is either "nightly" or "dev". +/// +/// Note that support for specific `rustc` features can be enabled or disabled +/// via the `allow-features` compiler flag, which this function _does not_ +/// check. That is, this function _does not_ check whether a _specific_ feature +/// is supported, but instead whether features are supported at all. To check +/// for support for a specific feature, use [`supports_feature()`]. +/// +/// If the version could not be determined, returns `None`. Otherwise returns +/// `true` if the running version supports feature flags and `false` otherwise. +pub fn is_feature_flaggable() -> Option { + Channel::read().map(|c| c.supports_features()) +} + +/// Checks whether the running or installed `rustc` supports `feature`. +/// +/// Returns _true_ _iff_ [`is_feature_flaggable()`] returns `true` _and_ the +/// feature is not disabled via exclusion in `allow-features` via `RUSTFLAGS` or +/// `CARGO_ENCODED_RUSTFLAGS`. If the version could not be determined, returns +/// `None`. +/// +/// # Example +/// +/// ```rust +/// use version_check as rustc; +/// +/// if let Some(true) = rustc::supports_feature("doc_cfg") { +/// println!("cargo:rustc-cfg=has_doc_cfg"); +/// } +/// ``` +pub fn supports_feature(feature: &str) -> Option { + match is_feature_flaggable() { + Some(true) => { /* continue */ } + Some(false) => return Some(false), + None => return None, + } + + let env_flags = env::var_os("CARGO_ENCODED_RUSTFLAGS") + .map(|flags| (flags, '\x1f')) + .or_else(|| env::var_os("RUSTFLAGS").map(|flags| (flags, ' '))); + + if let Some((flags, delim)) = env_flags { + const ALLOW_FEATURES: &'static str = "allow-features="; + + let rustflags = flags.to_string_lossy(); + let allow_features = rustflags.split(delim) + .map(|flag| flag.trim_left_matches("-Z").trim()) + .filter(|flag| flag.starts_with(ALLOW_FEATURES)) + .map(|flag| &flag[ALLOW_FEATURES.len()..]); + + if let Some(allow_features) = allow_features.last() { + return Some(allow_features.split(',').any(|f| f.trim() == feature)); + } + } + + // If there are no `RUSTFLAGS` or `CARGO_ENCODED_RUSTFLAGS` or they don't + // contain an `allow-features` flag, assume compiler allows all features. + Some(true) +} + +#[cfg(test)] +mod tests { + use std::{env, fs}; + + use super::version_and_date_from_rustc_version; + use super::version_and_date_from_rustc_verbose_version; + + macro_rules! check_parse { + (@ $f:expr, $s:expr => $v:expr, $d:expr) => ({ + if let (Some(v), d) = $f(&$s) { + let e_d: Option<&str> = $d.into(); + assert_eq!((v, d), ($v.to_string(), e_d.map(|s| s.into()))); + } else { + panic!("{:?} didn't parse for version testing.", $s); + } + }); + ($f:expr, $s:expr => $v:expr, $d:expr) => ({ + let warn = "warning: invalid logging spec 'warning', ignoring it"; + let warn2 = "warning: sorry, something went wrong :(sad)"; + check_parse!(@ $f, $s => $v, $d); + check_parse!(@ $f, &format!("{}\n{}", warn, $s) => $v, $d); + check_parse!(@ $f, &format!("{}\n{}", warn2, $s) => $v, $d); + check_parse!(@ $f, &format!("{}\n{}\n{}", warn, warn2, $s) => $v, $d); + check_parse!(@ $f, &format!("{}\n{}\n{}", warn2, warn, $s) => $v, $d); + }) + } + + macro_rules! check_terse_parse { + ($($s:expr => $v:expr, $d:expr,)+) => {$( + check_parse!(version_and_date_from_rustc_version, $s => $v, $d); + )+} + } + + macro_rules! check_verbose_parse { + ($($s:expr => $v:expr, $d:expr,)+) => {$( + check_parse!(version_and_date_from_rustc_verbose_version, $s => $v, $d); + )+} + } + + #[test] + fn test_version_parse() { + check_terse_parse! { + "rustc 1.18.0" => "1.18.0", None, + "rustc 1.8.0" => "1.8.0", None, + "rustc 1.20.0-nightly" => "1.20.0-nightly", None, + "rustc 1.20" => "1.20", None, + "rustc 1.3" => "1.3", None, + "rustc 1" => "1", None, + "rustc 1.5.1-beta" => "1.5.1-beta", None, + "rustc 1.20.0 (2017-07-09)" => "1.20.0", Some("2017-07-09"), + "rustc 1.20.0-dev (2017-07-09)" => "1.20.0-dev", Some("2017-07-09"), + "rustc 1.20.0-nightly (d84693b93 2017-07-09)" => "1.20.0-nightly", Some("2017-07-09"), + "rustc 1.20.0 (d84693b93 2017-07-09)" => "1.20.0", Some("2017-07-09"), + "rustc 1.30.0-nightly (3bc2ca7e4 2018-09-20)" => "1.30.0-nightly", Some("2018-09-20"), + }; + } + + #[test] + fn test_verbose_version_parse() { + check_verbose_parse! { + "rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)\n\ + binary: rustc\n\ + commit-hash: a59de37e99060162a2674e3ff45409ac73595c0e\n\ + commit-date: 2015-05-13\n\ + build-date: 2015-05-14\n\ + host: x86_64-unknown-linux-gnu\n\ + release: 1.0.0" => "1.0.0", Some("2015-05-13"), + + "rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)\n\ + commit-hash: a59de37e99060162a2674e3ff45409ac73595c0e\n\ + commit-date: 2015-05-13\n\ + build-date: 2015-05-14\n\ + host: x86_64-unknown-linux-gnu\n\ + release: 1.0.0" => "1.0.0", Some("2015-05-13"), + + "rustc 1.50.0 (cb75ad5db 2021-02-10)\n\ + binary: rustc\n\ + commit-hash: cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\n\ + commit-date: 2021-02-10\n\ + host: x86_64-unknown-linux-gnu\n\ + release: 1.50.0" => "1.50.0", Some("2021-02-10"), + + "rustc 1.52.0-nightly (234781afe 2021-03-07)\n\ + binary: rustc\n\ + commit-hash: 234781afe33d3f339b002f85f948046d8476cfc9\n\ + commit-date: 2021-03-07\n\ + host: x86_64-unknown-linux-gnu\n\ + release: 1.52.0-nightly\n\ + LLVM version: 12.0.0" => "1.52.0-nightly", Some("2021-03-07"), + + "rustc 1.41.1\n\ + binary: rustc\n\ + commit-hash: unknown\n\ + commit-date: unknown\n\ + host: x86_64-unknown-linux-gnu\n\ + release: 1.41.1\n\ + LLVM version: 7.0" => "1.41.1", None, + + "rustc 1.49.0\n\ + binary: rustc\n\ + commit-hash: unknown\n\ + commit-date: unknown\n\ + host: x86_64-unknown-linux-gnu\n\ + release: 1.49.0" => "1.49.0", None, + + "rustc 1.50.0 (Fedora 1.50.0-1.fc33)\n\ + binary: rustc\n\ + commit-hash: unknown\n\ + commit-date: unknown\n\ + host: x86_64-unknown-linux-gnu\n\ + release: 1.50.0" => "1.50.0", None, + }; + } + + fn read_static(verbose: bool, channel: &str, minor: usize) -> String { + use std::fs::File; + use std::path::Path; + use std::io::{BufReader, Read}; + + let subdir = if verbose { "verbose" } else { "terse" }; + let path = Path::new(STATIC_PATH) + .join(channel) + .join(subdir) + .join(format!("rustc-1.{}.0", minor)); + + let file = File::open(path).unwrap(); + let mut buf_reader = BufReader::new(file); + let mut contents = String::new(); + buf_reader.read_to_string(&mut contents).unwrap(); + contents + } + + static STATIC_PATH: &'static str = concat!(env!("CARGO_MANIFEST_DIR"), "/static"); + + static DATES: [&'static str; 51] = [ + "2015-05-13", "2015-06-19", "2015-08-03", "2015-09-15", "2015-10-27", + "2015-12-04", "2016-01-19", "2016-02-29", "2016-04-11", "2016-05-18", + "2016-07-03", "2016-08-15", "2016-09-23", "2016-11-07", "2016-12-16", + "2017-01-19", "2017-03-10", "2017-04-24", "2017-06-06", "2017-07-17", + "2017-08-27", "2017-10-09", "2017-11-20", "2018-01-01", "2018-02-12", + "2018-03-25", "2018-05-07", "2018-06-19", "2018-07-30", "2018-09-11", + "2018-10-24", "2018-12-04", "2019-01-16", "2019-02-28", "2019-04-10", + "2019-05-20", "2019-07-03", "2019-08-13", "2019-09-23", "2019-11-04", + "2019-12-16", "2020-01-27", "2020-03-09", "2020-04-20", "2020-06-01", + "2020-07-13", "2020-08-24", "2020-10-07", "2020-11-16", "2020-12-29", + "2021-02-10", + ]; + + #[test] + fn test_stable_compatibility() { + if env::var_os("FORCE_STATIC").is_none() && fs::metadata(STATIC_PATH).is_err() { + // We exclude `/static` when we package `version_check`, so don't + // run if static files aren't present unless we know they should be. + return; + } + + // Ensure we can parse all output from all Linux stable releases. + for v in 0..DATES.len() { + let (version, date) = (&format!("1.{}.0", v), Some(DATES[v])); + check_terse_parse!(read_static(false, "stable", v) => version, date,); + check_verbose_parse!(read_static(true, "stable", v) => version, date,); + } + } + + #[test] + fn test_parse_current() { + let (version, channel) = (::Version::read(), ::Channel::read()); + assert!(version.is_some()); + assert!(channel.is_some()); + + if let Ok(known_channel) = env::var("KNOWN_CHANNEL") { + assert_eq!(channel, ::Channel::parse(&known_channel)); + } + } +} diff --git a/third_party/rust/version_check/src/version.rs b/third_party/rust/version_check/src/version.rs new file mode 100644 index 0000000000..2bc18aae2b --- /dev/null +++ b/third_party/rust/version_check/src/version.rs @@ -0,0 +1,316 @@ +use std::fmt; + +/// Version number: `major.minor.patch`, ignoring release channel. +#[derive(PartialEq, Eq, Copy, Clone, PartialOrd, Ord)] +pub struct Version(u64); + +impl Version { + /// Reads the version of the running compiler. If it cannot be determined + /// (see the [top-level documentation](crate)), returns `None`. + /// + /// # Example + /// + /// ```rust + /// use version_check::Version; + /// + /// match Version::read() { + /// Some(d) => format!("Version is: {}", d), + /// None => format!("Failed to read the version.") + /// }; + /// ``` + pub fn read() -> Option { + ::get_version_and_date() + .and_then(|(version, _)| version) + .and_then(|version| Version::parse(&version)) + } + + + /// Parse a Rust release version (of the form + /// `major[.minor[.patch[-channel]]]`), ignoring the release channel, if + /// any. Returns `None` if `version` is not a valid Rust version string. + /// + /// # Example + /// + /// ```rust + /// use version_check::Version; + /// + /// let version = Version::parse("1.18.0").unwrap(); + /// assert!(version.exactly("1.18.0")); + /// + /// let version = Version::parse("1.20.0-nightly").unwrap(); + /// assert!(version.exactly("1.20.0")); + /// assert!(version.exactly("1.20.0-beta")); + /// + /// let version = Version::parse("1.3").unwrap(); + /// assert!(version.exactly("1.3.0")); + /// + /// let version = Version::parse("1").unwrap(); + /// assert!(version.exactly("1.0.0")); + /// + /// assert!(Version::parse("one.two.three").is_none()); + /// assert!(Version::parse("1.65536.2").is_none()); + /// assert!(Version::parse("1. 2").is_none()); + /// assert!(Version::parse("").is_none()); + /// assert!(Version::parse("1.").is_none()); + /// assert!(Version::parse("1.2.3.4").is_none()); + /// ``` + pub fn parse(version: &str) -> Option { + let splits = version.split('-') + .nth(0) + .unwrap_or("") + .split('.') + .map(|s| s.parse::()); + + let mut mmp = [0u16; 3]; + for (i, split) in splits.enumerate() { + mmp[i] = match (i, split) { + (3, _) | (_, Err(_)) => return None, + (_, Ok(v)) => v, + }; + } + + let (maj, min, patch) = (mmp[0], mmp[1], mmp[2]); + Some(Version::from_mmp(maj, min, patch)) + } + + /// Creates a `Version` from `(major, minor, patch)` version components. + /// + /// # Example + /// + /// ```rust + /// use version_check::Version; + /// + /// assert!(Version::from_mmp(1, 35, 0).exactly("1.35.0")); + /// assert!(Version::from_mmp(1, 33, 0).exactly("1.33.0")); + /// assert!(Version::from_mmp(1, 35, 1).exactly("1.35.1")); + /// assert!(Version::from_mmp(1, 13, 2).exactly("1.13.2")); + /// ``` + pub fn from_mmp(major: u16, minor: u16, patch: u16) -> Version { + Version(((major as u64) << 32) | ((minor as u64) << 16) | patch as u64) + } + + /// Returns the `(major, minor, patch)` version components of `self`. + /// + /// # Example + /// + /// ```rust + /// use version_check::Version; + /// + /// assert_eq!(Version::parse("1.35.0").unwrap().to_mmp(), (1, 35, 0)); + /// assert_eq!(Version::parse("1.33.0").unwrap().to_mmp(), (1, 33, 0)); + /// assert_eq!(Version::parse("1.35.1").unwrap().to_mmp(), (1, 35, 1)); + /// assert_eq!(Version::parse("1.13.2").unwrap().to_mmp(), (1, 13, 2)); + /// ``` + pub fn to_mmp(&self) -> (u16, u16, u16) { + let major = self.0 >> 32; + let minor = self.0 >> 16; + let patch = self.0; + (major as u16, minor as u16, patch as u16) + } + + /// Returns `true` if `self` is greater than or equal to `version`. + /// + /// If `version` is greater than `self`, or if `version` is not a valid Rust + /// version string, returns `false`. + /// + /// # Example + /// + /// ```rust + /// use version_check::Version; + /// + /// let version = Version::parse("1.35.0").unwrap(); + /// + /// assert!(version.at_least("1.33.0")); + /// assert!(version.at_least("1.35.0")); + /// assert!(version.at_least("1.13.2")); + /// + /// assert!(!version.at_least("1.35.1")); + /// assert!(!version.at_least("1.55.0")); + /// + /// let version = Version::parse("1.12.5").unwrap(); + /// + /// assert!(version.at_least("1.12.0")); + /// assert!(!version.at_least("1.35.0")); + /// ``` + pub fn at_least(&self, version: &str) -> bool { + Version::parse(version) + .map(|version| self >= &version) + .unwrap_or(false) + } + + /// Returns `true` if `self` is less than or equal to `version`. + /// + /// If `version` is less than `self`, or if `version` is not a valid Rust + /// version string, returns `false`. + /// + /// # Example + /// + /// ```rust + /// use version_check::Version; + /// + /// let version = Version::parse("1.35.0").unwrap(); + /// + /// assert!(version.at_most("1.35.1")); + /// assert!(version.at_most("1.55.0")); + /// assert!(version.at_most("1.35.0")); + /// + /// assert!(!version.at_most("1.33.0")); + /// assert!(!version.at_most("1.13.2")); + /// ``` + pub fn at_most(&self, version: &str) -> bool { + Version::parse(version) + .map(|version| self <= &version) + .unwrap_or(false) + } + + /// Returns `true` if `self` is exactly equal to `version`. + /// + /// If `version` is not equal to `self`, or if `version` is not a valid Rust + /// version string, returns `false`. + /// + /// # Example + /// + /// ```rust + /// use version_check::Version; + /// + /// let version = Version::parse("1.35.0").unwrap(); + /// + /// assert!(version.exactly("1.35.0")); + /// + /// assert!(!version.exactly("1.33.0")); + /// assert!(!version.exactly("1.35.1")); + /// assert!(!version.exactly("1.13.2")); + /// ``` + pub fn exactly(&self, version: &str) -> bool { + Version::parse(version) + .map(|version| self == &version) + .unwrap_or(false) + } +} + +impl fmt::Display for Version { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let (major, minor, patch) = self.to_mmp(); + write!(f, "{}.{}.{}", major, minor, patch) + } +} + +impl fmt::Debug for Version { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + // We don't use `debug_*` because it's not available in `1.0.0`. + write!(f, "Version({:?}, {:?})", self.0, self.to_mmp()) + } +} + +#[cfg(test)] +mod tests { + use super::Version; + + macro_rules! assert_to_mmp { + // We don't use `.into::>` because it's not available in 1.0. + // We don't use the message part of `assert!` for the same reason. + ($s:expr, None) => ( + assert_eq!(Version::parse($s), None); + ); + ($s:expr, $mmp:expr) => ( + assert_eq!(Version::parse($s).map(|v| v.to_mmp()), Some($mmp)); + ) + } + + macro_rules! assert_from_mmp { + (($x:expr, $y:expr, $z:expr) => $s:expr) => { + assert_eq!(Some(Version::from_mmp($x, $y, $z)), Version::parse($s)); + }; + } + + #[test] + fn test_str_to_mmp() { + assert_to_mmp!("1", (1, 0, 0)); + assert_to_mmp!("1.2", (1, 2, 0)); + assert_to_mmp!("1.18.0", (1, 18, 0)); + assert_to_mmp!("3.19.0", (3, 19, 0)); + assert_to_mmp!("1.19.0-nightly", (1, 19, 0)); + assert_to_mmp!("1.12.2349", (1, 12, 2349)); + assert_to_mmp!("0.12", (0, 12, 0)); + assert_to_mmp!("1.12.5", (1, 12, 5)); + assert_to_mmp!("1.12", (1, 12, 0)); + assert_to_mmp!("1", (1, 0, 0)); + assert_to_mmp!("1.4.4-nightly (d84693b93 2017-07-09))", (1, 4, 4)); + assert_to_mmp!("1.58879.4478-dev", (1, 58879, 4478)); + assert_to_mmp!("1.58879.4478-dev (d84693b93 2017-07-09))", (1, 58879, 4478)); + } + + #[test] + fn test_malformed() { + assert_to_mmp!("1.65536.2", None); + assert_to_mmp!("-1.2.3", None); + assert_to_mmp!("1. 2", None); + assert_to_mmp!("", None); + assert_to_mmp!(" ", None); + assert_to_mmp!(".", None); + assert_to_mmp!("one", None); + assert_to_mmp!("1.", None); + assert_to_mmp!("1.2.3.4.5.6", None); + } + + #[test] + fn test_from_mmp() { + assert_from_mmp!((1, 18, 0) => "1.18.0"); + assert_from_mmp!((3, 19, 0) => "3.19.0"); + assert_from_mmp!((1, 19, 0) => "1.19.0"); + assert_from_mmp!((1, 12, 2349) => "1.12.2349"); + assert_from_mmp!((0, 12, 0) => "0.12"); + assert_from_mmp!((1, 12, 5) => "1.12.5"); + assert_from_mmp!((1, 12, 0) => "1.12"); + assert_from_mmp!((1, 0, 0) => "1"); + assert_from_mmp!((1, 4, 4) => "1.4.4"); + assert_from_mmp!((1, 58879, 4478) => "1.58879.4478"); + } + + #[test] + fn test_comparisons() { + let version = Version::parse("1.18.0").unwrap(); + assert!(version.exactly("1.18.0")); + assert!(version.at_least("1.12.0")); + assert!(version.at_least("1.12")); + assert!(version.at_least("1")); + assert!(version.at_most("1.18.1")); + assert!(!version.exactly("1.19.0")); + assert!(!version.exactly("1.18.1")); + + let version = Version::parse("1.20.0-nightly").unwrap(); + assert!(version.exactly("1.20.0-beta")); + assert!(version.exactly("1.20.0-nightly")); + assert!(version.exactly("1.20.0")); + assert!(!version.exactly("1.19")); + + let version = Version::parse("1.3").unwrap(); + assert!(version.exactly("1.3.0")); + assert!(version.exactly("1.3.0-stable")); + assert!(version.exactly("1.3")); + assert!(!version.exactly("1.5.0-stable")); + + let version = Version::parse("1").unwrap(); + assert!(version.exactly("1.0.0")); + assert!(version.exactly("1.0")); + assert!(version.exactly("1")); + + assert!(Version::parse("one.two.three").is_none()); + } + + macro_rules! reflexive_display { + ($s:expr) => ( + assert_eq!(Version::parse($s).unwrap().to_string(), $s); + ) + } + + #[test] + fn display() { + reflexive_display!("1.0.0"); + reflexive_display!("1.2.3"); + reflexive_display!("1.12.1438"); + reflexive_display!("1.44.0"); + reflexive_display!("2.44.0"); + reflexive_display!("23459.28923.3483"); + } +} -- cgit v1.2.3