]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/externalfiles.rs
Rollup merge of #86165 - m-ou-se:proc-macro-span-shrink, r=dtolnay
[rust.git] / src / librustdoc / externalfiles.rs
1 use crate::html::markdown::{ErrorCodes, IdMap, Markdown, Playground};
2 use crate::rustc_span::edition::Edition;
3 use std::fs;
4 use std::path::Path;
5 use std::str;
6
7 use serde::Serialize;
8
9 #[derive(Clone, Debug, Serialize)]
10 crate struct ExternalHtml {
11     /// Content that will be included inline in the <head> section of a
12     /// rendered Markdown file or generated documentation
13     crate in_header: String,
14     /// Content that will be included inline between <body> and the content of
15     /// a rendered Markdown file or generated documentation
16     crate before_content: String,
17     /// Content that will be included inline between the content and </body> of
18     /// a rendered Markdown file or generated documentation
19     crate after_content: String,
20 }
21
22 impl ExternalHtml {
23     crate fn load(
24         in_header: &[String],
25         before_content: &[String],
26         after_content: &[String],
27         md_before_content: &[String],
28         md_after_content: &[String],
29         nightly_build: bool,
30         diag: &rustc_errors::Handler,
31         id_map: &mut IdMap,
32         edition: Edition,
33         playground: &Option<Playground>,
34     ) -> Option<ExternalHtml> {
35         let codes = ErrorCodes::from(nightly_build);
36         let ih = load_external_files(in_header, diag)?;
37         let bc = load_external_files(before_content, diag)?;
38         let m_bc = load_external_files(md_before_content, diag)?;
39         let bc = format!(
40             "{}{}",
41             bc,
42             Markdown(&m_bc, &[], id_map, codes, edition, playground).into_string()
43         );
44         let ac = load_external_files(after_content, diag)?;
45         let m_ac = load_external_files(md_after_content, diag)?;
46         let ac = format!(
47             "{}{}",
48             ac,
49             Markdown(&m_ac, &[], id_map, codes, edition, playground).into_string()
50         );
51         Some(ExternalHtml { in_header: ih, before_content: bc, after_content: ac })
52     }
53 }
54
55 crate enum LoadStringError {
56     ReadFail,
57     BadUtf8,
58 }
59
60 crate fn load_string<P: AsRef<Path>>(
61     file_path: P,
62     diag: &rustc_errors::Handler,
63 ) -> Result<String, LoadStringError> {
64     let file_path = file_path.as_ref();
65     let contents = match fs::read(file_path) {
66         Ok(bytes) => bytes,
67         Err(e) => {
68             diag.struct_err(&format!("error reading `{}`: {}", file_path.display(), e)).emit();
69             return Err(LoadStringError::ReadFail);
70         }
71     };
72     match str::from_utf8(&contents) {
73         Ok(s) => Ok(s.to_string()),
74         Err(_) => {
75             diag.struct_err(&format!("error reading `{}`: not UTF-8", file_path.display())).emit();
76             Err(LoadStringError::BadUtf8)
77         }
78     }
79 }
80
81 fn load_external_files(names: &[String], diag: &rustc_errors::Handler) -> Option<String> {
82     let mut out = String::new();
83     for name in names {
84         let s = match load_string(name, diag) {
85             Ok(s) => s,
86             Err(_) => return None,
87         };
88         out.push_str(&s);
89         out.push('\n');
90     }
91     Some(out)
92 }