|
1 | 1 | // Copyright 2025 Heath Stewart. |
2 | 2 | // Licensed under the MIT License. See LICENSE.txt in the project root for license information. |
3 | 3 |
|
| 4 | +use std::{cmp::Ordering, env, process::Command, str::FromStr}; |
| 5 | + |
| 6 | +const MIN_SPAN_LOCATIONS_VER: Version = Version::new(1, 88, 0); |
| 7 | + |
4 | 8 | fn main() { |
5 | 9 | println!("cargo::rerun-if-changed=README.md"); |
| 10 | + if matches!(rustc_version(), Ok(version) if version >= MIN_SPAN_LOCATIONS_VER) { |
| 11 | + println!("cargo::rustc-cfg=span_locations"); |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +fn rustc_version() -> Result<Version, Box<dyn std::error::Error>> { |
| 16 | + let output = Command::new(env::var("RUSTC")?).arg("--version").output()?; |
| 17 | + let stdout = String::from_utf8(output.stdout)?; |
| 18 | + let mut words = stdout.split_whitespace(); |
| 19 | + words.next().ok_or("expected `rustc`")?; |
| 20 | + |
| 21 | + let version: Version = words.next().ok_or("expected version")?.parse()?; |
| 22 | + Ok(version) |
| 23 | +} |
| 24 | + |
| 25 | +#[derive(Debug, Default, Eq)] |
| 26 | +struct Version { |
| 27 | + major: u16, |
| 28 | + minor: u16, |
| 29 | + patch: u16, |
| 30 | +} |
| 31 | + |
| 32 | +impl Version { |
| 33 | + const fn new(major: u16, minor: u16, patch: u16) -> Self { |
| 34 | + Self { |
| 35 | + major, |
| 36 | + minor, |
| 37 | + patch, |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl FromStr for Version { |
| 43 | + type Err = String; |
| 44 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 45 | + // cspell:ignore splitn |
| 46 | + let mut values = s.splitn(3, ".").map(str::parse::<u16>); |
| 47 | + Ok(Self { |
| 48 | + major: values |
| 49 | + .next() |
| 50 | + .ok_or("no major version")? |
| 51 | + .map_err(|err| err.to_string())?, |
| 52 | + minor: values |
| 53 | + .next() |
| 54 | + .ok_or("no minor version")? |
| 55 | + .map_err(|err| err.to_string())?, |
| 56 | + patch: values |
| 57 | + .next() |
| 58 | + .ok_or("no patch version")? |
| 59 | + .map_err(|err| err.to_string())?, |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +impl PartialEq for Version { |
| 65 | + fn eq(&self, other: &Self) -> bool { |
| 66 | + self.major == other.major && self.minor == other.minor && self.patch == other.patch |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl Ord for Version { |
| 71 | + fn cmp(&self, other: &Self) -> Ordering { |
| 72 | + let cmp = self.major.cmp(&other.major); |
| 73 | + if cmp != Ordering::Equal { |
| 74 | + return cmp; |
| 75 | + } |
| 76 | + let cmp = self.minor.cmp(&other.minor); |
| 77 | + if cmp != Ordering::Equal { |
| 78 | + return cmp; |
| 79 | + } |
| 80 | + self.patch.cmp(&other.patch) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl PartialOrd for Version { |
| 85 | + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 86 | + Some(self.cmp(other)) |
| 87 | + } |
6 | 88 | } |
0 commit comments