11use 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} ;
33use anyhow:: Context ;
4- use chrono:: Utc ;
4+ use chrono:: prelude :: * ;
55use iron:: {
66 headers:: CacheDirective ,
77 headers:: { CacheControl , ContentLength , ContentType , LastModified } ,
@@ -14,8 +14,30 @@ use std::{ffi::OsStr, fs, path::Path};
1414const VENDORED_CSS : & str = include_str ! ( concat!( env!( "OUT_DIR" ) , "/vendored.css" ) ) ;
1515const STYLE_CSS : & str = include_str ! ( concat!( env!( "OUT_DIR" ) , "/style.css" ) ) ;
1616const 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" ) ) ;
1718const STATIC_SEARCH_PATHS : & [ & str ] = & [ "static" , "vendor" ] ;
1819
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+
1941pub ( crate ) fn static_handler ( req : & mut Request ) -> IronResult < Response > {
2042 let mut file = req. url . path ( ) ;
2143 file. drain ( ..2 ) . for_each ( std:: mem:: drop) ;
@@ -24,7 +46,12 @@ pub(crate) fn static_handler(req: &mut Request) -> IronResult<Response> {
2446 Ok ( match file. as_str ( ) {
2547 "vendored.css" => serve_resource ( VENDORED_CSS , ContentType ( "text/css" . parse ( ) . unwrap ( ) ) ) ,
2648 "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+ ) ,
2855 file => serve_file ( file) ?,
2956 } )
3057}
0 commit comments