├── .github ├── FUNDING.yml ├── dependabot.yml └── workflows │ └── rust.yml ├── .gitignore ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── deny.toml ├── src └── lib.rs └── tests └── all.rs /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [djc] 2 | patreon: dochtman 3 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: cargo 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | time: "04:00" 8 | open-pull-requests-limit: 10 9 | ignore: 10 | - dependency-name: semver 11 | versions: 12 | - "> 1.0, < 2" 13 | -------------------------------------------------------------------------------- /.github/workflows/rust.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: ['master'] 6 | pull_request: 7 | 8 | jobs: 9 | test: 10 | strategy: 11 | matrix: 12 | os: [ubuntu-latest, macos-latest, windows-latest] 13 | rust: [stable, beta, nightly, 1.32.0] 14 | exclude: 15 | - os: macos-latest 16 | rust: beta 17 | - os: windows-latest 18 | rust: beta 19 | - os: macos-latest 20 | rust: nightly 21 | - os: windows-latest 22 | rust: nightly 23 | - os: macos-latest 24 | rust: 1.32.0 25 | - os: windows-latest 26 | rust: 1.32.0 27 | 28 | runs-on: ${{ matrix.os }} 29 | 30 | steps: 31 | - uses: actions/checkout@v2 32 | - uses: actions-rs/toolchain@v1 33 | with: 34 | profile: minimal 35 | toolchain: ${{ matrix.rust }} 36 | override: true 37 | - uses: actions-rs/cargo@v1 38 | with: 39 | command: build 40 | args: --all-features --all-targets 41 | - uses: actions-rs/cargo@v1 42 | with: 43 | command: test 44 | args: --all-features 45 | 46 | lint: 47 | runs-on: ubuntu-latest 48 | steps: 49 | - uses: actions/checkout@v2 50 | - uses: actions-rs/toolchain@v1 51 | with: 52 | profile: minimal 53 | toolchain: stable 54 | override: true 55 | components: rustfmt, clippy 56 | - uses: actions-rs/cargo@v1 57 | with: 58 | command: fmt 59 | args: --all -- --check 60 | - uses: actions-rs/cargo@v1 61 | if: always() 62 | with: 63 | command: clippy 64 | args: --all-targets --all-features -- -D warnings 65 | 66 | audit: 67 | runs-on: ubuntu-latest 68 | steps: 69 | - uses: actions/checkout@v2 70 | - uses: EmbarkStudios/cargo-deny-action@v1 71 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | Cargo.lock 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rustc_version" 3 | version = "0.4.1" 4 | rust-version = "1.32" 5 | license = "MIT OR Apache-2.0" 6 | description = "A library for querying the version of a installed rustc compiler" 7 | readme = "README.md" 8 | documentation = "https://docs.rs/rustc_version/" 9 | repository = "https://github.com/djc/rustc-version-rs" 10 | keywords = ["version", "rustc"] 11 | edition = "2018" 12 | 13 | [dependencies] 14 | semver = "1.0" 15 | 16 | [dev-dependencies] 17 | doc-comment = "0.3" 18 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 The Rust Project Developers 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | rustc-version-rs 2 | ================ 3 | 4 | [![Documentation](https://docs.rs/rustc_version/badge.svg)](https://docs.rs/rustc_version/) 5 | [![Crates.io](https://img.shields.io/crates/v/rustc_version.svg)](https://crates.io/crates/rustc_version) 6 | [![Build status](https://github.com/djc/rustc-version-rs/workflows/CI/badge.svg)](https://github.com/djc/rustc-version-rs/actions?query=workflow%3ACI) 7 | 8 | A library for querying the version of a `rustc` compiler. 9 | 10 | This can be used by build scripts or other tools dealing with Rust sources 11 | to make decisions based on the version of the compiler. Current MSRV is 1.32.0. 12 | 13 | If this is of interest, also consider looking at these other crates: 14 | 15 | * [autocfg](https://crates.io/crates/autocfg/), which helps with feature detection instead of depending on compiler versions 16 | * [rustversion](https://github.com/dtolnay/rustversion) provides a procedural macro with no other dependencies 17 | 18 | # Getting Started 19 | 20 | [rustc-version-rs is available on crates.io](https://crates.io/crates/rustc_version). 21 | It is recommended to look there for the newest released version, as well as links to the newest builds of the docs. 22 | 23 | At the point of the last update of this README, the latest published version could be used like this: 24 | 25 | Add the following dependency to your Cargo manifest... 26 | 27 | ```toml 28 | [build-dependencies] 29 | rustc_version = "0.2" 30 | ``` 31 | 32 | ... and see the [docs](https://docs.rs/rustc_version) for how to use it. 33 | 34 | # Example 35 | 36 | ```rust 37 | // This could be a cargo build script 38 | 39 | use rustc_version::{version, version_meta, Channel, Version}; 40 | 41 | fn main() { 42 | // Assert we haven't travelled back in time 43 | assert!(version().unwrap().major >= 1); 44 | 45 | // Set cfg flags depending on release channel 46 | match version_meta().unwrap().channel { 47 | Channel::Stable => { 48 | println!("cargo:rustc-cfg=RUSTC_IS_STABLE"); 49 | } 50 | Channel::Beta => { 51 | println!("cargo:rustc-cfg=RUSTC_IS_BETA"); 52 | } 53 | Channel::Nightly => { 54 | println!("cargo:rustc-cfg=RUSTC_IS_NIGHTLY"); 55 | } 56 | Channel::Dev => { 57 | println!("cargo:rustc-cfg=RUSTC_IS_DEV"); 58 | } 59 | } 60 | 61 | // Check for a minimum version 62 | if version().unwrap() >= Version::parse("1.4.0").unwrap() { 63 | println!("cargo:rustc-cfg=compiler_has_important_bugfix"); 64 | } 65 | } 66 | ``` 67 | 68 | ## License 69 | 70 | Licensed under either of 71 | 72 | * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) 73 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) 74 | 75 | at your option. 76 | 77 | ### Contribution 78 | 79 | Unless you explicitly state otherwise, any contribution intentionally submitted 80 | for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any 81 | additional terms or conditions. 82 | -------------------------------------------------------------------------------- /deny.toml: -------------------------------------------------------------------------------- 1 | [licenses] 2 | allow = ["MIT"] 3 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | // Copyright 2016 rustc-version-rs developers 2 | // 3 | // Licensed under the Apache License, Version 2.0 or the MIT license 5 | // , at your 6 | // option. This file may not be copied, modified, or distributed 7 | // except according to those terms. 8 | 9 | #![warn(missing_docs)] 10 | 11 | //! Simple library for getting the version information of a `rustc` 12 | //! compiler. 13 | //! 14 | //! This can be used by build scripts or other tools dealing with Rust sources 15 | //! to make decisions based on the version of the compiler. 16 | //! 17 | //! It calls `$RUSTC --version -v` and parses the output, falling 18 | //! back to `rustc` if `$RUSTC` is not set. 19 | //! 20 | //! # Example 21 | //! 22 | //! ```rust 23 | //! // This could be a cargo build script 24 | //! 25 | //! use rustc_version::{version, version_meta, Channel, Version}; 26 | //! 27 | //! // Assert we haven't travelled back in time 28 | //! assert!(version().unwrap().major >= 1); 29 | //! 30 | //! // Set cfg flags depending on release channel 31 | //! match version_meta().unwrap().channel { 32 | //! Channel::Stable => { 33 | //! println!("cargo:rustc-cfg=RUSTC_IS_STABLE"); 34 | //! } 35 | //! Channel::Beta => { 36 | //! println!("cargo:rustc-cfg=RUSTC_IS_BETA"); 37 | //! } 38 | //! Channel::Nightly => { 39 | //! println!("cargo:rustc-cfg=RUSTC_IS_NIGHTLY"); 40 | //! } 41 | //! Channel::Dev => { 42 | //! println!("cargo:rustc-cfg=RUSTC_IS_DEV"); 43 | //! } 44 | //! } 45 | //! 46 | //! // Check for a minimum version 47 | //! if version().unwrap() >= Version::parse("1.4.0").unwrap() { 48 | //! println!("cargo:rustc-cfg=compiler_has_important_bugfix"); 49 | //! } 50 | //! ``` 51 | 52 | #[cfg(test)] 53 | #[macro_use] 54 | extern crate doc_comment; 55 | 56 | #[cfg(test)] 57 | doctest!("../README.md"); 58 | 59 | use std::collections::HashMap; 60 | use std::process::Command; 61 | use std::{env, error, fmt, io, num, str}; 62 | use std::{ffi::OsString, str::FromStr}; 63 | 64 | // Convenience re-export to allow version comparison without needing to add 65 | // semver crate. 66 | pub use semver::Version; 67 | 68 | use Error::*; 69 | 70 | /// Release channel of the compiler. 71 | #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] 72 | pub enum Channel { 73 | /// Development release channel 74 | Dev, 75 | /// Nightly release channel 76 | Nightly, 77 | /// Beta release channel 78 | Beta, 79 | /// Stable release channel 80 | Stable, 81 | } 82 | 83 | /// LLVM version 84 | /// 85 | /// LLVM's version numbering scheme is not semver compatible until version 4.0 86 | /// 87 | /// rustc [just prints the major and minor versions], so other parts of the version are not included. 88 | /// 89 | /// [just prints the major and minor versions]: https://github.com/rust-lang/rust/blob/b5c9e2448c9ace53ad5c11585803894651b18b0a/compiler/rustc_codegen_llvm/src/llvm_util.rs#L173-L178 90 | #[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] 91 | pub struct LlvmVersion { 92 | // fields must be ordered major, minor for comparison to be correct 93 | /// Major version 94 | pub major: u64, 95 | /// Minor version 96 | pub minor: u64, 97 | // TODO: expose micro version here 98 | } 99 | 100 | impl fmt::Display for LlvmVersion { 101 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 102 | write!(f, "{}.{}", self.major, self.minor) 103 | } 104 | } 105 | 106 | impl FromStr for LlvmVersion { 107 | type Err = LlvmVersionParseError; 108 | 109 | fn from_str(s: &str) -> Result { 110 | let mut parts = s 111 | .split('.') 112 | .map(|part| -> Result { 113 | if part == "0" { 114 | Ok(0) 115 | } else if part.starts_with('0') { 116 | Err(LlvmVersionParseError::ComponentMustNotHaveLeadingZeros) 117 | } else if part.starts_with('-') || part.starts_with('+') { 118 | Err(LlvmVersionParseError::ComponentMustNotHaveSign) 119 | } else { 120 | Ok(part.parse()?) 121 | } 122 | }); 123 | 124 | let major = parts.next().unwrap()?; 125 | let mut minor = 0; 126 | 127 | if let Some(part) = parts.next() { 128 | minor = part?; 129 | } else if major < 4 { 130 | // LLVM versions earlier than 4.0 have significant minor versions, so require the minor version in this case. 131 | return Err(LlvmVersionParseError::MinorVersionRequiredBefore4); 132 | } 133 | 134 | if let Some(Err(e)) = parts.next() { 135 | return Err(e); 136 | } 137 | 138 | if parts.next().is_some() { 139 | return Err(LlvmVersionParseError::TooManyComponents); 140 | } 141 | 142 | Ok(Self { major, minor }) 143 | } 144 | } 145 | 146 | /// Rustc version plus metadata like git short hash and build date. 147 | #[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] 148 | pub struct VersionMeta { 149 | /// Version of the compiler 150 | pub semver: Version, 151 | 152 | /// Git short hash of the build of the compiler 153 | pub commit_hash: Option, 154 | 155 | /// Commit date of the compiler 156 | pub commit_date: Option, 157 | 158 | /// Build date of the compiler; this was removed between Rust 1.0.0 and 1.1.0. 159 | pub build_date: Option, 160 | 161 | /// Release channel of the compiler 162 | pub channel: Channel, 163 | 164 | /// Host target triple of the compiler 165 | pub host: String, 166 | 167 | /// Short version string of the compiler 168 | pub short_version_string: String, 169 | 170 | /// Version of LLVM used by the compiler 171 | pub llvm_version: Option, 172 | } 173 | 174 | impl VersionMeta { 175 | /// Returns the version metadata for `cmd`, which should be a `rustc` command. 176 | pub fn for_command(mut cmd: Command) -> Result { 177 | let out = cmd 178 | .arg("-vV") 179 | .output() 180 | .map_err(Error::CouldNotExecuteCommand)?; 181 | 182 | if !out.status.success() { 183 | return Err(Error::CommandError { 184 | stdout: String::from_utf8_lossy(&out.stdout).into(), 185 | stderr: String::from_utf8_lossy(&out.stderr).into(), 186 | }); 187 | } 188 | 189 | version_meta_for(str::from_utf8(&out.stdout)?) 190 | } 191 | } 192 | 193 | /// Returns the `rustc` SemVer version. 194 | pub fn version() -> Result { 195 | Ok(version_meta()?.semver) 196 | } 197 | 198 | /// Returns the `rustc` SemVer version and additional metadata 199 | /// like the git short hash and build date. 200 | pub fn version_meta() -> Result { 201 | let rustc = env::var_os("RUSTC").unwrap_or_else(|| OsString::from("rustc")); 202 | let cmd = if let Some(wrapper) = env::var_os("RUSTC_WRAPPER").filter(|w| !w.is_empty()) { 203 | let mut cmd = Command::new(wrapper); 204 | cmd.arg(rustc); 205 | cmd 206 | } else { 207 | Command::new(rustc) 208 | }; 209 | 210 | VersionMeta::for_command(cmd) 211 | } 212 | 213 | /// Parses a "rustc -vV" output string and returns 214 | /// the SemVer version and additional metadata 215 | /// like the git short hash and build date. 216 | pub fn version_meta_for(verbose_version_string: &str) -> Result { 217 | let mut map = HashMap::new(); 218 | for (i, line) in verbose_version_string.lines().enumerate() { 219 | if i == 0 { 220 | map.insert("short", line); 221 | continue; 222 | } 223 | 224 | let mut parts = line.splitn(2, ": "); 225 | let key = match parts.next() { 226 | Some(key) => key, 227 | None => continue, 228 | }; 229 | 230 | if let Some(value) = parts.next() { 231 | map.insert(key, value); 232 | } 233 | } 234 | 235 | let short_version_string = expect_key("short", &map)?; 236 | let host = expect_key("host", &map)?; 237 | let release = expect_key("release", &map)?; 238 | let semver: Version = release.parse()?; 239 | 240 | let channel = match semver.pre.split('.').next().unwrap() { 241 | "" => Channel::Stable, 242 | "dev" => Channel::Dev, 243 | "beta" => Channel::Beta, 244 | "nightly" => Channel::Nightly, 245 | x => return Err(Error::UnknownPreReleaseTag(x.to_owned())), 246 | }; 247 | 248 | let commit_hash = expect_key_or_unknown("commit-hash", &map)?; 249 | let commit_date = expect_key_or_unknown("commit-date", &map)?; 250 | let build_date = map 251 | .get("build-date") 252 | .filter(|&v| *v != "unknown") 253 | .map(|&v| String::from(v)); 254 | let llvm_version = match map.get("LLVM version") { 255 | Some(&v) => Some(v.parse()?), 256 | None => None, 257 | }; 258 | 259 | Ok(VersionMeta { 260 | semver, 261 | commit_hash, 262 | commit_date, 263 | build_date, 264 | channel, 265 | host, 266 | short_version_string, 267 | llvm_version, 268 | }) 269 | } 270 | 271 | fn expect_key_or_unknown(key: &str, map: &HashMap<&str, &str>) -> Result, Error> { 272 | match map.get(key) { 273 | Some(&"unknown") => Ok(None), 274 | Some(&v) => Ok(Some(String::from(v))), 275 | None => Err(Error::UnexpectedVersionFormat), 276 | } 277 | } 278 | 279 | fn expect_key(key: &str, map: &HashMap<&str, &str>) -> Result { 280 | map.get(key) 281 | .map(|&v| String::from(v)) 282 | .ok_or(Error::UnexpectedVersionFormat) 283 | } 284 | 285 | /// LLVM Version Parse Error 286 | #[derive(Debug)] 287 | pub enum LlvmVersionParseError { 288 | /// An error occurred in parsing a version component as an integer 289 | ParseIntError(num::ParseIntError), 290 | /// A version component must not have leading zeros 291 | ComponentMustNotHaveLeadingZeros, 292 | /// A version component has a sign 293 | ComponentMustNotHaveSign, 294 | /// Minor version component must be zero on LLVM versions later than 4.0 295 | MinorVersionMustBeZeroAfter4, 296 | /// Minor version component is required on LLVM versions earlier than 4.0 297 | MinorVersionRequiredBefore4, 298 | /// Too many components 299 | TooManyComponents, 300 | } 301 | 302 | impl From for LlvmVersionParseError { 303 | fn from(e: num::ParseIntError) -> Self { 304 | LlvmVersionParseError::ParseIntError(e) 305 | } 306 | } 307 | 308 | impl fmt::Display for LlvmVersionParseError { 309 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 310 | match self { 311 | LlvmVersionParseError::ParseIntError(e) => { 312 | write!(f, "error parsing LLVM version component: {}", e) 313 | } 314 | LlvmVersionParseError::ComponentMustNotHaveLeadingZeros => { 315 | write!(f, "a version component must not have leading zeros") 316 | } 317 | LlvmVersionParseError::ComponentMustNotHaveSign => { 318 | write!(f, "a version component must not have a sign") 319 | } 320 | LlvmVersionParseError::MinorVersionMustBeZeroAfter4 => write!( 321 | f, 322 | "LLVM's minor version component must be 0 for versions greater than 4.0" 323 | ), 324 | LlvmVersionParseError::MinorVersionRequiredBefore4 => write!( 325 | f, 326 | "LLVM's minor version component is required for versions less than 4.0" 327 | ), 328 | LlvmVersionParseError::TooManyComponents => write!(f, "too many version components"), 329 | } 330 | } 331 | } 332 | 333 | impl error::Error for LlvmVersionParseError { 334 | fn source(&self) -> Option<&(dyn error::Error + 'static)> { 335 | match self { 336 | LlvmVersionParseError::ParseIntError(e) => Some(e), 337 | LlvmVersionParseError::ComponentMustNotHaveLeadingZeros 338 | | LlvmVersionParseError::ComponentMustNotHaveSign 339 | | LlvmVersionParseError::MinorVersionMustBeZeroAfter4 340 | | LlvmVersionParseError::MinorVersionRequiredBefore4 341 | | LlvmVersionParseError::TooManyComponents => None, 342 | } 343 | } 344 | } 345 | 346 | /// The error type for this crate. 347 | #[derive(Debug)] 348 | pub enum Error { 349 | /// An error occurred while trying to find the `rustc` to run. 350 | CouldNotExecuteCommand(io::Error), 351 | /// Error output from the command that was run. 352 | CommandError { 353 | /// stdout output from the command 354 | stdout: String, 355 | /// stderr output from the command 356 | stderr: String, 357 | }, 358 | /// The output of `rustc -vV` was not valid utf-8. 359 | Utf8Error(str::Utf8Error), 360 | /// The output of `rustc -vV` was not in the expected format. 361 | UnexpectedVersionFormat, 362 | /// An error occurred in parsing the semver. 363 | SemVerError(semver::Error), 364 | /// The pre-release tag is unknown. 365 | UnknownPreReleaseTag(String), 366 | /// An error occurred in parsing a `LlvmVersion`. 367 | LlvmVersionError(LlvmVersionParseError), 368 | } 369 | 370 | impl fmt::Display for Error { 371 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 372 | match *self { 373 | CouldNotExecuteCommand(ref e) => write!(f, "could not execute command: {}", e), 374 | CommandError { 375 | ref stdout, 376 | ref stderr, 377 | } => write!( 378 | f, 379 | "error from command -- stderr:\n\n{}\n\nstderr:\n\n{}", 380 | stderr, stdout, 381 | ), 382 | Utf8Error(_) => write!(f, "invalid UTF-8 output from `rustc -vV`"), 383 | UnexpectedVersionFormat => write!(f, "unexpected `rustc -vV` format"), 384 | SemVerError(ref e) => write!(f, "error parsing version: {}", e), 385 | UnknownPreReleaseTag(ref i) => write!(f, "unknown pre-release tag: {}", i), 386 | LlvmVersionError(ref e) => write!(f, "error parsing LLVM's version: {}", e), 387 | } 388 | } 389 | } 390 | 391 | impl error::Error for Error { 392 | fn source(&self) -> Option<&(dyn error::Error + 'static)> { 393 | match *self { 394 | CouldNotExecuteCommand(ref e) => Some(e), 395 | CommandError { .. } => None, 396 | Utf8Error(ref e) => Some(e), 397 | UnexpectedVersionFormat => None, 398 | SemVerError(ref e) => Some(e), 399 | UnknownPreReleaseTag(_) => None, 400 | LlvmVersionError(ref e) => Some(e), 401 | } 402 | } 403 | } 404 | 405 | macro_rules! impl_from { 406 | ($($err_ty:ty => $variant:ident),* $(,)*) => { 407 | $( 408 | impl From<$err_ty> for Error { 409 | fn from(e: $err_ty) -> Error { 410 | Error::$variant(e) 411 | } 412 | } 413 | )* 414 | } 415 | } 416 | 417 | impl_from! { 418 | str::Utf8Error => Utf8Error, 419 | semver::Error => SemVerError, 420 | LlvmVersionParseError => LlvmVersionError, 421 | } 422 | 423 | /// The result type for this crate. 424 | pub type Result = std::result::Result; 425 | -------------------------------------------------------------------------------- /tests/all.rs: -------------------------------------------------------------------------------- 1 | #![allow(clippy::match_like_matches_macro)] 2 | 3 | use std::process::Command; 4 | 5 | use rustc_version::{ 6 | version, version_meta, version_meta_for, Channel, Error, LlvmVersion, LlvmVersionParseError, 7 | Version, VersionMeta, 8 | }; 9 | 10 | #[test] 11 | fn rustc_error() { 12 | let mut cmd = Command::new("rustc"); 13 | cmd.arg("--FOO"); 14 | let stderr = match VersionMeta::for_command(cmd) { 15 | Err(Error::CommandError { stdout: _, stderr }) => stderr, 16 | _ => panic!("command error expected"), 17 | }; 18 | assert_eq!(stderr, "error: Unrecognized option: \'FOO\'\n\n"); 19 | } 20 | 21 | #[test] 22 | fn smoketest() { 23 | let v = version().unwrap(); 24 | assert!(v.major >= 1); 25 | 26 | let v = version_meta().unwrap(); 27 | assert!(v.semver.major >= 1); 28 | 29 | assert!(version().unwrap() >= Version::parse("1.0.0").unwrap()); 30 | } 31 | 32 | #[test] 33 | fn parse_1_0_0() { 34 | let version = version_meta_for( 35 | "rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14) 36 | binary: rustc 37 | commit-hash: a59de37e99060162a2674e3ff45409ac73595c0e 38 | commit-date: 2015-05-13 39 | build-date: 2015-05-14 40 | host: x86_64-unknown-linux-gnu 41 | release: 1.0.0", 42 | ) 43 | .unwrap(); 44 | 45 | assert_eq!(version.semver, Version::parse("1.0.0").unwrap()); 46 | assert_eq!( 47 | version.commit_hash, 48 | Some("a59de37e99060162a2674e3ff45409ac73595c0e".into()) 49 | ); 50 | assert_eq!(version.commit_date, Some("2015-05-13".into())); 51 | assert_eq!(version.build_date, Some("2015-05-14".into())); 52 | assert_eq!(version.channel, Channel::Stable); 53 | assert_eq!(version.host, "x86_64-unknown-linux-gnu"); 54 | assert_eq!( 55 | version.short_version_string, 56 | "rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)" 57 | ); 58 | assert_eq!(version.llvm_version, None); 59 | } 60 | 61 | #[test] 62 | fn parse_unknown() { 63 | let version = version_meta_for( 64 | "rustc 1.3.0 65 | binary: rustc 66 | commit-hash: unknown 67 | commit-date: unknown 68 | host: x86_64-unknown-linux-gnu 69 | release: 1.3.0", 70 | ) 71 | .unwrap(); 72 | 73 | assert_eq!(version.semver, Version::parse("1.3.0").unwrap()); 74 | assert_eq!(version.commit_hash, None); 75 | assert_eq!(version.commit_date, None); 76 | assert_eq!(version.channel, Channel::Stable); 77 | assert_eq!(version.host, "x86_64-unknown-linux-gnu"); 78 | assert_eq!(version.short_version_string, "rustc 1.3.0"); 79 | assert_eq!(version.llvm_version, None); 80 | } 81 | 82 | #[test] 83 | fn parse_nightly() { 84 | let version = version_meta_for( 85 | "rustc 1.5.0-nightly (65d5c0833 2015-09-29) 86 | binary: rustc 87 | commit-hash: 65d5c083377645a115c4ac23a620d3581b9562b6 88 | commit-date: 2015-09-29 89 | host: x86_64-unknown-linux-gnu 90 | release: 1.5.0-nightly", 91 | ) 92 | .unwrap(); 93 | 94 | assert_eq!(version.semver, Version::parse("1.5.0-nightly").unwrap()); 95 | assert_eq!( 96 | version.commit_hash, 97 | Some("65d5c083377645a115c4ac23a620d3581b9562b6".into()) 98 | ); 99 | assert_eq!(version.commit_date, Some("2015-09-29".into())); 100 | assert_eq!(version.channel, Channel::Nightly); 101 | assert_eq!(version.host, "x86_64-unknown-linux-gnu"); 102 | assert_eq!( 103 | version.short_version_string, 104 | "rustc 1.5.0-nightly (65d5c0833 2015-09-29)" 105 | ); 106 | assert_eq!(version.llvm_version, None); 107 | } 108 | 109 | #[test] 110 | fn parse_stable() { 111 | let version = version_meta_for( 112 | "rustc 1.3.0 (9a92aaf19 2015-09-15) 113 | binary: rustc 114 | commit-hash: 9a92aaf19a64603b02b4130fe52958cc12488900 115 | commit-date: 2015-09-15 116 | host: x86_64-unknown-linux-gnu 117 | release: 1.3.0", 118 | ) 119 | .unwrap(); 120 | 121 | assert_eq!(version.semver, Version::parse("1.3.0").unwrap()); 122 | assert_eq!( 123 | version.commit_hash, 124 | Some("9a92aaf19a64603b02b4130fe52958cc12488900".into()) 125 | ); 126 | assert_eq!(version.commit_date, Some("2015-09-15".into())); 127 | assert_eq!(version.channel, Channel::Stable); 128 | assert_eq!(version.host, "x86_64-unknown-linux-gnu"); 129 | assert_eq!( 130 | version.short_version_string, 131 | "rustc 1.3.0 (9a92aaf19 2015-09-15)" 132 | ); 133 | assert_eq!(version.llvm_version, None); 134 | } 135 | 136 | #[test] 137 | fn parse_1_16_0_nightly() { 138 | let version = version_meta_for( 139 | "rustc 1.16.0-nightly (5d994d8b7 2017-01-05) 140 | binary: rustc 141 | commit-hash: 5d994d8b7e482e87467d4a521911477bd8284ce3 142 | commit-date: 2017-01-05 143 | host: x86_64-unknown-linux-gnu 144 | release: 1.16.0-nightly 145 | LLVM version: 3.9", 146 | ) 147 | .unwrap(); 148 | 149 | assert_eq!(version.semver, Version::parse("1.16.0-nightly").unwrap()); 150 | assert_eq!( 151 | version.commit_hash, 152 | Some("5d994d8b7e482e87467d4a521911477bd8284ce3".into()) 153 | ); 154 | assert_eq!(version.commit_date, Some("2017-01-05".into())); 155 | assert_eq!(version.channel, Channel::Nightly); 156 | assert_eq!(version.host, "x86_64-unknown-linux-gnu"); 157 | assert_eq!( 158 | version.short_version_string, 159 | "rustc 1.16.0-nightly (5d994d8b7 2017-01-05)" 160 | ); 161 | assert_eq!( 162 | version.llvm_version, 163 | Some(LlvmVersion { major: 3, minor: 9 }) 164 | ); 165 | } 166 | 167 | #[test] 168 | fn parse_1_47_0_stable() { 169 | let version = version_meta_for( 170 | "rustc 1.47.0 (18bf6b4f0 2020-10-07) 171 | binary: rustc 172 | commit-hash: 18bf6b4f01a6feaf7259ba7cdae58031af1b7b39 173 | commit-date: 2020-10-07 174 | host: powerpc64le-unknown-linux-gnu 175 | release: 1.47.0 176 | LLVM version: 11.0", 177 | ) 178 | .unwrap(); 179 | 180 | assert_eq!(version.semver, Version::parse("1.47.0").unwrap()); 181 | assert_eq!( 182 | version.commit_hash, 183 | Some("18bf6b4f01a6feaf7259ba7cdae58031af1b7b39".into()) 184 | ); 185 | assert_eq!(version.commit_date, Some("2020-10-07".into())); 186 | assert_eq!(version.channel, Channel::Stable); 187 | assert_eq!(version.host, "powerpc64le-unknown-linux-gnu"); 188 | assert_eq!( 189 | version.short_version_string, 190 | "rustc 1.47.0 (18bf6b4f0 2020-10-07)" 191 | ); 192 | assert_eq!( 193 | version.llvm_version, 194 | Some(LlvmVersion { 195 | major: 11, 196 | minor: 0, 197 | }) 198 | ); 199 | } 200 | 201 | #[test] 202 | fn parse_llvm_micro() { 203 | let version = version_meta_for( 204 | "rustc 1.51.0-nightly (4253153db 2021-01-17) 205 | binary: rustc 206 | commit-hash: 4253153db205251f72ea4493687a31e04a2a8ca0 207 | commit-date: 2021-01-17 208 | host: x86_64-pc-windows-msvc 209 | release: 1.51.0-nightly 210 | LLVM version: 11.0.1", 211 | ) 212 | .unwrap(); 213 | 214 | assert_eq!(version.semver, Version::parse("1.51.0-nightly").unwrap()); 215 | assert_eq!( 216 | version.commit_hash.unwrap(), 217 | "4253153db205251f72ea4493687a31e04a2a8ca0" 218 | ); 219 | assert_eq!(version.commit_date.unwrap(), "2021-01-17"); 220 | assert_eq!(version.host, "x86_64-pc-windows-msvc"); 221 | assert_eq!( 222 | version.short_version_string, 223 | "rustc 1.51.0-nightly (4253153db 2021-01-17)" 224 | ); 225 | assert_eq!( 226 | version.llvm_version, 227 | Some(LlvmVersion { 228 | major: 11, 229 | minor: 0 230 | }) 231 | ); 232 | } 233 | 234 | #[test] 235 | fn parse_debian_buster() { 236 | let version = version_meta_for( 237 | "rustc 1.41.1 238 | binary: rustc 239 | commit-hash: unknown 240 | commit-date: unknown 241 | host: powerpc64le-unknown-linux-gnu 242 | release: 1.41.1 243 | LLVM version: 7.0", 244 | ) 245 | .unwrap(); 246 | 247 | assert_eq!(version.semver, Version::parse("1.41.1").unwrap()); 248 | assert_eq!(version.commit_hash, None); 249 | assert_eq!(version.commit_date, None); 250 | assert_eq!(version.channel, Channel::Stable); 251 | assert_eq!(version.host, "powerpc64le-unknown-linux-gnu"); 252 | assert_eq!(version.short_version_string, "rustc 1.41.1"); 253 | assert_eq!( 254 | version.llvm_version, 255 | Some(LlvmVersion { major: 7, minor: 0 }) 256 | ); 257 | } 258 | 259 | #[test] 260 | fn parse_termux() { 261 | let version = version_meta_for( 262 | "rustc 1.46.0 263 | binary: rustc 264 | commit-hash: unknown 265 | commit-date: unknown 266 | host: aarch64-linux-android 267 | release: 1.46.0 268 | LLVM version: 10.0", 269 | ) 270 | .unwrap(); 271 | 272 | assert_eq!(version.semver, Version::parse("1.46.0").unwrap()); 273 | assert_eq!(version.commit_hash, None); 274 | assert_eq!(version.commit_date, None); 275 | assert_eq!(version.channel, Channel::Stable); 276 | assert_eq!(version.host, "aarch64-linux-android"); 277 | assert_eq!(version.short_version_string, "rustc 1.46.0"); 278 | assert_eq!( 279 | version.llvm_version, 280 | Some(LlvmVersion { 281 | major: 10, 282 | minor: 0, 283 | }) 284 | ); 285 | } 286 | 287 | #[test] 288 | fn parse_llvm_version_empty() { 289 | let res: Result = "".parse(); 290 | assert!(match res { 291 | Err(LlvmVersionParseError::ParseIntError(_)) => true, 292 | _ => false, 293 | }); 294 | } 295 | 296 | #[test] 297 | fn parse_llvm_version_invalid_char() { 298 | let res: Result = "A".parse(); 299 | assert!(match res { 300 | Err(LlvmVersionParseError::ParseIntError(_)) => true, 301 | _ => false, 302 | }); 303 | } 304 | 305 | #[test] 306 | fn parse_llvm_version_overflow() { 307 | let res: Result = "9999999999999999999999999999999".parse(); 308 | assert!(match res { 309 | Err(LlvmVersionParseError::ParseIntError(_)) => true, 310 | _ => false, 311 | }); 312 | } 313 | 314 | #[test] 315 | fn parse_llvm_version_leading_zero_on_zero() { 316 | let res: Result = "00".parse(); 317 | assert!(match res { 318 | Err(LlvmVersionParseError::ComponentMustNotHaveLeadingZeros) => true, 319 | _ => false, 320 | }); 321 | } 322 | 323 | #[test] 324 | fn parse_llvm_version_leading_zero_on_nonzero() { 325 | let res: Result = "01".parse(); 326 | assert!(match res { 327 | Err(LlvmVersionParseError::ComponentMustNotHaveLeadingZeros) => true, 328 | _ => false, 329 | }); 330 | } 331 | 332 | #[test] 333 | fn parse_llvm_version_4_components() { 334 | let res: Result = "4.0.0.0".parse(); 335 | 336 | assert!(match res { 337 | Err(LlvmVersionParseError::TooManyComponents) => true, 338 | _ => false, 339 | }); 340 | } 341 | 342 | #[test] 343 | fn parse_llvm_version_component_sign_plus() { 344 | let res: Result = "1.+3".parse(); 345 | 346 | assert!(match res { 347 | Err(LlvmVersionParseError::ComponentMustNotHaveSign) => true, 348 | _ => false, 349 | }); 350 | } 351 | 352 | #[test] 353 | fn parse_llvm_version_component_sign_minus() { 354 | let res: Result = "1.-3".parse(); 355 | 356 | assert!(match res { 357 | Err(LlvmVersionParseError::ComponentMustNotHaveSign) => true, 358 | _ => false, 359 | }); 360 | } 361 | 362 | #[test] 363 | fn parse_llvm_version_3() { 364 | let res: Result = "3".parse(); 365 | 366 | assert!(match res { 367 | Err(LlvmVersionParseError::MinorVersionRequiredBefore4) => true, 368 | _ => false, 369 | }); 370 | } 371 | 372 | #[test] 373 | fn parse_llvm_version_5() { 374 | let v: LlvmVersion = "5".parse().unwrap(); 375 | assert_eq!(v, LlvmVersion { major: 5, minor: 0 }); 376 | } 377 | 378 | #[test] 379 | fn parse_llvm_version_5_0() { 380 | let v: LlvmVersion = "5.0".parse().unwrap(); 381 | assert_eq!(v, LlvmVersion { major: 5, minor: 0 }); 382 | } 383 | 384 | #[test] 385 | fn parse_llvm_version_4_0() { 386 | let v: LlvmVersion = "4.0".parse().unwrap(); 387 | assert_eq!(v, LlvmVersion { major: 4, minor: 0 }); 388 | } 389 | 390 | #[test] 391 | fn parse_llvm_version_3_0() { 392 | let v: LlvmVersion = "3.0".parse().unwrap(); 393 | assert_eq!(v, LlvmVersion { major: 3, minor: 0 }); 394 | } 395 | 396 | #[test] 397 | fn parse_llvm_version_3_9() { 398 | let v: LlvmVersion = "3.9".parse().unwrap(); 399 | assert_eq!(v, LlvmVersion { major: 3, minor: 9 }); 400 | } 401 | 402 | #[test] 403 | fn parse_llvm_version_11_0() { 404 | let v: LlvmVersion = "11.0".parse().unwrap(); 405 | assert_eq!( 406 | v, 407 | LlvmVersion { 408 | major: 11, 409 | minor: 0 410 | } 411 | ); 412 | } 413 | 414 | #[test] 415 | fn parse_llvm_version_11() { 416 | let v: LlvmVersion = "11".parse().unwrap(); 417 | assert_eq!( 418 | v, 419 | LlvmVersion { 420 | major: 11, 421 | minor: 0 422 | } 423 | ); 424 | } 425 | 426 | #[test] 427 | fn test_llvm_version_comparison() { 428 | // check that field order is correct 429 | assert!(LlvmVersion { major: 3, minor: 9 } < LlvmVersion { major: 4, minor: 0 }); 430 | } 431 | 432 | /* 433 | #[test] 434 | fn version_matches_replacement() { 435 | let f = |s1: &str, s2: &str| { 436 | let a = Version::parse(s1).unwrap(); 437 | let b = Version::parse(s2).unwrap(); 438 | println!("{} <= {} : {}", s1, s2, a <= b); 439 | }; 440 | 441 | println!(); 442 | 443 | f("1.5.0", "1.5.0"); 444 | f("1.5.0-nightly", "1.5.0"); 445 | f("1.5.0", "1.5.0-nightly"); 446 | f("1.5.0-nightly", "1.5.0-nightly"); 447 | 448 | f("1.5.0", "1.6.0"); 449 | f("1.5.0-nightly", "1.6.0"); 450 | f("1.5.0", "1.6.0-nightly"); 451 | f("1.5.0-nightly", "1.6.0-nightly"); 452 | 453 | panic!(); 454 | 455 | } 456 | */ 457 | --------------------------------------------------------------------------------