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