From 8cf0ba7fe2c93dd4839a0aabf670a11c6adee867 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 20 Jan 2022 17:59:21 -0800 Subject: [PATCH] Make serde_test build script buildable with older rustc --- serde_test/build.rs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/serde_test/build.rs b/serde_test/build.rs index 53f3fa23..15870655 100644 --- a/serde_test/build.rs +++ b/serde_test/build.rs @@ -1,6 +1,6 @@ use std::env; use std::process::Command; -use std::str; +use std::str::{self, FromStr}; // The rustc-cfg strings below are *not* public API. Please let us know by // opening a GitHub issue if your build environment requires some way to enable @@ -19,12 +19,30 @@ fn main() { } fn rustc_minor_version() -> Option { - let rustc = env::var_os("RUSTC")?; - let output = Command::new(rustc).arg("--version").output().ok()?; - let version = str::from_utf8(&output.stdout).ok()?; + let rustc = match env::var_os("RUSTC") { + Some(rustc) => rustc, + None => return None, + }; + + let output = match Command::new(rustc).arg("--version").output() { + Ok(output) => output, + Err(_) => return None, + }; + + let version = match str::from_utf8(&output.stdout) { + Ok(version) => version, + Err(_) => return None, + }; + let mut pieces = version.split('.'); if pieces.next() != Some("rustc 1") { return None; } - pieces.next()?.parse().ok() + + let next = match pieces.next() { + Some(next) => next, + None => return None, + }; + + u32::from_str(next).ok() }