forked from rust-lang/docs.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rustc_version.rs
79 lines (70 loc) · 2.59 KB
/
rustc_version.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use crate::error::Result;
use anyhow::{anyhow, Context};
use chrono::prelude::*;
use once_cell::sync::Lazy;
use regex::Regex;
/// Parses rustc commit hash from rustc version string
pub fn parse_rustc_version<S: AsRef<str>>(version: S) -> Result<String> {
let version_regex = Regex::new(r" ([\w.-]+) \((\w+) (\d+)-(\d+)-(\d+)\)")?;
let captures = version_regex
.captures(version.as_ref())
.with_context(|| anyhow!("Failed to parse rustc version"))?;
Ok(format!(
"{}{}{}-{}-{}",
captures.get(3).unwrap().as_str(),
captures.get(4).unwrap().as_str(),
captures.get(5).unwrap().as_str(),
captures.get(1).unwrap().as_str(),
captures.get(2).unwrap().as_str()
))
}
fn parse_rustc_date<S: AsRef<str>>(version: S) -> Result<Date<Utc>> {
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r" (\d+)-(\d+)-(\d+)\)$").unwrap());
let cap = RE
.captures(version.as_ref())
.with_context(|| anyhow!("Failed to parse rustc date"))?;
let year = cap.get(1).unwrap().as_str();
let month = cap.get(2).unwrap().as_str();
let day = cap.get(3).unwrap().as_str();
Ok(Utc.ymd(
year.parse::<i32>().unwrap(),
month.parse::<u32>().unwrap(),
day.parse::<u32>().unwrap(),
))
}
/// Picks the correct "rustdoc.css" static file depending on which rustdoc version was used to
/// generate this version of this crate.
pub fn get_correct_docsrs_style_file(version: &str) -> Result<String> {
let date = parse_rustc_date(version)?;
// This is the date where https://github.com/rust-lang/rust/pull/91356 was merged.
if Utc.ymd(2021, 12, 5) < date {
// If this is the new rustdoc layout, we need the newer docs.rs CSS file.
Ok("rustdoc-2021-12-05.css".to_owned())
} else {
// By default, we return the old docs.rs CSS file.
Ok("rustdoc.css".to_owned())
}
}
#[test]
fn test_parse_rustc_version() {
assert_eq!(
parse_rustc_version("rustc 1.10.0-nightly (57ef01513 2016-05-23)").unwrap(),
"20160523-1.10.0-nightly-57ef01513"
);
assert_eq!(
parse_rustc_version("docsrs 0.2.0 (ba9ae23 2016-05-26)").unwrap(),
"20160526-0.2.0-ba9ae23"
);
}
#[test]
fn test_get_correct_docsrs_style_file() {
assert_eq!(
get_correct_docsrs_style_file("rustc 1.10.0-nightly (57ef01513 2016-05-23)").unwrap(),
"rustdoc.css"
);
assert_eq!(
get_correct_docsrs_style_file("docsrs 0.2.0 (ba9ae23 2022-05-26)").unwrap(),
"rustdoc-2021-12-05.css"
);
assert!(get_correct_docsrs_style_file("docsrs 0.2.0").is_err(),);
}