1
1
use super :: { error:: Nope , redirect, redirect_base, STATIC_FILE_CACHE_DURATION } ;
2
- use crate :: utils:: report_error;
2
+ use crate :: utils:: { parse_rustc_date , report_error} ;
3
3
use anyhow:: Context ;
4
- use chrono:: Utc ;
4
+ use chrono:: prelude :: * ;
5
5
use iron:: {
6
6
headers:: CacheDirective ,
7
7
headers:: { CacheControl , ContentLength , ContentType , LastModified } ,
@@ -14,8 +14,30 @@ use std::{ffi::OsStr, fs, path::Path};
14
14
const VENDORED_CSS : & str = include_str ! ( concat!( env!( "OUT_DIR" ) , "/vendored.css" ) ) ;
15
15
const STYLE_CSS : & str = include_str ! ( concat!( env!( "OUT_DIR" ) , "/style.css" ) ) ;
16
16
const RUSTDOC_CSS : & str = include_str ! ( concat!( env!( "OUT_DIR" ) , "/rustdoc.css" ) ) ;
17
+ const RUSTDOC_CSS_2 : & str = include_str ! ( concat!( env!( "OUT_DIR" ) , "/rustdoc-2.css" ) ) ;
17
18
const STATIC_SEARCH_PATHS : & [ & str ] = & [ "static" , "vendor" ] ;
18
19
20
+ fn get_correct_docsrs_style ( req : & Request ) -> & ' static str {
21
+ if let Some ( query) = req. url . query ( ) {
22
+ // First comes the docs.rs version then the rustdoc version. They are separated with a "--".
23
+ if let Some ( date) = query
24
+ . split ( "--" )
25
+ . nth ( 1 )
26
+ . and_then ( |s| parse_rustc_date ( s) . ok ( ) )
27
+ {
28
+ // This is the date where https://github.com/rust-lang/rust/pull/91356 was merged.
29
+ return if Utc . ymd ( 2021 , 12 , 6 ) > date {
30
+ RUSTDOC_CSS
31
+ } else {
32
+ // If this is the new rustdoc layout, we need the newer docs.rs CSS file.
33
+ RUSTDOC_CSS_2
34
+ } ;
35
+ }
36
+ }
37
+ // By default, we return the old docs.rs CSS file.
38
+ RUSTDOC_CSS
39
+ }
40
+
19
41
pub ( crate ) fn static_handler ( req : & mut Request ) -> IronResult < Response > {
20
42
let mut file = req. url . path ( ) ;
21
43
file. drain ( ..2 ) . for_each ( std:: mem:: drop) ;
@@ -24,7 +46,12 @@ pub(crate) fn static_handler(req: &mut Request) -> IronResult<Response> {
24
46
Ok ( match file. as_str ( ) {
25
47
"vendored.css" => serve_resource ( VENDORED_CSS , ContentType ( "text/css" . parse ( ) . unwrap ( ) ) ) ,
26
48
"style.css" => serve_resource ( STYLE_CSS , ContentType ( "text/css" . parse ( ) . unwrap ( ) ) ) ,
27
- "rustdoc.css" => serve_resource ( RUSTDOC_CSS , ContentType ( "text/css" . parse ( ) . unwrap ( ) ) ) ,
49
+ "rustdoc.css" => serve_resource (
50
+ // We pick the correct "rustdoc.css" static file depending on which rustdoc version
51
+ // was used to generate this version of this crate.
52
+ get_correct_docsrs_style ( req) ,
53
+ ContentType ( "text/css" . parse ( ) . unwrap ( ) ) ,
54
+ ) ,
28
55
file => serve_file ( file) ?,
29
56
} )
30
57
}
0 commit comments